Module:PageType

From Chill

RJAlvarez (talk) 19:51, 7 June 2021 (UTC)


local p = {}
local h = require("Module:HF")

-- returns namespace of the page
function p.get_namespace(pagename)
	local title
	local output = ''
	
	if not h.isempty(pagename)
		then
			pagename = string.gsub(pagename,'&','&')
			pagename = string.gsub(pagename,''',"'") 
			title = mw.title.new(pagename)
			if title.namespace == 0
				then 
					if title.exists
						then return "Main"
					end
				else return title.nsText
			end
			if title.exists 
				then return "Main"
				elseif h.exists('File:'..pagename)
					then return "File"
					elseif h.exists('Category:'..pagename)
						then return "Category"
						else return ''
			end
	end
	
	return output
end


-- returns type of page based on the name of its main template (workd only for templates in 'Marvel Database' namespace.
function p.get_page_type(pagename)
	local namespace = p.get_namespace(pagename)
	local content = ''
	local s_match = '{{Marvel Database:(.-) Template'
	local output = namespace

	if namespace == 'Main'
		then
			content = h.get_content(pagename)
			if not h.isempty(content)
				then 
					output = string.match(content, s_match) 
					if not h.isempty(output)
						then output = mw.text.trim(output)
						elseif string.find(content, '{{Marvel Database:Series List') ~= nil
							then output = 'Series'
						else output = 'Unknown'
					end
			end
	end
	
	return output
end


-- returns 'true' if page is a disambiguation
function p.disambiguation(pagename)
	local content = ''
	local i
	local j
	local k
	local output = false
	local n = 0
	
	if h.exists(pagename)
		then
			content = h.get_content(pagename)
			i = string.find(content, 'DisambiguationFull')
			j = string.find(content, 'Disambiguation')
			k = string.find(content, 'Disambig')
			if i ~= nil
				then 
					output = true
					n = 1
				elseif j ~= nil
					then output = true
						n = 2
				elseif k ~= nil
					then 
						output = true
						n = 3
			end
	end
	
	return output, n
end


-- returns main page of a disambiguation
function p.get_disambiguation_main(pagename)
	local content = ''
	local i
	local j
	local output = ''
	
	i, j = p.disambiguation(pagename)
	
	if i
		then
			content = h.get_content(pagename)
			if j == 1 -- Template:DisambiguationFull
				then output = h.get_field_value(content, 'main')
				elseif j == 2 -- Template:Disambiguation
					then output = h.get_field_value(content, 'Main Character')
					elseif j == 3 -- Template:Disambig
						then output = h.get_field_value(content, 'MainPage')
			end
			output = h.break_link(output, 1)
	end
	
	return output
end


--Check if "page" is redirect or not
function p.redirect(page)
	local output = false
	
	if not h.isempty(page)
		then output = mw.title.new(page).isRedirect
	end
	
	return output
end


--return target of redirect
function p.get_redirect_target(page)
	local content = ''
	local output = ''
	
	if not h.isempty(page) and p.redirect(page)
		then 
			content = h.get_content(page)
			output = mw.ustring.gsub(content, "^%s*#[Rr][Ee][Dd][Ii][Rr][Ee][Cc][Tt]%s*:?%s*%[%[([^%[%]|]-)%]%]", "%1") or
				mw.ustring.gsub(content,"^%s*#[Rr][Ee][Dd][Ii][Rr][Ee][Cc][Tt]%s*:?%s*%[%[([^%[%]|]-)|[^%[%]]-%]%]", "%1")
	end
	
	return output
end

return p