求真百科歡迎當事人提供第一手真實資料,洗刷冤屈,終結網路霸凌。

變更

前往: 導覽搜尋

模块:Message box

增加 1,155 位元組, 4 年前
导入1个版本
-- This is a meta-module for producing message box templates, including-- {{mbox}}, {{ambox}}, {{imbox}}, {{tmbox}}, {{ombox}}, {{cmbox}} and {{fmbox}}.
-- Load Require necessary modules.local getArgs = require('Module:No globalsArguments').getArgslocal getArgscategoryHandler = require('Module:Category handler').main
local yesno = require('Module:Yesno')
 
-- Load the configuration page.
local cfgTables = mw.loadData('Module:Message box/configuration')
-- Get a language object for formatDate and ucfirst.
local lang = mw.language.getContentLanguage()
-- Define constantsSet aliases for often-used functions to reduce table lookups.local CONFIG_MODULE format = 'Module:Message box/configuration'mw.ustring.formatlocal DEMOSPACES tinsert = {talk table.insertlocal tconcat = 'tmbox', image = 'imbox', file = 'imbox', category = 'cmbox', article = 'ambox', main table.concatlocal trim = 'ambox'}mw.text.trim
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
local function getTitleObject(page, ...) if type(page) == 'string' then -- Get the title object, passing the function through pcall -- in case we are over the expensive function count limit. local success, title = pcall(mw.title.new, page, ...) if success then return title end
end
end
local ret = {}
for k in pairs(vals) do
table.inserttinsert(ret, k)
end
table.sort(ret)
local num = mw.ustring.match(tostring(k), '^' .. prefix .. '([1-9]%d*)$')
if num then
table.inserttinsert(nums, tonumber(num))
end
end
--------------------------------------------------------------------------------
local MessageBox box = {}MessageBoxbox.__index = MessageBoxbox
function MessageBoxbox.new(boxType, args, cfg) args = args or {}
local obj = {}
setmetatable(obj, box)
return obj
end
function box.getNamespaceId(ns) -- Set the title object and the namespace.if not ns then return end obj.title if type(ns) == 'string' then ns = getTitleObjectlang:ucfirst(argsmw.pageustring.lower(ns) or ) if ns == 'Main' then ns = 0 end end local nsTable = mw.titlesite.getCurrentTitle()namespaces[ns] if nsTable then return nsTable.id endend
function box.getMboxType(nsid) -- Set Gets the config for our box mbox typefrom a namespace number. obj.cfg if nsid == cfg[boxType] if not obj.cfg 0 then local ns = obj.title.namespace -- boxType is "mbox" or invalid input if args.demospace and args.demospace ~= return 'ambox' then -- implement demospace parameter of mboxmain namespace local demospace elseif nsid == string.lower(args.demospace) if DEMOSPACES[demospace] 6 then -- use template from DEMOSPACES obj.cfg = cfg[DEMOSPACES[demospace]] elseif string.find( demospace, return 'talkimbox' ) then -- demo as a talk pagefile namespace obj.cfg = cfg.tmbox else -- default to ombox obj.cfg = cfg.ombox end elseif ns nsid == 0 14 then obj.cfg = cfg.ambox return 'cmbox' -- main category namespace else elseif ns local nsTable == 6 then objmw.cfg = cfgsite.imbox -- file namespacenamespaces[nsid] elseif ns == 14 if nsTable and nsTable.isTalk then obj.cfg = cfg.cmbox return 'tmbox' -- category any talk namespace
else
local nsTable = mw.site.namespaces[ns] if nsTable and nsTable.isTalk then obj.cfg = cfg.tmbox -- any talk namespace else obj.cfg = cfg.return 'ombox ' -- other namespaces or invalid input end
end
end
end
-- Set the argumentsfunction box:addCat(ns, cat, and remove all blank arguments except for the onessort) -- listed in cfg.allowBlankParams.if type(cat) ~= 'string' then return end do local newArgs nsVals = {'main', 'template', 'all'} local tname for ki, v val in pairsipairs(argsnsVals) do if v ~ns == '' val then newArgs[k] tname = v endns .. 'Cats'
end
end if not tname then for i, param val in ipairs(obj.cfg.allowBlankParams or {}nsVals) do newArgsnsVals[parami] = args[param]format('"%s"', val)
end
objerror('無效的ns參數傳送到box:addCat;有效的數值為' .args = newArgs. mw.text.listToText(nsVals, '、', '或'))
end
self[tname] = self[tname] or {}
if type(sort) == 'string' then
tinsert(self[tname], format('[[Category:%s|%s]]', cat, sort))
else
tinsert(self[tname], format('[[Category:%s]]', cat))
end
end
-- Define internal data structure.function box:addClass(class) obj.categories if type(class) ~= {}'string' then return end objself.classes = self.classes or {} -- For lazy loading of [[Module:Category handler]]tinsert(self.classes, class) obj.hasCategories = falseend
function box:setTitle(args) -- Get the title object and the namespace. self.pageTitle = getTitleObject(args.page ~= '' and args.page) self.title = self.pageTitle or mw.title.getCurrentTitle() self.demospace = args.demospace ~= '' and args.demospace or nil return setmetatableself.nsid = box.getNamespaceId(obj, MessageBoxself.demospace)or self.title.namespace
end
function MessageBoxbox:addCatgetConfig(ns, cat, sortboxType) -- Get the box config data from the data page. if not cat boxType == 'mbox' then return nilboxType = box.getMboxType(self.nsid)
end
local cfg = cfgTables[boxType] if sort not cfg then cat local boxTypes = string.{} for k, v in pairs(dataTables) do tinsert(boxTypes, format('[[Category:%s|"%s]]"', catk)) end tinsert(boxTypes, sort'"mbox"') else cat = string.error(format('[[Category:無效的訊息框類型「%s」;有效的類型為%s]]', cattostring(boxType), mw.text.listToText(boxTypes)), 2)
end
self.hasCategories = true self.categories[ns] = self.categories[ns] or {} table.insert(self.categories[ns], cat)return cfg
end
function MessageBoxbox:addClassremoveBlankArgs(classcfg, args) -- Only allow blank arguments for the parameter names listed in cfg.allowBlankParams. local newArgs = {} for k, v in pairs(args) do if not class v ~= '' then newArgs[k] = v end end for i, param in ipairs(cfg.allowBlankParams or {}) do return nilnewArgs[param] = args[param]
end
table.insert(self.classes, class)return newArgs
end
function MessageBoxbox:setParameterssetBoxParameters(cfg, args) local args = self.args local cfg = self.cfg 
-- Get type data.
self.type = args.type
local typeData = cfg.types[self.type]
self.invalidTypeError = cfg.showInvalidTypeError and self.type and not typeDataand true or false
typeData = typeData or cfg.types[cfg.default]
self.typeClass = typeData.class
-- Find if the box has been wrongly substituted.
self.isSubstituted = if cfg.substCheck and args.subst == 'SUBST'then self.isSubstituted = true end
-- Find whether we are using a small message box.
self.isSmall = if cfg.allowSmall and (
cfg.smallParam and args.small == cfg.smallParam
or not cfg.smallParam and yesno(args.small)
)
then
self.isSmall = true
else
self.isSmall = false
end
-- Add attributes, classes and styles.
if cfg.allowId then self.id = args.id self.name = args.name if self.name then self:addClass('box-' .. string.gsub(self.name,' ','_')) end if yesno(args.plainlinks) ~= false then self:addClass('plainlinks')
end
self:addClass(cfg.usePlainlinksParam and yesno(args.plainlinks or true) and 'plainlinks')
for _, class in ipairs(cfg.classes or {}) do
self:addClass(class)
if self.isSmall then
self:addClass(cfg.smallClass or 'mbox-small')
end
if yesno(args.hidden) then
self:addClass('infobox editsection')
end
self:addClass(self.typeClass)
self:addClass(args.class)
self.style = args.style
self.attrs = args.attrs
-- Set text style.
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.templateCategoryRequireNamethen 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}}.
if self.useCollapsibleTextFields then
-- Get the self.issue value.
local sect
if args.sect == '' then
sect = 'This ' .. (cfg.sectionDefault or 'page頁面')
elseif type(args.sect) == 'string' then
sect = 'This ' .. args.sect
end
local issue = args.issue
text = type(text) == 'string' and text or nil
local issues = {}
table.inserttinsert(issues, sect) table.inserttinsert(issues, issue) table.inserttinsert(issues, text) self.issue = table.concattconcat(issues, ' ')
end
-- Get the self.talk value.
local talk = args.talk
if talk == '' -- Show talk links on the template page or template subpages if the talk -- parameter is blank. if talk == '' and self.templateTitle and ( mw.title.equals(self.templateTitle, self.title) or self.title:isSubpageOf(self.templateTitle) )
then
talk = '#'
end
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 talkArgIsTalkPage = true
if not talkTitle or not talkTitle.isTalkPage then
talkArgIsTalkPage = false
talkTitle = getTitleObject( self.title.text, mw.site.namespaces[self.title.namespace].talk.id )
end
if talkTitle and talkTitle.exists then
local 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
self.talk = talkText
-- Get other values.
self.fix = args.fix ~= '' and args.fix or nil
local date
if args.date and args.date ~= '' then
date = args.date
elseif args.date time == '' and self.isTemplatePage then date = lang:formatDate('F Y年n月j日') elseif args.time and args.time ~= '' then date = lang:formatDate('Y年n月j日', args.time) end if date then local ok, tempdate = pcall(lang.formatDate, lang, 'Y年n月j日', date) -- 正規化日期 if ok then date = tempdate end
end
if date then
self.date = string.format(" <small class='date-container'>''(<span class='date'>%s</span>)''</small>", date)
end
if args.fix and args.fix ~= '' then
self.fix = format("<br /><small>%s</small>", args.fix)
else
self.fix = ''
end
self.info = args.info
if yesno(args.removalnotice) then
self.removalNotice = cfg.removalNotice
end
end
-- Set the non-collapsible text field. At the moment this is used by all box -- types other than ambox, -- and also by ambox when small=yes.
if self.isSmall then
self.text = args.smalltext or args.text
-- General image settings.
self.imageCellDiv = not self.isSmall and cfg.imageCellDivand true or false
self.imageEmptyCell = cfg.imageEmptyCell
if cfg.imageEmptyCellStyle then
self.imageLeft = imageLeft
if not imageLeft then
local imageSize = self.isSmall and (cfg.imageSmallSize or '30x30px') or '40x40px' self.imageLeft = string.format('[[File:%s|%s|link=|alt=]]', self.typeImage or 'Imbox notice.png', imageSize)
end
end
self.imageRight = imageRight
end
end
function MessageBox:setMainspaceCategories() local args = self-- Add mainspace categories.args local cfg = selfAt the moment these are only used in {{ambox}}.cfg  if not cfg.allowMainspaceCategories then return nilif args.cat then args.cat1 = args.cat end  local nums self.catNums = {} for _getArgNums(args, prefix in ipairs{'cat') if args.category then args.category1 = args.category end self.categoryNums = getArgNums(args, 'category', 'all'} do) if args[prefix .all then args. '1'] all1 = args[prefix].all end nums self.allNums = union(nums, getArgNums(args, prefix'all') self.categoryParamNums = union(self.catNums, self.categoryNums) end self.categoryParamNums = union(self.categoryParamNums, self.allNums)  -- The following is roughly equivalent to the old {{Ambox/category}}. local date local sortDay local dayName = { [1] = '㏠', [2] = args.date'㏡', [3] = '㏢', [4] = '㏣', [5] = '㏤', [6] = '㏥', [7] = '㏦', [8] = '㏧', [9] = '㏨', [10] = '㏩', [11] = '㏪', [12] = '㏫', date [13] = type(date) '㏬', [14] ='㏭', [15] = 'string' and date, local preposition [16] = 'from', for _ [17] = '㏰', num in ipairs(nums) do local mainCat [18] = args'㏱', [19] = 'cat' .. tostring(num), [20]= '㏳', or args[21] = 'category' .. tostring(num), [22]= '㏵', local allCat [23] = args'㏶', [24] = 'all' .. tostring(num), [25]= '㏸', mainCat [26] = type(mainCat) '㏹', [27] ='㏺', [28] = 'string' and mainCat, allCat [29] = type(allCat) '㏼', [30] ='㏽', [31] = 'string' and allCat } if mainCat and args.date and args.date ~= '' then local catTitle date = stringargs.format('%s %s %s', mainCat, preposition, date) self:addCat(0local ok, catTitle) catTitle tempdate = getTitleObjectpcall(lang.formatDate, lang, 'Category:Y年n月' .. catTitle, date)-- 正規化日期 if not catTitle or not catTitle.exists ok then self:addCat(0, 'Articles with invalid date parameter in template')= tempdate
end
elseif mainCat args.time and (not date or args.time ~= '' then date == lang:formatDate('Y年n月', args.time) then selfsortDay = lang:addCatformatDate(0'j', mainCatargs.time) sortDay = tonumber(sortDay) sortDay = dayName[sortDay]
end
date = type(date) == 'string' and date local preposition = '自' for _, num in ipairs(self.categoryParamNums) do local mainCat = args['cat' .. tostring(num)] or args['category' .. tostring(num)] local allCat = args['all' .. tostring(num)] mainCat = type(mainCat) == 'string' and mainCat allCat = type(allCat) == 'string' and allCat if mainCat and date and date ~= '' then local catTitle = format('%s%s%s', preposition, date, mainCat) if sortDay then self:addCat('main', catTitle, sortDay) else self:addCat('main', catTitle) end catTitle = getTitleObject('Category:' .. catTitle) if not catTitle or not catTitle.exists then self:addCat('main', '模板中使用无效日期参数的条目') end elseif mainCat and (not date or date == '') then self:addCat('main', mainCat) end if allCat then self:addCat(0'main', allCat) end
end
end
end
 
function MessageBox:setTemplateCategories()
local args = self.args
local cfg = self.cfg
-- Add template -namespace categories.
if cfg.templateCategory then
if cfg.templateCategoryRequireName then
if self.isTemplatePage then
self:addCat(10'template', cfg.templateCategory)
end
elseif not self.title.isSubpage then
self:addCat(10'template', cfg.templateCategory)
end
end
-- Add template error categoriescategory.
if cfg.templateErrorCategory then
local templateErrorCategory = cfg.templateErrorCategory
end
end
self:addCat(10'template', templateCat, templateSort)
end
end
function MessageBox:setAllNamespaceCategories() -- Set categories Categories for all namespaces.
if self.invalidTypeError then
local allSort = (self.title.namespace nsid == 0 and 'Main:' or '') .. self.title.prefixedText self:addCat('all', 'Wikipedia message box parameter needs fixing需要修复的信息框', allSort)
end
if self.isSubstituted then
self:addCat('all', 'Pages with incorrectly substituted templates錯誤使用替換引用的頁面') endend 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() if not self.hasCategories then -- No categories added, no need to pass them to Category handler so, -- if it was invoked, it would return the empty string. -- So we shortcut and return the empty string. return "" end -- Convert category tables to strings and pass them through -- [[Module:Category handler]]. return require('Module:Category handler')self._maincategories = categoryHandler{ main = table.concattconcat(self.categories[0] mainCats or {}), template = table.concattconcat(self.categories[10] templateCats or {}), all = table.concattconcat(self.categories.all allCats or {}), nocat = self.args.nocat, demospace = self.demospace, page = self.argspageTitle and self.pageTitle.pageprefixedText or nil
}
end
function MessageBoxbox:export()
local root = mw.html.create()
-- Add the subst check error.
if self.isSubstituted and self.name then
root :tag('b') :addClass('error') :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:tag('table')
boxTable :attr('id', self.id or nil)
for i, class in ipairs(self.classes or {}) do
boxTable :addClass(class or nil)
end
boxTable
:cssText(self.style or nil)
:attr('role', 'presentation')
 
if self.attrs then
boxTable:attr(self.attrs)
end
-- Add the left-hand image.
local imageLeftCell = row:tag('td'):addClass('mbox-image')
if self.imageCellDiv then
-- If we are using a div, redefine imageLeftCell so that the imageis inside it. -- is inside it. Divs use style="width: 52px;", which limits the -- 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:tag('div'):css('width', '52px')
end
imageLeftCell :wikitext(self.imageLeft or nil)
elseif self.imageEmptyCell then
-- Some message boxes define an empty cell if no image is specified, and -- some don't. -- The old template code in templates where empty cells are -- specified gives the following hint: -- "No image. Cell with some width -- or padding necessary for text cell to have 100% width."
row:tag('td')
:addClass('mbox-empty-cell') :cssText(self.imageEmptyCellStyle or nil)
end
local textCell = row:tag('td'):addClass('mbox-text')
if self.useCollapsibleTextFields then
-- The message box uses advanced text parameters that allow things to becollapsible. At the -- collapsible. At the moment, only ambox uses this. textCell :cssText(self.textstyle or nil) local textCellDiv textCellSpan = textCell:tag('divspan') textCellDivtextCellSpan
:addClass('mbox-text-span')
:wikitext(self.issue or nil) if (self.talk or self.fix) and not self.isSmall then textCellDivtextCellSpan :tag('span') :addClass('hide-when-compact') :wikitext(self.talk and (' ' .. self.talk) or nil) :wikitext(self.fix and (' ' .. self.fix) or nil)
end
textCellDivtextCellSpan :wikitext(self.date and (' ' .. self.date) or nil) if not self.info isSmall and not self.isSmall fix ~= '' then textCellDivtextCellSpan
:tag('span')
:addClass('hide-when-compact') :wikitext(self.info fix and (' ' .. self.info) or nilfix)
end
if not self.removalNotice isSmall then textCellDivtextCellSpan :tag('smallspan')
:addClass('hide-when-compact')
:tag('i') :wikitext(stringself.format(" (%s)", info and self.removalNotice)info)
end
else
-- Default text formatting - anything goes.
textCell
:cssText(self.textstyle or nil) :wikitext(self.text or nil)
end
local imageRightCell = row:tag('td'):addClass('mbox-imageright')
if self.imageCellDiv then
-- If we are using a div, redefine imageRightCell so that the image -- is inside it. imageRightCell = imageRightCell:tag('div'):css('width', '52px')-- If we are using a div, redefine imageRightCell so that the image is inside it.
end
imageRightCell
:wikitext(self.imageRight or nil)
end
:attr('colspan', self.imageRight and '3' or '2')
:addClass('mbox-text')
:cssText(self.textstyle or nil) :wikitext(self.below or nil)
end
-- Add error message for invalid type parameters.
if self.invalidTypeError then
root :tag('div') :css('text-align', 'center') :wikitext(string.format( 'This message box is using an invalid "type此訊息框使用無效的「type=%s" parameter and needs fixing.」參數,需要修復。', self.type or '' ))
end
-- Add categories.
root :wikitext(self:renderCategories() or nil.categories)
return tostring(root)
end
--------------------------------------------------------------------------------local function main(boxType, args)-- Exports local outputBox = box.new()-------------------------------------------------------------------------------- outputBox:setTitle(args) local cfg = outputBox:getConfig(boxType) args = outputBox:removeBlankArgs(cfg, args) outputBox:setBoxParameters(cfg, args) return outputBox:export()end
local p, mt = {}, {} function p._exportClassesmakeWrapper(boxType) -- For testing.return function (frame) return local args = getArgs(frame, {trim = false, removeBlanks = false}) MessageBox = MessageBoxreturn main(boxType, args) }end
end
function local p.main(boxType, args, cfgTables)= { local box main = MessageBox.new(boxType, argsmain, cfgTables or mw.loadData(CONFIG_MODULE)) box:setParameters() box:setCategories() return box:exportmbox = makeWrapper('mbox')end}
function mt.__indexfor boxType in pairs(t, kcfgTables)do return function (frame) if not getArgs then getArgs p[boxType] = require('Module:Arguments').getArgs end return t.main(k, getArgsmakeWrapper(frame, {trim = false, removeBlanks = false}boxType)) end
end
return setmetatable(p, mt)
行政員使用者查核員拒收員巡檢員管理員、water、waterc、waterh、waterm、waterp、waters、watert、waterv、waterw、waterz
10,317
次編輯