Module:Article history/config: Difference between revisions

Content deleted Content added
make the article name available to the featured topic categories function
add ITN and OTD and generalise the isActive, data and text functions as helper functions
Line 50:
return title
end
end
 
local function isActiveDatedObject(articleHistoryObj, prefix)
local args = articleHistoryObj.args
local prefixArgs = articleHistoryObj.prefixArgs
return args[prefix .. 'date'] or args[prefix .. 'date2'] or prefixArgs[prefix]
end
 
local function makeDateData(articleHistoryObj, prefix, suffixes)
local args = articleHistoryObj.args
local prefixArgs = articleHistoryObj.prefixArgs
 
-- Sanity checks
if prefixArgs[prefix] then
for _, t in ipairs(prefixArgs[prefix]) do
if not t.date then
articleHistoryObj:raiseError(
string.format(
"an argument starting with '%s%d' was detected, " ..
"but no '%s%ddate' parameter was specified",
prefix, t[1],
prefix, t[1]
),
'Template:Article history#Errors'
)
end
end
end
 
local function validateDate(key, date)
local longDate = getLongDate(date)
if longDate then
return longDate
else
articleHistoryObj:raiseError(
string.format(
"invalid date '%s' detected in parameter '%s'",
tostring(date),
key
),
'Template:Article history#Errors'
)
end
end
 
local data = {}
 
