Modul:Namespace detect: Unterschied zwischen den Versionen

K
Schützte „Modul:Namespace detect“: Häufig eingebundene Vorlage ([Bearbeiten=Nur Administratoren erlauben] (unbeschränkt) [Verschieben=Nur Administratoren erlauben] (unbeschränkt))
(use demopage instead of page as the main "page" parameter)
K (Schützte „Modul:Namespace detect“: Häufig eingebundene Vorlage ([Bearbeiten=Nur Administratoren erlauben] (unbeschränkt) [Verschieben=Nur Administratoren erlauben] (unbeschränkt)))
Zeile 1: Zeile 1:
--[[
----------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------
--                                                                                               --
--                                                                           --
--                                           NAMESPACE DETECT                                     --
--                           NAMESPACE DETECT                               --
--                                                                                               --
--                                                                           --
--     This module implements the {{namespace detect}} template in Lua, with a few              --
-- This module implements the {{namespace detect}} template in Lua, with a   --
--     improvements: all namespaces and all namespace aliases are supported, and namespace      --
-- few improvements: all namespaces and all namespace aliases are supported, --
--     names are detected automatically for the local wiki. Function names can be configured     --
-- and namespace names are detected automatically for the local wiki. The    --
--     for different wikis by altering the values in the "cfg" table.                            --
-- module can also use the corresponding subject namespace value if it is     --
--                                                                                               --
-- used on a talk page. Parameter names can be configured for different wikis --
----------------------------------------------------------------------------------------------------
-- by altering the values in the "cfg" table in                              --
-- Module:Namespace detect/config.                                            --
--                                                                           --
--------------------------------------------------------------------------------
--]]


local data = mw.loadData('Module:Namespace detect/data')
----------------------------------------------------------------------------------------------------
local argKeys = data.argKeys
--                                          Configuration data                                   --
local cfg = data.cfg
--      Language-specific parameter names can be set here.                                       --
local mappings = data.mappings
----------------------------------------------------------------------------------------------------


local yesno = require('Module:Yesno')
local cfg = {}
local mArguments -- Lazily initialise Module:Arguments
local mTableTools -- Lazily initilalise Module:TableTools
local ustringLower = mw.ustring.lower


-- The name for the parameter to display content for the main namespace:
cfg.main = 'main'
-- The name for the parameter to display content in talk namespaces:
cfg.talk = 'talk'
-- The name for the parameter to display content for "other" namespaces (namespaces for which
-- parameters have not been specified, or for when cfg.demospace is set to cfg.other):
cfg.other = 'other'
-- The name for the parameter to set a demonstration namespace:
cfg.demospace = 'demospace'
-- The name for the parameter to set a specific page to compare:
cfg.page = 'page'
-- The header for the namespace column in the wikitable containing the list of possible
-- subject-space parameters.
cfg.wikitableNamespaceHeader = 'Namespace'
-- The header for the wikitable containing the list of possible subject-space parameters.
cfg.wikitableAliasesHeader = 'Aliases'
----------------------------------------------------------------------------------------------------
--                                      End configuration data                                  --
----------------------------------------------------------------------------------------------------
-- Declare the table of functions to return.
local p = {}
local p = {}


local function fetchValue(t1, t2)
-- Get the page object. This will return the page object for the page specified, or nil if there are
-- Fetches a value from the table t1 for the first key in array t2 where
-- errors in the title or if the expensive function count has been exceeded.
-- a non-nil value of t1 exists.
function p.getPageObject(page)
for i, key in ipairs(t2) do
    if page then
local value = t1[key]
        -- Get the page object, passing the function through pcall in case we are over the expensive
if value ~= nil then
-- function count limit.
return value
        local noError, pageObject = pcall(mw.title.new, page)
end
        if not noError then
end
            return nil
return nil
        else
            return pageObject
        end
    else
        return mw.title.getCurrentTitle()
    end   
end
end


local function equalsArrayValue(t, value)
--[[ Returns a table of how parameter names map to namespace names. The keys are the actual namespace
-- Returns true if value equals a value in the array t. Otherwise
  names, in lower case, and the values are the possible parameter names for that namespace, also in
-- returns false.
  lower case. The table entries are structured like this:
for i, arrayValue in ipairs(t) do
    [''] = {
if value == arrayValue then
        {'main'},
return true
    },
end
    ['wikipedia'] = {
end
        {'wikipedia', 'project', 'wp'}
return false
    }
]]
function p.getParamMappings()
    local mappings = {}
    mappings[mw.ustring.lower(mw.site.namespaces[0].name)] = {cfg.main}
    mappings[cfg.talk] = {cfg.talk}
    for nsid, ns in pairs(mw.site.subjectNamespaces) do
        if nsid ~= 0 then -- Exclude main namespace.
            local nsname = mw.ustring.lower(ns.name)
            local canonicalName = mw.ustring.lower(ns.canonicalName)
            mappings[nsname] = {nsname}
            if canonicalName ~= nsname then
                table.insert(mappings[nsname], canonicalName)
            end
            for _, alias in ipairs(ns.aliases) do
                table.insert(mappings[nsname], mw.ustring.lower(alias))
            end
        end
    end
    return mappings
