Modul:Message box: Unterschied zwischen den Versionen
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 arguments
KKeine Bearbeitungszusammenfassung |
(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 arguments) |
||
| Zeile 2: | Zeile 2: | ||
-- Require necessary modules. | -- Require necessary modules. | ||
local htmlBuilder = require(' | local getArgs = require('Module:Arguments').getArgs | ||
local categoryHandler = require(' | local htmlBuilder = require('Module:HtmlBuilder') | ||
local yesno = require(' | local categoryHandler = require('Module:Category handler').main | ||
local yesno = require('Module:Yesno') | |||
-- Load the configuration page. | -- Load the configuration page. | ||
local cfgTables = mw.loadData(' | local cfgTables = mw.loadData('Module:Message box/configuration') | ||
-- Get a language object for formatDate and ucfirst. | -- Get a language object for formatDate and ucfirst. | ||
| Zeile 18: | Zeile 19: | ||
local trim = mw.text.trim | local trim = mw.text.trim | ||
-------------------------------------------------------------------------------- | |||
-- Helper functions | |||
-------------------------------------------------------------------------------- | |||
local function getTitleObject(page) | local function getTitleObject(page, ...) | ||
if type(page) == 'string' then | if type(page) == 'string' then | ||
-- Get the title object, passing the function through pcall | -- Get the title object, passing the function through pcall | ||
-- in case we are over the expensive function count limit. | -- in case we are over the expensive function count limit. | ||
local success, title = pcall(mw.title.new, page) | local success, title = pcall(mw.title.new, page, ...) | ||
if success then | if success then | ||
return title | return title | ||
| Zeile 58: | Zeile 61: | ||
table.sort(nums) | table.sort(nums) | ||
return nums | return nums | ||
end | |||
-------------------------------------------------------------------------------- | |||
-- Box class definition | |||
-------------------------------------------------------------------------------- | |||
local box = {} | |||
box.__index = box | |||
function box.new() | |||
local obj = {} | |||
setmetatable(obj, box) | |||
return obj | |||
end | end | ||
| Zeile 195: | Zeile 211: | ||
if self.isSmall then | if self.isSmall then | ||
self:addClass(cfg.smallClass or 'mbox-small') | self:addClass(cfg.smallClass or 'mbox-small') | ||
end | |||
if yesno(args.hidden) then | |||
self:addClass('infobox editsection') | |||
end | end | ||
self:addClass(self.typeClass) | self:addClass(self.typeClass) | ||
| Zeile 202: | Zeile 221: | ||
-- Set text style. | -- Set text style. | ||
self.textstyle = args.textstyle | self.textstyle = args.textstyle | ||
-- Find if we are on the template page or not. This functionality is only used if useCollapsibleTextFields is set, | |||
-- or if both cfg.templateCategory and cfg.templateCategoryRequireName are set. | |||
self.useCollapsibleTextFields = cfg.useCollapsibleTextFields | |||
if self.useCollapsibleTextFields or cfg.templateCategory and cfg.templateCategoryRequireName then | |||
self.name = args.name | |||
if self.name then | |||
local templateName = mw.ustring.match(self.name, '^[tT][eE][mM][pP][lL][aA][tT][eE][%s_]*:[%s_]*(.*)$') or self.name | |||
templateName = 'Template:' .. templateName | |||
self.templateTitle = getTitleObject(templateName) | |||
end | |||
self.isTemplatePage = self.templateTitle and mw.title.equals(self.title, self.templateTitle) or false | |||
end | |||
-- Process data for collapsible text fields. At the moment these are only used in {{ambox}}. | -- Process data for collapsible text fields. At the moment these are only used in {{ambox}}. | ||
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 args.smalltext then | ||
| Zeile 233: | Zeile 260: | ||
-- Get the self.talk value. | -- Get the self.talk value. | ||
local talk = args.talk | local talk = args.talk | ||
if talk == '' and self. | if talk == '' -- Show talk links on the template page or template subpages if the talk parameter is blank. | ||
and self.templateTitle | |||
and (mw.title.equals(self.templateTitle, self.title) or self.title:isSubpageOf(self.templateTitle)) | |||
then | |||
talk = '#' | talk = '#' | ||
elseif talk == '' then | |||
talk = nil | |||
end | end | ||
if talk then | if talk then | ||
-- | -- If the talk value is a talk page, make a link to that page. Else assume that it's a section heading, | ||
-- and make a link to the talk page of the current page with that section heading. | |||
local talkTitle = getTitleObject(talk) | local talkTitle = getTitleObject(talk) | ||
local talkArgIsTalkPage = true | |||
if not talkTitle or not talkTitle.isTalkPage then | if not talkTitle or not talkTitle.isTalkPage then | ||
talkArgIsTalkPage = false | |||
talkTitle = getTitleObject(self.title.text, mw.site.namespaces[self.title.namespace].talk.id) | |||
end | end | ||
if talkTitle and talkTitle.exists then | if talkTitle and talkTitle.exists then | ||
local talkText = 'Relevant discussion may be found on' | local talkText = 'Relevant discussion may be found on' | ||
if | if talkArgIsTalkPage then | ||
talkText = format('%s [[%s|%s]].', talkText, talk, talkTitle.prefixedText) | talkText = format('%s [[%s|%s]].', talkText, talk, talkTitle.prefixedText) | ||
else | else | ||
| Zeile 338: | Zeile 368: | ||
catTitle = getTitleObject('Category:' .. catTitle) | catTitle = getTitleObject('Category:' .. catTitle) | ||
if not catTitle or not catTitle.exists then | if not catTitle or not catTitle.exists then | ||
self:addCat('main', ' | self:addCat('main', '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 | ||
| Zeile 352: | Zeile 382: | ||
if cfg.templateCategory then | if cfg.templateCategory then | ||
if cfg.templateCategoryRequireName then | if cfg.templateCategoryRequireName then | ||
if self.isTemplatePage then | if self.isTemplatePage then | ||
self:addCat('template', cfg.templateCategory) | self:addCat('template', cfg.templateCategory) | ||
| Zeile 390: | Zeile 419: | ||
if self.invalidTypeError then | if self.invalidTypeError then | ||
local allSort = (self.nsid == 0 and 'Main:' or '') .. self.title.prefixedText | local allSort = (self.nsid == 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 | ||
| Zeile 416: | Zeile 445: | ||
.addClass('error') | .addClass('error') | ||
.wikitext(format( | .wikitext(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('}}') | ||
)) | )) | ||
| Zeile 486: | Zeile 515: | ||
.cssText(self.textstyle) | .cssText(self.textstyle) | ||
.wikitext(self.text) | .wikitext(self.text) | ||
end | end | ||
| Zeile 530: | Zeile 553: | ||
local function main(boxType, args) | local function main(boxType, args) | ||
box:setTitle(args) | local outputBox = box.new() | ||
local cfg = | outputBox:setTitle(args) | ||
args = | local cfg = outputBox:getConfig(boxType) | ||
args = outputBox:removeBlankArgs(cfg, args) | |||
return | outputBox:setBoxParameters(cfg, args) | ||
return outputBox:export() | |||
end | end | ||
local function makeWrapper(boxType) | local function makeWrapper(boxType) | ||
return function (frame) | return function (frame) | ||
local args = getArgs(frame, {trim = false, removeBlanks = false}) | |||
return main(boxType, args) | return main(boxType, args) | ||
end | end | ||