Module:Article stub box: Difference between revisions

Content deleted Content added
comments and some code simplification
rewrote p.main as to not need buffer; disabled demo var in article space; more detailed comments
Line 5:
local args, stubCats
 
--[[
function demo(text, alt)
Formats category links. Stores them until called with cat.done=true
return args.demo and text or alt
Takes multiple or single categories in the form of 'cat'
end
or a table of strings and/or tables containing parts. (See below)
 
]]function category(cat)
--Formats category links. Stores them until called with cat.done=true
local attention, link = 'Stub message templates needing attention'
--Takes multiple or single categories in the form of 'cat' or {'cat1', 'cat2', {'cat3', 'sort3'}}
for _, v in ipairs((type(cat) == type'' or cat.t) and {cat} or cat) do--[[
--If it finds a category name of length 1, assumes it is a sort key.
If v is a table:
--Such keys must always be paired in a table {{'cat', 'sort'}}
local attention [1] = 'Stubfull messagecategory templatesname; needingdefaults to local attention' if blank
k = Category sort key. Prefix before v.t
function category(cat)
t = page.text or args.tempsort#; appended after k or in its place if omitted;t is required if v is not a string
for _, v in ipairs(type(cat) == type'' and {cat} or cat) do
Basically the same as v = (v[1] or attention) .. ' | ' .. (v.k or '') .. v.t
p.key = string.format(
]] link = string.format(
'[[%sCategory:%s]]',
p.demo( and ':', or ''),
not v.t and v or string.format(
--All user-inputed cats are always paired with a sort key
p.demo and '%s|Category:%s|%s' or '%s|%s%s',
--Since all non-user cat are longer than 2 chars, we can
v[1] or attention,
--compare length instead of checking type
# p.demo and (v[1] >or 2attention) andor v.k or string.format('',
p.demo and ('%s|Category:%s|%s',(v.k or '%s|%s%s'), .. v.t) or v.t
#v[1] < 2 and attention or v[1],
#v[1] < 2 and attention or v[1],
#v[1] < 2 and table.concat(v) or v[2]
)
)
if not p.cats.inv[p.keylink] then
table.insert(p.cats, p.keylink)
p.cats.inv[p.keylink] = #p.cats
end
end
return cat.done and table.concat(p.cats, p.demo( and ' | ', or '')) or ''
end
 
--Makes an ombox warning; will return an empty string instead
--Takes table {ifNot = Boolean, text, {cat. sort key, cat. sort name}}
--if optional v.ifNot is true
function ombox(v)--{text, {subcat, page}}
if v.ifNot then
return ''
Line 51 ⟶ 49:
end
 
function catStub(page, pageDoc)
 
stubCats = {missing = {}, v = {}}
function catStub(page, pageDoc)--p.templatepage passes page; p.main does not
stubCats = {
missing = {},
S = {},
v = {}
}
--Find category parameters and store the number (main cat = '')
for k, _ in pairs(args) do
--Find category parameters and store the number (main cat = '')
table.insert(stubCats, string.match(k, '^category(%d*)$'))
end
table.sort(stubCats)
local cat, tsort
for k, v in ipairs(stubCats) do
--Get category names and, if called by p.templatepage, the optional sort key
local cat, tsort = args['category' .. v], args['tempsort' .. v]
--Do not place in main category if |tempsort = 'no'; For example, if stub template merely
tsort = args['tempsort' .. v]
--adds |qualifier= to a meta stub and |category#= to a sub-category of meta's main category
--Does not place in main category if |tempsort = 'no'
--For example, if stub template simply adds |qualifier= to a meta stub
--and includes a |category#= to a sub-category of the main category
if v ~= '' or tsort ~= 'no' then
--p.templatepage passes page; p.main does not, i.e. articles are categorized without sort keys.
table.insert(stubCats.v, {cat, page and (tsort or ('*' .. page.text))})
table.insert(stubCats.v, page and
(tsort and {cat, t = tsort} or {cat, k = '*', t = page.text})
or cat
)
end
--Check category existance only if on the template page (i.e. stub documentation)
if page then
--Check category existance
--This check is only performed on the template page
--since it uses an expensive parser function
if not mw.title.new('Category:' .. cat).exists then
table.insert(stubCats.missing, '<code>category' .. v .. '</code>')
end
--[[
table.insert(stubCats.v,
Checks non--Onlydemo checkstub template for docs if main |category= is setdocumentation and flags if doc is present.
All stub cats names are checked and flagged if it does not match 'Category: [] stub'.
--Main categories with no docs and all numbered categories
The main stub cat is exempt from the name check if the stub template has its own doc
--are checked for proper naming and flagged if not
(presumably, this doc would have an explanation as to why the main stub cat is non-conforming).
v == '' and pageDoc.exists and
]] table.insert(stubCats.v,
v == '' and not p.demo and pageDoc.exists and
'Stub message templates with documentation subpages'
or not string.match(cat, ' stubs$') and {k = 'S', t = page.text}
or nil
)
end
end
--Add category names after loop is completed
category(stubCats.v)
return #stubCats.missing > 0 and ombox{
--Out params set to non-categories with <code> tags to ombox
--Changed, original msg:
return stubCats.missing and ombox{
--Changed, original msg: One or more of the stub categories defined in this template do not seem to exist! Please double-check the parameters {{para|category}}, {{para|category1}} and {{para|category2}}.
'The following parameter'
.. (#stubCats.missing == 1 and ' defines a stub category that does' or 's define stub categories that do')
.. ' not exist: ' .. mw.text.listToText(stubCats.missing),
{k = 'N', t = page.text}
} or ''
end
 
--Shows population of categories found by catStub(). Outputs demo values if none
--Outputs demo values if there are none
function population()
local wikitext, base = '', '* [[:Category:%s]] (population: (%s)\n'
if #not args.category and stubCats[1] =~= 0false then
table.insert(stubCats, 1, false)
return string.format(base, '{{{category}}}', 0)
end
for k, v in ipairs(stubCats) do
wikitext = wikitext .. string.format(
base,
v and args['category' .. v] or '{{{category}}}',
v and mw.site.stats.pagesInCategory(args['category' .. v], 'all')) or 0
)
end
return wikitext
end
 
--Includes standard stub documention and flags stub templates with bad parameter values.
--Flags bad stub templates and creates standard doc page
function p.templatepage(frame, page)
local tStubDoc = mw.title.new('Template:Stub documentation')
local pageDoc = mw.title.new(page.fullText .. '/doc')
page = page or mw.title.getCurrentTitle()
args = args or require('Module:Arguments').getArgs(frame, {
wrappers = WRAPPER_TEMPLATE
})
page = page or mw.title.getCurrentTitle()
category{
p.demo = p.demo or page.namespace ~= 0 and args.demo or nil
local tStubDoc = mw.title.new'Template:Stub documentation'
local pageDoc = mw.title.new(page.fullText .. '/doc')
--Reorganization note: Original Asbox alternates between outputting categories and checking on params |category#=.
--Rather than checking multiple times and switching tasks, all stub category param operations have been rolled into catStub()
return ombox{--Show ombox warnings for missing args. Ombox for args with non-existing categories is in catStub()
ifNot = args.category,
'The <code>|category</code> parameter is not set. Please add an appropriate stub category.',
{k = 'C', t = page.text}
}
.. ombox{
ifNot = args.subject or args.article or args.qualifier,
'This stub template contains no description! At least one of the parameters <code>|subject</code>, <code>|article</code> or <code>|qualifier</code> must be defined.',
{k = 'D', t = page.text}
}
.. catStub(page, pageDoc)
.. category{
done = p.demo ~= 'doc',
'Stub message templates',
'Exclude in print',
--Changed. Original Asbox does its first of 3 checks on params |category#= here.
--Rather than checking multiple times, all operations involving those params have been rolled into catStub()
(args.icon and
'Stub message templates using icon parameter'
Line 133 ⟶ 145:
(mw.title.new('Media:' .. mw.text.split(args.image, '|')[1]).exists and
nil
or {k = 'B', t = page.text}
)
or 'Stub message templates without images'
)
),
args.imagealt and {k = 'I', t = page.text} or nil
}
--Add standard stub template documentation; demo calls will not call Module:Documentation unless p.demo = 'doc'
--Show ombox warnings and check for bat stub categories
.. (p.demo and p.demo ~= 'doc' and '' or require('Module:Documentation').main{
return ombox{
ifNot = args.category,
'The <code>|category</code> parameter is not set. Please add an appropriate stub category.',
{'C', page.text}
}
.. ombox{
ifNot = args.subject or args.article or args.qualifier,
'This stub template contains no description! At least one of the parameters <code>|subject</code>, <code>|article</code> or <code>|qualifier</code> must be defined.',
{'D', page.text}
}
.. catStub(page, pageDoc)
--Add standard stub template documentation
.. ((args.demo and args.demo ~= 'doc' and '') or (require('Module:Documentation').main{
content =
(page.text ~= 'Stub' and --This comparison performed in {{Asbox/stubtree}} before it invokes Module:Asbox stubtree
require('Module:Asbox stubtree').subtree{args = {pagename = page.text}}
or ''
Line 168:
.. '}}</code> produces the message shown at the beginning, and adds the article to the following categor' .. (#stubCats > 1 and 'ies' or 'y') .. ':\n'
.. population()
.. (pageDoc.exists and --transclusion of /doc if it exists
frame:expandTemplate{title = pageDoc.text}
or ''
Line 183:
.. (page.protectionLevels.edit[1] == 'sysop' and
" <br/>This template is [[WP:PROTECT|fully protected]] and any [[WP:CAT|categories]] should be added to the template's ["
.. pageDoc:fullURL('action=edit&preload=Template:Category_interwiki/preload', 'relative')
.. '| /doc] subpage, which is not protected.'
or ''
)
})
--Output categories when done.
.. (args.demo ~= 'doc' and category{done = true} or '')))
end
 
--The template as it appears on articles.
function p.main(frame, page)
page = page or mw.title.getCurrentTitle()
args = args or require('Module:Arguments').getArgs(frame, {
wrappers = WRAPPER_TEMPLATE
})
page = page or mw.title.getCurrentTitle()
--Ensures demo parameter will never affect article space output. Necessary because demo now affects category()
p.demo = p.demo or page.namespace ~= 0 and args.demo or nil
local output = mw.html.create()
local asbox = output :tag'table'
:addClass('metadata plainlinks stub')
:css('background', 'transparent')
:attr('role', 'presentation')
if :node((args.icon or args.image) thenand
mw.html.create'td'
asbox:tag('td'):wikitext(args.icon or string.format(
:wikitext(args.icon or string.format(
'[[File:%s|%spx|alt=%s]]',
'[[File:%s|%spx|alt=%s]]',
args.image,
args.pix or '40x30'image,
args.imagealtpix or 'Stub icon40x30',
args.imagealt or 'Stub icon'
))
))
end
)
local buffer = asbox:tag('td')
:node(
buffer:tag('i'):wikitext(string.format(
mw.html.create'td'
'This %s %s %s is a [[Wikipedia:stub|stub]]. You can help Wikipedia by [%s expanding it].',
:tag'i'
args.subject or '',
:wikitext(string.format(
args.article or 'article',
'This %s %s %s is a [[Wikipedia:stub|stub]]. You can help Wikipedia by [%s expanding it].',
args.qualifier or '',
args.subject or '',
page:fullUrl('action=edit', 'relative')
args.article or 'article',
))
if args.namequalifier or then'',
page:fullUrl('action=edit', 'relative')
buffer:wikitext(require('Module:Navbar')._navbar{
args.name, ))
:done()
mini = 'yes',
:wikitext(args.name and
style = 'position: absolute; right: 15px; display: none;'
require('Module:Navbar')._navbar{
})
args.name,
end
mini = 'yes',
if args.note then
style = 'position: absolute; right: 15px; display: none;'
buffer:tag('br')
}
buffer:tag('span')
or nil
:css('font-style', 'normal')
)
:css('font-size', 'smaller')
:wikitextnode(args.note) and
mw.html.create()
:tag'br':done()
:tag'span'
:css('font-style', 'normal')
:css('font-size', 'smaller')
:wikitext(args.note)
:done()
)
)
:done()
--[[
Stub categories for templates include a sort key (Otherwise all will be indexed under the letter 'T' for 'Template:[] stubs')
Articles using the template do not need a sort key since they have unique names.
When p.demo equals 'doc', the demo stub categories will appear as those for a stub template.
Otherwise, any non-nil p.demo will emulate article space categories (plus any error cats unless set to 'art')
]] if p.demo ~= 'doc' then
catStub()
end
if page.namespace == 0 then -- Main namespace
category'All stub articles'
elseif p.demo then
catStub()
--Unless p.demo is set to 'art', it will also include error categories normally only shown on
elseif args.demo then
--the template but not in the article. The elseif after namespace == 0 means demo cats will never show in article space.
--Preview categories; not in original template, but shouldn't hurt
p.demodoc = p.demo ~= 'art' and p.templatepage(frame, page) or nil
output:wikitext(string.format('<small>Demo categories: %s</small>', category{done = true}))
else
--Checks for valid name; emulates original template's check using {{FULLPAGENAME:{{{name|}}}}}
local normalizedName = mw.title.new(args.name or '')
if normalizedName and normalizedName.fullText == page.fullText then
output:wikitext(p.templatepage(frame, page))
elseif not page.isSubpage and page.namespace == 10 then -- Template namespace and not a subpage
category{{args.name and 'E' or 'W', page.text}}
end
Line 250 ⟶ 267:
return output
:wikitext(
category{done = not argsp.demo},
argsp.demo == 'doc' and p.templatepage(frame, page)demodoc or nil
)
end