Changes

Jump to: navigation, search

Module:Message box

530 bytes added, 04:46, 26 September 2013
add mbox, various other fixes
-- This is a meta-module for producing message box templates, including {{mbox}}, {{ambox}}, {{imbox}}, {{tmbox}}, {{ombox}}, {{cmbox}} and {{fmbox}}.
-- Require necessary modules.
local htmlBuilder = require('Module:HtmlBuilder')
local nsDetect = require('Module:Namespace detect')
local categoryHandler = require('Module:Category handler').main
local yesno = require('Module:Yesno')
 
-- Get a language object for formatDate and ucfirst.
local lang = mw.language.getContentLanguage()
 
-- Set aliases for often-used functions to reduce table lookups.
local format = mw.ustring.format
local tinsert = table.insert
local tconcat = table.concat
local p = {}
-- Get the title object, passing the function through pcall
-- in case we are over the expensive function count limit.
local success success, page title = pcall(mw.title.new, page) if not success then page = nilreturn title
end
end
return page
end
local preposition = 'from'
if cat and date then
local catTitle = mw.ustring.format('Category:%s %s %s', cat, preposition, date) table.inserttinsert(ret, mw.ustring.format('[[%s]]', catTitle))
catTitle = getTitleObject(catTitle)
if not catTitle or not catTitle.exists then
table.inserttinsert(ret, '[[Category:Articles with invalid date parameter in template]]')
end
elseif cat and not date then
table.inserttinsert(ret, mw.ustring.format('[[Category:%s]]', cat))
end
if all then
table.inserttinsert(ret, mw.ustring.format('[[Category:%s]]', all))
end
return table.concattconcat(ret)
end
end
local ret = {}
for k, v in pairs(vals) do table.inserttinsert(ret, k)
end
table.sort(ret)
return ret
end
local num = mw.ustring.match(tostring(k), '^' .. prefix .. '([1-9]%d*)$')
if num then
table.inserttinsert(nums, tonumber(num))
end
end
table.sort(nums)
return nums
end
 
local function getNamespaceId(ns)
if type(ns) == 'string' then
ns = lang:ucfirst(mw.ustring.lower(ns))
if ns == 'Main' then
ns = 0
end
end
local nsTable = mw.site.namespaces[ns]
if nsTable then
return nsTable.id
end
end
 
local function getMboxType(nsid)
-- Gets the mbox type from a namespace number.
if nsid == 0 then
return 'ambox' -- main namespace
elseif nsid == 6 then
return 'imbox' -- file namespace
elseif nsid == 14 then
return 'cmbox' -- category namespace
else
local nsTable = mw.site.namespaces[nsid]
if nsTable and nsTable.isTalk then
return 'tmbox' -- any talk namespace
else
return 'ombox' -- other namespaces or invalid input
end
end
end
function p.build(boxType, args)
if type(args) ~= 'table' then
error(format('invalid "args" parameter type; expected type "table", got type "%s"', type(args)), 2)
end
 
-- Get the title object and the namespace.
local title = getTitleObject(args.page) or mw.title.getCurrentTitle()
local nsid = getNamespaceId(args.demospace) or title.namespace
 
-- Get the box config data from the data page.
if boxType == 'mbox' then
boxType = getMboxType(nsid)
end
local dataTables = mw.loadData('Module:Message box/data')
local data = dataTables[boxType]
local boxTypes = {}
for k, v in pairs(dataTables) do
table.inserttinsert(boxTypes, mw.ustring.format('"%s"', k))
end
tinsert(boxTypes, '"mbox"') error(mw.ustring.format('invalid message box type "%s"; valid types are %s', tostring(boxType), mw.text.listToText(boxTypes)), 2)
end
 
-- Get the title object and the namespace.
local title = mw.title.getCurrentTitle()
local nsid = title.namespace
 
-- Get a language object for formatDate.
local lang = mw.language.getContentLanguage()
-- Commenting this out for now - this will require tinkering with Namespace detect to differentiate between
-- invalid titles and pages where the expensive parser function count has been exceeded.
--[[
local title = nsDetect.getPageObject(args.page)
local namespace = nsDetect.main{
page = args.page,
demospace = args.demospace,
main = 'main',
talk = 'talk',
file = 'file',
category = 'category',
other = 'other'
}
]]
------------------------ Process config data ----------------------------
local sect = args.sect
if presentButBlank(sect) then
sect = mw.ustring.format('This %s ', data.sectionDefault or 'page')
elseif type(sect) == 'string' then
sect = 'This ' .. sect .. ' '
local talkText = ' Relevant discussion may be found on'
if talkTitle.isTalkPage then
talkText = mw.ustring.format('%s [[%s|%s]].', talkText, talk, talkTitle.prefixedText)
else
talkText = mw.ustring.format('%s the [[%s#%s|talk page]].', talkText, talkTitle.prefixedText, talk)
end
talk = talkText
local cat = args['cat' .. tostring(num)] or args['category' .. tostring(num)]
local all = args['all' .. tostring(num)]
table.inserttinsert(mainCats, formatCategory(cat, args.date, all))
end
end
local templateCats = {}
if data.templateCategory and not title.isSubpage and not yesno(args.nocat) then
table.inserttinsert(templateCats, mw.ustring.format('[[Category:%s]]', data.templateCategory))
end
local templateCat
if not name and not title.isSubpage then
templateCat = mw.ustring.format('[[Category:%s]]', catName)
elseif type(name) == 'string' and title.prefixedText == ('Template:' .. name) then
local paramsToCheck = data.templateErrorParamsToCheck or {}
end
if count > 0 then
templateCat = mw.ustring.format('[[Category:%s|%d]]', catName, count)
end
if origCategoryNums and #origCategoryNums > 0 then
templateCat = mw.ustring.format('[[Category:%s|C]]', catName)
end
end
table.inserttinsert(templateCats, templatecat)
end
if invalidType then
local catsort = (nsid == 0 and 'Main:' or '') .. title.prefixedText
table.inserttinsert(allCats, mw.ustring.format('[[Category:Wikipedia message box parameter needs fixing|%s]]', catsort))
end
.tag('b')
.addClass('error')
.wikitext(mw.ustring.format(
'Template <code>%s%s%s</code> has been incorrectly substituted.',
mw.text.nowiki('{{'), name, mw.text.nowiki('}}')
))
end
table.inserttinsert(allCats, '[[Category:Pages with incorrectly substituted templates]]')
end
end
imageLeftCell
.wikitext(image or mw.ustring.format('[[File:%s|%s|link=|alt=]]', typeData.image, imageSize))
elseif data.imageEmptyCell then
row.tag('td')
end
textCellSpan
.wikitext(date and mw.ustring.format(" <small>''(%s)''</small>", date))
if not isSmall then
textCellSpan
.addClass('error')
.css('text-align', 'center')
.wikitext(mw.ustring.format('This message box is using an invalid type parameter (<code>type=%s</code>) and needs fixing.', args.type or ''))
end
root
.wikitext(categoryHandler{
main = table.concattconcat(mainCats), template = table.concattconcat(templateCats), all = table.concattconcat(allCats)
})
end
p.mbox = makeWrapper('mbox')
p.ambox = makeWrapper('ambox')
p.cmbox = makeWrapper('cmbox')
p.fmbox = makeWrapper('fmbox')
p.imbox = makeWrapper('imbox')
p.ombox = makeWrapper('ombox')
p.cmbox = makeWrapper('cmbox')
p.tmbox = makeWrapper('tmbox')
return p
Anonymous user

Navigation menu