VOOZH about

URL: https://en.wikipedia.org/wiki/Module:Category_main_article

⇱ Module:Category main article - Wikipedia


Jump to content
From Wikipedia, the free encyclopedia
👁 Image
Module documentation
[view] [edit] [history] [purge]
👁 Warning
This Lua module is used on approximately 224,000 pages.
To avoid major disruption and server load, any changes should be tested in the module's /sandbox or /testcases subpages, or in your own module sandbox. The tested changes can be added to this page in a single edit. Consider discussing changes on the talk page before implementing them.
👁 Image
This module depends on the following other modules:

This module produces hatnote saying "The main article for this category is x." It implements the {{Category main article}} template.

Use from wikitext

This module should usually be used via the {{Category main article}} template. However, it can also be used from #invoke with the syntax {{#invoke:Category main article|catMain|parameters}}. Please see the {{Category main article}} template documentation for available parameters.

Use from other Lua modules

Load the module:

localmCatMain=require('Module:Category main article')

You can then use the _catMain function like this:

mCatMain._catMain(options,...)

options is an optional table that can be used to configure the function's output. There are two available options, "article" and "selfref".

  • article - if this is set to false, "no", "n", "false", or 0, the module outputs "The main page" rather than "The main article". Use the code {article=false}.
  • selfref - this is used when the output is a self-reference to Wikipedia. To set this option, use {selfref=true}. (See the {{selfref}} template for more details on self-references.)

The remaining arguments are page names to be turned into link(s) following the text "The main article for this category is". If no page names are specified, the current page name (minus the namespace name) is used for the first link.

Example 1
mCatMain._catMain(nil,'Foo')

Produces:

<div class="hatnote relarticle mainarticle">The main article for this [[Help:Categories|category]] is '''[[Foo]]'''.</div>

Displays as:

Example 2
mCatMain._catMain(nil,'Foo','Bar','Baz')

Produces:

<div class="hatnote relarticle mainarticle">The main articles for this [[Help:Categories|category]] are '''[[Foo]]''', '''[[Bar]]''' and '''[[Baz]]'''.</div>

Displays as:

Example 3
mCatMain._catMain({article=false},'Foo')

Produces:

<div class="hatnote relarticle mainarticle">The main page for this [[Help:Categories|category]] is '''[[Foo]]'''.</div>

Displays as:

Technical details

This module uses Module:Hatnote to format the hatnote text.

Text output

This module has five lines that must be translated when exported to wikis in other languages.

