Module:Category handler

--                                                                                                          CATEGORY HANDLER                                                                                                                This module implements the template in Lua,            with a few improvements: all namespaces and all namespace aliases           are supported, and namespace names are detected automatically for           the local wiki. This module requires Module:Namespace detect            and Module:Yesno to be available on the local wiki. It can be          configured for different wikis by altering the values in                    Module:Category handler/config, and pages can be blacklisted            from categorisation by using Module:Category handler/blacklist. -- -- Load required moduleslocal yesno = require('Module:Yesno') -- Lazily load things we don't always needlocal mShared, mappings local p = {} -- Helper functions local function trimWhitespace(s, removeBlanks)if type(s) ~= 'string' thenreturn sends = s:match('^%s*(.-)%s*$')if removeBlanks thenif s ~= '' thenreturn selsereturn nilendelsereturn sendend -- CategoryHandler class local CategoryHandler = {}CategoryHandler.__index = CategoryHandler function CategoryHandler.new(data, args)local obj = setmetatable({ _data = data, _args = args }, CategoryHandler) -- Set the title objectdolocal pagename = obj:parameter('demopage')local success, titleObjif pagename thensuccess, titleObj = pcall(mw.title.new, pagename)endif success and titleObj thenobj.title = titleObjif titleObj == mw.title.getCurrentTitle thenobj._usesCurrentTitle = trueendelseobj.title = mw.title.getCurrentTitleobj._usesCurrentTitle = trueendend -- Set suppression parameter valuesfor _, key in ipairs{'nocat', 'categories'} dolocal value = obj:parameter(key)value = trimWhitespace(value, true)obj['_' .. key] = yesno(value)enddolocal subpage = obj:parameter('subpage')local category2 = obj:parameter('category2')if type(subpage) == 'string' thensubpage = mw.ustring.lower(subpage)endif type(category2) == 'string' thensubpage = mw.ustring.lower(category2)endobj._subpage = trimWhitespace(subpage, true)obj._category2 = trimWhitespace(category2) -- don't remove blank valuesendreturn objend function CategoryHandler:parameter(key)local parameterNames = self._data.parameters[key]local pntype = type(parameterNames)if pntype == 'string' or pntype == 'number' thenreturn self._args[parameterNames]elseif pntype == 'table' thenfor _, name in ipairs(parameterNames) dolocal value = self._args[name]if value ~= nil thenreturn valueendendreturn nilelseerror(string.format('invalid config key "%s"',tostring(key)), 2)endend function CategoryHandler:isSuppressedByArgumentsreturn-- See if a category suppression argument has been set.self._nocat == trueor self._categories == falseor (self._category2and self._category2 ~= self._data.category2Yesand self._category2 ~= self._data.category2Negative) -- Check whether we are on a subpage, and see if categories are-- suppressed based on our subpage status.or self._subpage == self._data.subpageNo and self.title.isSubpageor self._subpage == self._data.subpageOnly and not self.title.isSubpageend function CategoryHandler:shouldSkipBlacklistCheck-- Check whether the category suppression arguments indicate we-- should skip the blacklist check.return self._nocat == falseor self._categories == trueor self._category2 == self._data.category2Yesend function CategoryHandler:matchesBlacklistif self._usesCurrentTitle thenreturn self._data.currentTitleMatchesBlacklistelsemShared = mShared or require('Module:Category handler/shared')return mShared.matchesBlacklist(self.title.prefixedText,mw.loadData('Module:Category handler/blacklist'))endend function CategoryHandler:isSuppressed-- Find if categories are suppressed by either the arguments or by-- matching the blacklist.return self:isSuppressedByArgumentsor not self:shouldSkipBlacklistCheck and self:matchesBlacklistend function CategoryHandler:getNamespaceParametersif self._usesCurrentTitle thenreturn self._data.currentTitleNamespaceParameterselseif not mappings thenmShared = mShared or require('Module:Category handler/shared')mappings = mShared.getParamMappings(true) -- gets mappings with mw.loadDataendreturn mShared.getNamespaceParameters(self.title,mappings)endend function CategoryHandler:namespaceParametersExist-- Find whether any namespace parameters have been specified.-- We use the order "all" --> namespace params --> "other" as this is what-- the old template did.if self:parameter('all') thenreturn trueendif not mappings thenmShared = mShared or require('Module:Category handler/shared')mappings = mShared.getParamMappings(true) -- gets mappings with mw.loadDataendfor ns, params in pairs(mappings) dofor i, param in ipairs(params) doif self._args[param] thenreturn trueendendendif self:parameter('other') thenreturn trueendreturn falseend function CategoryHandler:getCategorieslocal params = self:getNamespaceParameterslocal nsCategoryfor i, param in ipairs(params) dolocal value = self._args[param]if value ~= nil thennsCategory = valuebreakendendif nsCategory ~= nil or self:namespaceParametersExist then-- Namespace parameters exist - advanced usage.if nsCategory == nil thennsCategory = self:parameter('other')endlocal ret = {self:parameter('all')}local numParam = tonumber(nsCategory)if numParam and numParam >= 1 and math.floor(numParam) == numParam then-- nsCategory is an integerret[#ret + 1] = self._args[numParam]elseret[#ret + 1] = nsCategoryendif #ret < 1 thenreturn nilelsereturn table.concat(ret)endelseif self._data.defaultNamespaces[self.title.namespace] then-- Namespace parameters don't exist, simple usage.return self._args[1]endreturn nilend -- Exports local p = {} function p._exportClasses-- Used for testing purposes.return {CategoryHandler = CategoryHandler}end function p._main(args, data)data = data or mw.loadData('Module:Category handler/data')local handler = CategoryHandler.new(data, args)if handler:isSuppressed thenreturn nilendreturn handler:getCategoriesend function p.main(frame, data)data = data or mw.loadData('Module:Category handler/data')local args = require('Module:Arguments').getArgs(frame, {wrappers = data.wrappers,valueFunc = function (k, v)v = trimWhitespace(v)if type(k) == 'number' thenif v ~= '' thenreturn velsereturn nilendelsereturn vendend})return p._main(args, data)end return p