Jump to navigation Jump to search

Module:Namespace detect

1,850 bytes removed, 06:32, 27 October 2019
18 revisions imported
--[[---------------------------------------------------------------------------------- ---- NAMESPACE DETECT ---- ---- This module implements the {{namespace detect}} template in Lua, with a ---- in Lua, with a few improvements: all namespaces and all ---- namespace aliases are supported, and namespace names are ---- and namespace names are detected automatically for the local wiki. Function names The ---- 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 ---- by altering the values in the "cfg" tablein ---- Module:Namespace detect/config. ---- ------------------------------------------------------------------------------------]]
----------------------------------------------------------------------local data = mw.loadData('Module:Namespace detect/data')-- Configuration local argKeys = data --.argKeys-- Language-specific parameter names can be set herelocal cfg = data. --cfg----------------------------------------------------------------------local mappings = data.mappings
local cfg yesno = {}require('Module:Yesno')local mArguments -- Lazily initialise Module:Argumentslocal mTableTools -- Lazily initilalise Module:TableToolslocal ustringLower = mw.ustring.lower
-- The name for the parameter to display content for the main namespace:cfg.main local p = 'main'{}
local function fetchValue(t1, t2) -- The name Fetches a value from the table t1 for the parameter to display content first key in talk namespaces:array t2 wherecfg -- a non-nil value of t1 for i, key in ipairs(t2) do local value = t1[key] if value ~= 'talk'nil then return value end end return nilend
-- The name for the parameter to display content for "other" namespaces-- local function equalsArrayValue(namespaces for which parameters have not been specifiedt, or for when-- cfg.demospace is set to cfg.othervalue):cfg.other = 'other'  -- The name for Returns true if value equals a value in the parameter to set a demonstration namespace:cfgarray t.demospace = 'demospace'Otherwise -- The name for the parameter to set a specific page to compare:cfgreturns = 'page' -- The header for the namespace column i, arrayValue in the wikitable containing the ipairs(t) do-- list of possible subject-space parameters.cfg.wikitableNamespaceHeader if value = 'Namespace' -- The header for the wikitable containing the list of possible-- subject-space parameters.cfg.wikitableAliasesHeader = 'Aliases'arrayValue then return true---------------------------------------------------------------------- end-- End configuration data -- end---------------------------------------------------------------------- return false -- Specify variables available to the whole modulelocal p = {}local args = {}end
-- Get the page object. This will return the page object for the page-- specified, or nil if there are errors in the title or if the-- expensive function count has been exceededp.local function getPageObject(page) -- Get the title object for if it is specified. Otherwise -- get the title object for the current page. if args[] then -- Get the page object, passing the function through pcall in case of -- in case we are errors, e.g. being over the expensive function count limit. if page then local noErrorsuccess, pageObject = pcall(, args[]) if not noError success then return nilpageObject else return pageObjectnil end else return mw.title.getCurrentTitle() end
-- Detects the namespace Provided for a page object.backward compatibility with other moduleslocal function detectNamespacep.getParamMappings(pageObject) if pageObject.isTalkPage then -- Returns the value of or the local "Talk" namespace name. return or[1].name else return pageObject.nsText endmappings
local function getNamespace(args) -- Gets This function gets the namespace name to compare to from the argumentspage object. local page = fetchValue(args, argKeys. The returned valuedemopage) if page == '' then page = nil end-- is lower-case local demospace = fetchValue(args, argKeys.demospace) if demospace == '' then demospace = nil end local function getNamespacesubjectns = fetchValue(args, argKeys.subjectns) local ret if args[cfg.demospace] then -- Handle "demospace = main" properly. if mwequalsArrayValue(argKeys.ustring.lowermain, ustringLower( args[cfg.demospace] ) == cfg.main ) then ret =[0].name else ret = args[cfg.demospace] end else local pageObject = p.getPageObject(page) if pageObject then if pageObject .isTalkPage then -- Get the subject namespace if the option is set, -- otherwise use "talk". if yesno(subjectns) then ret = detectNamespace( getPageObject() )[pageObject.namespace] 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 mw.ustring.lowerustringLower(ret)
function p._main(args) -- Compare the namespace found with Check the parameters that have been-- specified, and return content of stored in the appropriate parametermappings table for any matches.local function compare() local namespace = getNamespace(args) or 'other' -- First, compare mainspace parameters."other" avoids nil table keys if namespace = local params =[0namespace].name and args[cfg.main] thenor {} return args[cfg.main] end -- Next, compare parameters for non-main namespaces. for nsid, ns in pairs( ) do local nsname ret = mw.ustring.lowerfetchValue( args, params) local canonicalName = mw.ustring.lower( ns.canonicalName ) -- Check the namespace, and ignore main namespace values. if nsid ~= 0 and nsname == namespace then -- Check local namespace name. if args[nsname] then return args[nsname] -- Check canonical namespace name. elseif args[canonicalName] then return args[canonicalName] else -- Check alias names. for _, alias in ipairs( ns.aliases ) do local aliasArg = args[ mw.ustring.lower( alias ) ] if aliasArg then return aliasArg end end end end end -- FinallyIf there were no matches, return parameters for other namespaces. -- This happens if -- there was no text specified for the namespace that was detected -- was detected or if the demospace parameter is not a valid -- namespace. Note that -- the parameter for the detected namespace must be completely -- completely absent for this to happen, not merely blank. --]] if args[cfg.other] ret == nil then return ret = fetchValue(args[cfg, argKeys.other]) end return ret
-- Process the arguments.
function p.main(frame)
-- If called via #invoke, use the args passed into the invoking -- template, or the args passed to #invoke if any exist. Otherwise -- assume args are being passed directly in. local origArgs if frame mArguments == mw.getCurrentFramerequire() then origArgs = frame'Module:getParent(Arguments').args for k, v in pairs( frame. local args ) do origArgs = framemArguments.args break end else origArgs = getArgs(frame end -- Trim whitespace and remove blank arguments for demospace and -- page parameters. for k, v in pairs(origArgs{removeBlanks = false}) do v local ret = mwp.text.trim_main(vargs) -- Trim whitespace. if k == cfg.demospace return ret or k == then if v ~= '' then args[k] = v end else args[k] = v end end return compare()
function p.table(frame) --[[ -- Create a wikitable of all possible subject namespace parameters, for -- documentation purposes.The talk parameter is optional, in case itfunction p -- needs to be excluded in the documentation.table() --]] -- Start the wikitableLoad modules and initialise variables. local ret mTableTools = require('{| class="wikitable"Module:TableTools') local namespaces = '\n|-'namespaces . local cfg = data. cfg local useTalk = type(frame) == '\n! table' .. cfg.wikitableNamespaceHeader . and type(frame. args) == '\n! table' and yesno(frame.args. cfgtalk) -- Whether to use the talk parameter.wikitableAliasesHeader -- Generate the row for Get the main namespaceheader names. ret local function checkValue(value, default) if type(value) == ret .. '\n|-string'then return value . else return default end end local nsHeader = checkValue(cfg. wikitableNamespaceHeader, '\n| Namespace' .. ) local aliasesHeader = checkValue(cfg.main .. wikitableAliasesHeader, '\n|Aliases') -- Generate Put the other wikitable rowsnamespaces in order. local mappingsOrdered = {} for nsidnsname, ns params in pairs( mappings) do if nsid useTalk or nsname ~= 0 'talk' then local nsid = namespaces[nsname].id -- Ignore the main namespaceAdd 1, as it is set in cfgthe array must start with 1; nsid 0 would be lost otherwise. local name nsid = nsid + 1 mappingsOrdered[nsid] = params end end mappingsOrdered = '<code>' mTableTools.. mw.ustring.lowercompressSparseArray( mappingsOrdered)   -- Build the table.. '</code>' local aliases ret = '{}| class="wikitable"' if ns .canonicalName ~= then'\n|-' table ..insert( aliases, '<code>\n! ' .. mwnsHeader .ustring.lower( ns'\n! ' .canonicalName ) .. '</code>' aliasesHeader for i, params in ipairs(mappingsOrdered)do end for _j, v param in ipairs( ns.aliases params) do table if j == 1 then ret = ret .. '\n|-' ..insert( aliases, '\n| <code>' .. mw.ustring.lower(v) param .. '</code>' ) end ret = ret .. '\n|-' elseif j == 2 then ret = ret .. '\n| <code>' .. nameparam .. '</code>' else ret = ret .. '\n| , <code>' .. tableparam ..concat( aliases, ', </code>' ) end end end -- End the wikitable. ret = ret .. '\n|-' .. '\n|}' return ret
return p

Navigation menu