Editing Module:Pagetype
Jump to navigation
Jump to search
Warning: You are not logged in. Your IP address will be publicly visible if you make any edits. If you log in or create an account, your edits will be attributed to your username, along with other benefits.
The edit can be undone. Please check the comparison below to verify that this is what you want to do, and then publish the changes below to finish undoing the edit.
Latest revision | Your text | ||
Line 16: | Line 16: | ||
local getArgs = require('Module:Arguments').getArgs | local getArgs = require('Module:Arguments').getArgs | ||
local yesno = require('Module:Yesno') | local yesno = require('Module:Yesno') | ||
local | local nsDetectModule = require('Module:Namespace detect') | ||
local nsDetect = nsDetectModule._main | |||
local getParamMappings = nsDetectModule.getParamMappings | |||
local getPageObject = nsDetectModule.getPageObject | |||
local p = {} | local p = {} | ||
local function shallowCopy(t) | |||
local function | -- Makes a shallow copy of a table. | ||
local ret = {} | |||
-- | for k, v in pairs(t) do | ||
ret[k] = v | |||
local | |||
for | |||
end | end | ||
return ret | |||
end | end | ||
local function checkPagetypeInput(namespace, val) | |||
local function | -- Checks to see whether we need the default value for the given namespace, | ||
-- and if so gets it from the pagetypes table. | |||
-- The yesno function returns true/false for "yes", "no", etc., and returns | |||
-- val for other input. | |||
local ret = yesno(val, val) | |||
if ret and type(ret) ~= 'string' then | |||
ret = cfg.pagetypes[namespace] | |||
if | |||
end | end | ||
return ret | |||
return | |||
end | end | ||
-- | local function getPagetypeFromClass(class, param, aliasTable, default) | ||
-- | -- Gets the pagetype from a class specified from the first positional | ||
-- parameter. | |||
if | param = yesno(param, param) | ||
if param ~= false then -- No check if specifically disallowed. | |||
for _, alias in ipairs(aliasTable) do | |||
if class == alias then | |||
if type(param) == 'string' then | |||
return param | |||
else | |||
return default | |||
end | |||
end | |||
end | end | ||
end | end | ||
end | end | ||
local function getNsDetectValue(args) | |||
local function | -- Builds the arguments to pass to [[Module:Namespace detect]] and returns | ||
-- the result. | |||
-- Get | -- Get the default values. | ||
local | local ndArgs = {} | ||
local | local defaultns = args[cfg.defaultns] | ||
if | if defaultns == cfg.defaultnsAll then | ||
ndArgs = shallowCopy(cfg.pagetypes) | |||
else | else | ||
local defaultnsArray | |||
if defaultns == cfg.defaultnsExtended then | |||
defaultnsArray = cfg.extendedNamespaces | |||
elseif defaultns == cfg.defaultnsNone then | |||
defaultnsArray = {} | |||
else | |||
defaultnsArray = cfg.defaultNamespaces | |||
end | |||
for _, namespace in ipairs(defaultnsArray) do | |||
ndArgs[namespace] = cfg.pagetypes[namespace] | |||
end | |||
end | end | ||
local | --[[ | ||
-- Add custom values passed in from the arguments. These overwrite the | |||
-- defaults. The possible argument names are fetched from | |||
-- Module:Namespace detect automatically in case new namespaces are | |||
-- added. Although we accept namespace aliases as parameters, we only pass | |||
-- the local namespace name as a parameter to Module:Namespace detect. | |||
-- This means that the "image" parameter can overwrite defaults for the | |||
-- File: namespace, which wouldn't work if we passed the parameters through | |||
-- separately. | |||
--]] | |||
local mappings = getParamMappings() | |||
for ns, paramAliases in pairs(mappings) do | |||
-- Copy the aliases table, as # doesn't work with tables returned from | |||
-- mw.loadData. | |||
paramAliases = shallowCopy(paramAliases) | |||
local paramName = paramAliases[1] | |||
-- Iterate backwards along the array so that any values for the local | |||
-- namespace names overwrite those for namespace aliases. | |||
for i = #paramAliases, 1, -1 do | |||
local paramAlias = paramAliases[i] | |||
local ndArg = checkPagetypeInput(paramAlias, args[paramAlias]) | |||
if ndArg == false then | |||
-- If any arguments are false, convert them to nil to protect | |||
-- against breakage by future changes to | |||
-- [[Module:Namespace detect]]. | |||
ndArgs[paramName] = nil | |||
elseif ndArg then | |||
ndArgs[paramName] = ndArg | |||
end | |||
end | |||
end | end | ||
-- Check for disambiguation-class and N/A-class pages in mainspace. | |||
-- | if ndArgs.main then | ||
if | local class = args[1] | ||
if type(class) == 'string' then | |||
-- Put in lower case so e.g. "Dab" and "dab" will both match. | |||
class = mw.ustring.lower(class) | |||
end | |||
local dab = getPagetypeFromClass( | |||
class, | |||
args[cfg.dab], | |||
cfg.dabAliases, | |||
cfg.dabDefault | |||
) | |||
if dab then | |||
ndArgs.main = dab | |||
if | |||
else | else | ||
local na = getPagetypeFromClass( | |||
class, | |||
args[cfg.na], | |||
cfg.naAliases, | |||
cfg.naDefault | |||
) | |||
if na then | |||
ndArgs.main = na | |||
end | |||
end | end | ||
end | end | ||
-- If there is no talk value specified, use the corresponding subject | |||
-- namespace for talk pages. | |||
if not ndArgs.talk then | |||
-- | ndArgs.subjectns = true | ||
if | |||
end | end | ||
-- Add the fallback value. This can also be customised, but it cannot be | |||
-- disabled. | |||
local other = args[cfg.other] | |||
-- We will ignore true/false/nil results from yesno here, but using it | |||
-- anyway for consistency. | |||
other = yesno(other, other) | |||
if type(other) == 'string' then | |||
ndArgs.other = other | |||
-- | |||
local | |||
else | else | ||
ndArgs.other = cfg.otherDefault | |||
end | end | ||
-- Allow custom page values. | |||
ndArgs.page = args.page | |||
return nsDetect(ndArgs) | |||
end | end | ||
local function detectRedirects(args) | |||
local redirect = args[cfg.redirect] | |||
local function | -- The yesno function returns true/false for "yes", "no", etc., and returns | ||
local | -- redirect for other input. | ||
if | redirect = yesno(redirect, redirect) | ||
return | if redirect == false then | ||
-- Detect redirects unless they have been explicitly disallowed with | |||
-- "redirect=no" or similar. | |||
return | |||
end | end | ||
local pageObject = getPageObject(args.page) | |||
-- If we are using subject namespaces elsewhere, do so here as well. | |||
if | if pageObject | ||
and not yesno(args.talk, true) | |||
and args[cfg.defaultns] ~= cfg.defaultnsAll | |||
then | |||
pageObject = getPageObject( | |||
pageObject.subjectNsText .. ':' .. pageObject.text | |||
) | |||
end | end | ||
-- Allow custom values for redirects. | |||
if pageObject and pageObject.isRedirect then | |||
if type(redirect) == 'string' then | |||
-- | return redirect | ||
else | |||
if | return cfg.redirectDefault | ||
end | end | ||
end | end | ||
end | end | ||
local | function p._main(args) | ||
if | local redirect = detectRedirects(args) | ||
local pagetype = "" | |||
if redirect then | |||
pagetype = redirect | |||
else | else | ||
pagetype = getNsDetectValue(args) | |||
end | end | ||
if yesno(args.plural, false) then | if yesno(args.plural, false) then | ||
if cfg.irregularPlurals[pagetype] then | |||
pagetype = cfg.irregularPlurals[pagetype] | |||
else | |||
pagetype = pagetype .. cfg.plural -- often 's' | |||
end | |||
end | end | ||
if yesno(args.caps, false) then | if yesno(args.caps, false) then | ||
pagetype = mw.ustring.upper(mw.ustring.sub(pagetype, 1, 1)) .. | |||
mw.ustring.sub(pagetype, 2) | |||
end | end | ||
return | return pagetype | ||
end | end | ||