https://ru.wiki-protopia.org/index.php?title=%D0%9C%D0%BE%D0%B4%D1%83%D0%BB%D1%8C:Message_box&feed=atom&action=historyМодуль:Message box - История изменений2024-03-29T13:23:55ZИстория изменений этой страницы в викиMediaWiki 1.29.1https://ru.wiki-protopia.org/index.php?title=%D0%9C%D0%BE%D0%B4%D1%83%D0%BB%D1%8C:Message_box&diff=1683&oldid=prevPrincenoire: Новая страница: «-- This is a meta-module for producing message box templates, including -- {{mbox}}, {{ambox}}, {{imbox}}, {{tmbox}}, {{ombox}}, {{cmbox}} and {{fmbox}}. -- Load…»2017-08-30T09:55:05Z<p>Новая страница: «-- This is a meta-module for producing message box templates, including -- {{mbox}}, {{ambox}}, {{imbox}}, {{tmbox}}, {{ombox}}, {{cmbox}} and {{fmbox}}. -- Load…»</p>
<p><b>Новая страница</b></p><div>-- This is a meta-module for producing message box templates, including<br />
-- {{mbox}}, {{ambox}}, {{imbox}}, {{tmbox}}, {{ombox}}, {{cmbox}} and {{fmbox}}.<br />
<br />
-- Load necessary modules.<br />
require('Module:No globals')<br />
local getArgs<br />
local categoryHandler = require('Module:Category handler')._main<br />
local yesno = require('Module:Yesno')<br />
<br />
-- Get a language object for formatDate and ucfirst.<br />
local lang = mw.language.getContentLanguage()<br />
<br />
-- Define constants<br />
local CONFIG_MODULE = 'Module:Message box/configuration'<br />
<br />
--------------------------------------------------------------------------------<br />
-- Helper functions<br />
--------------------------------------------------------------------------------<br />
<br />
local function getTitleObject(...)<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, ...)<br />
if success then<br />
return title<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 />
table.insert(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 />
table.insert(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 MessageBox = {}<br />
MessageBox.__index = MessageBox<br />
<br />
function MessageBox.new(boxType, args, cfg)<br />
args = args or {}<br />
local obj = {}<br />
<br />
-- Set the title object and the namespace.<br />
obj.title = getTitleObject(args.page) or mw.title.getCurrentTitle()<br />
<br />
-- Set the config for our box type.<br />
obj.cfg = cfg[boxType]<br />
if not obj.cfg then<br />
local ns = obj.title.namespace<br />
-- boxType is "mbox" or invalid input<br />
if ns == 0 then<br />
obj.cfg = cfg.ambox -- main namespace<br />
elseif ns == 6 then<br />
obj.cfg = cfg.imbox -- file namespace<br />
elseif ns == 14 then<br />
obj.cfg = cfg.cmbox -- category namespace<br />
else<br />
local nsTable = mw.site.namespaces[ns]<br />
if nsTable and nsTable.isTalk then<br />
obj.cfg = cfg.tmbox -- any talk namespace<br />
else<br />
obj.cfg = cfg.ombox -- other namespaces or invalid input<br />
end<br />
end<br />
end<br />
<br />
-- Set the arguments, and remove all blank arguments except for the ones<br />
-- listed in cfg.allowBlankParams.<br />
do<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(obj.cfg.allowBlankParams or {}) do<br />
newArgs[param] = args[param]<br />
end<br />
obj.args = newArgs<br />
end<br />
<br />
-- Define internal data structure.<br />
obj.categories = {}<br />
obj.classes = {}<br />
<br />
return setmetatable(obj, MessageBox)<br />
end<br />
<br />
function MessageBox:addCat(ns, cat, sort)<br />
if not cat then<br />
return nil<br />
end<br />
if sort then<br />
cat = string.format('[[Category:%s|%s]]', cat, sort)<br />
else<br />
cat = string.format('[[Category:%s]]', cat)<br />
end<br />
self.categories[ns] = self.categories[ns] or {}<br />
table.insert(self.categories[ns], cat)<br />
end<br />
<br />
function MessageBox:addClass(class)<br />
if not class then<br />
return nil<br />
end<br />
table.insert(self.classes, class)<br />
end<br />
<br />
function MessageBox:setParameters()<br />
local args = self.args<br />
local cfg = self.cfg<br />
<br />
-- Get type data.<br />
self.type = args.type<br />
local typeData = cfg.types[self.type]<br />
self.invalidTypeError = cfg.showInvalidTypeError<br />
and self.type<br />
and not typeData<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 />
self.isSubstituted = cfg.substCheck and args.subst == 'SUBST'<br />
<br />
-- Find whether we are using a small message box.<br />
self.isSmall = cfg.allowSmall and (<br />
cfg.smallParam and args.small == cfg.smallParam<br />
or not cfg.smallParam and yesno(args.small)<br />
)<br />
<br />
-- Add attributes, classes and styles.<br />
self.id = args.id<br />
if yesno(args.plainlinks) ~= false then<br />
self:addClass('plainlinks')<br />
end<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 />
self:addClass(self.typeClass)<br />
self:addClass(args.class)<br />
self.style = args.style<br />
self.attrs = args.attrs<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<br />
-- used if useCollapsibleTextFields is set, or if both cfg.templateCategory<br />
-- and cfg.templateCategoryRequireName are set.<br />
self.useCollapsibleTextFields = cfg.useCollapsibleTextFields<br />
if self.useCollapsibleTextFields<br />
or cfg.templateCategory<br />
and cfg.templateCategoryRequireName<br />
then<br />
self.name = args.name<br />
if self.name then<br />
local templateName = mw.ustring.match(<br />
self.name,<br />
'^[tT][eE][mM][pP][lL][aA][tT][eE][%s_]*:[%s_]*(.*)$'<br />
) or self.name<br />
templateName = 'Template:' .. templateName<br />
self.templateTitle = getTitleObject(templateName)<br />
end<br />
self.isTemplatePage = self.templateTitle<br />
and mw.title.equals(self.title, self.templateTitle)<br />
end<br />
<br />
-- Process data for collapsible text fields. At the moment these are only<br />
-- 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 />
table.insert(issues, sect)<br />
table.insert(issues, issue)<br />
table.insert(issues, text)<br />
self.issue = table.concat(issues, ' ')<br />
end<br />
<br />
-- Get the self.talk value.<br />
local talk = args.talk<br />
-- Show talk links on the template page or template subpages if the talk<br />
-- parameter is blank.<br />
if talk == ''<br />
and self.templateTitle<br />
and (<br />
mw.title.equals(self.templateTitle, self.title)<br />
or self.title:isSubpageOf(self.templateTitle)<br />
)<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<br />
-- assume that it's a section heading, and make a link to the talk<br />
-- 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(<br />
self.title.text,<br />
mw.site.namespaces[self.title.namespace].talk.id<br />
)<br />
end<br />
if talkTitle and talkTitle.exists then<br />
local talkText = 'Relevant discussion may be found on'<br />
if talkArgIsTalkPage then<br />
talkText = string.format(<br />
'%s [[%s|%s]].',<br />
talkText,<br />
talk,<br />
talkTitle.prefixedText<br />
)<br />
else<br />
talkText = string.format(<br />
'%s the [[%s#%s|talk page]].',<br />
talkText,<br />
talkTitle.prefixedText,<br />
talk<br />
)<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 = string.format(" <small>''(%s)''</small>", date)<br />
end<br />
self.info = args.info<br />
if yesno(args.removalnotice) then<br />
self.removalNotice = cfg.removalNotice<br />
end<br />
end<br />
<br />
-- Set the non-collapsible text field. At the moment this is used by all box<br />
-- types other than ambox, 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<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<br />
and (cfg.imageSmallSize or '30x30px')<br />
or '40x40px'<br />
self.imageLeft = string.format('[[File:%s|%s|link=|alt=]]', self.typeImage<br />
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 />
end<br />
<br />
function MessageBox:setMainspaceCategories()<br />
local args = self.args<br />
local cfg = self.cfg<br />
<br />
if not cfg.allowMainspaceCategories then<br />
return nil<br />
end<br />
<br />
local nums = {}<br />
for _, prefix in ipairs{'cat', 'category', 'all'} do<br />
args[prefix .. '1'] = args[prefix]<br />
nums = union(nums, getArgNums(args, prefix))<br />
end<br />
<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(nums) do<br />
local mainCat = args['cat' .. tostring(num)]<br />
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 = string.format('%s %s %s', mainCat, preposition, date)<br />
self:addCat(0, catTitle)<br />
catTitle = getTitleObject('Category:' .. catTitle)<br />
if not catTitle or not catTitle.exists then<br />
self:addCat(0, 'Articles with invalid date parameter in template')<br />
end<br />
elseif mainCat and (not date or date == '') then<br />
self:addCat(0, mainCat)<br />
end<br />
if allCat then<br />
self:addCat(0, allCat)<br />
end<br />
end<br />
end<br />
<br />
function MessageBox:setTemplateCategories()<br />
local args = self.args<br />
local cfg = self.cfg<br />
<br />
-- Add template categories.<br />
if cfg.templateCategory then<br />
if cfg.templateCategoryRequireName then<br />
if self.isTemplatePage then<br />
self:addCat(10, cfg.templateCategory)<br />
end<br />
elseif not self.title.isSubpage then<br />
self:addCat(10, cfg.templateCategory)<br />
end<br />
end<br />
<br />
-- Add template error categories.<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(10, templateCat, templateSort)<br />
end<br />
end<br />
<br />
function MessageBox:setAllNamespaceCategories()<br />
-- Set categories for all namespaces.<br />
if self.invalidTypeError then<br />
local allSort = (self.title.namespace == 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 />
end<br />
<br />
function MessageBox:setCategories()<br />
if self.title.namespace == 0 then<br />
self:setMainspaceCategories()<br />
elseif self.title.namespace == 10 then<br />
self:setTemplateCategories()<br />
end<br />
self:setAllNamespaceCategories()<br />
end<br />
<br />
function MessageBox:renderCategories()<br />
-- Convert category tables to strings and pass them through<br />
-- [[Module:Category handler]].<br />
return categoryHandler{<br />
main = table.concat(self.categories[0] or {}),<br />
template = table.concat(self.categories[10] or {}),<br />
all = table.concat(self.categories.all or {}),<br />
nocat = self.args.nocat,<br />
page = self.args.page<br />
}<br />
end<br />
<br />
function MessageBox:export()<br />
local root = mw.html.create()<br />
<br />
-- Add the subst check error.<br />
if self.isSubstituted and self.name then<br />
root:tag('b')<br />
:addClass('error')<br />
:wikitext(string.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:attr('id', self.id or nil)<br />
for i, class in ipairs(self.classes or {}) do<br />
boxTable:addClass(class or nil)<br />
end<br />
boxTable<br />
:cssText(self.style or nil)<br />
:attr('role', 'presentation')<br />
<br />
if self.attrs then<br />
boxTable:attr(self.attrs)<br />
end<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<br />
-- is inside it. Divs use style="width: 52px;", which limits the<br />
-- image width to 52px. If any images in a div are wider than that,<br />
-- they may overlap with the text or cause other display problems.<br />
imageLeftCell = imageLeftCell:tag('div'):css('width', '52px')<br />
end<br />
imageLeftCell:wikitext(self.imageLeft or nil)<br />
elseif self.imageEmptyCell then<br />
-- Some message boxes define an empty cell if no image is specified, and<br />
-- some don't. The old template code in templates where empty cells are<br />
-- specified gives the following hint: "No image. Cell with some width<br />
-- or padding necessary for text cell to have 100% width."<br />
row:tag('td')<br />
:addClass('mbox-empty-cell')<br />
:cssText(self.imageEmptyCellStyle or nil)<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<br />
-- collapsible. At the moment, only ambox uses this.<br />
textCell:cssText(self.textstyle or nil)<br />
local textCellSpan = textCell:tag('span')<br />
textCellSpan<br />
:addClass('mbox-text-span')<br />
:wikitext(self.issue or nil)<br />
if (self.talk or self.fix) and not self.isSmall then<br />
textCellSpan:tag('span')<br />
:addClass('hide-when-compact')<br />
:wikitext(self.talk and (' ' .. self.talk) or nil)<br />
:wikitext(self.fix and (' ' .. self.fix) or nil)<br />
end<br />
textCellSpan:wikitext(self.date and (' ' .. self.date) or nil)<br />
if self.info and not self.isSmall then<br />
textCellSpan<br />
:tag('span')<br />
:addClass('hide-when-compact')<br />
:wikitext(self.info and (' ' .. self.info) or nil)<br />
end<br />
if self.removalNotice then<br />
textCellSpan:tag('small')<br />
:addClass('hide-when-compact')<br />
:tag('i')<br />
:wikitext(string.format(" (%s)", self.removalNotice))<br />
end<br />
else<br />
-- Default text formatting - anything goes.<br />
textCell<br />
:cssText(self.textstyle or nil)<br />
:wikitext(self.text or nil)<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 />
-- If we are using a div, redefine imageRightCell so that the image<br />
-- is inside it.<br />
imageRightCell = imageRightCell:tag('div'):css('width', '52px')<br />
end<br />
imageRightCell<br />
:wikitext(self.imageRight or nil)<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 or nil)<br />
:wikitext(self.below or nil)<br />
end<br />
<br />
-- Add error message for invalid type parameters.<br />
if self.invalidTypeError then<br />
root:tag('div')<br />
:css('text-align', 'center')<br />
:wikitext(string.format(<br />
'This message box is using an invalid "type=%s" parameter and needs fixing.',<br />
self.type or ''<br />
))<br />
end<br />
<br />
-- Add categories.<br />
root:wikitext(self:renderCategories() or nil)<br />
<br />
return tostring(root)<br />
end<br />
<br />
--------------------------------------------------------------------------------<br />
-- Exports<br />
--------------------------------------------------------------------------------<br />
<br />
local p, mt = {}, {}<br />
<br />
function p._exportClasses()<br />
-- For testing.<br />
return {<br />
MessageBox = MessageBox<br />
}<br />
end<br />
<br />
function p.main(boxType, args, cfgTables)<br />
local box = MessageBox.new(boxType, args, cfgTables or mw.loadData(CONFIG_MODULE))<br />
box:setParameters()<br />
box:setCategories()<br />
return box:export()<br />
end<br />
<br />
function mt.__index(t, k)<br />
return function (frame)<br />
if not getArgs then<br />
getArgs = require('Module:Arguments').getArgs<br />
end<br />
return t.main(k, getArgs(frame, {trim = false, removeBlanks = false}))<br />
end<br />
end<br />
<br />
return setmetatable(p, mt)</div>Princenoire