Module:Unsubst and Module:Namespace detect/data: Difference between pages

From Frontierpedia, the Microsoft Agent encyclopedia
(Difference between pages)
m (1 revision imported)
 
en>MusikAnimal
m (1 revision imported)
 
Line 1: Line 1:
local checkType = require('libraryUtil').checkType
--------------------------------------------------------------------------------
--                          Namespace detect data                            --
-- This module holds data for [[Module:Namespace detect]] to be loaded per    --
-- page, rather than per #invoke, for performance reasons.                   --
--------------------------------------------------------------------------------


local p = {}
local cfg = require('Module:Namespace detect/config')


local BODY_PARAM = '$B'
local function addKey(t, key, defaultKey)
 
if key ~= defaultKey then
local specialParams = {
t[#t + 1] = key
['$params'] = 'parameter list',
['$aliases'] = 'parameter aliases',
['$flags'] = 'flags',
['$B'] = 'template content',
['$template-name'] = 'template invocation name override',
}
 
function p.main(frame, body)
-- If we are substing, this function returns a template invocation, and if
-- not, it returns the template body. The template body can be specified in
-- the body parameter, or in the template parameter defined in the
-- BODY_PARAM variable. This function can be called from Lua or from
-- #invoke.
 
-- Return the template body if we aren't substing.
if not mw.isSubsting() then
if body ~= nil then
return body
elseif frame.args[BODY_PARAM] ~= nil then
return frame.args[BODY_PARAM]
else
error(string.format(
"no template content specified (use parameter '%s' from #invoke)",
BODY_PARAM
), 2)
end
end
end
end


-- Sanity check for the frame object.
-- Get a table of parameters to query for each default parameter name.
if type(frame) ~= 'table'
-- This allows wikis to customise parameter names in the cfg table while
or type(frame.getParent) ~= 'function'
-- ensuring that default parameter names will always work. The cfg table
or not frame:getParent()
-- values can be added as a string, or as an array of strings.
then
error(
"argument #1 to 'main' must be a frame object with a parent " ..
"frame available",
2
)
end


-- Find the invocation name.
local defaultKeys = {
local mTemplateInvocation = require('Module:Template invocation')
'main',
local name
'talk',
'other',
'subjectns',
'demospace',
'demopage'
}


if frame.args['$template-name'] and '' ~= frame.args['$template-name'] then
local argKeys = {}
name = frame.args['$template-name'] -- override whatever the template name is with this name
for i, defaultKey in ipairs(defaultKeys) do
else
argKeys[defaultKey] = {defaultKey}
name = mTemplateInvocation.name(frame:getParent():getTitle())
end
end
 
-- Combine passed args with passed defaults
local args = {}
if string.find( ','..(frame.args['$flags'] or '')..',', ',%s*override%s*,' ) then
for k, v in pairs( frame:getParent().args ) do
args[k] = v
end
for k, v in pairs( frame.args ) do
if not specialParams[k] then
if v == '__DATE__' then
v = mw.getContentLanguage():formatDate( 'F Y' )
end
args[k] = v
end
end
else
for k, v in pairs( frame.args ) do
if not specialParams[k] then
if v == '__DATE__' then
v = mw.getContentLanguage():formatDate( 'F Y' )
end
args[k] = v
end
end
for k, v in pairs( frame:getParent().args ) do
args[k] = v
end
end
 
-- Trim parameters, if not specified otherwise
if not string.find( ','..(frame.args['$flags'] or '')..',', ',%s*keep%-whitespace%s*,' ) then
for k, v in pairs( args ) do args[k] = mw.ustring.match(v, '^%s*(.*)%s*$') or '' end
end


-- Pull information from parameter aliases
for defaultKey, t in pairs(argKeys) do
local aliases = {}
local cfgValue = cfg[defaultKey]
if frame.args['$aliases'] then
local cfgValueType = type(cfgValue)
local list = mw.text.split( frame.args['$aliases'], '%s*,%s*' )
if cfgValueType == 'string' then
for k, v in ipairs( list ) do
addKey(t, cfgValue, defaultKey)
local tmp = mw.text.split( v, '%s*>%s*' )
elseif cfgValueType == 'table' then
aliases[tonumber(mw.ustring.match(tmp[1], '^[1-9][0-9]*$')) or tmp[1]] = ((tonumber(mw.ustring.match(tmp[2], '^[1-9][0-9]*$'))) or tmp[2])
for i, key in ipairs(cfgValue) do
addKey(t, key, defaultKey)
end
end
end
end
for k, v in pairs( aliases ) do
cfg[defaultKey] = nil -- Free the cfg value as we don't need it any more.
if args[k] and ( not args[v] or args[v] == '' ) then
end
args[v] = args[k]
end
args[k] = nil
end


-- Remove empty parameters, if specified
local function getParamMappings()
if string.find( ','..(frame.args['$flags'] or '')..',', ',%s*remove%-empty%s*,' ) then
--[[
local tmp = 0
-- Returns a table of how parameter names map to namespace names. The keys
for k, v in ipairs( args ) do
-- are the actual namespace names, in lower case, and the values are the
if v ~= '' or ( args[k+1] and args[k+1] ~= '' ) or ( args[k+2] and args[k+2] ~= '' ) then
-- possible parameter names for that namespace, also in lower case. The
tmp = k
-- table entries are structured like this:
else
-- {
break
--  [''] = {'main'},
--  ['wikipedia'] = {'wikipedia', 'project', 'wp'},
--  ...
-- }
--]]
local mappings = {}
local mainNsName = mw.site.subjectNamespaces[0].name
mainNsName = mw.ustring.lower(mainNsName)
mappings[mainNsName] = mw.clone(argKeys.main)
mappings['talk'] = mw.clone(argKeys.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
end
end
for _, alias in ipairs(ns.aliases) do
for k, v in pairs( args ) do
table.insert(mappings[nsname], mw.ustring.lower(alias))
if v == '' then
if not (type(k) == 'number' and k < tmp) then args[k] = nil end
end
end
end
end
end
end
 
return mappings
-- Order parameters
if frame.args['$params'] then
local params, tmp = mw.text.split( frame.args['$params'], '%s*,%s*' ), {}
for k, v in ipairs(params) do
v = tonumber(mw.ustring.match(v, '^[1-9][0-9]*$')) or v
if args[v] then tmp[v], args[v] = args[v], nil end
end
for k, v in pairs(args) do tmp[k], args[k] = args[k], nil end
args = tmp
end
 
return mTemplateInvocation.invocation(name, args)
end
end


p[''] = p.main -- For backwards compatibility
return {
 
argKeys = argKeys,
return p
cfg = cfg,
mappings = getParamMappings()
}

Revision as of 06:12, 1 April 2020

Documentation for this module may be created at Module:Namespace detect/data/doc

--------------------------------------------------------------------------------
--                          Namespace detect data                             --
-- This module holds data for [[Module:Namespace detect]] to be loaded per    --
-- page, rather than per #invoke, for performance reasons.                    --
--------------------------------------------------------------------------------

local cfg = require('Module:Namespace detect/config')

local function addKey(t, key, defaultKey)
	if key ~= defaultKey then
		t[#t + 1] = key
	end
end

-- Get a table of parameters to query for each default parameter name.
-- This allows wikis to customise parameter names in the cfg table while
-- ensuring that default parameter names will always work. The cfg table
-- values can be added as a string, or as an array of strings.

local defaultKeys = {
	'main',
	'talk',
	'other',
	'subjectns',
	'demospace',
	'demopage'
}

local argKeys = {}
for i, defaultKey in ipairs(defaultKeys) do
	argKeys[defaultKey] = {defaultKey}
end

for defaultKey, t in pairs(argKeys) do
	local cfgValue = cfg[defaultKey]
	local cfgValueType = type(cfgValue)
	if cfgValueType == 'string' then
		addKey(t, cfgValue, defaultKey)
	elseif cfgValueType == 'table' then
		for i, key in ipairs(cfgValue) do
			addKey(t, key, defaultKey)
		end
	end
	cfg[defaultKey] = nil -- Free the cfg value as we don't need it any more.
end

local function getParamMappings()
	--[[
	-- Returns a table of how parameter names map to namespace names. The keys
	-- are the actual namespace names, in lower case, and the values are the
	-- possible parameter names for that namespace, also in lower case. The
	-- table entries are structured like this:
	-- {
	--   [''] = {'main'},
	--   ['wikipedia'] = {'wikipedia', 'project', 'wp'},
	--   ...
	-- }
	--]]
	local mappings = {}
	local mainNsName = mw.site.subjectNamespaces[0].name
	mainNsName = mw.ustring.lower(mainNsName)
	mappings[mainNsName] = mw.clone(argKeys.main)
	mappings['talk'] = mw.clone(argKeys.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

return {
	argKeys = argKeys,
	cfg = cfg,
	mappings = getParamMappings()
}