FANDOM


local p = {}
local h = require("Module:HF")
 
-- returns namespace of the page
function p.lua_getNamespace(pagename)
    local title = mw.title.new(pagename)
    local output = ''
 
    if not h.isempty(pagename)
        then
            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.lua_isexists('File:'..pagename)
                    then return "File"
                    elseif h.lua_isexists('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.lua_getPageType(pagename)
    local namespace = p.lua_getNamespace(pagename)
    local content = ''
    local s_match = '{{Marvel Database:(.-) Template'
    local output = namespace
 
    if namespace == 'Main'
        then
            content = h.lua_getContent(pagename)
            if not h.isempty(content)
                then output = string.match(content, s_match) or 'Unknown'
            end
    end
 
    return output
end
 
 
-- returns 'true' if page is a disambiguation
function p.lua_isDisambiguation(pagename)
    local content = ''
    local i
    local j
    local k
    local output = false
    local n = 0
 
    if h.lua_isexists(pagename)
        then
            content = h.lua_getContent(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.lua_getDisambiguationMain(pagename)
    local content = ''
    local i
    local j
    local output = ''
 
    i, j = p.lua_isDisambiguation(pagename)
 
    if i
        then
            content = h.lua_getContent(pagename)
            if j == 1 -- Template:DisambiguationFull
                then output = h.lua_getFieldValue(content, 'main')
                elseif j == 2 -- Template:Disambiguation
                    then output = h.lua_getFieldValue(content, 'Main Character')
                    elseif j == 3 -- Template:Disambig
                        then output = h.lua_getFieldValue(content, 'MainPage')
            end
            output = h.lua_breaklink(output, 1)
    end
 
    return output
end
 
 
--Check if "page" is redirect or not
function p.lua_isRedirect(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.lua_getRedirectTarget(page)
    local content = ''
    local output = ''
 
    if not h.isempty(page) and p.lua_isRedirect(page)
        then 
            content = h.lua_getContent(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
Community content is available under CC-BY-SA unless otherwise noted.

Fandom may earn an affiliate commission on sales made from links on this page.

Stream the best stories.

Fandom may earn an affiliate commission on sales made from links on this page.

Get Disney+