Module:IfExistLink

From JoJo's Bizarre Encyclopedia - JoJo Wiki
Jump to navigation Jump to search

Documentation for this module may be created at Module:IfExistLink/doc

local p = {}

-- Function to check if a given page exists
function p.ifExists(titleName)
    local titleObj = mw.title.new(titleName)
    if titleObj and titleObj.exists then
        return true
    else
        return false
    end
end

-- Function to generate a language-specific link
function p.link(frame)
    local title = frame.args[1]
    local display = frame.args[2] or title
    local lang = frame.args[3] -- Language code (e.g., "fr", "ru", "pt-br", "pl", "es")

    -- Language code to suffix mapping
    local langSuffix = {
        fr = "/fr",
        ru = "/ru",
        ["pt-br"] = "/pt-br",
        pl = "/pl",
        es = "/es"
    }

    -- Determine the suffix based on the provided language code
    local suffix = langSuffix[lang] or ""
    local titleLang = title .. suffix

    -- Use the ifExists function to check if the language-specific page exists
    if p.ifExists(titleLang) then
        -- The language-specific page exists
        return string.format("[[%s|%s]]", titleLang, display)
    else
        -- The language-specific page does not exist, link to the base page
        return string.format("[[%s|%s]]", title, display)
    end
end

return p