end
end


function p.getPageObject(page)
--[[ Create a wikitable of all subject namespace parameters, for documentation purposes. The talk
-- Get the page object, passing the function through pcall in case of
  parameter is optional, in case it needs to be excluded in the documentation.
-- errors, e.g. being over the expensive function count limit.
]]
if page then
function p.table(frame)
local success, pageObject = pcall(mw.title.new, page)
-- Find whether to use the talk link or not.
if success then
    local useTalk = type(frame) == 'table' and type(frame.args) == 'table' and frame.args.talk == 'yes'
return pageObject
 
else
    -- Get the parameter mappings.
return nil
    local mappings = p.getParamMappings()
end
   
else
    -- Start the wikitable.
return mw.title.getCurrentTitle()
    local ret = '{| class="wikitable"'
end
        .. '\n|-'
end
        .. '\n! ' .. cfg.wikitableNamespaceHeader
        .. '\n! ' .. cfg.wikitableAliasesHeader
   
    -- Generate the row for the main namespace, as we want this
    -- to be first in the list.
    ret = ret .. '\n|-'
        .. '\n| <code>' .. cfg.main .. '</code>'
        .. '\n|'


-- Provided for backward compatibility with other modules
    if useTalk then
function p.getParamMappings()
        ret = ret .. '\n|-'
return mappings
            .. '\n| <code>' .. cfg.talk .. '</code>'
            .. '\n|'
    end
       
    -- Enclose all parameter names in <code> tags.
    for ns, params in pairs(mappings) do
        if ns ~= mw.site.namespaces[0].name then
            for i, param in ipairs(params) do
                mappings[ns][i] = '<code>' .. param .. '</code>'
            end
        end
    end
   
    -- Generate the other wikitable rows.
    for ns, params in pairs(mappings) do
        if ns ~= mw.site.namespaces[0].name then -- Ignore the main namespace.
            ret = ret .. '\n|-'
                .. '\n| ' .. params[1]
                .. '\n| ' .. table.concat(params, ', ', 2)
        end
    end
   
    -- End the wikitable.
    ret = ret .. '\n|-'
        .. '\n|}'
   
    return ret
end
end


local function getNamespace(args)
-- Gets the namespace name to compare to the arguments. The returned value is lower-case.
-- This function gets the namespace name from the page object.
local function getNamespace(page, demospace)
local page = fetchValue(args, argKeys.demopage)
    local ret
if page == '' then
    if demospace then
page = nil
        -- Handle "demospace = main" properly.
end
        if mw.ustring.lower(demospace) == cfg.main then
local demospace = fetchValue(args, argKeys.demospace)
            ret = mw.site.namespaces[0].name
if demospace == '' then
        else
demospace = nil
            ret = demospace
end
        end
local subjectns = fetchValue(args, argKeys.subjectns)
    else
local ret
        local pageObject = p.getPageObject(page)
if demospace then
        if pageObject then
-- Handle "demospace = main" properly.
            if pageObject.isTalkPage then
if equalsArrayValue(argKeys.main, ustringLower(demospace)) then
                -- {{namespace detect}} uses the same value for all talk namespaces, so that's what
ret = mw.site.namespaces[0].name
-- the module should do too.
else
                ret = cfg.talk
ret = demospace
            else
end
                ret = pageObject.nsText
else
            end
local pageObject = p.getPageObject(page)
        else
if pageObject then
            return nil -- return nil if the page object doesn't exist.
if pageObject.isTalkPage then
        end
-- Get the subject namespace if the option is set,
    end
-- otherwise use "talk".
    return mw.ustring.lower(ret)
if yesno(subjectns) then
ret = mw.site.namespaces[pageObject.namespace].subject.name
else
ret = 'talk'
end
else
ret = pageObject.nsText
end
else
return nil -- return nil if the page object doesn't exist.
end
end
ret = ret:gsub('_', ' ')
return ustringLower(ret)
end
end


