Module:Pagetype: Difference between revisions
Jump to navigation
Jump to search
Add exist check per discussion
m (1 revision imported) |
Template:Tooltip>Plastikspork (Add exist check per discussion) |
||
Line 1: | Line 1: | ||
-------------------------------------------------------------------------------- | -------------------------------------------------------------------------------- | ||
-- -- | -- -- | ||
-- This meta-module which automatically detects namespaces, and allows -- | |||
-- for a great deal of customisation. It can easily be ported to other -- | |||
-- This | |||
-- great deal of customisation. It can easily be ported to other | |||
-- wikis by changing the values in the [[Module:Pagetype/config]]. -- | -- wikis by changing the values in the [[Module:Pagetype/config]]. -- | ||
-- -- | -- -- | ||
Line 16: | Line 13: | ||
local getArgs = require('Module:Arguments').getArgs | local getArgs = require('Module:Arguments').getArgs | ||
local yesno = require('Module:Yesno') | local yesno = require('Module:Yesno') | ||
local p = {} | local p = {} | ||
Line 51: | Line 46: | ||
end | end | ||
-- Get the argument for a title's namespace, if it was specified in the args | -- Get the argument for a title's namespace, if it was specified in the args table. | ||
local function getNamespaceArg(title, args, cfg) | local function getNamespaceArg(title, args, cfg) | ||
if title.isTalkPage then | if title.isTalkPage then | ||
Line 102: | Line 96: | ||
end | end | ||
local function | local function capitalize(pageType) | ||
if title.namespace ~= 0 | local first = mw.ustring.sub(pageType, 1, 1) | ||
-- | local rest = mw.ustring.sub(pageType, 2) | ||
return mw.ustring.upper(first) .. rest | |||
end | |||
local function pluralize(pageType, cfg) | |||
if cfg.irregularPlurals[pageType] then | |||
return cfg.irregularPlurals[pageType] | |||
else | |||
return pageType .. cfg.plural -- often 's' | |||
end | |||
end | |||
local function parseContent(title, args, list, parameter, default, articleOnly) | |||
if articleOnly and title.namespace~=0 -- only detect in mainspace | |||
or title.namespace==828 and title.subpageText~='doc' -- don't detect modules | |||
or not title.exists -- can't check unless page exists | |||
then | |||
return nil | return nil | ||
end | end | ||
local templates = mw.loadData('Module:Pagetype/' .. list) | |||
local | local content = title:getContent() | ||
if | if content == nil then | ||
return nil | return nil | ||
end | end | ||
content = require('Module:Wikitext Parsing').PrepareText(content) -- disregard templates which do not have any affect | |||
local templateNames = {} | |||
local templateFound = false | |||
for template in string.gmatch(content, "{{%s*([^|}]-)%s*[|}]") do | |||
if templates[capitalize(template)] then | |||
templateFound = true | |||
break | |||
end | |||
end | |||
local out = lookUpNamespaceArg(args, parameter) | |||
if not templateFound then | |||
return nil | return nil | ||
elseif type( | elseif type(out)=='string' then | ||
return | return out | ||
else | else | ||
return | return default | ||
end | end | ||
end | end | ||
-- Gets the pagetype from a class specified from the first positional | -- Gets the pagetype from a class specified from the first positional parameter. | ||
local function getPageTypeFromClass(args, class, key, aliasTable, default) | local function getPageTypeFromClass(args, class, key, aliasTable, default) | ||
local arg = lookUpNamespaceArg(args, key) | local arg = lookUpNamespaceArg(args, key) | ||
Line 178: | Line 195: | ||
end | end | ||
-- Whether the title is in the set of default active namespaces which are | -- Whether the title is in the set of default active namespaces which are looked up in cfg.pagetypes. | ||
local function isInDefaultActiveNamespace(title, args, cfg) | local function isInDefaultActiveNamespace(title, args, cfg) | ||
local defaultNamespacesKey = args[cfg.defaultns] | local defaultNamespacesKey = args[cfg.defaultns] | ||
Line 209: | Line 225: | ||
return ( | return ( | ||
detectRedirects(title, args, cfg) | detectRedirects(title, args, cfg) | ||
or | or parseContent(title, args, 'softredirect', cfg.softRedirect, cfg.softRedirectDefault) | ||
or parseContent(title, args, 'setindex', cfg.sia, cfg.siaDefault, true) | |||
or parseContent(title, args, 'disambiguation', cfg.dab, cfg.dabDefault, true) | |||
or getMainNamespaceClassPageType(title, args, cfg) | or getMainNamespaceClassPageType(title, args, cfg) | ||
or getNamespaceArgPageType(title, args, cfg) | or getNamespaceArgPageType(title, args, cfg) | ||
Line 237: | Line 255: | ||
return title | return title | ||
end | end | ||
end | end | ||