-- Organise the input
if args[prefix .. 'date'] then
local t = {}
do
local key = prefix .. 'date'
t.date = validateDate(key, args[key])
end
for _, suffix in ipairs(suffixes) do
local key = prefix .. suffix
t[suffix] = args[key]
end
data[#data + 1] = t
end
if args[prefix .. 'date2'] then
local t, oldKeys, newKeys = {}, {}, {}
do
local oldKey = prefix .. 'date2'
local newKey = prefix .. '2date'
t.date = validateDate(oldKey, args[oldKey])
table.insert(oldKeys, "'" .. oldKey .. "'")
table.insert(newKeys, "'" .. newKey .. "'")
end
for i, suffix in ipairs(suffixes) do
local oldKey = prefix .. suffix .. '2'
local newKey = prefix .. '2' .. suffix
table.insert(oldKeys, "'" .. oldKey .. "'")
table.insert(newKeys, "'" .. newKey .. "'")
t[suffix] = args[oldKey]
end
data[#data + 1] = t
articleHistoryObj:addWarning(
string.format(
"the %s parameters are deprecated; use %s instead",
mw.text.listToText(oldKeys),
mw.text.listToText(newKeys)
),
'Template:Article history#Errors'
)
end
if prefixArgs[prefix] then
for _, prefixData in ipairs(prefixArgs[prefix]) do
local t = {}
do
local key = prefix .. tostring(prefixData[1]) .. 'date'
t.date = validateDate(key, args[key])
end
for i, suffix in ipairs(suffixes) do
local key = prefix .. tostring(prefixData[1]) .. suffix
t[suffix] = args[key]
end
data[#data + 1] = t
end
end
if #data < 1 then
error(string.format(
"no data items found for prefix '%s' and parameter checks failed'",
tostring(prefix)
))
end
 
return data
end
 
local function makeDateText(dateData, blurb)
-- This makes the text for Main Page features such as DYKs and ITNs for the
-- dates contained in dateData (made with the makeDateData function).
-- The parameter $1 in the blurb will be replaced with the list of dates.
local dates = {}
for i, t in ipairs(dateData) do
if t.link then
dates[i] = string.format('on [[%s|%s]]', t.link, t.date)
else
dates[i] = string.format('on %s', t.date)
end
end
local dateList = mw.text.listToText(dates, ', ', ', and ')
return mw.message.newRawMessage(blurb, dateList):plain()
end
 
Line 1,479 ⟶ 1,603:
icon = 'DYK questionmark icon.svg',
iconCaption = 'Did You Know',
iconSmallSize = '15px',
isActive = function (articleHistoryObj)
return isActiveDatedObject(articleHistoryObj, 'dyk')
local args = articleHistoryObj.args
local prefixArgs = articleHistoryObj.prefixArgs
return args.dykdate or args.dykdate2 or prefixArgs.dyk
end,
makeData = function (articleHistoryObj)
return makeDateData(articleHistoryObj, 'dyk', {'entry'})
local args = articleHistoryObj.args
local prefixArgs = articleHistoryObj.prefixArgs
 
-- Sanity checks
if prefixArgs.dyk then
for _, t in ipairs(prefixArgs.dyk) do
if not t.date then
articleHistoryObj:raiseError(
string.format(
"an argument starting with 'dyk%d' was detected, " ..
"but no 'dyk%ddate' parameter was specified",
t[1], t[1]
),
'Template:Article history#Errors'
)
end
end
end
 
local function validateDate(key, date)
local longDate = getLongDate(date)
if longDate then
return longDate
else
articleHistoryObj:raiseError(
string.format(
"invalid date '%s' detected in parameter '%s'",
tostring(date),
key
),
'Template:Article history#Errors'
)
end
end
 
local data = {}
 
-- Organise the input
if args.dykdate then
data[#data + 1] = {
date = validateDate('dykdate', args.dykdate),
entry = args.dykentry
}
end
if args.dykdate2 then
data[#data + 1] = {
date = validateDate('dykdate2', args.dykdate2),
entry = args.dykentry2
}
articleHistoryObj:addWarning(
"the 'dykdate2' and 'dykentry2' parameters are " ..
"deprecated; use 'dyk2date' and 'dyk2entry' instead",
'Template:Article history#Errors'
)
end
if prefixArgs.dyk then
for _, t in ipairs(prefixArgs.dyk) do
data[#data + 1] = {
date = validateDate('dyk' .. t[1] .. 'date', t.date),
entry = t.entry
}
end
end
if #data < 1 then
error('no DYKs found and parameter checks failed')
end
 
return data
end,
text = function (articleHistoryObj, collapsibleNoticeObj)
local data = collapsibleNoticeObj:getData(articleHistoryObj)
local textraPage = {}'Wikipedia:Recent additions/'
.. getDate('Y/F#j F Y', data[1].date)
 
local raPage
do
raPage = 'Wikipedia:Recent additions/'
.. getDate('Y/F#j F Y', data[1].date)
local raTitle = makeTitle(raPage)
if not raTitle or not raTitle.exists then
Line 1,567 ⟶ 1,620:
end
end
local blurb = string.format(
 
"A [[%s|'''fact from this article''']] appeared on " ..
text[#text + 1] = string.format(
"AWikipedia's [[%s|'''factMain from this article'''Page]] appearedin on Wikipedia'sthe " ..
"[[Main Page]] in the ''\"[[:Template:Did you know|Did you know?]]\"'' column ", ..
"column on $1.",
raPage
)
return makeDateText(data, blurb)
 
local dates = {}
for i, t in ipairs(data) do
dates[i] = 'on ' .. t.date
end
text[#text + 1] = mw.text.listToText(dates, ', ', ', and ')
text[#text + 1] = '.'
return table.concat(text)
end,
collapsibleText = function (articleHistoryObj, collapsibleNoticeObj)
Line 1,639 ⟶ 1,685:
return cats
end
},
 
-- ITN
{
id = 'ITN',
isActive = function (articleHistoryObj)
return isActiveDatedObject(articleHistoryObj, 'itn')
end,
makeData = function (articleHistoryObj)
return makeDateData(articleHistoryObj, 'itn', {'link'})
end,
icon = 'Globe current.svg',
iconCaption = 'In the news',
text = function (articleHistoryObj, collapsibleNoticeObj)
local data = collapsibleNoticeObj:getData(articleHistoryObj)
local blurb = "A news item involving this article was featured " ..
"on Wikipedia's [[Main Page]] in the " ..
"''\"[[Template:In the news|In the news]]\"'' column $1."
return makeDateText(data, blurb)
end,
categories = {'Wikipedia In the news articles'}
},
 
-- On this day
{
id = 'OTD',
isActive = function (articleHistoryObj)
return isActiveDatedObject(articleHistoryObj, 'otd')
end,
makeData = function (articleHistoryObj)
return makeDateData(articleHistoryObj, 'otd', {'link'})
end,
icon = 'Nuvola apps date.svg',
iconCaption = 'On this day...',
text = function (articleHistoryObj, collapsibleNoticeObj)
local data = collapsibleNoticeObj:getData(articleHistoryObj)
local blurb = "A fact from this article was featured " ..
"on Wikipedia's [[Main Page]] in the " ..
"''\"[[Wikipedia:Selected anniversaries|On this day...]]\"'' " ..
"column $1."
return makeDateText(data, blurb)
end,
categories = {'Selected anniversaries articles'}
}
},
Line 1,688 ⟶ 1,777:
-- The default size for collapsible status icons for small templates. The
-- default is 30px.
defaultSmallCollapsibleNoticeIconSize = '15px20px',
 
-------------------------------------------------------------------------------