-- Compare the namespace found with the parameters that have been specified, and return content of
-- the appropriate parameter.
function p._main(args)
function p._main(args)
-- Check the parameters stored in the mappings table for any matches.
    -- Get the namespace to compare the parameters to, and the parameter mapping table.
local namespace = getNamespace(args) or 'other' -- "other" avoids nil table keys
    local namespace = getNamespace(args[cfg.page], args[cfg.demospace])
local params = mappings[namespace] or {}
    local mappings = p.getParamMappings()
local ret = fetchValue(args, params)
   
--[[
    -- Check for any matches in the namespace arguments. The order we check them doesn't matter,
-- If there were no matches, return parameters for other namespaces.
-- as there can only be one match.
-- This happens if there was no text specified for the namespace that
    for ns, params in pairs(mappings) do
-- was detected or if the demospace parameter is not a valid
        if ns == namespace then
-- namespace. Note that the parameter for the detected namespace must be
            -- Check all aliases for matches. The default local namespace is checked first, as
-- {{namespace detect}} checked these before alias names.
            for _, param in ipairs(params) do
                if args[param] then
                    return args[param]
                end
            end
        end
    end
   
    -- If there were no matches, return parameters for other namespaces. This happens if there
-- was no text specified for the namespace that was detected or if the demospace parameter
-- is not a valid namespace. Note that the parameter for the detected namespace must be
-- completely absent for this to happen, not merely blank.
-- completely absent for this to happen, not merely blank.
--]]
    if args[cfg.other] then
if ret == nil then
        return args[cfg.other]
ret = fetchValue(args, argKeys.other)
    end
end
return ret
end
end


function p.main(frame)
function p.main(frame)
mArguments = require('Module:Arguments')
    -- If called via #invoke, use the args passed into the invoking template, or the args
local args = mArguments.getArgs(frame, {removeBlanks = false})
-- passed to #invoke if any exist. Otherwise assume args are being passed directly in.
local ret = p._main(args)
    local origArgs
return ret or ''
    if frame == mw.getCurrentFrame() then
end
        origArgs = frame:getParent().args
 
        for k, v in pairs(frame.args) do
function p.table(frame)
            origArgs = frame.args
--[[
            break
-- Create a wikitable of all subject namespace parameters, for
        end
-- documentation purposes. The talk parameter is optional, in case it
    else
-- needs to be excluded in the documentation.
        origArgs = frame
--]]
    end
   
-- Load modules and initialise variables.
    -- Trim whitespace and remove blank arguments for demospace and page parameters.
mTableTools = require('Module:TableTools')
    local args = {}
local namespaces = mw.site.namespaces
    for k, v in pairs(origArgs) do
local cfg = data.cfg
        if type(v) == 'string' then
local useTalk = type(frame) == 'table'
            v = mw.text.trim(v) -- Trim whitespace.
and type(frame.args) == 'table'
        end
and yesno(frame.args.talk) -- Whether to use the talk parameter.
        if k == cfg.demospace or k == cfg.page then
            if v ~= '' then
-- Get the header names.
                args[k] = v
local function checkValue(value, default)
            end
if type(value) == 'string' then
        else
return value
            args[k] = v
else
        end
return default
    end
end
   
end
    return p._main(args)
local nsHeader = checkValue(cfg.wikitableNamespaceHeader, 'Namespace')
local aliasesHeader = checkValue(cfg.wikitableAliasesHeader, 'Aliases')
 
-- Put the namespaces in order.
local mappingsOrdered = {}
for nsname, params in pairs(mappings) do
if useTalk or nsname ~= 'talk' then
local nsid = namespaces[nsname].id
-- Add 1, as the array must start with 1; nsid 0 would be lost otherwise.
nsid = nsid + 1
mappingsOrdered[nsid] = params
end
end
mappingsOrdered = mTableTools.compressSparseArray(mappingsOrdered)
 
-- Build the table.
local ret = '{| class="wikitable"'
.. '\n|-'
.. '\n! ' .. nsHeader
.. '\n! ' .. aliasesHeader
for i, params in ipairs(mappingsOrdered) do
for j, param in ipairs(params) do
if j == 1 then
ret = ret .. '\n|-'
.. '\n| <code>' .. param .. '</code>'
.. '\n| '
elseif j == 2 then
ret = ret .. '<code>' .. param .. '</code>'
else
ret = ret .. ', <code>' .. param .. '</code>'
end
end
end
ret = ret .. '\n|-'
.. '\n|}'
return ret
end
end


return p
return p
Anonymer Benutzer