https://sarkarverse.org/index.php?title=Module:Category_handler&feed=atom&action=historyModule:Category handler - Revision history2024-03-28T10:26:47ZRevision history for this page on the wikiMediaWiki 1.39.0https://sarkarverse.org/index.php?title=Module:Category_handler&diff=27110&oldid=prevT12: 1 revision2014-07-22T15:11:13Z<p>1 revision</p>
<p><b>New page</b></p><div>--------------------------------------------------------------------------------<br />
-- --<br />
-- CATEGORY HANDLER --<br />
-- --<br />
-- This module implements the {{category handler}} template in Lua, --<br />
-- with a few improvements: all namespaces and all namespace aliases --<br />
-- are supported, and namespace names are detected automatically for --<br />
-- the local wiki. This module requires [[Module:Namespace detect]] --<br />
-- and [[Module:Yesno]] to be available on the local wiki. It can be --<br />
-- configured for different wikis by altering the values in --<br />
-- [[Module:Category handler/config]], and pages can be blacklisted --<br />
-- from categorisation by using [[Module:Category handler/blacklist]]. --<br />
-- --<br />
--------------------------------------------------------------------------------<br />
<br />
-- Load required modules<br />
local yesno = require('Module:Yesno')<br />
<br />
-- Lazily load things we don't always need<br />
local mShared, mappings<br />
<br />
local p = {}<br />
<br />
--------------------------------------------------------------------------------<br />
-- Helper functions<br />
--------------------------------------------------------------------------------<br />
<br />
local function trimWhitespace(s, removeBlanks)<br />
if type(s) ~= 'string' then<br />
return s<br />
end<br />
s = s:match('^%s*(.-)%s*$')<br />
if removeBlanks then<br />
if s ~= '' then<br />
return s<br />
else<br />
return nil<br />
end<br />
else<br />
return s<br />
end<br />
end<br />
<br />
--------------------------------------------------------------------------------<br />
-- CategoryHandler class<br />
--------------------------------------------------------------------------------<br />
<br />
local CategoryHandler = {}<br />
CategoryHandler.__index = CategoryHandler<br />
<br />
function CategoryHandler.new(data, args)<br />
local obj = setmetatable({ _data = data, _args = args }, CategoryHandler)<br />
<br />
-- Set the title object<br />
do<br />
local pagename = obj:parameter('demopage')<br />
local success, titleObj<br />
if pagename then<br />
success, titleObj = pcall(mw.title.new, pagename)<br />
end<br />
if success and titleObj then<br />
obj.title = titleObj<br />
if titleObj == mw.title.getCurrentTitle() then<br />
obj._usesCurrentTitle = true<br />
end<br />
else<br />
obj.title = mw.title.getCurrentTitle()<br />
obj._usesCurrentTitle = true<br />
end<br />
end<br />
<br />
-- Set suppression parameter values<br />
for _, key in ipairs{'nocat', 'categories'} do<br />
local value = obj:parameter(key)<br />
value = trimWhitespace(value, true)<br />
obj['_' .. key] = yesno(value)<br />
end<br />
do<br />
local subpage = obj:parameter('subpage')<br />
local category2 = obj:parameter('category2')<br />
if type(subpage) == 'string' then<br />
subpage = mw.ustring.lower(subpage)<br />
end<br />
if type(category2) == 'string' then<br />
subpage = mw.ustring.lower(category2)<br />
end<br />
obj._subpage = trimWhitespace(subpage, true)<br />
obj._category2 = trimWhitespace(category2) -- don't remove blank values<br />
end<br />
return obj<br />
end<br />
<br />
function CategoryHandler:parameter(key)<br />
local parameterNames = self._data.parameters[key]<br />
local pntype = type(parameterNames)<br />
if pntype == 'string' or pntype == 'number' then<br />
return self._args[parameterNames]<br />
elseif pntype == 'table' then<br />
for _, name in ipairs(parameterNames) do<br />
local value = self._args[name]<br />
if value ~= nil then<br />
return value<br />
end<br />
end<br />
return nil<br />
else<br />
error(string.format(<br />
'invalid config key "%s"',<br />
tostring(key)<br />
), 2)<br />
end<br />
end<br />
<br />
function CategoryHandler:isSuppressedByArguments()<br />
return<br />
-- See if a category suppression argument has been set.<br />
self._nocat == true<br />
or self._categories == false<br />
or (<br />
self._category2<br />
and self._category2 ~= self._data.category2Yes<br />
and self._category2 ~= self._data.category2Negative<br />
)<br />
<br />
-- Check whether we are on a subpage, and see if categories are<br />
-- suppressed based on our subpage status.<br />
or self._subpage == self._data.subpageNo and self.title.isSubpage<br />
or self._subpage == self._data.subpageOnly and not self.title.isSubpage<br />
end<br />
<br />
function CategoryHandler:shouldSkipBlacklistCheck()<br />
-- Check whether the category suppression arguments indicate we<br />
-- should skip the blacklist check.<br />
return self._nocat == false<br />
or self._categories == true<br />
or self._category2 == self._data.category2Yes<br />
end<br />
<br />
function CategoryHandler:matchesBlacklist()<br />
if self._usesCurrentTitle then<br />
return self._data.currentTitleMatchesBlacklist<br />
else<br />
mShared = mShared or require('Module:Category handler/shared')<br />
return mShared.matchesBlacklist(<br />
self.title.prefixedText,<br />
mw.loadData('Module:Category handler/blacklist')<br />
)<br />
end<br />
end<br />
<br />
function CategoryHandler:isSuppressed()<br />
-- Find if categories are suppressed by either the arguments or by<br />
-- matching the blacklist.<br />
return self:isSuppressedByArguments()<br />
or not self:shouldSkipBlacklistCheck() and self:matchesBlacklist()<br />
end<br />
<br />
function CategoryHandler:getNamespaceParameters()<br />
if self._usesCurrentTitle then<br />
return self._data.currentTitleNamespaceParameters<br />
else<br />
if not mappings then<br />
mShared = mShared or require('Module:Category handler/shared')<br />
mappings = mShared.getParamMappings(true) -- gets mappings with mw.loadData<br />
end<br />
return mShared.getNamespaceParameters(<br />
self.title,<br />
mappings<br />
)<br />
end<br />
end<br />
<br />
function CategoryHandler:namespaceParametersExist()<br />
-- Find whether any namespace parameters have been specified.<br />
-- We use the order "all" --> namespace params --> "other" as this is what<br />
-- the old template did.<br />
if self:parameter('all') then<br />
return true<br />
end<br />
if not mappings then<br />
mShared = mShared or require('Module:Category handler/shared')<br />
mappings = mShared.getParamMappings(true) -- gets mappings with mw.loadData<br />
end<br />
for ns, params in pairs(mappings) do<br />
for i, param in ipairs(params) do<br />
if self._args[param] then<br />
return true<br />
end<br />
end<br />
end<br />
if self:parameter('other') then<br />
return true<br />
end<br />
return false<br />
end<br />
<br />
function CategoryHandler:getCategories()<br />
local params = self:getNamespaceParameters()<br />
local nsCategory<br />
for i, param in ipairs(params) do<br />
local value = self._args[param]<br />
if value ~= nil then<br />
nsCategory = value<br />
break<br />
end<br />
end<br />
if nsCategory ~= nil or self:namespaceParametersExist() then<br />
-- Namespace parameters exist - advanced usage.<br />
if nsCategory == nil then<br />
nsCategory = self:parameter('other')<br />
end<br />
local ret = {self:parameter('all')}<br />
local numParam = tonumber(nsCategory)<br />
if numParam and numParam >= 1 and math.floor(numParam) == numParam then<br />
-- nsCategory is an integer<br />
ret[#ret + 1] = self._args[numParam]<br />
else<br />
ret[#ret + 1] = nsCategory<br />
end<br />
if #ret < 1 then<br />
return nil<br />
else<br />
return table.concat(ret)<br />
end<br />
elseif self._data.defaultNamespaces[self.title.namespace] then<br />
-- Namespace parameters don't exist, simple usage.<br />
return self._args[1]<br />
end<br />
return nil<br />
end<br />
<br />
--------------------------------------------------------------------------------<br />
-- Exports<br />
--------------------------------------------------------------------------------<br />
<br />
local p = {}<br />
<br />
function p._exportClasses()<br />
-- Used for testing purposes.<br />
return {<br />
CategoryHandler = CategoryHandler<br />
}<br />
end<br />
<br />
function p._main(args, data)<br />
data = data or mw.loadData('Module:Category handler/data')<br />
local handler = CategoryHandler.new(data, args)<br />
if handler:isSuppressed() then<br />
return nil<br />
end<br />
return handler:getCategories()<br />
end<br />
<br />
function p.main(frame, data)<br />
data = data or mw.loadData('Module:Category handler/data')<br />
local args = require('Module:Arguments').getArgs(frame, {<br />
wrappers = data.wrappers,<br />
valueFunc = function (k, v)<br />
v = trimWhitespace(v)<br />
if type(k) == 'number' then<br />
if v ~= '' then<br />
return v<br />
else<br />
return nil<br />
end<br />
else<br />
return v<br />
end<br />
end<br />
})<br />
return p._main(args, data)<br />
end<br />
<br />
return p</div>T12https://sarkarverse.org/index.php?title=Module:Category_handler&diff=26880&oldid=prevT12: 1 revision2014-07-17T14:26:08Z<p>1 revision</p>
<p><b>New page</b></p><div>--------------------------------------------------------------------------------<br />
-- --<br />
-- CATEGORY HANDLER --<br />
-- --<br />
-- This module implements the {{category handler}} template in Lua, --<br />
-- with a few improvements: all namespaces and all namespace aliases --<br />
-- are supported, and namespace names are detected automatically for --<br />
-- the local wiki. This module requires [[Module:Namespace detect]] --<br />
-- and [[Module:Yesno]] to be available on the local wiki. It can be --<br />
-- configured for different wikis by altering the values in --<br />
-- [[Module:Category handler/config]], and pages can be blacklisted --<br />
-- from categorisation by using [[Module:Category handler/blacklist]]. --<br />
-- --<br />
--------------------------------------------------------------------------------<br />
<br />
-- Load required modules<br />
local yesno = require('Module:Yesno')<br />
<br />
-- Lazily load things we don't always need<br />
local mShared, mappings<br />
<br />
local p = {}<br />
<br />
--------------------------------------------------------------------------------<br />
-- Helper functions<br />
--------------------------------------------------------------------------------<br />
<br />
local function trimWhitespace(s, removeBlanks)<br />
if type(s) ~= 'string' then<br />
return s<br />
end<br />
s = s:match('^%s*(.-)%s*$')<br />
if removeBlanks then<br />
if s ~= '' then<br />
return s<br />
else<br />
return nil<br />
end<br />
else<br />
return s<br />
end<br />
end<br />
<br />
--------------------------------------------------------------------------------<br />
-- CategoryHandler class<br />
--------------------------------------------------------------------------------<br />
<br />
local CategoryHandler = {}<br />
CategoryHandler.__index = CategoryHandler<br />
<br />
function CategoryHandler.new(data, args)<br />
local obj = setmetatable({ _data = data, _args = args }, CategoryHandler)<br />
<br />
-- Set the title object<br />
do<br />
local pagename = obj:parameter('demopage')<br />
local success, titleObj = pcall(mw.title.new, pagename)<br />
if success and titleObj then<br />
obj.title = titleObj<br />
else<br />
obj.title = mw.title.getCurrentTitle()<br />
obj._usesCurrentTitle = true<br />
end<br />
end<br />
<br />
-- Set suppression parameter values<br />
for _, key in ipairs{'nocat', 'categories'} do<br />
local value = obj:parameter(key)<br />
value = trimWhitespace(value, true)<br />
obj['_' .. key] = yesno(value)<br />
end<br />
do<br />
local subpage = obj:parameter('subpage')<br />
local category2 = obj:parameter('category2')<br />
if type(subpage) == 'string' then<br />
subpage = mw.ustring.lower(subpage)<br />
end<br />
if type(category2) == 'string' then<br />
subpage = mw.ustring.lower(category2)<br />
end<br />
obj._subpage = trimWhitespace(subpage, true)<br />
obj._category2 = trimWhitespace(category2) -- don't remove blank values<br />
end<br />
return obj<br />
end<br />
<br />
function CategoryHandler:parameter(key)<br />
local parameterNames = self._data.parameters[key]<br />
local pntype = type(parameterNames)<br />
if pntype == 'string' or pntype == 'number' then<br />
return self._args[parameterNames]<br />
elseif pntype == 'table' then<br />
for _, name in ipairs(parameterNames) do<br />
local value = self._args[name]<br />
if value ~= nil then<br />
return value<br />
end<br />
end<br />
return nil<br />
else<br />
error(string.format(<br />
'invalid config key "%s"',<br />
tostring(key)<br />
), 2)<br />
end<br />
end<br />
<br />
function CategoryHandler:isSuppressedByArguments()<br />
return<br />
-- See if a category suppression argument has been set.<br />
self._nocat == true<br />
or self._categories == false<br />
or (<br />
self._category2<br />
and self._category2 ~= self._data.category2Yes<br />
and self._category2 ~= self._data.category2Negative<br />
)<br />
<br />
-- Check whether we are on a subpage, and see if categories are<br />
-- suppressed based on our subpage status.<br />
or self._subpage == self._data.subpageNo and self.title.isSubpage<br />
or self._subpage == self._data.subpageOnly and not self.title.isSubpage<br />
end<br />
<br />
function CategoryHandler:shouldSkipBlacklistCheck()<br />
-- Check whether the category suppression arguments indicate we<br />
-- should skip the blacklist check.<br />
return self._nocat == false<br />
or self._categories == true<br />
or self._category2 == self._data.category2Yes<br />
end<br />
<br />
function CategoryHandler:matchesBlacklist()<br />
if self._usesCurrentTitle then<br />
return self._data.currentTitleMatchesBlacklist<br />
else<br />
mShared = mShared or require('Module:Category handler/shared')<br />
return mShared.matchesBlacklist(<br />
self.title.prefixedText,<br />
mw.loadData('Module:Category handler/blacklist')<br />
)<br />
end<br />
end<br />
<br />
function CategoryHandler:isSuppressed()<br />
-- Find if categories are suppressed by either the arguments or by<br />
-- matching the blacklist.<br />
return self:isSuppressedByArguments()<br />
or not self:shouldSkipBlacklistCheck() and self:matchesBlacklist()<br />
end<br />
<br />
function CategoryHandler:getNamespaceParameters()<br />
if self._usesCurrentTitle then<br />
return self._data.currentTitleNamespaceParameters<br />
else<br />
if not mappings then<br />
mShared = mShared or require('Module:Category handler/shared')<br />
mappings = mShared.getParamMappings(true) -- gets mappings with mw.loadData<br />
end<br />
return mShared.getNamespaceParameters(<br />
self.title,<br />
mappings<br />
)<br />
end<br />
end<br />
<br />
function CategoryHandler:namespaceParametersExist()<br />
-- Find whether any namespace parameters have been specified.<br />
-- We use the order "all" --> namespace params --> "other" as this is what<br />
-- the old template did.<br />
if self:parameter('all') then<br />
return true<br />
end<br />
if not mappings then<br />
mShared = mShared or require('Module:Category handler/shared')<br />
mappings = mShared.getParamMappings(true) -- gets mappings with mw.loadData<br />
end<br />
for ns, params in pairs(mappings) do<br />
for i, param in ipairs(params) do<br />
if self._args[param] then<br />
return true<br />
end<br />
end<br />
end<br />
if self:parameter('other') then<br />
return true<br />
end<br />
return false<br />
end<br />
<br />
function CategoryHandler:getCategories()<br />
local params = self:getNamespaceParameters()<br />
local nsCategory<br />
for i, param in ipairs(params) do<br />
local value = self._args[param]<br />
if value ~= nil then<br />
nsCategory = value<br />
break<br />
end<br />
end<br />
if nsCategory ~= nil or self:namespaceParametersExist() then<br />
-- Namespace parameters exist - advanced usage.<br />
if nsCategory == nil then<br />
nsCategory = self:parameter('other')<br />
end<br />
local ret = {self:parameter('all')}<br />
local numParam = tonumber(nsCategory)<br />
if numParam and numParam >= 1 and math.floor(numParam) == numParam then<br />
-- nsCategory is an integer<br />
ret[#ret + 1] = self._args[numParam]<br />
else<br />
ret[#ret + 1] = nsCategory<br />
end<br />
if #ret < 1 then<br />
return nil<br />
else<br />
return table.concat(ret)<br />
end<br />
elseif self._data.defaultNamespaces[self.title.namespace] then<br />
-- Namespace parameters don't exist, simple usage.<br />
return self._args[1]<br />
end<br />
return nil<br />
end<br />
<br />
--------------------------------------------------------------------------------<br />
-- Exports<br />
--------------------------------------------------------------------------------<br />
<br />
local p = {}<br />
<br />
function p._exportClasses()<br />
-- Used for testing purposes.<br />
return {<br />
CategoryHandler = CategoryHandler<br />
}<br />
end<br />
<br />
function p._main(args, data)<br />
data = data or mw.loadData('Module:Category handler/data')<br />
local handler = CategoryHandler.new(data, args)<br />
if handler:isSuppressed() then<br />
return nil<br />
end<br />
return handler:getCategories()<br />
end<br />
<br />
function p.main(frame, data)<br />
data = data or mw.loadData('Module:Category handler/data')<br />
local args = require('Module:Arguments').getArgs(frame, {<br />
wrappers = data.wrappers,<br />
valueFunc = function (k, v)<br />
v = trimWhitespace(v)<br />
if type(k) == 'number' then<br />
if v ~= '' then<br />
return v<br />
else<br />
return nil<br />
end<br />
else<br />
return v<br />
end<br />
end<br />
})<br />
return p._main(args, data)<br />
end<br />
<br />
return p</div>T12https://sarkarverse.org/index.php?title=Module:Category_handler&diff=4336&oldid=prevT12: 1 revision2013-10-21T11:30:11Z<p>1 revision</p>
<p><b>New page</b></p><div>----------------------------------------------------------------------------------------------------------<br />
-- --<br />
-- CATEGORY HANDLER --<br />
-- --<br />
-- This module implements the {{category handler}} template in Lua, with a few improvements: all --<br />
-- namespaces and all namespace aliases are supported, and namespace names are detected --<br />
-- automatically for the local wiki. This module requires [[Module:Namespace detect]] and --<br />
-- [[Module:Yesno]] to be available on the local wiki. It can be configured for different wikis --<br />
-- by altering the values in the "cfg" table. --<br />
-- --<br />
----------------------------------------------------------------------------------------------------------<br />
<br />
----------------------------------------------------------------------------------------------------------<br />
-- Configuration data --<br />
-- Language-specific parameter names and values can be set here. --<br />
----------------------------------------------------------------------------------------------------------<br />
<br />
local cfg = {}<br />
<br />
-- The following config values set the names of parameters that suppress categorisation. They are used<br />
-- with Module:Yesno, and work as follows:<br />
--<br />
-- cfg.nocat:<br />
-- Result of yesno(args[cfg.nocat]) Effect<br />
-- true Categorisation is suppressed<br />
-- false Categorisation is allowed, and the blacklist check is skipped<br />
-- nil Categorisation is allowed<br />
--<br />
-- cfg.categories:<br />
-- Result of yesno(args[cfg.categories]) Effect<br />
-- true Categorisation is allowed, and the blacklist check is skipped<br />
-- false Categorisation is suppressed<br />
-- nil Categorisation is allowed<br />
cfg.nocat = 'nocat' <br />
cfg.categories = 'categories'<br />
<br />
-- The parameter name for the legacy "category2" parameter. This skips the blacklist if set to the<br />
-- cfg.category2Yes value, and suppresses categorisation if present but equal to anything other than<br />
-- cfg.category2Yes or cfg.category2Negative.<br />
cfg.category2 = 'category2'<br />
cfg.category2Yes = 'yes'<br />
cfg.category2Negative = '¬'<br />
<br />
-- cfg.subpage is the parameter name to specify how to behave on subpages. cfg.subpageNo is the value to<br />
-- specify to not categorise on subpages; cfg.only is the value to specify to only categorise on subpages.<br />
cfg.subpage = 'subpage'<br />
cfg.subpageNo = 'no'<br />
cfg.subpageOnly = 'only'<br />
<br />
-- The parameter for data to return in all namespaces.<br />
cfg.all = 'all'<br />
<br />
-- The parameter name for data to return if no data is specified for the namespace that is detected. This<br />
-- must be the same as the cfg.other parameter in [[Module:Namespace detect]].<br />
cfg.other = 'other'<br />
<br />
-- The parameter name used to specify a page other than the current page; used for testing and<br />
-- demonstration. This must be the same as the cfg.page parameter in [[Module:Namespace detect]].<br />
cfg.page = 'page'<br />
<br />
-- The categorisation blacklist. Pages that match Lua patterns in this list will not be categorised.<br />
-- (However, see the explanation of cfg.nocat, cfg.categories and cfg.category2 for some exceptions.)<br />
-- If the namespace name has a space in, it must be written with an underscore, e.g. "Wikipedia_talk".<br />
-- Other parts of the title can have either underscores or spaces.<br />
cfg.blacklist = {<br />
'^Main Page$', -- don't categorise the main page.<br />
<br />
-- Don't categorise the following pages or their subpages.<br />
'^Wikipedia:Cascade%-protected items$',<br />
'^Wikipedia:Cascade%-protected items/.*$',<br />
'^User:UBX$', -- The userbox "template" space.<br />
'^User:UBX/.*$',<br />
'^User_talk:UBX$',<br />
'^User_talk:UBX/.*$',<br />
<br />
-- Don't categorise subpages of these pages, but allow<br />
-- categorisation of the base page.<br />
'^Wikipedia:Template messages/.+$',<br />
<br />
'/[aA]rchive' -- Don't categorise archives.<br />
}<br />
<br />
-- This is a table of namespaces to categorise by default. They should be in the format of parameter<br />
-- names accepted by [[Module:Namespace detect]].<br />
cfg.defaultNamespaces = {<br />
'main',<br />
'file',<br />
'help',<br />
'category'<br />
}<br />
<br />
----------------------------------------------------------------------------------------------------------<br />
-- End configuration data --<br />
----------------------------------------------------------------------------------------------------------<br />
<br />
-- Get dependent modules<br />
local nsDetect = require('Module:Namespace detect')<br />
local yesno = require('Module:Yesno')<br />
<br />
----------------------------------------------------------------------------------------------------------<br />
-- Local functions --<br />
-- The following are internal functions, which we do not want to be accessible from other modules. --<br />
----------------------------------------------------------------------------------------------------------<br />
<br />
-- Find whether we need to return a category or not.<br />
local function needsCategory(pageObject, args)<br />
-- Don't categorise if the relevant options are set.<br />
if yesno(args[cfg.nocat])<br />
or yesno(args[cfg.categories]) == false<br />
or (<br />
args[cfg.category2] <br />
and args[cfg.category2] ~= cfg.category2Yes <br />
and args[cfg.category2] ~= cfg.category2Negative<br />
)<br />
then<br />
return false<br />
end<br />
-- If there is no pageObject available, then that either means that we are over<br />
-- the expensive function limit or that the title specified was invalid. Invalid<br />
-- titles will probably only be a problem during testing, so we choose the best<br />
-- fallback for being over the expensive function limit. The fallback behaviour<br />
-- of the old template was to assume the page was not a subpage, so we will do<br />
-- the same here.<br />
if args[cfg.subpage] == cfg.subpageNo and pageObject and pageObject.isSubpage then<br />
return false<br />
end<br />
if args[cfg.subpage] == cfg.subpageOnly <br />
and (not pageObject or (pageObject and not pageObject.isSubpage))<br />
then<br />
return false<br />
end<br />
return true<br />
end<br />
<br />
-- Find whether we need to check the blacklist or not.<br />
local function needsBlacklistCheck(args)<br />
if yesno(args[cfg.nocat]) == false<br />
or yesno(args[cfg.categories]) == true<br />
or args[cfg.category2] == cfg.category2Yes<br />
then<br />
return false<br />
else<br />
return true<br />
end<br />
end<br />
<br />
-- Find whether any namespace parameters have been specified.<br />
-- Mappings is the table of parameter mappings taken from<br />
-- [[Module:Namespace detect]].<br />
local function nsParamsExist(mappings, args)<br />
if args[cfg.all] or args[cfg.other] then<br />
return true<br />
end<br />
for ns, params in pairs(mappings) do<br />
for i, param in ipairs(params) do<br />
if args[param] then<br />
return true<br />
end<br />
end<br />
end<br />
return false<br />
end<br />
<br />
----------------------------------------------------------------------------------------------------------<br />
-- Global functions --<br />
-- The following functions are global, because we want them to be accessible from #invoke and --<br />
-- from other Lua modules. --<br />
----------------------------------------------------------------------------------------------------------<br />
<br />
local p = {}<br />
<br />
-- Find if a string matches the blacklist. Returns the match if one is found, or nil otherwise.<br />
-- Input should be a page title with a namespace prefix, e.g. "Wikipedia talk:Articles for deletion".<br />
function p.matchesBlacklist(page)<br />
if type(page) ~= 'string' then return end<br />
for i, pattern in ipairs(cfg.blacklist) do<br />
local match = mw.ustring.match(page, pattern)<br />
if match then<br />
return match<br />
end<br />
end<br />
end<br />
<br />
-- The main structure of the module. Checks whether we need to categorise,<br />
-- and then passes the relevant arguments to [[Module:Namespace detect]].<br />
function p._main(args)<br />
-- Get the page object and argument mappings from<br />
-- [[Module:Namespace detect]], to save us from having to rewrite the<br />
-- code.<br />
local pageObject = nsDetect.getPageObject(args[cfg.page])<br />
local mappings = nsDetect.getParamMappings()<br />
<br />
if not needsCategory(pageObject, args) then return end<br />
<br />
local ret = ''<br />
-- Check blacklist if necessary.<br />
if not needsBlacklistCheck(args) or not p.matchesBlacklist(pageObject.prefixedText) then<br />
if not nsParamsExist(mappings, args) then<br />
-- No namespace parameters exist; basic usage. Pass args[1] to<br />
-- [[Module:Namespace detect]] using the default namespace<br />
-- parameters, and return the result.<br />
local ndargs = {}<br />
for _, ndarg in ipairs(cfg.defaultNamespaces) do<br />
ndargs[ndarg] = args[1]<br />
end<br />
ndargs.page = args.page<br />
ndargs.demospace = args.demospace<br />
local ndresult = nsDetect._main(ndargs)<br />
if ndresult then<br />
ret = ret .. ndresult<br />
end<br />
else<br />
-- Namespace parameters exist; advanced usage.<br />
-- If the all parameter is specified, return it.<br />
local all = args.all<br />
if type(all) == 'string' then<br />
ret = ret .. all<br />
end<br />
<br />
-- Get the arguments to pass to [[Module:Namespace detect]].<br />
local ndargs = {}<br />
for ns, params in pairs(mappings) do<br />
for _, param in ipairs(params) do<br />
ndargs[param] = args[param] or args[cfg.other] or nil<br />
end<br />
end<br />
ndargs.other = args.other<br />
ndargs.page = args.page<br />
ndargs.demospace = args.demospace<br />
<br />
local data = nsDetect._main(ndargs)<br />
<br />
-- Work out what to return based on the result of the namespace detect call.<br />
local datanum = tonumber(data)<br />
if type(datanum) == 'number' then<br />
-- "data" is a number, so return that positional parameter.<br />
-- Remove non-positive integer values, as only positive integers<br />
-- from 1-10 were used with the old template.<br />
if datanum > 0 and math.floor(datanum) == datanum then<br />
local dataArg = args[datanum]<br />
if type(dataArg) == 'string' then<br />
ret = ret .. dataArg<br />
end<br />
end<br />
else<br />
-- "data" is not a number, so return it as it is.<br />
if type(data) == 'string' then<br />
ret = ret .. data<br />
end<br />
end<br />
end<br />
end<br />
return ret<br />
end<br />
<br />
function p.main(frame)<br />
-- If called via #invoke, use the args passed into the invoking<br />
-- template, or the args passed to #invoke if any exist. Otherwise<br />
-- assume args are being passed directly in.<br />
local origArgs<br />
if frame == mw.getCurrentFrame() then<br />
origArgs = frame:getParent().args<br />
for k, v in pairs(frame.args) do<br />
origArgs = frame.args<br />
break<br />
end<br />
else<br />
origArgs = frame<br />
end<br />
<br />
-- Trim whitespace and remove blank arguments for the following args:<br />
-- 1, 2, 3 etc., "nocat", "categories", "subpage", and "page".<br />
local args = {}<br />
for k, v in pairs(origArgs) do<br />
if type(v) == 'string' then<br />
v = mw.text.trim(v) -- Trim whitespace.<br />
end<br />
if type(k) == 'number'<br />
or k == cfg.nocat<br />
or k == cfg.categories<br />
or k == cfg.subpage<br />
or k == cfg.page<br />
then<br />
if v ~= '' then<br />
args[k] = v<br />
end<br />
else<br />
args[k] = v<br />
end<br />
end<br />
<br />
-- Lower-case "nocat", "categories", "category2", and "subpage". These<br />
-- parameters are put in lower case whenever they appear in the old<br />
-- template, so we can just do it once here and save ourselves some work.<br />
local lowercase = {cfg.nocat, cfg.categories, cfg.category2, cfg.subpage}<br />
for _, v in ipairs(lowercase) do<br />
local argVal = args[v]<br />
if type(argVal) == 'string' then<br />
args[v] = mw.ustring.lower(argVal)<br />
end<br />
end<br />
<br />
return p._main(args)<br />
end<br />
<br />
return p</div>T12