Module:Archive: Difference between revisions

Content deleted Content added
pushing some changes from sandbox live; details at talk page
moved sandbox changes live per talk page notes and TfD discussion; this allows the template to better handle annually archived pages
 
(2 intermediate revisions by the same user not shown)
Line 41:
return archiveTable
end
 
-- Check to see if the page is likely an annual archive. No talk pages exist
-- before 2001. Some pages have the next year created in advance.
local function isYearlyArchive(num)
local currentYear = tonumber(os.date("%Y"))
return num and num >= 2001 and num <= currentYear + 1 -- possible years
end
local function detectArchiveFormat(title, args)
-- Check if next/previous are set. Some archives swap between annual and
-- sequential archives at some point and will need to accept whatever an
-- editor says the next/previous link should be for these kind of weird
-- or unsual orders.
if args and (args.prev or args.next) then
return nil, nil, nil
end
-- Check if "/Archive 2" exists to prevent false positives on noticeboards
local archiveBase = title.baseText
local archive2Title = mw.title.new(archiveBase .. "/Archive 2")
if archive2Title and archive2Title.exists then
return nil, nil, nil -- Exit early for sequential archives
end
 
-- How is the year formatted?
local patterns = {
{pattern = "^(.-)/Archive (%d+)$", prefix = nil}, -- "Talk:Base page/Archive YYYY"
-- "nil" triggers the default. There is some kind of quirk with how
-- the module give spaces that Template:Yearly archive list cannot
-- read with either a space or the HTML space entity
{pattern = "^(.-)/Archive(%d+)$", prefix = "/Archive"}, -- "Talk:Base page/ArchiveYYYY"
{pattern = "^(.-)/Archive/(%d+)$", prefix = "/Archive/"}, -- "Talk:Base page/Archive/YYYY"
{pattern = "^(.-)/Archives/(%d+)$", prefix = "/Archives/"}, -- "Talk:Base page/Archives/YYYY"
{pattern = "^(.-)/(%d+)$", prefix = "/"} -- "Talk:Base page/YYYY"
}
 
for _, p in ipairs(patterns) do
local basePage, archiveNum = title.prefixedText:match(p.pattern)
archiveNum = tonumber(archiveNum)
if basePage and isYearlyArchive(archiveNum) then
return basePage, true, p.prefix
end
end
return nil, nil, nil
end
 
 
 
Line 80 ⟶ 125:
obj.currentTitle.baseText ..
'/' ..
obj.archivePrefix,
obj.currentArchiveNum
)
 
Line 93 ⟶ 139:
return msg
end
end
 
function Navigator:getNamespacePreposition()
-- Most talk archives are about a subject. Some will be "with" an editor
-- or "on" a noticeboard.
-- Function to get the namespace preposition
-- Namespace number where transcluded
local namespaceNumber = mw.title.getCurrentTitle().namespace
-- Preposition from table to make it easy for wikis to translate or ignore
local namespacePrepositionTable = self.cfg['namespace-prepositions']
 
-- Default preposition if not exception from above
return namespacePrepositionTable[namespaceNumber] or self.cfg["preposition-default"]
end
 
