Diferencia entre revisiones de «Módulo:Message box»
wikitves>CaiusSPQR m um pouco de cleanup |
wikitves>Izno per tper |
||
Línea 3: | Línea 3: | ||
-- Load necessary modules. | -- Load necessary modules. | ||
require(' | require('Module:No globals') | ||
local getArgs | local getArgs | ||
local yesno = require(' | local yesno = require('Module:Yesno') | ||
-- Get a language object for formatDate and ucfirst. | -- Get a language object for formatDate and ucfirst. | ||
Línea 11: | Línea 11: | ||
-- Define constants | -- Define constants | ||
local CONFIG_MODULE = ' | local CONFIG_MODULE = 'Module:Message box/configuration' | ||
local DEMOSPACES = { | local DEMOSPACES = {talk = 'tmbox', image = 'imbox', file = 'imbox', category = 'cmbox', article = 'ambox', main = 'ambox'} | ||
} | |||
-------------------------------------------------------------------------------- | -------------------------------------------------------------------------------- | ||
Línea 81: | Línea 68: | ||
-- Set the title object and the namespace. | -- Set the title object and the namespace. | ||
obj.title = getTitleObject(args.page | obj.title = getTitleObject(args.page) or mw.title.getCurrentTitle() | ||
-- Set the config for our box type. | -- Set the config for our box type. | ||
Línea 94: | Línea 81: | ||
-- use template from DEMOSPACES | -- use template from DEMOSPACES | ||
obj.cfg = cfg[DEMOSPACES[demospace]] | obj.cfg = cfg[DEMOSPACES[demospace]] | ||
elseif string.find( demospace, 'talk | elseif string.find( demospace, 'talk' ) then | ||
-- demo as a talk page | -- demo as a talk page | ||
obj.cfg = cfg.tmbox | obj.cfg = cfg.tmbox | ||
Línea 146: | Línea 133: | ||
end | end | ||
if sort then | if sort then | ||
cat = string.format('[[ | cat = string.format('[[Category:%s|%s]]', cat, sort) | ||
else | else | ||
cat = string.format('[[ | cat = string.format('[[Category:%s]]', cat) | ||
end | end | ||
self.hasCategories = true | self.hasCategories = true | ||
Línea 167: | Línea 154: | ||
-- Get type data. | -- Get type data. | ||
self.type = args.type | self.type = args.type | ||
local typeData = cfg.types[self.type] | local typeData = cfg.types[self.type] | ||
self.invalidTypeError = cfg.showInvalidTypeError | self.invalidTypeError = cfg.showInvalidTypeError | ||
Línea 173: | Línea 160: | ||
and not typeData | and not typeData | ||
typeData = typeData or cfg.types[cfg.default] | typeData = typeData or cfg.types[cfg.default] | ||
self.typeClass = typeData.class | self.typeClass = typeData.class | ||
self.typeImage = typeData.image | self.typeImage = typeData.image | ||
-- Find if the box has been wrongly substituted. | -- Find if the box has been wrongly substituted. | ||
Línea 182: | Línea 169: | ||
self.isSmall = cfg.allowSmall and ( | self.isSmall = cfg.allowSmall and ( | ||
cfg.smallParam and args.small == cfg.smallParam | cfg.smallParam and args.small == cfg.smallParam | ||
or not cfg.smallParam and yesno(args.small) | or not cfg.smallParam and yesno(args.small) | ||
) | ) | ||
-- Add attributes, classes and styles. | -- Add attributes, classes and styles. | ||
self.id = args.id | self.id = args.id | ||
self.name = args.name | self.name = args.name | ||
if self.name then | if self.name then | ||
self:addClass('box-' .. string.gsub(self.name,' ','_')) | self:addClass('box-' .. string.gsub(self.name,' ','_')) | ||
Línea 208: | Línea 188: | ||
end | end | ||
self:addClass(self.typeClass) | self:addClass(self.typeClass) | ||
self:addClass(args.class) | |||
self.style = args.style | |||
self.attrs = args.attrs | |||
-- Set text style. | -- Set text style. | ||
self.textstyle = args.textstyle | |||
-- Find if we are on the template page or not. This functionality is only | -- Find if we are on the template page or not. This functionality is only | ||
-- used if useCollapsibleTextFields is set, or if both cfg.templateCategory | -- used if useCollapsibleTextFields is set, or if both cfg.templateCategory | ||
Línea 240: | Línea 206: | ||
local templateName = mw.ustring.match( | local templateName = mw.ustring.match( | ||
self.name, | self.name, | ||
'^[ | '^[tT][eE][mM][pP][lL][aA][tT][eE][%s_]*:[%s_]*(.*)$' | ||
) or self.name | ) or self.name | ||
templateName = ' | templateName = 'Template:' .. templateName | ||
self.templateTitle = getTitleObject(templateName) | self.templateTitle = getTitleObject(templateName) | ||
end | end | ||
Línea 253: | Línea 219: | ||
if self.useCollapsibleTextFields then | if self.useCollapsibleTextFields then | ||
-- Get the self.issue value. | -- Get the self.issue value. | ||
if self.isSmall and args.smalltext then | |||
if self.isSmall and | self.issue = args.smalltext | ||
self.issue = | |||
else | else | ||
local sect | local sect | ||
if args.sect == '' then | |||
sect = 'This ' .. (cfg.sectionDefault or 'page') | |||
elseif type(args.sect) == 'string' then | |||
sect = 'This ' .. args.sect | |||
elseif type( | |||
sect = | |||
end | end | ||
local issue = args.issue | local issue = args.issue | ||
issue = type(issue) == 'string' and issue ~= '' and issue or nil | issue = type(issue) == 'string' and issue ~= '' and issue or nil | ||
local text = args.text | local text = args.text | ||
text = type(text) == 'string' and text or nil | text = type(text) == 'string' and text or nil | ||
local issues = {} | local issues = {} | ||
Línea 280: | Línea 240: | ||
-- Get the self.talk value. | -- Get the self.talk value. | ||
local talk = args.talk | local talk = args.talk | ||
-- Show talk links on the template page or template subpages if the talk | -- Show talk links on the template page or template subpages if the talk | ||
-- parameter is blank. | -- parameter is blank. | ||
Línea 308: | Línea 268: | ||
end | end | ||
if talkTitle and talkTitle.exists then | if talkTitle and talkTitle.exists then | ||
local talkText | |||
if self.isSmall then | |||
local talkLink = talkArgIsTalkPage and talk or (talkTitle.prefixedText .. '#' .. talk) | |||
talkText = string.format('([[%s|talk]])', talkLink) | |||
else | |||
talkText = 'Relevant discussion may be found on' | |||
if talkArgIsTalkPage then | |||
talkText = string.format( | |||
'%s [[%s|%s]].', | |||
talkText, | |||
talk, | |||
talkTitle.prefixedText | |||
) | |||
else | |||
talkText = string.format( | |||
'%s the [[%s#%s|talk page]].', | |||
talkText, | |||
talkTitle.prefixedText, | |||
talk | |||
) | |||
end | |||
end | |||
self.talk = talkText | self.talk = talkText | ||
end | end | ||
Línea 329: | Línea 295: | ||
-- Get other values. | -- Get other values. | ||
self.fix = args.fix ~= '' and args.fix | self.fix = args.fix ~= '' and args.fix or nil | ||
local date | local date | ||
if args.date | if args.date and args.date ~= '' then | ||
date = args.date | |||
elseif args.date == '' and self.isTemplatePage then | |||
date = lang:formatDate('F Y') | |||
end | end | ||
if date then | if date then | ||
self.date = string.format(" < | self.date = string.format(" <span class='date-container'>''(<span class='date'>%s</span>)''</span>", date) | ||
end | end | ||
self.info = args.info | self.info = args.info | ||
if yesno(args.removalnotice | if yesno(args.removalnotice) then | ||
self.removalNotice = cfg.removalNotice | self.removalNotice = cfg.removalNotice | ||
end | end | ||
Línea 356: | Línea 314: | ||
-- types other than ambox, and also by ambox when small=yes. | -- types other than ambox, and also by ambox when small=yes. | ||
if self.isSmall then | if self.isSmall then | ||
self.text = args.smalltext or args.text | self.text = args.smalltext or args.text | ||
else | else | ||
self.text = args.text | self.text = args.text | ||
end | end | ||
-- Set the below row. | -- Set the below row. | ||
self.below = | self.below = cfg.below and args.below | ||
-- General image settings. | -- General image settings. | ||
Línea 368: | Línea 326: | ||
self.imageEmptyCell = cfg.imageEmptyCell | self.imageEmptyCell = cfg.imageEmptyCell | ||
if cfg.imageEmptyCellStyle then | if cfg.imageEmptyCellStyle then | ||
self.imageEmptyCellStyle = 'border: none; padding: | self.imageEmptyCellStyle = 'border:none;padding:0;width:1px' | ||
end | end | ||
-- Left image settings. | -- Left image settings. | ||
local imageLeft = | local imageLeft = self.isSmall and args.smallimage or args.image | ||
if cfg.imageCheckBlank and imageLeft ~= 'blank' and imageLeft ~= 'none' | |||
or not cfg.imageCheckBlank and imageLeft ~= 'none' | |||
if cfg.imageCheckBlank and imageLeft ~= 'blank' and imageLeft ~= 'none | |||
then | then | ||
self.imageLeft = imageLeft | self.imageLeft = imageLeft | ||
Línea 386: | Línea 339: | ||
and (cfg.imageSmallSize or '30x30px') | and (cfg.imageSmallSize or '30x30px') | ||
or '40x40px' | or '40x40px' | ||
self.imageLeft = string.format('[[ | self.imageLeft = string.format('[[File:%s|%s|link=|alt=]]', self.typeImage | ||
or 'Imbox notice.png', imageSize) | or 'Imbox notice.png', imageSize) | ||
end | end | ||
Línea 392: | Línea 345: | ||
-- Right image settings. | -- Right image settings. | ||
local imageRight = | local imageRight = self.isSmall and args.smallimageright or args.imageright | ||
if not (cfg.imageRightNone and imageRight == 'none') then | |||
if not | |||
self.imageRight = imageRight | self.imageRight = imageRight | ||
end | end | ||
Línea 411: | Línea 360: | ||
local nums = {} | local nums = {} | ||
for _, prefix in ipairs{'cat', 'category', 'all | for _, prefix in ipairs{'cat', 'category', 'all'} do | ||
args[prefix .. '1'] = args[prefix] | args[prefix .. '1'] = args[prefix] | ||
nums = union(nums, getArgNums(args, prefix)) | nums = union(nums, getArgNums(args, prefix)) | ||
Línea 417: | Línea 366: | ||
-- The following is roughly equivalent to the old {{Ambox/category}}. | -- The following is roughly equivalent to the old {{Ambox/category}}. | ||
local date = args.date | local date = args.date | ||
date = type(date) == 'string' and date | date = type(date) == 'string' and date | ||
local preposition = ' | local preposition = 'from' | ||
for _, num in ipairs(nums) do | for _, num in ipairs(nums) do | ||
local mainCat = args['cat' .. tostring(num)] | local mainCat = args['cat' .. tostring(num)] | ||
or args['category | or args['category' .. tostring(num)] | ||
local allCat = args['all | local allCat = args['all' .. tostring(num)] | ||
mainCat = type(mainCat) == 'string' and mainCat | mainCat = type(mainCat) == 'string' and mainCat | ||
allCat = type(allCat) == 'string' and allCat | allCat = type(allCat) == 'string' and allCat | ||
if mainCat and date and date ~= '' then | if mainCat and date and date ~= '' then | ||
local catTitle = string.format('%s %s %s', mainCat, preposition, | local catTitle = string.format('%s %s %s', mainCat, preposition, date) | ||
self:addCat(0, catTitle) | self:addCat(0, catTitle) | ||
catTitle = getTitleObject(' | catTitle = getTitleObject('Category:' .. catTitle) | ||
if not catTitle or not catTitle.exists then | if not catTitle or not catTitle.exists then | ||
self:addCat(0, ' | self:addCat(0, 'Articles with invalid date parameter in template') | ||
end | end | ||
elseif mainCat and (not date or date == '') then | elseif mainCat and (not date or date == '') then | ||
Línea 487: | Línea 436: | ||
-- Set categories for all namespaces. | -- Set categories for all namespaces. | ||
if self.invalidTypeError then | if self.invalidTypeError then | ||
local allSort = (self.title.namespace == 0 and ' | local allSort = (self.title.namespace == 0 and 'Main:' or '') .. self.title.prefixedText | ||
self:addCat('all', ' | self:addCat('all', 'Wikipedia message box parameter needs fixing', allSort) | ||
end | end | ||
if self.isSubstituted then | if self.isSubstituted then | ||
self:addCat('all', ' | self:addCat('all', 'Pages with incorrectly substituted templates') | ||
end | end | ||
end | end | ||
Línea 513: | Línea 462: | ||
-- Convert category tables to strings and pass them through | -- Convert category tables to strings and pass them through | ||
-- [[Module:Category handler]]. | -- [[Module:Category handler]]. | ||
return require(' | return require('Module:Category handler')._main{ | ||
main = table.concat(self.categories[0] or {}), | main = table.concat(self.categories[0] or {}), | ||
template = table.concat(self.categories[10] or {}), | template = table.concat(self.categories[10] or {}), | ||
all = table.concat(self.categories.all | all = table.concat(self.categories.all or {}), | ||
nocat = self.args.nocat | nocat = self.args.nocat, | ||
page = self.args.page | page = self.args.page | ||
} | } | ||
end | end | ||
Línea 524: | Línea 473: | ||
function MessageBox:export() | function MessageBox:export() | ||
local root = mw.html.create() | local root = mw.html.create() | ||
-- Add the subst check error. | -- Add the subst check error. | ||
Línea 531: | Línea 479: | ||
:addClass('error') | :addClass('error') | ||
:wikitext(string.format( | :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('}}') | mw.text.nowiki('{{'), self.name, self.name, mw.text.nowiki('}}') | ||
)) | )) | ||
Línea 582: | Línea 530: | ||
:addClass('mbox-text-span') | :addClass('mbox-text-span') | ||
:wikitext(self.issue or nil) | :wikitext(self.issue or nil) | ||
if (self.talk or self.fix) | if (self.talk or self.fix) then | ||
textCellDiv:tag('span') | textCellDiv:tag('span') | ||
:addClass('hide-when-compact') | :addClass('hide-when-compact') | ||
Línea 596: | Línea 544: | ||
end | end | ||
if self.removalNotice then | if self.removalNotice then | ||
textCellDiv:tag(' | textCellDiv:tag('span') | ||
:addClass('hide-when-compact') | :addClass('hide-when-compact') | ||
:tag('i') | :tag('i') | ||
Línea 635: | Línea 583: | ||
:css('text-align', 'center') | :css('text-align', 'center') | ||
:wikitext(string.format( | :wikitext(string.format( | ||
' | 'This message box is using an invalid "type=%s" parameter and needs fixing.', | ||
self.type or '' | self.type or '' | ||
)) | )) | ||
Línea 669: | Línea 617: | ||
return function (frame) | return function (frame) | ||
if not getArgs then | if not getArgs then | ||
getArgs = require(' | getArgs = require('Module:Arguments').getArgs | ||
end | end | ||
return t.main(k, getArgs(frame, {trim = false, removeBlanks = false})) | return t.main(k, getArgs(frame, {trim = false, removeBlanks = false})) |