8,987
edits
m (1 revision imported) |
m (1 revision imported) |
||
| (3 intermediate revisions by 3 users not shown) | |||
| 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 | local first = mw.ustring.sub(pageType, 1, 1) | ||
local rest = mw.ustring.sub(pageType, 2) | |||
return | 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 | ||
end | |||
local function parseContent(title, args, optionsList) | |||
if | if 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 content = title:getContent() | |||
if content == nil then | |||
return nil | return nil | ||
end | |||
local templates -- lazily evaluated | |||
for _, options in next, optionsList do | |||
local list, parameter, default, articleOnly = unpack(options, 1, 4) | |||
if not articleOnly or title.namespace==0 then -- only check for templates if we should... | |||
local out = lookUpNamespaceArg(args, parameter) | |||
if type(out) == "string" or (out ~= false and default) then -- ...and if we actually have anything to say about them | |||
if not templates then | |||
templates = {} -- do our delayed evaluation now that we are required to | |||
content = require('Module:Wikitext Parsing').PrepareText(content) -- disregard templates which do not have any affect | |||
for template in string.gmatch(content, "{{%s*([^|}]-)%s*[|}]") do | |||
templates[#templates+1] = capitalize(template) | |||
end | |||
end | |||
local wantedTemplates = mw.loadData('Module:Pagetype/' .. list) | |||
local templateFound = false | |||
for _, template in next, templates do | |||
if wantedTemplates[template] then | |||
templateFound = true | |||
break | |||
end | |||
end | |||
if templateFound then | |||
if type(out)=='string' then | |||
return out | |||
elseif out ~= false and default then | |||
return default | |||
end | |||
end | |||
end | |||
end | |||
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 207: | ||
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 237: | ||
return ( | return ( | ||
detectRedirects(title, args, cfg) | detectRedirects(title, args, cfg) | ||
or | or parseContent(title, args, { | ||
{'softredirect', cfg.softRedirect, cfg.softRedirectDefault}, | |||
{'setindex', cfg.sia, cfg.siaDefault, true}, | |||
{'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 269: | ||
return title | return title | ||
end | end | ||
end | end | ||