Modul:Vorlage:Soft redirect

Die Dokumentation für dieses Modul kann unter Modul:Vorlage:Soft redirect/Doku erstellt werden

local Softredirect = { suite   = "Softredirect",
                       serial  = "2020-12-16",
                       item    = 104215207,
                       globals = { Multilingual = 47541920 }
                     }
--[=[
Q4844001 Template:Soft redirect
]=]
local Failsafe  = Softredirect
local GlobalMod = Softredirect



Softredirect.trsl = { I18n       = "I18n/Template:Soft redirect",
                      err_target = { en = "missing target",
                                     cs = "¸ádný cíl",
                                     de = "Ziel fehlt" },
                      err_syntax = { en = "no link syntax",
                                     de = "Keine Wikisyntax" }
                    }



local foreignModule = function ( access, advanced, append, alt, alert )
    -- Fetch global module
    -- Precondition:
    --     access    -- string, with name of base module
    --     advanced  -- true, for require(); else mw.loadData()
    --     append    -- string, with subpage part, if any; or false
    --     alt       -- number, of wikidata item of root; or false
    --     alert     -- true, for throwing error on data problem
    -- Postcondition:
    --     Returns whatever, probably table
    -- 2020-01-01
    local storage = access
    local finer = function ()
                      if append then
                          storage = string.format( "%s/%s",
                                                   storage,
                                                   append )
                      end
                  end
    local fun, lucky, r, suited
    if advanced then
        fun = require
    else
        fun = mw.loadData
    end
    GlobalMod.globalModules = GlobalMod.globalModules or { }
    suited = GlobalMod.globalModules[ access ]
    if not suited then
        finer()
        lucky, r = pcall( fun,  "Module:" .. storage )
    end
    if not lucky then
        if not suited  and
           type( alt ) == "number"  and
           alt > 0 then
            suited = string.format( "Q%d", alt )
            suited = mw.wikibase.getSitelink( suited )
            GlobalMod.globalModules[ access ] = suited or true
        end
        if type( suited ) == "string" then
            storage = suited
            finer()
            lucky, r = pcall( fun, storage )
        end
        if not lucky and alert then
            error( "Missing or invalid page: " .. storage )
        end
    end
    return r
end -- foreignModule()



local function faraway( alert )
    -- Retrieve message in appropriate language
    -- Parameter:
    --     alert  -- string, with message key
    -- Returns string
    local got, r
    if type( Softredirect.ext ) ~= "table" then
        Softredirect.ext = { }
    end
    got = Softredirect.ext.Multilingual
    if not got  and  got ~= false then
        local item = Softredirect.globals.Multilingual
        got = foreignModule( "Multilingual", true, false, item )
        if type( got ) == "table"   and
           type( got.Multilingual ) == "function" then
            got = got.Multilingual()
        else
            got = false
        end
        Softredirect.ext.Multilingual = got
    end
    if got   and
       type( got.tabData ) == "function" then
        local r1, r2 = got.tabData( Softredirect.trsl.I18n, alert )
        if r2 ~= "error" then
            r = r1
        end
    end
    if not r then
        local slang = mw.language.getContentLanguage():getCode()
        local trsl  = Softredirect.trsl[ alert ]
        slang = slang:match( "^(%l+)%-?.*" )
        r     = trsl[ slang ]  or  trsl.en
    end
    return r
end -- faraway()



local function fault( alert )
    -- Format error message
    -- Parameter:
    --     alert  -- string, with complaint
    -- Returns string
    local e = mw.html.create( "span" )
                     :attr( "class", "error" )
                     :wikitext( alert )
    return tostring( e )
end -- fault()



Failsafe.failsafe = function ( atleast )
    -- Retrieve versioning and check for compliance
    -- Precondition:
    --     atleast  -- string, with required version
    --                         or wikidata|item|~|@ or false
    -- Postcondition:
    --     Returns  string  -- with queried version/item, also if problem
    --              false   -- if appropriate
    -- 2020-08-17
    local since = atleast
    local last    = ( since == "~" )
    local linked  = ( since == "@" )
    local link    = ( since == "item" )
    local r
    if last  or  link  or  linked  or  since == "wikidata" then
        local item = Failsafe.item
        since = false
        if type( item ) == "number"  and  item > 0 then
            local suited = string.format( "Q%d", item )
            if link then
                r = suited
            else
                local entity = mw.wikibase.getEntity( suited )
                if type( entity ) == "table" then
                    local seek = Failsafe.serialProperty or "P348"
                    local vsn  = entity:formatPropertyValues( seek )
                    if type( vsn ) == "table"  and
                       type( vsn.value ) == "string"  and
                       vsn.value ~= "" then
                        if last  and  vsn.value == Failsafe.serial then
                            r = false
                        elseif linked then
                            if mw.title.getCurrentTitle().prefixedText
                               ==  mw.wikibase.getSitelink( suited ) then
                                r = false
                            else
                                r = suited
                            end
                        else
                            r = vsn.value
                        end
                    end
                end
            end
        end
    end
    if type( r ) == "nil" then
        if not since  or  since <= Failsafe.serial then
            r = Failsafe.serial
        else
            r = false
        end
    end
    return r
end -- Failsafe.failsafe()



-- Export
local p = { }

function p.f( frame )
    local params = frame:getParent().args
    local shift = mw.text.trim( params[ 1 ] or "" )
    local r
    if shift:sub( 1, 1 ) == ":" then
        shift = mw.text.trim( shift:sub( 2 ) )
    end
    if shift == "" then
        r = fault( faraway( "err_target" ) )
    else
        if shift:match( "^%[%[.+%]%]$" )   or
           shift:match( "^%[%l*:?//[^/%s]+%.%l%l.+%]$" ) then
            r = shift
        elseif shift:match( "^%l*:?//[^/%[%]<>%s]+%.%l%l[^<>%[%]]+ [^<>%[%]]+$" ) then
            r = string.format( "[%s]", shift )
        else
            if shift:match( "[%[|%]]" ) then
                r = fault( faraway( "err_syntax" ) )
            else
                local show = mw.text.trim( params[ 2 ] or "" )
                local s    = shift .. "/"
                if show == "" then
                    show = shift
                end
                if s:match( "^%l*:?//[^/%s]+%.%l%l+/%S*$" ) then
                    r = string.format( "[%s %s]", shift, show )
                else
                    r = string.format( "[[:%s|%s]]", shift, show )
                end
            end
        end
    end
    return r
end

p.failsafe = function ( frame )
    -- Versioning interface
    local s = type( frame )
    local since
    if s == "table" then
        since = frame.args[ 1 ]
    elseif s == "string" then
        since = frame
    end
    if since then
        since = mw.text.trim( since )
        if since == "" then
            since = false
        end
    end
    return Failsafe.failsafe( since )  or  ""
end -- p.failsafe

p.Softredirect = function ()
    return Softredirect
end -- p.Softredirect

return p