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:
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
--                                                                            --
--                                                                            --
--                                PAGETYPE                                    --
--      This meta-module which automatically detects namespaces, and allows   --
--                                                                            --
--      for a great deal of customisation. It can easily be ported to other   --
--      This is a meta-module intended to replace {{pagetype}} and similar    --
--      templates. It automatically detects namespaces, and allows for a      --
--      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 mDisambiguation = require('Module:Disambiguation')
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.
-- 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 detectDisambiguationPages(title, args, cfg)
local function capitalize(pageType)
if title.namespace ~= 0 then
local first = mw.ustring.sub(pageType, 1, 1)
-- Only detect disambiguation pages in mainspace
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 dab = lookUpNamespaceArg(args, cfg.dab)
local content = title:getContent()
if dab == false then
if content == nil then
-- Don't detect disambiguation pages if explicitly disallowed
return nil
return nil
end
end
 
content = require('Module:Wikitext Parsing').PrepareText(content) -- disregard templates which do not have any affect
if not mDisambiguation.isDisambiguation(title:getContent()) then
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(dab) == 'string' then
elseif type(out)=='string' then
return dab
return out
else
else
return cfg.dabDefault
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.
-- 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.
-- 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 detectDisambiguationPages(title, args, cfg)
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
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 capitalize(pageType)
local first = mw.ustring.sub(pageType, 1, 1)
local rest = mw.ustring.sub(pageType, 2)
return mw.ustring.upper(first) .. rest
end
end


Navigation menu