Module:Article history: Difference between revisions

Content deleted Content added
cache all of the things that used to be called with maybeCallFunc
undo memoization changes in the Row object, and instead memoize errors in CollapsibleNotice:exportHtml, which is what was actually getting called more than once
Line 35:
local function makeUrlLink(url, display)
return string.format('[%s %s]', url, display)
end
 
local function maybeCallFunc(val, ...)
-- Checks whether val is a function, and if so calls it with the specified
-- arguments. Otherwise val is returned as-is. The result is cached with
if type(val) == 'function' then
ret =return val(...)
else
return retval
end
end
 
Line 120 ⟶ 130:
obj.makeData = data.makeData -- used by Row:getData
return obj
end
 
function Row:cacheResult(cacheKey, val, ...)
-- Checks whether val is a function, and if so calls it with the specified
-- arguments. Otherwise val is returned as-is. The result is cached with
-- a key of cacheKey.
if self[cacheKey] ~= nil then
return self[cacheKey]
end
local ret
if type(val) == 'function' then
ret = val(...)
else
ret = val
end
self[cacheKey] = ret
return ret
end
 
Line 162 ⟶ 155:
 
function Row:getIcon(articleHistoryObj)
return self:cacheResultmaybeCallFunc('_iconCache', self.icon, articleHistoryObj, self)
end
 
function Row:getIconCaption(articleHistoryObj)
return maybeCallFunc(self.iconCaption, articleHistoryObj, self)
return self:cacheResult(
'_iconCaptionCache',
self.iconCaption,
articleHistoryObj,
self
)
end
 
Line 201 ⟶ 189:
 
function Row:getNoticeBarIcon(articleHistoryObj)
local icon = maybeCallFunc(self.noticeBarIcon, articleHistoryObj, self)
local icon = self:cacheResult(
'_noticeBarIconCache',
self.noticeBarIcon,
articleHistoryObj,
self
)
if icon == true then
icon = self:getIcon(articleHistoryObj)
Line 220 ⟶ 203:
 
function Row:getNoticeBarIconCaption(articleHistoryObj)
local caption = self:cacheResultmaybeCallFunc(
'_noticeBarIconCaption',
self.noticeBarIconCaption,
articleHistoryObj,
Line 253 ⟶ 235:
 
function Row:getText(articleHistoryObj)
return self:cacheResultmaybeCallFunc('_textCache', self.text, articleHistoryObj, self)
end
 
Line 609 ⟶ 591:
 
function Action:getName(articleHistoryObj)
return maybeCallFunc(self.actionCfg.name, articleHistoryObj, self)
return self:cacheResult(
'_nameCache',
self.actionCfg.name,
articleHistoryObj,
self
)
end
 
function Action:getResult(articleHistoryObj)
return self:cacheResultmaybeCallFunc(
'_resultCache',
self.actionCfg.results[self.resultId].text,
articleHistoryObj,
Line 704 ⟶ 680:
 
function CollapsibleNotice:getCollapsibleText(articleHistoryObj)
return maybeCallFunc(self.collapsibleText, articleHistoryObj, self)
return self:cacheResult(
'_collapsibleTextCache',
self.collapsibleText,
articleHistoryObj,
self
)
end
 
Line 725 ⟶ 696:
 
function CollapsibleNotice:exportHtml(articleHistoryObj, isInCollapsibleTable)
return if self._htmlCache[cacheKey].isError then
return nil
end
local cacheKey = isInCollapsibleTable and 'collapsible' or 'default'
iflocal html = self._htmlCache[cacheKey] then
if html then
return self._htmlCache[cacheKey]
return html
elseif html == false then
return nil
end
 
local textfunction = self:getTextcachedTry(articleHistoryObjfunc, ...)
local success, val = pcall(func, ...)
if success then
ret = return val
else
self._htmlCache.isError = true
self:raiseError(val)
end
end
 
local text = cachedTry(self.getText, self, articleHistoryObj)
if not text then
if self.htmlCache[cacheKey] ~= nil thenfalse
return nil
end
Line 767 ⟶ 755:
end
 
local html = mw.html.create('tr')
local icon = cachedTry(self:.renderIcon(, self, articleHistoryObj)
local collapsibleText = cachedTry(
self:.getCollapsibleText(,
self,
articleHistoryObj)
)
if isInCollapsibleTable then
local textCell = html:tag('td')