Module:Main: Difference between revisions

m
16 revisions imported from templatewiki:Module:Main
(Tweaked tracking to make exclusive to mainspace)
m (16 revisions imported from templatewiki:Module:Main)
 
(5 intermediate revisions by 3 users not shown)
Line 9:
 
local mHatnote = require('Module:Hatnote')
mTableToolslocal mHatlist = require('Module:TableToolsHatnote list')
local mTableTools -- lazily initialise
local mArguments -- lazily initialise
 
local p = {}
 
function p.main(frame)
mTableTools = require('Module:TableTools')
mArguments = require('Module:Arguments')
local args = mArguments.getArgs(frame, {parentOnly = true})
Line 21 ⟶ 19:
for k, v in pairs(args) do
if type(k) == 'number' then
local display = args['label ' .. k] or args['l' .. tostring(k)]
local page = {v, display} and
string.format('%s|%s', string.gsub(v, '|.*$', ''), display) or v
pages[k#pages + 1] = page
end
end
localif emptyArgs#pages == ((0 and mw.title.getCurrentTitle().namespace == 0) andthen
pages = mTableTools.compressSparseArray(pages)
return mHatnote.makeWikitextError(
'no page names specified',
'Template:Main#Errors',
args.category
)
end
local options = {
selfref = args.selfref
}
return p._main(optionspages, unpack(pages)options)
end
 
function p._main(optionsargs, ...options)
-- Get the list of pages. If no first page was specified we use the current
-- page name.
local pages = {...}
local currentTitle = mw.title.getCurrentTitle()
if #args == 0 then args = {currentTitle.text} end
local firstPageTable = pages[1]
local firstPage = string.gsub(args[1], '|.*$', '')
local emptyArgs = ((mw.title.getCurrentTitle().namespace == 0) and
not firstPageTable) and
'[[Category:Hatnote templates using unusual parameters]]' or ''
if firstPageTable then
firstPage = firstPageTable[1]
else
firstPage = currentTitle.text
firstPageTable = {firstPage}
pages[1] = firstPageTable
end
 
-- Find the pagetype.
local firstPageNspageType = mHatnote.findNamespaceId(firstPage) == 0 and 'article' or 'page'
local pagetype = firstPageNs == 0 and 'article' or 'page'
pagetype = pagetype .. emptyArgs
 
-- Make the formatted link text
list = mHatlist.andList(args, true)
local links = mHatnote.formatPageTables(unpack(pages))
links = mw.text.listToText(links)
 
-- Build the text.
local isPlural = #pagesargs > 1
local mainForm
local currentNs = currentTitle.namespace
firstPage local curNs = currentTitle.textnamespace
local isCategoryNamespace = currentNs - currentNs % 2 == 14
if (curNs == 14) or (curNs == 15) then --category/talk namespaces
local stringToFormat
ifmainForm = isPlural thenand
if isCategoryNamespace then
'The main %ss for this [[Help:Categories|category]] are %s'
if isPlural then
or
stringToFormat = 'The main %ss for this'
.. 'The main %s for this [[WikipediaHelp:CategorizationCategories|category]] areis %s'
else
stringToFormat = 'The main %s for this'
.. ' [[Wikipedia:Categorization|category]] is %s'
end
else
ifmainForm = isPlural thenand 'Main %ss: %s' or 'Main %s: %s'
stringToFormat = 'Main %ss: %s'
else
stringToFormat = 'Main %s: %s'
end
end
local text = string.format(stringToFormatmainForm, pagetypepageType, linkslist)
 
-- Process the options and pass the text to the _rellink function in
-- [[Module:Hatnote]].
options = options or {}
local hnOptions = {
extraclasses = 'relarticle mainarticle',
selfref = options.selfref
}