http://viper.lbl.gov:8080/cctbx.xfel/index.php?title=Message_box&feed=atom&action=historyMessage box - Revision history2024-03-29T07:30:29ZRevision history for this page on the wikiMediaWiki 1.23.1http://viper.lbl.gov:8080/cctbx.xfel/index.php?title=Message_box&diff=405&oldid=prevHattne: 1 revision: Eigth attempt at importing mbox templates from en.wikipedia.org.2014-02-05T10:48:21Z<p>1 revision: Eigth attempt at importing mbox templates from en.wikipedia.org.</p>
<table class='diff diff-contentalign-left'>
<tr style='vertical-align: top;'>
<td colspan='1' style="background-color: white; color:black; text-align: center;">← Older revision</td>
<td colspan='1' style="background-color: white; color:black; text-align: center;">Revision as of 10:48, 5 February 2014</td>
</tr><tr><td colspan='2' style='text-align: center;'><div class="mw-diff-empty">(No difference)</div>
</td></tr></table>Hattnehttp://viper.lbl.gov:8080/cctbx.xfel/index.php?title=Message_box&diff=404&oldid=prevMr. Stradivarius: fix bug for data being shared between multiple message boxes called from the same module; allow boxes to be hidden; use Module:Arguments to fetch the arguments2014-01-27T01:04:43Z<p>fix bug for data being shared between multiple message boxes called from the same module; allow boxes to be hidden; use <a href="/cctbx.xfel/index.php?title=Module:Arguments&action=edit&redlink=1" class="new" title="Module:Arguments (page does not exist)">Module:Arguments</a> to fetch the arguments</p>
<p><b>New page</b></p><div>-- This is a meta-module for producing message box templates, including {{mbox}}, {{ambox}}, {{imbox}}, {{tmbox}}, {{ombox}}, {{cmbox}} and {{fmbox}}.<br />
<br />
-- Require necessary modules.<br />
local getArgs = require('Module:Arguments').getArgs<br />
local htmlBuilder = require('Module:HtmlBuilder')<br />
local categoryHandler = require('Module:Category handler').main<br />
local yesno = require('Module:Yesno')<br />
<br />
-- Load the configuration page.<br />
local cfgTables = mw.loadData('Module:Message box/configuration')<br />
<br />
-- Get a language object for formatDate and ucfirst.<br />
local lang = mw.language.getContentLanguage()<br />
<br />
-- Set aliases for often-used functions to reduce table lookups.<br />
local format = mw.ustring.format<br />
local tinsert = table.insert<br />
local tconcat = table.concat<br />
local trim = mw.text.trim<br />
<br />
--------------------------------------------------------------------------------<br />
-- Helper functions<br />
--------------------------------------------------------------------------------<br />
<br />
local function getTitleObject(page, ...)<br />
if type(page) == 'string' then<br />
-- Get the title object, passing the function through pcall <br />
-- in case we are over the expensive function count limit.<br />
local success, title = pcall(mw.title.new, page, ...)<br />
if success then<br />
return title<br />
end<br />
end<br />
end<br />
<br />
local function union(t1, t2)<br />
-- Returns the union of two arrays.<br />
local vals = {}<br />
for i, v in ipairs(t1) do<br />
vals[v] = true<br />
end<br />
for i, v in ipairs(t2) do<br />
vals[v] = true<br />
end<br />
local ret = {}<br />
for k in pairs(vals) do<br />
tinsert(ret, k)<br />
end<br />
table.sort(ret)<br />
return ret<br />
end<br />
<br />
local function getArgNums(args, prefix)<br />
local nums = {}<br />
for k, v in pairs(args) do<br />
local num = mw.ustring.match(tostring(k), '^' .. prefix .. '([1-9]%d*)$')<br />
if num then<br />
tinsert(nums, tonumber(num))<br />
end<br />
end<br />
table.sort(nums)<br />
return nums<br />
end<br />
<br />
--------------------------------------------------------------------------------<br />
-- Box class definition<br />
--------------------------------------------------------------------------------<br />
<br />
local box = {}<br />
box.__index = box<br />
<br />
function box.new()<br />
local obj = {}<br />
setmetatable(obj, box)<br />
return obj<br />
end<br />
<br />
function box.getNamespaceId(ns)<br />
if not ns then return end<br />
if type(ns) == 'string' then<br />
ns = lang:ucfirst(mw.ustring.lower(ns))<br />
if ns == 'Main' then<br />
ns = 0<br />
end<br />
end<br />
local nsTable = mw.site.namespaces[ns]<br />
if nsTable then<br />
return nsTable.id<br />
end<br />
end<br />
<br />
function box.getMboxType(nsid)<br />
-- Gets the mbox type from a namespace number.<br />
if nsid == 0 then<br />
return 'ambox' -- main namespace<br />
elseif nsid == 6 then<br />
return 'imbox' -- file namespace<br />
elseif nsid == 14 then<br />
return 'cmbox' -- category namespace<br />
else<br />
local nsTable = mw.site.namespaces[nsid]<br />
if nsTable and nsTable.isTalk then<br />
return 'tmbox' -- any talk namespace<br />
else<br />
return 'ombox' -- other namespaces or invalid input<br />
end<br />
end<br />
end<br />
<br />
function box:addCat(ns, cat, sort)<br />
if type(cat) ~= 'string' then return end<br />
local nsVals = {'main', 'template', 'all'}<br />
local tname<br />
for i, val in ipairs(nsVals) do<br />
if ns == val then<br />
tname = ns .. 'Cats'<br />
end<br />
end<br />
if not tname then<br />
for i, val in ipairs(nsVals) do<br />
nsVals[i] = format('"%s"', val)<br />
end<br />
error('invalid ns parameter passed to box:addCat; valid values are ' .. mw.text.listToText(nsVals, nil, ' or '))<br />
end<br />
self[tname] = self[tname] or {}<br />
if type(sort) == 'string' then<br />
tinsert(self[tname], format('[[Category:%s|%s]]', cat, sort))<br />
else<br />
tinsert(self[tname], format('[[Category:%s]]', cat))<br />
end<br />
end<br />
<br />
function box:addClass(class)<br />
if type(class) ~= 'string' then return end<br />
self.classes = self.classes or {}<br />
tinsert(self.classes, class)<br />
end<br />
<br />
function box:setTitle(args)<br />
-- Get the title object and the namespace.<br />
self.pageTitle = getTitleObject(args.page ~= '' and args.page)<br />
self.title = self.pageTitle or mw.title.getCurrentTitle()<br />
self.demospace = args.demospace ~= '' and args.demospace or nil<br />
self.nsid = box.getNamespaceId(self.demospace) or self.title.namespace<br />
end<br />
<br />
function box:getConfig(boxType)<br />
-- Get the box config data from the data page.<br />
if boxType == 'mbox' then<br />
boxType = box.getMboxType(self.nsid)<br />
end<br />
local cfg = cfgTables[boxType]<br />
if not cfg then<br />
local boxTypes = {}<br />
for k, v in pairs(dataTables) do<br />
tinsert(boxTypes, format('"%s"', k))<br />
end<br />
tinsert(boxTypes, '"mbox"')<br />
error(format('invalid message box type "%s"; valid types are %s', tostring(boxType), mw.text.listToText(boxTypes)), 2)<br />
end<br />
return cfg<br />
end<br />
<br />
function box:removeBlankArgs(cfg, args)<br />
-- Only allow blank arguments for the parameter names listed in cfg.allowBlankParams.<br />
local newArgs = {}<br />
for k, v in pairs(args) do<br />
if v ~= '' then<br />
newArgs[k] = v<br />
end<br />
end<br />
for i, param in ipairs(cfg.allowBlankParams or {}) do<br />
newArgs[param] = args[param]<br />
end<br />
return newArgs<br />
end<br />
<br />
function box:setBoxParameters(cfg, args)<br />
-- Get type data.<br />
self.type = args.type<br />
local typeData = cfg.types[self.type]<br />
self.invalidTypeError = cfg.showInvalidTypeError and self.type and not typeData and true or false<br />
typeData = typeData or cfg.types[cfg.default]<br />
self.typeClass = typeData.class<br />
self.typeImage = typeData.image<br />
<br />
-- Find if the box has been wrongly substituted.<br />
if cfg.substCheck and args.subst == 'SUBST' then<br />
self.isSubstituted = true<br />
end<br />
<br />
-- Find whether we are using a small message box.<br />
if cfg.allowSmall and (<br />
cfg.smallParam and args.small == cfg.smallParam<br />
or not cfg.smallParam and yesno(args.small)<br />
)<br />
then<br />
self.isSmall = true<br />
else<br />
self.isSmall = false<br />
end<br />
<br />
-- Add attributes, classes and styles.<br />
if cfg.allowId then<br />
self.id = args.id<br />
end<br />
self:addClass(cfg.usePlainlinksParam and yesno(args.plainlinks or true) and 'plainlinks')<br />
for _, class in ipairs(cfg.classes or {}) do<br />
self:addClass(class)<br />
end<br />
if self.isSmall then<br />
self:addClass(cfg.smallClass or 'mbox-small')<br />
end<br />
if yesno(args.hidden) then<br />
self:addClass('infobox editsection')<br />
end<br />
self:addClass(self.typeClass)<br />
self:addClass(args.class)<br />
self.style = args.style<br />
<br />
-- Set text style.<br />
self.textstyle = args.textstyle<br />
<br />
-- Find if we are on the template page or not. This functionality is only used if useCollapsibleTextFields is set,<br />
-- or if both cfg.templateCategory and cfg.templateCategoryRequireName are set.<br />
self.useCollapsibleTextFields = cfg.useCollapsibleTextFields<br />
if self.useCollapsibleTextFields or cfg.templateCategory and cfg.templateCategoryRequireName then<br />
self.name = args.name<br />
if self.name then<br />
local templateName = mw.ustring.match(self.name, '^[tT][eE][mM][pP][lL][aA][tT][eE][%s_]*:[%s_]*(.*)$') or self.name<br />
templateName = 'Template:' .. templateName<br />
self.templateTitle = getTitleObject(templateName)<br />
end<br />
self.isTemplatePage = self.templateTitle and mw.title.equals(self.title, self.templateTitle) or false<br />
end<br />
<br />
-- Process data for collapsible text fields. At the moment these are only used in {{ambox}}.<br />
if self.useCollapsibleTextFields then<br />
-- Get the self.issue value.<br />
if self.isSmall and args.smalltext then<br />
self.issue = args.smalltext<br />
else<br />
local sect<br />
if args.sect == '' then<br />
sect = 'This ' .. (cfg.sectionDefault or 'page')<br />
elseif type(args.sect) == 'string' then<br />
sect = 'This ' .. args.sect<br />
end<br />
local issue = args.issue<br />
issue = type(issue) == 'string' and issue ~= '' and issue or nil<br />
local text = args.text<br />
text = type(text) == 'string' and text or nil<br />
local issues = {}<br />
tinsert(issues, sect)<br />
tinsert(issues, issue)<br />
tinsert(issues, text)<br />
self.issue = tconcat(issues, ' ')<br />
end<br />
<br />
-- Get the self.talk value.<br />
local talk = args.talk<br />
if talk == '' -- Show talk links on the template page or template subpages if the talk parameter is blank.<br />
and self.templateTitle <br />
and (mw.title.equals(self.templateTitle, self.title) or self.title:isSubpageOf(self.templateTitle))<br />
then<br />
talk = '#'<br />
elseif talk == '' then<br />
talk = nil<br />
end<br />
if talk then<br />
-- If the talk value is a talk page, make a link to that page. Else assume that it's a section heading,<br />
-- and make a link to the talk page of the current page with that section heading.<br />
local talkTitle = getTitleObject(talk)<br />
local talkArgIsTalkPage = true<br />
if not talkTitle or not talkTitle.isTalkPage then<br />
talkArgIsTalkPage = false<br />
talkTitle = getTitleObject(self.title.text, mw.site.namespaces[self.title.namespace].talk.id)<br />
end<br />
if talkTitle and talkTitle.exists then<br />
local talkText = 'Relevant discussion may be found on'<br />
if talkArgIsTalkPage then<br />
talkText = format('%s [[%s|%s]].', talkText, talk, talkTitle.prefixedText)<br />
else<br />
talkText = format('%s the [[%s#%s|talk page]].', talkText, talkTitle.prefixedText, talk)<br />
end<br />
self.talk = talkText<br />
end<br />
end<br />
<br />
-- Get other values.<br />
self.fix = args.fix ~= '' and args.fix or nil<br />
local date<br />
if args.date and args.date ~= '' then<br />
date = args.date<br />
elseif args.date == '' and self.isTemplatePage then<br />
date = lang:formatDate('F Y')<br />
end<br />
if date then<br />
self.date = format(" <small>''(%s)''</small>", date)<br />
end<br />
self.info = args.info<br />
end<br />
<br />
-- Set the non-collapsible text field. At the moment this is used by all box types other than ambox,<br />
-- and also by ambox when small=yes.<br />
if self.isSmall then<br />
self.text = args.smalltext or args.text<br />
else<br />
self.text = args.text<br />
end<br />
<br />
-- Set the below row.<br />
self.below = cfg.below and args.below<br />
<br />
-- General image settings.<br />
self.imageCellDiv = not self.isSmall and cfg.imageCellDiv and true or false<br />
self.imageEmptyCell = cfg.imageEmptyCell<br />
if cfg.imageEmptyCellStyle then<br />
self.imageEmptyCellStyle = 'border:none;padding:0px;width:1px'<br />
end<br />
<br />
-- Left image settings.<br />
local imageLeft = self.isSmall and args.smallimage or args.image<br />
if cfg.imageCheckBlank and imageLeft ~= 'blank' and imageLeft ~= 'none'<br />
or not cfg.imageCheckBlank and imageLeft ~= 'none'<br />
then<br />
self.imageLeft = imageLeft<br />
if not imageLeft then<br />
local imageSize = self.isSmall and (cfg.imageSmallSize or '30x30px') or '40x40px'<br />
self.imageLeft = format('[[File:%s|%s|link=|alt=]]', self.typeImage or 'Imbox notice.png', imageSize)<br />
end<br />
end<br />
<br />
-- Right image settings.<br />
local imageRight = self.isSmall and args.smallimageright or args.imageright<br />
if not (cfg.imageRightNone and imageRight == 'none') then<br />
self.imageRight = imageRight<br />
end<br />
<br />
-- Add mainspace categories. At the moment these are only used in {{ambox}}.<br />
if cfg.allowMainspaceCategories then<br />
if args.cat then<br />
args.cat1 = args.cat<br />
end<br />
self.catNums = getArgNums(args, 'cat')<br />
if args.category then<br />
args.category1 = args.category<br />
end<br />
self.categoryNums = getArgNums(args, 'category')<br />
if args.all then<br />
args.all1 = args.all<br />
end<br />
self.allNums = getArgNums(args, 'all')<br />
self.categoryParamNums = union(self.catNums, self.categoryNums)<br />
self.categoryParamNums = union(self.categoryParamNums, self.allNums)<br />
-- The following is roughly equivalent to the old {{Ambox/category}}.<br />
local date = args.date<br />
date = type(date) == 'string' and date<br />
local preposition = 'from'<br />
for _, num in ipairs(self.categoryParamNums) do<br />
local mainCat = args['cat' .. tostring(num)] or args['category' .. tostring(num)]<br />
local allCat = args['all' .. tostring(num)]<br />
mainCat = type(mainCat) == 'string' and mainCat<br />
allCat = type(allCat) == 'string' and allCat<br />
if mainCat and date and date ~= '' then<br />
local catTitle = format('%s %s %s', mainCat, preposition, date)<br />
self:addCat('main', catTitle)<br />
catTitle = getTitleObject('Category:' .. catTitle)<br />
if not catTitle or not catTitle.exists then<br />
self:addCat('main', 'Articles with invalid date parameter in template')<br />
end<br />
elseif mainCat and (not date or date == '') then<br />
self:addCat('main', mainCat)<br />
end<br />
if allCat then<br />
self:addCat('main', allCat)<br />
end<br />
end<br />
end<br />
<br />
-- Add template-namespace categories.<br />
if cfg.templateCategory then<br />
if cfg.templateCategoryRequireName then<br />
if self.isTemplatePage then<br />
self:addCat('template', cfg.templateCategory)<br />
end<br />
elseif not self.title.isSubpage then<br />
self:addCat('template', cfg.templateCategory)<br />
end<br />
end<br />
<br />
-- Add template error category.<br />
if cfg.templateErrorCategory then<br />
local templateErrorCategory = cfg.templateErrorCategory<br />
local templateCat, templateSort<br />
if not self.name and not self.title.isSubpage then<br />
templateCat = templateErrorCategory<br />
elseif self.isTemplatePage then<br />
local paramsToCheck = cfg.templateErrorParamsToCheck or {}<br />
local count = 0<br />
for i, param in ipairs(paramsToCheck) do<br />
if not args[param] then<br />
count = count + 1<br />
end<br />
end<br />
if count > 0 then<br />
templateCat = templateErrorCategory<br />
templateSort = tostring(count)<br />
end<br />
if self.categoryNums and #self.categoryNums > 0 then<br />
templateCat = templateErrorCategory<br />
templateSort = 'C'<br />
end<br />
end<br />
self:addCat('template', templateCat, templateSort)<br />
end<br />
<br />
-- Categories for all namespaces.<br />
if self.invalidTypeError then<br />
local allSort = (self.nsid == 0 and 'Main:' or '') .. self.title.prefixedText<br />
self:addCat('all', 'Wikipedia message box parameter needs fixing', allSort)<br />
end<br />
if self.isSubstituted then<br />
self:addCat('all', 'Pages with incorrectly substituted templates')<br />
end<br />
<br />
-- Convert category tables to strings and pass them through [[Module:Category handler]].<br />
self.categories = categoryHandler{<br />
main = tconcat(self.mainCats or {}),<br />
template = tconcat(self.templateCats or {}),<br />
all = tconcat(self.allCats or {}),<br />
nocat = args.nocat,<br />
demospace = self.demospace,<br />
page = self.pageTitle and self.pageTitle.prefixedText or nil<br />
}<br />
end<br />
<br />
function box:export()<br />
local root = htmlBuilder.create()<br />
<br />
-- Add the subst check error.<br />
if self.isSubstituted and self.name then<br />
root<br />
.tag('b')<br />
.addClass('error')<br />
.wikitext(format(<br />
'Template <code>%s[[Template:%s|%s]]%s</code> has been incorrectly substituted.',<br />
mw.text.nowiki('{{'), self.name, self.name, mw.text.nowiki('}}')<br />
))<br />
end<br />
<br />
-- Create the box table.<br />
local boxTable = root.tag('table')<br />
boxTable<br />
.attr('id', self.id)<br />
for i, class in ipairs(self.classes or {}) do<br />
boxTable<br />
.addClass(class)<br />
end<br />
boxTable<br />
.cssText(self.style)<br />
.attr('role', 'presentation')<br />
<br />
-- Add the left-hand image.<br />
local row = boxTable.tag('tr')<br />
if self.imageLeft then<br />
local imageLeftCell = row.tag('td').addClass('mbox-image')<br />
if self.imageCellDiv then<br />
-- If we are using a div, redefine imageLeftCell so that the image is inside it.<br />
-- Divs use style="width: 52px;", which limits the image width to 52px. If any<br />
-- images in a div are wider than that, they may overlap with the text or cause<br />
-- other display problems.<br />
imageLeftCell = imageLeftCell.tag('div').css('width', '52px') <br />
end<br />
imageLeftCell<br />
.wikitext(self.imageLeft)<br />
elseif self.imageEmptyCell then<br />
-- Some message boxes define an empty cell if no image is specified, and some don't.<br />
-- The old template code in templates where empty cells are specified gives the following hint:<br />
-- "No image. Cell with some width or padding necessary for text cell to have 100% width."<br />
row.tag('td')<br />
.addClass('mbox-empty-cell') <br />
.cssText(self.imageEmptyCellStyle)<br />
end<br />
<br />
-- Add the text.<br />
local textCell = row.tag('td').addClass('mbox-text')<br />
if self.useCollapsibleTextFields then<br />
-- The message box uses advanced text parameters that allow things to be collapsible. At the<br />
-- moment, only ambox uses this.<br />
textCell<br />
.cssText(self.textstyle)<br />
local textCellSpan = textCell.tag('span')<br />
textCellSpan<br />
.addClass('mbox-text-span')<br />
.wikitext(self.issue)<br />
if not self.isSmall then<br />
textCellSpan<br />
.tag('span')<br />
.addClass('hide-when-compact')<br />
.wikitext(self.talk and ' ' .. self.talk)<br />
.wikitext(self.fix and ' ' .. self.fix)<br />
end<br />
textCellSpan<br />
.wikitext(self.date and ' ' .. self.date)<br />
if not self.isSmall then<br />
textCellSpan<br />
.tag('span')<br />
.addClass('hide-when-compact')<br />
.wikitext(self.info and ' ' .. self.info)<br />
end<br />
else<br />
-- Default text formatting - anything goes.<br />
textCell<br />
.cssText(self.textstyle)<br />
.wikitext(self.text)<br />
end<br />
<br />
-- Add the right-hand image.<br />
if self.imageRight then<br />
local imageRightCell = row.tag('td').addClass('mbox-imageright')<br />
if self.imageCellDiv then<br />
imageRightCell = imageRightCell.tag('div').css('width', '52px') -- If we are using a div, redefine imageRightCell so that the image is inside it.<br />
end<br />
imageRightCell<br />
.wikitext(self.imageRight)<br />
end<br />
<br />
-- Add the below row.<br />
if self.below then<br />
boxTable.tag('tr')<br />
.tag('td')<br />
.attr('colspan', self.imageRight and '3' or '2')<br />
.addClass('mbox-text')<br />
.cssText(self.textstyle)<br />
.wikitext(self.below)<br />
end<br />
<br />
-- Add error message for invalid type parameters.<br />
if self.invalidTypeError then<br />
root<br />
.tag('div')<br />
.css('text-align', 'center')<br />
.wikitext(format('This message box is using an invalid "type=%s" parameter and needs fixing.', self.type or ''))<br />
end<br />
<br />
-- Add categories.<br />
root<br />
.wikitext(self.categories)<br />
<br />
return tostring(root)<br />
end<br />
<br />
local function main(boxType, args)<br />
local outputBox = box.new()<br />
outputBox:setTitle(args)<br />
local cfg = outputBox:getConfig(boxType)<br />
args = outputBox:removeBlankArgs(cfg, args)<br />
outputBox:setBoxParameters(cfg, args)<br />
return outputBox:export()<br />
end<br />
<br />
local function makeWrapper(boxType)<br />
return function (frame)<br />
local args = getArgs(frame, {trim = false, removeBlanks = false})<br />
return main(boxType, args)<br />
end<br />
end<br />
<br />
local p = {<br />
main = main,<br />
mbox = makeWrapper('mbox')<br />
}<br />
<br />
for boxType in pairs(cfgTables) do<br />
p[boxType] = makeWrapper(boxType)<br />
end<br />
<br />
return p</div>Mr. Stradivarius