Variable Code Translatable text
pagetype pagetype = yesno(options.article) ~= false and 'article' or 'page' article page
pagetype = mw.title.new(page).namespace == 0 and "article" or "page" article page
pagetype = "article" article
stringToFormat stringToFormat = 'The main %ss for this [[Help:Categories|category]] are %s.' The main %ss for this [[Help:Categories|category]] are %s.
stringToFormat = 'The main %s for this [[Help:Categories|category]] is %s.' The main %s for this [[Help:Categories|category]] is %s.
-- This module implements {{Category main article}}.
localmHatnote=require('Module:Hatnote')
localmFormatLink=require('Module:Format link')
localyesno=require('Module:Yesno')
localmTableTools-- lazily initialise
localmArguments-- lazily initialise
localp={}
-- Helper: true if the page is in Template: or Module: namespace
localfunctionisTemplateOrModule(title)
localt=(type(title)=='string'andtitle)or(titleandtitle.prefixedText)or''
ift==''then
returnfalse
end
localok,tt=pcall(mw.title.new,t)
ifnotokornotttthen
returnfalse
end
localns=tt.namespace
returnns==10orns==828
end
functionp.catMain(frame)
mTableTools=require('Module:TableTools')
mArguments=require('Module:Arguments')
-- Grab args
localargs=mArguments.getArgs(frame,{wrappers='Template:Category main article'})
localpages=mTableTools.compressSparseArray(args)
if#pages==0andargs[1]then
pages={args[1]}
end
localoptions={
article=args.article,
selfref=args.selfref,
_rawPages=pages,
}
-- Determine the outer page (the page using the template)
localthisTitle=frameandframe.getParentandframe:getParent()andframe:getParent().titleormw.title.getCurrentTitle()
localtitleText=thisTitleandthisTitle.prefixedTextor''
localns=thisTitleandthisTitle.namespaceornil
-- Skip tracking categories on template/module pages
localisTemplate=isTemplateOrModule(titleText)
-- Remember original arg count
localorigCount=#pages
-- Generate hatnote text
localtext,firstLink,usedAutoFill=p._catMain(options,thisTitle,unpack(pages))
ifisTemplatethen
-- On templates/modules, just return the hatnote (no tracking categories)
returntext
end
-- Add tracking categories
localcats={}
-- Wrong namespace detection (Article, Draft)
ifns==0orns==118then
table.insert(cats,'[[Category:Articles using category hatnotes]]')
end
-- Title mismatch detection
iffirstLinkand(origCount==1or(origCount==0andusedAutoFill))then
localfunctionnormalizeTitle(s)
return(sor''):gsub('^%s+',''):gsub('%s+$',''):gsub('^[^:]*:',''):gsub('%s+',' '):gsub('^%l',string.upper)
end
localcompareStr
ifusedAutoFillthen
compareStr=normalizeTitle(firstLink)
else
localraw=(options._rawPagesandoptions._rawPages[1])orfirstLink
compareStr=normalizeTitle(raw:gsub("|.*",""))
end
localcatName=normalizeTitle(thisTitleandthisTitle.textor'')
ifcompareStr~=catNamethen
table.insert(cats,'[[Category:Category main article does not match category title]]')
end
end
if#cats>0then
text=text..'\n'..table.concat(cats,'\n')
end
returntext
end
functionp._catMain(options,thisTitle,...)
options=optionsor{}
localpages={...}
thisTitle=thisTitleormw.title.getCurrentTitle()
localfullTitle=thisTitleandthisTitle.prefixedTextor''
localthisText=thisTitleandthisTitle.textor(pages[1]or'')
localisTemplate=isTemplateOrModule(fullTitle)
-- Red link detection
localrawLinks=mFormatLink.formatPages({
categorizeMissing=notisTemplateand'Categories with hatnote templates targeting a non-existent page'ornil
},pages)
-- Convert to plain strings
locallinks={}
fori,linkinipairs(rawLinksor{})do
links[i]=tostring(link)
end
-- Auto-fill if empty
localfirstOutputLink=nil
localusedAutoFill=false
ifnotlinks[1]orlinks[1]==''then
localtitle=mw.title.new(thisText)
iftitleandtitle.isRedirectthen
title=title.redirectTarget
end
firstOutputLink=titleandtitle.textorthisText
usedAutoFill=true
links[1]=tostring(mFormatLink._formatLink{
link=firstOutputLink,
categorizeMissing=notisTemplateand'Categories with hatnote templates targeting a non-existent page'ornil
})
pages={firstOutputLink}
else
localvisible=links[1]
visible=visible:gsub("^'''",""):gsub("'''$","")
visible=visible:gsub("%[%[",""):gsub("%]%]","")
visible=visible:gsub("|.*$","")
visible=visible:gsub("#.*$","")
firstOutputLink=visible
usedAutoFill=false
end
-- Bold links
fori,linkinipairs(links)do
links[i]=string.format("'''%s'''",link)
end
-- Determine pagetype
localpagetype
ifoptions.article~=nilthen
pagetype=yesno(options.article)~=falseand'article'or'page'
elseifpagesandpages[1]then
localpage=pages[1]:gsub("|.*","")
localtt=mw.title.new(page)
pagetype=ttandtt.namespace==0and"article"or"page"
else
pagetype="article"
end
-- Work out whether we need to be singular or plural
localstringToFormat
if#links>1then
stringToFormat='The main %ss for this [[Help:Categories|category]] are %s.'
else
stringToFormat='The main %s for this [[Help:Categories|category]] is %s.'
end
-- Build hatnote text
localtext=string.format(
stringToFormat,
pagetype,
mw.text.listToText(links)
)
-- Pass through Module:Hatnote
localhnOptions={selfref=options.selfref}
text=mHatnote._hatnote(text,hnOptions)
returntext,firstOutputLink,usedAutoFill
end
returnp