Module:Category main article/sandbox
| This is the module sandbox page for Module:Category main article (diff). See also the companion subpage for test cases (run). |
| This Lua module is used on approximately 234,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. |
| 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:
local mCatMain = 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}}.
local mHatnote = require('Module:Hatnote')
local mFormatLink = require('Module:Format link')
local yesno = require('Module:Yesno')
local mTableTools -- lazily initialise
local mArguments -- lazily initialise
local p = {}
-- Helper: true if title is Template:Category main article or subpage
local function isTemplateOrSubpage(title)
local t = type(title) == 'string' and title or title.prefixedText
return t:match('^Template:Category main article')
end
function p.catMain(frame)
mTableTools = require('Module:TableTools')
mArguments = require('Module:Arguments')
local args = mArguments.getArgs(frame)
local pages = mTableTools.compressSparseArray(args)
if #pages == 0 and args[1] then
pages = { args[1] }
end
local options = {
article = args.article,
selfref = args.selfref,
_rawPages = pages
}
local thisTitle = mw.title.getCurrentTitle()
local titleText = thisTitle.prefixedText
local ns = thisTitle.namespace
-- Add tracking categories
if not isTemplateOrSubpage(titleText) and frame and frame.addCategories then
-- 1) Wrong namespace (only article namespace)
if ns == 0 then
frame:addCategories('Articles using category hatnotes')
end
-- 2) Redlink detection (all pages)
for i, page in ipairs(pages) do
local checkPage = page:gsub("|.*", "")
if checkPage ~= '' and not isTemplateOrSubpage(checkPage) then
local t = mw.title.new(checkPage)
if t and not t.exists then
frame:addCategories('Categories with hatnote templates targeting a non-existent page')
break
end
end
end
-- 3) Mismatch detection (only single-page input)
if #pages == 1 then
local usedAutoFill = false
local pageName = pages[1]
if not pageName or pageName == '' then
pageName = thisTitle.text
usedAutoFill = true
end
local compareStr
if usedAutoFill then
compareStr = pageName:gsub('^[^:]*:', '')
else
compareStr = options._rawPages[1]:gsub('^[^:]*:', '')
end
local catName = thisTitle.text:gsub('^[^:]*:', '')
if compareStr ~= catName then
frame:addCategories('Category main article does not match category title')
end
end
end
-- Generate the hatnote text
return p._catMain(options, unpack(pages))
end
function p._catMain(options, ...)
options = options or {}
local pages = {...}
local thisTitle = mw.title.getCurrentTitle()
local thisText = thisTitle.text
-- Get the links table.
local rawLinks = mFormatLink.formatPages({}, {...})
local links = {}
for i, link in ipairs(rawLinks) do
links[i] = tostring(link)
end
local usedAutoFill = false
if not links[1] then
local title = mw.title.new(thisText)
if title.isRedirect then
title = title.redirectTarget
end
links[1] = tostring(mFormatLink._formatLink{link = title.text})
usedAutoFill = true
pages = { title.text }
end
-- Bold links
for i, link in ipairs(links) do
links[i] = string.format("'''%s'''", link)
end
-- Get the pagetype.
local pagetype
if options.article ~= nil then
pagetype = yesno(options.article) ~= false and 'article' or 'page'
elseif pages and pages[1] then
local page = pages[1]:gsub("|.*","")
pagetype = mw.title.new(page).namespace == 0 and "article" or "page"
else
pagetype = "article"
end
-- Work out whether we need to be singular or plural.
local stringToFormat
if #links > 1 then
stringToFormat = 'The main %ss for this [[Help:Categories|category]] are %s.'
else
stringToFormat = 'The main %s for this [[Help:Categories|category]] is %s.'
end
-- Get the text.
local text = string.format(
stringToFormat,
pagetype,
mw.text.listToText(links)
)
-- Pass it through to Module:Hatnote.
if not isTemplateOrSubpage(thisTitle) then
local hnOptions = { selfref = options.selfref }
text = mHatnote._hatnote(text, hnOptions)
end
return text
end
return p