🇮🇷 Iran Proxy | https://www.wikipedia.org/wiki/Module:Link_if_exists
Jump to content

Module:Link if exists

Permanently protected module
From Wikipedia, the free encyclopedia

require('strict')

local p = {}

local function titleExistsUnprotected(titleObject)
    return titleObject.exists
end

-- test for title existing
-- if we get an error accessing titleObject.exists, assume it doesn't exist
local function titleExists(titleObject)
    local success, exists = pcall(titleExistsUnprotected, titleObject)
    return success and exists
end

function p._main(args)
    local title = args[1]
    if not title then
        return title
    end
    local display = args[2] or title
    title = args.prefix and args.prefix..':'..title or title
    local titleObject = mw.title.new(title, args.nsp)
    local result = ''
    if titleObject and titleExists(titleObject) then
       -- use prefix only if args[2] is empty/false
        display = args[2] or title
        result = result..'[['
        result = result..(titleObject.namespace ~= 0 and ':' or '')
        result = result..(titleObject.fullText ~= display and titleObject.fullText..'|' or '')
        result = result..display..']]'
    elseif not args.hide_display then
        result = result..(args.color and '<span style="color:'..args.color..'">' or '')
        result = result..display
        result = result..(args.color and '</span>' or '')
    end
    return result
end

function p.main(frame)
    local getArgs = require('Module:Arguments').getArgs
    local args = getArgs(frame)
    return p._main(args) or ""
end

return p