Module:Category handler

---------------------------------------------------------------------------------- ---- CATEGORY HANDLER ---- ---- This module implements the {{category handler}} 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 moduleslocalyesno=require('Module:Yesno')-- Lazily load things we don't always needlocalmShared,mappingslocalp={}---------------------------------------------------------------------------------- Helper functions--------------------------------------------------------------------------------localfunctiontrimWhitespace(s,removeBlanks)iftype(s)~='string'thenreturnsends=s:match('^%s*(.-)%s*$')ifremoveBlanksthenifs~=''thenreturnselsereturnnilendelsereturnsendend---------------------------------------------------------------------------------- CategoryHandler class--------------------------------------------------------------------------------localCategoryHandler={}CategoryHandler.__index=CategoryHandlerfunctionCategoryHandler.new(data,args)localobj=setmetatable({_data=data,_args=args},CategoryHandler)-- Set the title objectdolocalpagename=obj:parameter('demopage')localsuccess,titleObjifpagenamethensuccess,titleObj=pcall(mw.title.new,pagename)endifsuccessandtitleObjthenobj.title=titleObjiftitleObj==mw.title.getCurrentTitle()thenobj._usesCurrentTitle=trueendelseobj.title=mw.title.getCurrentTitle()obj._usesCurrentTitle=trueendend-- Set suppression parameter valuesfor_,keyinipairs{'nocat','categories'}dolocalvalue=obj:parameter(key)value=trimWhitespace(value,true)obj['_'..key]=yesno(value)enddolocalsubpage=obj:parameter('subpage')localcategory2=obj:parameter('category2')iftype(subpage)=='string'thensubpage=mw.ustring.lower(subpage)endiftype(category2)=='string'thensubpage=mw.ustring.lower(category2)endobj._subpage=trimWhitespace(subpage,true)obj._category2=trimWhitespace(category2)-- don't remove blank valuesendreturnobjendfunctionCategoryHandler:parameter(key)localparameterNames=self._data.parameters[key]localpntype=type(parameterNames)ifpntype=='string'orpntype=='number'thenreturnself._args[parameterNames]elseifpntype=='table'thenfor_,nameinipairs(parameterNames)dolocalvalue=self._args[name]ifvalue~=nilthenreturnvalueendendreturnnilelseerror(string.format('invalid config key "%s"',tostring(key)),2)endendfunctionCategoryHandler:isSuppressedByArguments()return-- See if a category suppression argument has been set.self._nocat==trueorself._categories==falseor(self._category2andself._category2~=self._data.category2Yesandself._category2~=self._data.category2Negative)-- Check whether we are on a subpage, and see if categories are-- suppressed based on our subpage status.orself._subpage==self._data.subpageNoandself.title.isSubpageorself._subpage==self._data.subpageOnlyandnotself.title.isSubpageendfunctionCategoryHandler:shouldSkipBlacklistCheck()-- Check whether the category suppression arguments indicate we-- should skip the blacklist check.returnself._nocat==falseorself._categories==trueorself._category2==self._data.category2YesendfunctionCategoryHandler:matchesBlacklist()ifself._usesCurrentTitlethenreturnself._data.currentTitleMatchesBlacklistelsemShared=mSharedorrequire('Module:Category handler/shared')returnmShared.matchesBlacklist(self.title.prefixedText,mw.loadData('Module:Category handler/blacklist'))endendfunctionCategoryHandler:isSuppressed()-- Find if categories are suppressed by either the arguments or by-- matching the blacklist.returnself:isSuppressedByArguments()ornotself:shouldSkipBlacklistCheck()andself:matchesBlacklist()endfunctionCategoryHandler:getNamespaceParameters()ifself._usesCurrentTitlethenreturnself._data.currentTitleNamespaceParameterselseifnotmappingsthenmShared=mSharedorrequire('Module:Category handler/shared')mappings=mShared.getParamMappings(true)-- gets mappings with mw.loadDataendreturnmShared.getNamespaceParameters(self.title,mappings)endendfunctionCategoryHandler: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.ifself:parameter('all')thenreturntrueendifnotmappingsthenmShared=mSharedorrequire('Module:Category handler/shared')mappings=mShared.getParamMappings(true)-- gets mappings with mw.loadDataendforns,paramsinpairs(mappings)dofori,paraminipairs(params)doifself._args[param]thenreturntrueendendendifself:parameter('other')thenreturntrueendreturnfalseendfunctionCategoryHandler:getCategories()localparams=self:getNamespaceParameters()localnsCategoryfori,paraminipairs(params)dolocalvalue=self._args[param]ifvalue~=nilthennsCategory=valuebreakendendifnsCategory~=nilorself:namespaceParametersExist()then-- Namespace parameters exist - advanced usage.ifnsCategory==nilthennsCategory=self:parameter('other')endlocalret={self:parameter('all')}localnumParam=tonumber(nsCategory)ifnumParamandnumParam>=1andmath.floor(numParam)==numParamthen-- nsCategory is an integerret[#ret+1]=self._args[numParam]elseret[#ret+1]=nsCategoryendif#ret<1thenreturnnilelsereturntable.concat(ret)endelseifself._data.defaultNamespaces[self.title.namespace]then-- Namespace parameters don't exist, simple usage.returnself._args[1]endreturnnilend---------------------------------------------------------------------------------- Exports--------------------------------------------------------------------------------localp={}functionp._exportClasses()-- Used for testing purposes.return{CategoryHandler=CategoryHandler}endfunctionp._main(args,data)data=dataormw.loadData('Module:Category handler/data')localhandler=CategoryHandler.new(data,args)ifhandler:isSuppressed()thenreturnnilendreturnhandler:getCategories()endfunctionp.main(frame,data)data=dataormw.loadData('Module:Category handler/data')localargs=require('Module:Arguments').getArgs(frame,{wrappers=data.wrappers,valueFunc=function(k,v)v=trimWhitespace(v)iftype(k)=='number'thenifv~=''thenreturnvelsereturnnilendelsereturnvendend})returnp._main(args,data)endreturnp