Line 105 ⟶ 165:
else
-- Set parent talk page.
local talkPageparentTalkPage = current.nsText .. ':'basePageTitle
local talkPageTitle
if args.prefix then
local pageUnderDiscussion
talkPage = talkPage .. current.baseText
-- If the parent talk page exists (and it's not the root talk page)
-- we should link to it in both the "main talk page" and
-- "discussions about" parts of the blurb.
if args.prefix or (parentTalkPage.exists and parentTalkPage.isRedirect == false and current.baseText ~= current.rootText) then
talkPageTitle = parentTalkPage.fullText
pageUnderDiscussion = talkPageTitle
-- If it doesn't, set "main talk page" to the root talk page
-- and "discussions about" to the root subject.
else
talkPagetalkPageTitle = talkPagecurrent.nsText .. current':' .. parentTalkPage.rootText
-- Set page under discussion.
pageUnderDiscussion = current.subjectNsText .. ':' .. current.rootText
-- Prepend colon for non-mainspace pages.
if current.subjectNsText ~= '' then
pageUnderDiscussion = ':' .. pageUnderDiscussion
end
end
Line 118 ⟶ 194:
end
-- What kind of blurb to use in the message box?
-- Most talk pages are "about" an article. For user talk page archives
local function getBlurbKey(args)
-- use "with" instead as User:X will also be a participant. User talk
if args.type == 'index' then
-- archives will be found in both User and User talk namespaces (2,3).
-- For manually-indexed archives only
-- For noticeboards and wikiprojects use "on" as the discussions are
return 'blurb-index', args.type
-- typically not about the noticeboard itself (4,5).
elseif args.type == 'annual' then
local namespacePreposition = "about"
-- Grab the year of the current archive.
if current:inNamespaces(2, 3) == true then
return 'blurb-annual', mw.getCurrentFrame():expandTemplate {
namespacePreposition = "with"
title = 'Title year', args = { page = current.fullText }
elseif current:inNamespaces(4, 5) == true then
}
namespacePreposition = "on"
elseif args.period then
return 'blurb-period', args.period
else
return 'blurb-noperiod', ''
end
end
 
-- Generate a blurb from Module:Archive/config
-- Set page under discussion.
local blurbKey, argValue = getBlurbKey(args)
local subjectPage = current.subjectNsText .. ':' .. current.rootText
local namespacePreposition = self:getNamespacePreposition()
-- Prepend colon for non-mainspace pages.
ret = self:message(blurbKey, talkPageTitle, pageUnderDiscussion, argValue, namespace, namespacePreposition)
if current.subjectNsText ~= '' then
 
subjectPage = ':' .. subjectPage
end
subjectPage = makeWikilink(subjectPage)
if args.type == 'index' then
-- For manually-indexed archives only
ret = self:message('blurb-index', talkPage, subjectPage,
args.type, namespace, namespacePreposition)
elseif args.period then
ret = self:message('blurb-period', talkPage, subjectPage,
args.period, namespace, namespacePreposition)
else
ret = self:message('blurb-noperiod', talkPage, subjectPage,
'', namespace, namespacePreposition)
end
end
return ret
Line 351 ⟶ 418:
local boxComponents
-- Is |omit filled? If not, make the whole box.
if args.omit == nil then
 
boxComponents = self:makeMessageBox() .. '\n' .. self:makeArchiveLinksWikitable()
-- Check for annual archives
-- We're omitting the banner, so we should only make the links table.
local currentTitle = self.currentTitle
local yearlyBase, isYearly, yearlyPattern = detectArchiveFormat(currentTitle, args)
 
if isYearly then
-- Use a yearly archive format
local linksYearlyListSeparator = "&#32;" -- Space separator
local linksYearlyList = mw.getCurrentFrame():expandTemplate {
title = 'Yearly archive list',
args = {
root = yearlyBase,
sep = linksYearlyListSeparator,
prefix = yearlyPattern
}
}
boxComponents = self:makeMessageBox() .. '\n' ..
'<div style="font-size:115%; width:100%; word-spacing:1em; text-align:center;">' .. linksYearlyList .. '</div>'
else
-- Default numbered archive format
boxComponents = self:makeMessageBox() .. '\n' .. self:makeArchiveLinksWikitable()
end -- We're omitting the banner, so we should only make the links table.
elseif args.omit == 'banner' then
boxComponents = self:makeArchiveLinksWikitable()
Line 361 ⟶ 448:
boxComponents = self:makeMessageBox()
end
 
-- Allow for demo pages to be edited freely.
if not args.demospace then
boxComponents = boxComponents .. ' __NONEWSECTIONLINK__ __NOEDITSECTION__ __ARCHIVEDTALK__'
end
 
return boxComponents
end