Module:Message box: Difference between revisions
Content deleted Content added
m 1 revision |
bring in changes from sandbox |
||
Line 2:
-- {{mbox}}, {{ambox}}, {{imbox}}, {{tmbox}}, {{ombox}}, {{cmbox}} and {{fmbox}}.
--
local getArgs
local categoryHandler = require('Module:Category handler').
local yesno = require('Module:Yesno')
-- Get a language object for formatDate and ucfirst.
local lang = mw.language.getContentLanguage()
--------------------------------------------------------------------------------
Line 24 ⟶ 15:
--------------------------------------------------------------------------------
local function getTitleObject(
-- Get the title object, passing the function through pcall
local success, title = pcall(mw.title.new, ...)
if success then
return title
end
end
Line 46 ⟶ 35:
local ret = {}
for k in pairs(vals) do
end
table.sort(ret)
Line 57 ⟶ 46:
local num = mw.ustring.match(tostring(k), '^' .. prefix .. '([1-9]%d*)$')
if num then
end
end
Line 68 ⟶ 57:
--------------------------------------------------------------------------------
local
function
args = args or {}
local obj = {}
-- Set the title object and the namespace.
obj.title = getTitleObject(args.page) or mw.title.getCurrentTitle()
-- Set the config for our box type.
obj.cfg = cfg[boxType]
if
-- boxType is "mbox" or invalid input
if ns == 0 then
obj.cfg = cfg.ambox -- main namespace
elseif ns == 6 then
obj.cfg = cfg.imbox -- file namespace
elseif ns == 14 then
obj.cfg = cfg.cmbox -- category namespace
else
if nsTable and nsTable.isTalk then
obj.cfg = cfg.tmbox -- any talk namespace
else
obj.cfg = cfg.ombox -- other namespaces or invalid input
end
end
end
-- Set the arguments, and remove all blank arguments except for the ones
-- listed in cfg.allowBlankParams.
do
if
end
end
for i, param in ipairs(obj.cfg.allowBlankParams or {}) do
newArgs[param] = args[param]
end
obj.args = newArgs
end
-- Define internal data structure.
obj.categories = {}
return setmetatable(obj, MessageBox)
end
function
if not cat then
return nil
end
if sort then
cat = string.format('[[Category:%s|%s]]', cat, sort)
else
cat = string.format('[[Category:%s]]', cat)
end
self.categories[ns] = self.categories[ns] or {}
table.insert(self.categories[ns], cat)
end
function
if not class then
return nil
end
table.insert(self.classes, class)
end
function
local args = self.args
local cfg = self.cfg
-- Get type data.
self.type = args.type
Line 198 ⟶ 140:
and self.type
and not typeData
typeData = typeData or cfg.types[cfg.default]
self.typeClass = typeData.class
Line 205 ⟶ 145:
-- Find if the box has been wrongly substituted.
-- Find whether we are using a small message box.
cfg.smallParam and args.small == cfg.smallParam
or not cfg.smallParam and yesno(args.small)
)
-- Add attributes, classes and styles.
Line 260 ⟶ 193:
self.isTemplatePage = self.templateTitle
and mw.title.equals(self.title, self.templateTitle)
end
Line 281 ⟶ 213:
text = type(text) == 'string' and text or nil
local issues = {}
self.issue =
end
Line 292 ⟶ 224:
-- parameter is blank.
if talk == ''
and self.templateTitle
and (
mw.title.equals(self.templateTitle, self.title)
Line 318 ⟶ 250:
local talkText = 'Relevant discussion may be found on'
if talkArgIsTalkPage then
talkText = string.format(
'%s [[%s|%s]].',
talkText,
Line 325 ⟶ 257:
)
else
talkText = string.format(
'%s the [[%s#%s|talk page]].',
talkText,
Line 345 ⟶ 277:
end
if date then
self.date = string.format(" <small>''(%s)''</small>", date)
end
self.info = args.info
Line 362 ⟶ 294:
-- General image settings.
self.imageCellDiv = not self.isSmall and cfg.imageCellDiv
self.imageEmptyCell = cfg.imageEmptyCell
if cfg.imageEmptyCellStyle then
Line 378 ⟶ 310:
and (cfg.imageSmallSize or '30x30px')
or '40x40px'
self.imageLeft = string.format('[[File:%s|%s|link=|alt=]]', self.typeImage
or 'Imbox notice.png', imageSize)
end
Line 388 ⟶ 320:
self.imageRight = imageRight
end
end
function MessageBox:setMainspaceCategories()
local args = self.args
local cfg = self.cfg
if not cfg.allowMainspaceCategories then
return nil
end
local nums = {}
args[prefix .. '1'] = args[prefix]
nums = union(nums, getArgNums(args, prefix))
-- The following is roughly equivalent to the old {{Ambox/category}}.
local date = args.date
date = type(date) == 'string' and date
for _, num in ipairs(nums) do
local
or args['category' .. tostring(num)]
mainCat = type(mainCat) == 'string' and mainCat
local catTitle = string.format('%s %s %s', mainCat, preposition, date)
self:addCat(0, catTitle)
if not catTitle or not catTitle.exists then
self:addCat(0, 'Articles with invalid date parameter in template')
end
elseif mainCat and (not date or date == '') then
self:addCat(0, mainCat)
end
if allCat then
self:addCat(0, allCat)
end
end
end
function MessageBox:setTemplateCategories()
local args = self.args
local cfg = self.cfg
-- Add template
if cfg.templateCategory then
if cfg.templateCategoryRequireName then
if self.isTemplatePage then
self:addCat(
end
elseif not self.title.isSubpage then
self:addCat(
end
end
-- Add template error
if cfg.templateErrorCategory then
local templateErrorCategory = cfg.templateErrorCategory
Line 468 ⟶ 400:
end
end
self:addCat(
end
end
function MessageBox:setAllNamespaceCategories()
-- Set categories for all namespaces.
if self.invalidTypeError then
local allSort = (self.
self:addCat('all', 'Wikipedia message box parameter needs fixing', allSort)
end
Line 479 ⟶ 413:
self:addCat('all', 'Pages with incorrectly substituted templates')
end
end
function MessageBox:setCategories()
if self.title.namespace == 0 then
self:setMainspaceCategories()
elseif self.title.namespace == 10 then
self:setTemplateCategories()
end
self:setAllNamespaceCategories()
end
function MessageBox:renderCategories()
-- Convert category tables to strings and pass them through
-- [[Module:Category handler]].
main =
template =
all =
nocat = self.args.nocat,
}
end
function
local root =
-- Add the subst check error.
if self.isSubstituted and self.name then
root:tag('b')
:wikitext(string.format(
'Template <code>%s[[Template:%s|%s]]%s</code> has been incorrectly substituted.',
mw.text.nowiki('{{'), self.name, self.name, mw.text.nowiki('}}')
))
end
-- Create the box table.
local boxTable = root
boxTable:attr('id', self.id or nil)
for i, class in ipairs(self.classes or {}) do
boxTable:addClass(class or nil)
end
boxTable
if self.attrs then
end
-- Add the left-hand image.
local row = boxTable
if self.imageLeft then
local imageLeftCell = row
if self.imageCellDiv then
-- If we are using a div, redefine imageLeftCell so that the image
Line 531 ⟶ 472:
-- image width to 52px. If any images in a div are wider than that,
-- they may overlap with the text or cause other display problems.
imageLeftCell = imageLeftCell
end
imageLeftCell:wikitext(self.imageLeft or nil)
elseif self.imageEmptyCell then
-- Some message boxes define an empty cell if no image is specified, and
Line 540 ⟶ 480:
-- specified gives the following hint: "No image. Cell with some width
-- or padding necessary for text cell to have 100% width."
row
end
-- Add the text.
local textCell = row
if self.useCollapsibleTextFields then
-- The message box uses advanced text parameters that allow things to be
-- collapsible. At the moment, only ambox uses this.
textCell:cssText(self.textstyle or nil)
local textCellSpan = textCell:tag('span')
textCellSpan
if not self.isSmall then
textCellSpan:tag('span')
:wikitext(self.talk and (' ' .. self.talk) or nil)
end
textCellSpan:wikitext(self.date and (' ' .. self.date) or nil)
if not self.isSmall then
textCellSpan
end
else
-- Default text formatting - anything goes.
textCell
end
-- Add the right-hand image.
if self.imageRight then
local imageRightCell = row
if self.imageCellDiv then
-- If we are using a div, redefine imageRightCell so that the image
-- is inside it.
imageRightCell = imageRightCell
end
imageRightCell
end
-- Add the below row.
if self.below then
boxTable
end
-- Add error message for invalid type parameters.
if self.invalidTypeError then
root:tag('div')
:wikitext(string.format(
'This message box is using an invalid "type=%s" parameter and needs fixing.',
self.type or ''
))
end
-- Add categories.
root:wikitext(self:renderCategories() or nil)
return tostring(root)
end
--------------------------------------------------------------------------------
-- Exports
--------------------------------------------------------------------------------
local p, mt = {}, {}
function p._exportClasses()
-- For testing.
return {
MessageBox = MessageBox
}
end
local box = MessageBox.new(boxType, args, cfgTables or mw.loadData('Module:Message box/configuration'))
box:setParameters()
box:setCategories()
return box:export()
end
function mt.__index(t, k)
return function (frame)
if not getArgs then
getArgs = require('Module:Arguments').getArgs
end
return t.main(k, getArgs(frame, {trim = false, removeBlanks = false}))
end
end
return setmetatable(p, mt)
|