---------------------------------------------------------------------------------- ---- PAGETYPE ---- ---- 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]]. ---- ------------------------------------------------------------------------------------ Load config.localcfg=mw.loadData('Module:Pagetype/config')-- Load required modules.localgetArgs=require('Module:Arguments').getArgslocalyesno=require('Module:Yesno')localnsDetectModule=require('Module:Namespace detect')localnsDetect=nsDetectModule._mainlocalgetParamMappings=nsDetectModule.getParamMappingslocalgetPageObject=nsDetectModule.getPageObjectlocalp={}localfunctionshallowCopy(t)-- Makes a shallow copy of a table.localret={}fork,vinpairs(t)doret[k]=vendreturnretendlocalfunctioncheckPagetypeInput(namespace,val)-- Checks to see whether we need the default value for the given namespace,-- and if so gets it from the pagetypes table.-- The yesno function returns true/false for "yes", "no", etc., and returns-- val for other input.localret=yesno(val,val)ifretandtype(ret)~='string'thenret=cfg.pagetypes[namespace]endreturnretendlocalfunctiongetPagetypeFromClass(class,param,aliasTable,default)-- Gets the pagetype from a class specified from the first positional-- parameter.param=yesno(param,param)ifparam~=falsethen-- No check if specifically disallowed.for_,aliasinipairs(aliasTable)doifclass==aliastheniftype(param)=='string'thenreturnparamelsereturndefaultendendendendendlocalfunctiongetNsDetectValue(args)-- Builds the arguments to pass to [[Module:Namespace detect]] and returns-- the result.-- Get the default values.localndArgs={}localdefaultns=args[cfg.defaultns]ifdefaultns==cfg.defaultnsAllthenndArgs=shallowCopy(cfg.pagetypes)elselocaldefaultnsArrayifdefaultns==cfg.defaultnsExtendedthendefaultnsArray=cfg.extendedNamespaceselseifdefaultns==cfg.defaultnsNonethendefaultnsArray={}elsedefaultnsArray=cfg.defaultNamespacesendfor_,namespaceinipairs(defaultnsArray)dondArgs[namespace]=cfg.pagetypes[namespace]endend--[[-- Add custom values passed in from the arguments. These overwrite the-- defaults. The possible argument names are fetched from-- Module:Namespace detect automatically in case new namespaces are-- added. Although we accept namespace aliases as parameters, we only pass-- the local namespace name as a parameter to Module:Namespace detect.-- This means that the "image" parameter can overwrite defaults for the-- File: namespace, which wouldn't work if we passed the parameters through-- separately.--]]localmappings=getParamMappings()forns,paramAliasesinpairs(mappings)do-- Copy the aliases table, as # doesn't work with tables returned from-- mw.loadData.paramAliases=shallowCopy(paramAliases)localparamName=paramAliases[1]-- Iterate backwards along the array so that any values for the local-- namespace names overwrite those for namespace aliases.fori=#paramAliases,1,-1dolocalparamAlias=paramAliases[i]localndArg=checkPagetypeInput(paramAlias,args[paramAlias])ifndArg==falsethen-- If any arguments are false, convert them to nil to protect-- against breakage by future changes to-- [[Module:Namespace detect]].ndArgs[paramName]=nilelseifndArgthenndArgs[paramName]=ndArgendendend-- Check for disambiguation-class and N/A-class pages in mainspace.ifndArgs.mainthenlocalclass=args[1]iftype(class)=='string'then-- Put in lower case so e.g. "Dab" and "dab" will both match.class=mw.ustring.lower(class)endlocaldab=getPagetypeFromClass(class,args[cfg.dab],cfg.dabAliases,cfg.dabDefault)ifdabthenndArgs.main=dabelselocalna=getPagetypeFromClass(class,args[cfg.na],cfg.naAliases,cfg.naDefault)ifnathenndArgs.main=naendendend-- If there is no talk value specified, use the corresponding subject-- namespace for talk pages.ifnotndArgs.talkthenndArgs.subjectns=trueend-- Add the fallback value. This can also be customised, but it cannot be-- disabled.localother=args[cfg.other]-- We will ignore true/false/nil results from yesno here, but using it-- anyway for consistency.other=yesno(other,other)iftype(other)=='string'thenndArgs.other=otherelsendArgs.other=cfg.otherDefaultend-- Allow custom page values.ndArgs.page=args.pagereturnnsDetect(ndArgs)endlocalfunctiondetectRedirects(args)localredirect=args[cfg.redirect]-- The yesno function returns true/false for "yes", "no", etc., and returns-- redirect for other input.redirect=yesno(redirect,redirect)ifredirect==falsethen-- Detect redirects unless they have been explicitly disallowed with-- "redirect=no" or similar.returnendlocalpageObject=getPageObject(args.page)-- If we are using subject namespaces elsewhere, do so here as well.ifpageObjectandnotyesno(args.talk,true)andargs[cfg.defaultns]~=cfg.defaultnsAllthenpageObject=getPageObject(pageObject.subjectNsText..':'..pageObject.text)end-- Allow custom values for redirects.ifpageObjectandpageObject.isRedirecttheniftype(redirect)=='string'thenreturnredirectelsereturncfg.redirectDefaultendendendfunctionp._main(args)localredirect=detectRedirects(args)localpagetype=""ifredirectthenpagetype=redirectelsepagetype=getNsDetectValue(args)endifyesno(args.plural,false)thenifcfg.irregularPlurals[pagetype]thenpagetype=cfg.irregularPlurals[pagetype]elsepagetype=pagetype..cfg.plural-- often 's'endendifyesno(args.caps,false)thenpagetype=mw.ustring.upper(mw.ustring.sub(pagetype,1,1))..mw.ustring.sub(pagetype,2)endreturnpagetypeendfunctionp.main(frame)localargs=getArgs(frame)returnp._main(args)endreturnp