Content deleted Content added
change the way collapsible notices are rendered - still needs work |
per edit request on talk page - remove line as unnecessary |
||
(60 intermediate revisions by 6 users not shown) | |||
Line 10:
local CONFIG_PAGE = 'Module:Article history/config'
local WRAPPER_TEMPLATE = 'Template:Article history'
local DEBUG_MODE =
-- Load required modules.
require('
local Category = require('Module:Article history/Category')
local yesno = require('Module:Yesno')
Line 72:
function Message:message(key, ...)
-- This fetches the message from the config with the specified key, and
-- substitutes parameters $1, $2 etc. with the subsequent values it is
-- passed.
local msg = self.cfg.msg[key]
if select('#', ...) > 0 then
Line 78 ⟶ 80:
else
return msg
end
end
Line 93 ⟶ 87:
-- stops unless the error is caught. This is used for errors where
-- subsequent processing becomes impossible.
local errorText
if help then
errorText = self:message('error-message-help', msg, help)
else
errorText = self:message('error-message-nohelp', msg)
end
error(errorText, 0)
end
Line 102 ⟶ 101:
-- prevent the module from outputting something useful.
self.warnings = self.warnings or {}
local warningText
if help then
warningText = self:message('warning-help', msg, help)
else
warningText = self:message('warning-nohelp', msg)
end
table.insert(self.warnings, warningText)
end
Line 122 ⟶ 127:
obj.cfg = data.cfg
obj.currentTitle = data.currentTitle
obj.
return obj
end
function Row:
-- This method is for use in Row object methods that are called more than
-- once. The results of such methods should be cached to avoid unnecessary
-- processing. We also cache any errors found and abort if an error was
-- raised previously, otherwise error messages could be displayed multiple
-- times.
--
-- We use false as a key to cache nil results, so func cannot return false.
--
-- @param cacheKey The key to cache successful results with
-- @param errorCacheKey The key to cache errors with
-- @param func an anonymous function that returns the method result
if self[errorCacheKey] then
return nil
end
local ret = self[cacheKey]
if ret then
return ret
elseif ret == false then
return nil
end
local success
if DEBUG_MODE then
success = true
ret = func()
else
success, ret = pcall(func)
end
if success then
if ret then
self[cacheKey] = ret
return ret
else
self[cacheKey] = false
return nil
end
else
self[errorCacheKey] = true
-- We have already formatted the error message, so no need to format it
-- again.
error(ret, 0)
end
end
function Row:getData(articleHistoryObj)
return self:_cachedTry('_dataCache', '_isDataError', function ()
return self.makeData(articleHistoryObj)
end)
end
function Row:setIconValues(icon, caption, size)
self.icon = icon
self.
self.iconSize = size
end
function Row:
return maybeCallFunc(self.icon, articleHistoryObj, self)
end
function Row:
return maybeCallFunc(self.iconCaption, articleHistoryObj, self)
end
function Row:getIconSize()
return self.iconSize or self.cfg.defaultIconSize or '30px'
end
function Row:renderIcon(articleHistoryObj)
local icon = self:getIcon(articleHistoryObj)
if not icon then
return nil
end
return renderImage(
icon,
self:getIconCaption(articleHistoryObj),
self:getIconSize()
)
end
function Row:setNoticeBarIconValues(icon, caption, size)
self.noticeBarIcon = icon
self.noticeBarIconCaption = caption
self.noticeBarIconSize = size
end
function Row:getNoticeBarIcon(articleHistoryObj)
local icon = maybeCallFunc(self.noticeBarIcon, articleHistoryObj, self)
if
icon = self:getIcon(articleHistoryObj)
if
self:raiseError(
self:message('row-error-missing-icon'),
self:message('row-error-missing-icon-help')
)
end
end
return icon
end
function Row:getNoticeBarIconCaption(articleHistoryObj)
local caption = maybeCallFunc(
self.noticeBarIconCaption,
articleHistoryObj,
self
)
if not caption then
caption = self:getIconCaption(articleHistoryObj)
end
return caption
end
function Row:getNoticeBarIconSize()
return self.noticeBarIconSize or self.cfg.defaultNoticeBarIconSize or '15px'
end
function Row:
local icon = self:getNoticeBarIcon(articleHistoryObj)
if not icon then
return nil
end
return renderImage(
icon,
self:getNoticeBarIconCaption(articleHistoryObj),
self:getNoticeBarIconSize()
)
end
function Row:
end
function Row:
return maybeCallFunc(self.text, articleHistoryObj, self)
end
function Row:
if
return self._html
end
local text = self:getText(articleHistoryObj)
if not text then
return nil
end
local html = mw.html.create('tr')
html
:tag('td')
:addClass('mbox-image')
:wikitext(self:renderIcon(articleHistoryObj))
:done()
:tag('td')
:addClass('mbox-text')
:wikitext(text)
self._html = html
return html
end
function Row:setCategories(val)
-- Set the categories from the object's config. val can be either an array
-- of strings or a function returning an array of category objects.
self.categories = val
end
function Row:getCategories(articleHistoryObj)
local ret = {}
if type(self.categories) == 'table' then
for _, cat in ipairs(self.categories) do
ret[#ret + 1] = Category.new(cat)
end
elseif type(self.categories) == 'function' then
local t = self.categories(articleHistoryObj, self) or {}
for _, categoryObj in ipairs(t) do
ret[#ret + 1] = categoryObj
end
end
return ret
end
Line 219 ⟶ 321:
setmetatable(obj, Status)
obj.id = data.id
obj.
obj:setIconValues(
obj.statusCfg.icon,
obj.statusCfg.iconCaption or obj.name,
)
obj:setNoticeBarIconValues(
obj.statusCfg.noticeBarIcon,
obj.statusCfg.noticeBarIconSize
)
obj:setText(obj.statusCfg.text)
obj:
return obj
end
function Status:
return self.iconSize
or self.cfg.defaultStatusIconSize
or '50px'
end
function Status:
if text then
return substituteParams(
text,
self.currentTitle.subjectPageTitle.prefixedText,
self.currentTitle.text
)
end
end
Line 266 ⟶ 371:
setmetatable(obj, MultiStatus)
obj.id = data.id
obj.statusCfg = obj.cfg.statuses[data.id]
obj.name = obj.statusCfg.name
Line 280 ⟶ 386:
end
obj.statuses = {}
local defaultIconSize = obj.cfg.
for
table.insert(obj.statuses, Status.new(getChildStatusData(
data,
Line 294 ⟶ 400:
function MultiStatus:exportHtml(articleHistoryObj)
local ret = mw.html.create()
for
ret:node(obj:exportHtml(articleHistoryObj))
end
Line 300 ⟶ 406:
end
function MultiStatus:
local ret = {}
for
for
ret[#ret + 1] = categoryObj
end
Line 312 ⟶ 418:
function MultiStatus:exportNoticeBarIcon()
local ret = {}
for
ret[#ret + 1] = obj:exportNoticeBarIcon()
end
Line 320 ⟶ 426:
function MultiStatus:getWarnings()
local ret = {}
for
for
ret[#ret + 1] = msg
end
Line 341 ⟶ 447:
setmetatable(obj, Notice)
obj:
data.icon,
data.iconCaption,
data.iconSize
)
obj:setNoticeBarIconValues(
data.noticeBarIcon,
data.noticeBarIconCaption,
data.noticeBarIconSize
)
obj:setText(data.text)
obj:
return obj
Line 442 ⟶ 552:
obj:addWarning(
obj:message(
'action-
data.date,
obj:getParameter('date')
),
obj:message('action-
)
end
Line 452 ⟶ 562:
obj:addWarning(
obj:message(
'action-
obj.paramNum,
obj:getParameter('date'),
obj:getParameter('code')
),
obj:message('action-
)
end
Line 464 ⟶ 575:
obj.oldid = tonumber(data.oldid)
if data.oldid and (not obj.oldid or not isPositiveInteger(obj.oldid)) then
obj
obj:addWarning(
obj:message(
'action-
data.oldid,
obj:getParameter('oldid')
),
obj:message('action-
)
end
-- Set the notice bar icon values
obj:setNoticeBarIconValues(
data.noticeBarIcon,
data.noticeBarIconCaption,
data.noticeBarIconSize
)
-- Set the categories
obj:setCategories(obj.actionCfg.categories)
return obj
Line 507 ⟶ 629:
function Action:exportHtml(articleHistoryObj)
if self._html then
return self._html
end
local row = mw.html.create('tr')
Line 531 ⟶ 657:
self:getName(articleHistoryObj)
))
-- Result cell
row
Line 537 ⟶ 663:
:wikitext(self:getResult(articleHistoryObj))
self._html = row
return row
end
Line 566 ⟶ 680:
setmetatable(obj, CollapsibleNotice)
obj:
data.icon,
data.iconCaption,
data.iconSize
)
obj:setNoticeBarIconValues(
data.noticeBarIcon,
data.noticeBarIconCaption,
data.noticeBarIconSize
)
obj:setText(data.text)
obj:
obj
return obj
end
function CollapsibleNotice:
self.collapsibleText = s
end
function CollapsibleNotice:getCollapsibleText(articleHistoryObj)
return maybeCallFunc(self.collapsibleText, articleHistoryObj, self)
end
function CollapsibleNotice:getIconSize()
return self.iconSize
end
function CollapsibleNotice:exportHtml(articleHistoryObj, isInCollapsibleTable)
local cacheKey = isInCollapsibleTable
and '_htmlCacheCollapsible'
or '_htmlCacheDefault'
return self:_cachedTry(cacheKey, '_isHtmlError', function ()
local text = self:getText(articleHistoryObj)
if not text then
return nil
end
local function maybeMakeCollapsibleTable(cell, text, collapsibleText)
-- If collapsible text is specified, makes a collapsible table
-- inside the cell with two rows, a header row with one cell and a
--
-- collapsedText, respectively. If no collapsible text is
--
if collapsibleText then
cell
:tag('div')
:addClass('mw-collapsible mw-collapsed')
:
:wikitext(text)
:done()
:
:
:css('border', '1px silver solid')
:wikitext(collapsibleText)
else
cell:wikitext(text)
end
end
local icon = self:renderIcon(articleHistoryObj)
local
if isInCollapsibleTable then
local textCell = html:tag('td')
:attr('colspan', 3)
:css('width', '100%')
local rowText
if icon then
rowText = icon .. ' ' .. text
else
rowText = text
end
maybeMakeCollapsibleTable(textCell, rowText, collapsibleText)
else
:tag('td')
:addClass('mbox-image')
:wikitext(icon)
:done()
:tag('td')
:addClass('mbox-text')
maybeMakeCollapsibleTable(textCell, text, collapsibleText)
end
end)
end
Line 675 ⟶ 788:
obj.currentTitle = currentTitle or mw.title.getCurrentTitle()
-- Define object structure.
obj._errors = {}
obj._allObjectsCache = {}
-- Format the config
local function substituteAliases(t, ret)
Line 773 ⟶ 884:
return val
else
table.insert(self.
return nil
end
Line 783 ⟶ 894:
-- user. We memoise this so that the parameters only have to be processed
-- once.
if self.actions
return self.actions
end
Line 797 ⟶ 908:
local actions = {}
local suffixes = self.cfg.actionParamSuffixes
for
local objArgs = {}
for k, v in pairs(t) do
Line 822 ⟶ 933:
end
local statuses = self.cfg.statuses
if statuses[
return statuses[
else
self:addWarning(
self:message('articlehistory-warning-invalid-status', code),
self:message('articlehistory-warning-invalid-status-help')
)
return nil
end
end
Line 832 ⟶ 947:
function ArticleHistory:getStatusObj()
-- Get the status object for the current status.
if self.statusObj
return nil
elseif self.statusObj ~= nil then
return self.statusObj
end
local statusId
if self.cfg.getStatusIdFunction then
statusId = self:try(self.cfg.getStatusIdFunction
else
statusId = self:try(
self.getStatusIdForCode, self,
self.args[self.
)
end
if not statusId then
self.statusObj = false
return
end
-- Check that some actions were specified, and if not add a warning.
local actions = self:getActionObjects()
if #actions < 1 then
self:addWarning(
self:message('articlehistory-warning-status-no-actions'),
self:message('articlehistory-warning-status-no-actions-help')
)
end
Line 853 ⟶ 979:
id = statusId,
currentTitle = self.currentTitle,
cfg = self.cfg
}
local isMulti = self.cfg.statuses[statusId].isMulti
local initFunc = isMulti and MultiStatus.new or Status.new
local statusObj = self:try(initFunc, statusObjData)
self.statusObj = statusObj or false
return self.statusObj or nil
end
Line 868 ⟶ 993:
end
function ArticleHistory:
-- This holds the logic for fetching tables of Notice and CollapsibleNotice
-- objects.
if self[memoizeKey] then
return self[memoizeKey]
end
local ret = {}
for
for k, v in pairs(t) do
if k ~= 'isActive' then
data[k] = v
end
end
data.cfg = self.cfg
data.currentTitle = self.currentTitle
ret[#ret + 1] =
end
end
self
return ret
end
function ArticleHistory:getNoticeObjects()
return self:_noticeFactory('notices', 'notices', Notice)
end
function ArticleHistory:getCollapsibleNoticeObjects()
return self:_noticeFactory(
CollapsibleNotice
)
end
function ArticleHistory:getAllObjects(addSelf)
local cacheKey = addSelf and 'addSelf' or 'default'
local ret = self._allObjectsCache[cacheKey]
if not ret then
ret = {}
local statusObj = self:getStatusObj()
if statusObj then
ret[#ret + 1] = statusObj
end
local objTables = {
self:getNoticeObjects(),
self:getActionObjects(),
self:getCollapsibleNoticeObjects()
}
for _, t in ipairs(objTables) do
for _, obj in ipairs(t) do
ret[#ret + 1] = obj
end
end
if addSelf then
ret[#ret + 1] = self
end
self._allObjectsCache[cacheKey] = ret
end
return ret
end
function ArticleHistory:
local ret = {}
-- Icons that aren't part of a row.
if
for _, data in ipairs(self.cfg.noticeBarIcons) do
if data.isActive(self) then
ret[#ret + 1] = renderImage(
data.icon,
nil,
data.size or self.cfg.defaultNoticeBarIconSize
)
end
end
end
-- Icons in row objects.
for _, obj in ipairs(self:getAllObjects()) do
ret[#ret + 1] = obj:exportNoticeBarIcon(self)
end
return ret
Line 929 ⟶ 1,080:
-- Returns an array of error/warning strings. Error strings come first.
local ret = {}
for _, msg in ipairs(self.
ret[#ret + 1] = msg
end
for
for
ret[#ret + 1] = msg
end
Line 940 ⟶ 1,091:
end
function ArticleHistory:
-- Returns a boolean indicating whether categories should be output or not.
local title = self.currentTitle
local ns = title.namespace
return title.isTalkPage
and ns ~= 3 -- not user talk
and ns ~= 119 -- not draft talk
end
function ArticleHistory:renderCategories()
local ret = {}
if self:categoriesAreActive() then
-- Child object categories
for _, obj in ipairs(self:getAllObjects()) do
local categories = self:try(obj.getCategories, obj, self)
for _, categoryObj in ipairs(categories or {}) do
ret[#ret + 1] = tostring(categoryObj)
end
end
-- Extra categories
for _, func in ipairs(self.cfg.extraCategories or {}) do
local cats = func(self) or {}
for _, categoryObj in ipairs(cats) do
ret[#ret + 1] = tostring(categoryObj)
end
end
end
return table.concat(ret)
end
function ArticleHistory:__tostring()
local root = mw.html.create()
-- Table root
local
-- Status
local statusObj = self:getStatusObj()
if statusObj then
end
Line 963 ⟶ 1,140:
local notices = self:getNoticeObjects()
for _, noticeObj in ipairs(notices) do
end
-- Get action objects and the collapsible notice objects, and generate the
-- HTML objects for the action objects. We need the action HTML objects so
-- that we can accurately
-- action objects may generate errors when the HTML is generated.
local actions = self:getActionObjects() or {}
local collapsibleNotices = self:getCollapsibleNoticeObjects() or {}
local collapsibleNoticeHtmlObjects, actionHtmlObjects = {}, {}
for _, obj in ipairs(actions) do
table.insert(
actionHtmlObjects,
Line 981 ⟶ 1,156:
)
end
table.insert(
collapsibleNoticeHtmlObjects,
self:try(obj.exportHtml, obj, self, true) -- Render the collapsed version
)
end
local nActionRows = #actionHtmlObjects
local nCollapsibleRows = nActionRows + #collapsibleNoticeHtmlObjects
-- Find out if we are collapsed or not.
local isCollapsed = yesno(self.args.collapse)
if isCollapsed == nil then
if self.cfg.uncollapsedRows == 'all' then
isCollapsed = false
elseif
isCollapsed = false
else
isCollapsed =
end
end
-- If we are not collapsed, re-render the collapsible notices in the
-- non-collapsed version.
if not isCollapsed then
collapsibleNoticeHtmlObjects = {}
for _, obj in ipairs(collapsibleNotices) do
table.insert(
collapsibleNoticeHtmlObjects,
self:try(obj.exportHtml, obj, self, false)
)
end
end
-- Collapsible table for actions and collapsible notices. Collapsible
-- notices are only included in the table if it is collapsed. Action rows
-- are always included.
local collapsibleTable
if isCollapsed or nActionRows > 0 then
-- Collapsible table base
:tag('tr')
:tag('td')
Line 1,002 ⟶ 1,201:
:css('width', '100%')
:tag('table')
:addClass('
:addClass(isCollapsed and 'mw-collapsible mw-collapsed' or nil)
:css('width', '100%')
:css('font-size', '90%')
-- Header row
local ctHeader = collapsibleTable
:tag('tr')
:tag('
:
-- Notice bar
if isCollapsed then
local noticeBarIcons = self:getNoticeBarIcons()
if #noticeBarIcons > 0 then
local noticeBar = ctHeader:tag('span'):css('float', 'left')
Line 1,028 ⟶ 1,223:
ctHeader:wikitext(' ')
end
end
-- Header text
if mw.site.namespaces[self.currentTitle.namespace].subject.id == 0 then
else
ctHeader:wikitext(self:message(
'milestones-header-other-ns',
self.currentTitle.subjectNsText
))
end
-- Subheadings
if nActionRows > 0 then
collapsibleTable
:tag('tr')
Line 1,055 ⟶ 1,254:
collapsibleTable:node(htmlObj)
end
end
-- Collapsible notices and current status
-- These are only included in the collapsible table if it is collapsed.
-- Otherwise, they are added afterwards, so that they align with the
-- notices.
do
local tableNode, statusColspan
if isCollapsed then
tableNode = collapsibleTable
statusColspan = 3
else
tableNode = tableRoot
statusColspan = 2
end
-- Collapsible notices
for _, obj in ipairs(collapsibleNotices) do
end
-- Current status
if
tableNode
:tag('tr')
:tag('td')
:attr('colspan',
:wikitext(self:message('status-blurb',
end
end
-- Get the categories. We have to do this before the error row, so that
-- category errors display.
local categories = self:renderCategories()
-- Error row and error category
local errors = self:getErrorMessages()
local errorCategory
if #errors > 0 then
local errorList =
:tag('tr')
:tag('td')
Line 1,090 ⟶ 1,304:
errorList:tag('li'):wikitext(msg)
end
if self:categoriesAreActive() then
errorCategory = tostring(Category.new(self:message(
'error-category'
)))
end
-- If there are no errors and no active objects, then exit. We can't make
-- this check earlier as we don't know where the errors may be until we
-- have finished rendering the banner.
elseif #self:getAllObjects() < 1 then
return ''
end
-- Add the categories
root:wikitext(categories)
root:wikitext(errorCategory)
local frame = mw.getCurrentFrame()
return frame:extensionTag{
name = 'templatestyles', args = { src = 'Module:Message box/tmbox.css' }
} .. frame:extensionTag{
name = 'templatestyles', args = { src = 'Module:Article history/styles.css' }
} .. tostring(root)
end
Line 1,139 ⟶ 1,345:
wrappers = WRAPPER_TEMPLATE
})
if frame:getTitle():find('sandbox', 1, true) then
CONFIG_PAGE = CONFIG_PAGE .. '/sandbox'
end
return p._main(args)
end
|