Module:Archive: Difference between revisions

Content deleted Content added
Per talk, this will use the yellow Template:tmbox in talk namespace and grey Template:mbox elsewhere.
moved sandbox changes live per talk page notes and TfD discussion; this allows the template to better handle annually archived pages
 
(15 intermediate revisions by 4 users not shown)
Line 4:
-- This module produces a talk archive banner, together with an automatically-
-- generated list of navigation links to other archives of the talk page in
-- question. It implements {{Automatic archive navigatorArchive}} and.
-- {{Talk archive navigation}}.
-------------------------------------------------------------------------------
 
Line 27 ⟶ 26:
return s
end
 
local function makeTable(width)
local archiveTable = mw.html.create('table')
archiveTable
:css({
['max-width'] = width,
['margin'] = '0 auto 0.5em',
['text-align'] = 'center'
})
-- Set width so that the table doesn't spill out on narrower skins
-- or when zooming in. It has to be defined multiple times because
-- "stretch" is experimental.
:cssText('width:100%;width:-moz-available;width:-webkit-fill-available;width:stretch')
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 65 ⟶ 125:
obj.currentTitle.baseText ..
'/' ..
obj.archivePrefix,
obj.currentArchiveNum
)
 
Line 78 ⟶ 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
 
function Navigator:makeBlurb()
local args = self.args
local current = self.currentTitle
if args[1] == '1' then
local ret
-- The old template used "|1" to suppress the blurb.
return ''
-- Skip if user provides their own blurb.
if args.text then
ret = args.text
else
-- Set parent talk page.
local ret
local parentTalkPage = current.basePageTitle
if args.text then
local talkPageTitle
ret = args.text
local pageUnderDiscussion
-- 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
local talkPagetalkPageTitle = self.currentTitlecurrent.nsText .. ':' .. parentTalkPage.rootText
-- Set page under discussion.
if args.prefix then
pageUnderDiscussion = current.subjectNsText .. ':' .. current.rootText
talkPage = talkPage .. self.currentTitle.baseText
else
talkPage = talkPage .. self.currentTitle.rootText
end
-- Prepend colon for non-mainspace pages.
local namespace
if selfcurrent.currentTitle.isTalkPagesubjectNsText =~= true'' then
pageUnderDiscussion = ':' .. pageUnderDiscussion
namespace = "talk"
else
namespace = "main"
end
if args.period then
ret = self:message('blurb-period', talkPage, args.period, namespace)
else
ret = self:message('blurb-noperiod', talkPage, namespace)
end
end
-- Check current namespace for blurb.
local namespace = 'main'
if current.isTalkPage == true then
namespace = 'talk'
end
-- What kind of blurb to use in the message box?
local function getBlurbKey(args)
if args.type == 'index' then
-- For manually-indexed archives only
return 'blurb-index', args.type
elseif args.type == 'annual' then
-- Grab the year of the current archive.
return 'blurb-annual', mw.getCurrentFrame():expandTemplate {
title = 'Title year', args = { page = current.fullText }
}
elseif args.period then
return 'blurb-period', args.period
else
return 'blurb-noperiod', ''
end
end
 
-- Generate a blurb from Module:Archive/config
local blurbKey, argValue = getBlurbKey(args)
local namespacePreposition = self:getNamespacePreposition()
ret = self:message(blurbKey, talkPageTitle, pageUnderDiscussion, argValue, namespace, namespacePreposition)
 
end
return ret
end
return ret
end
 
function Navigator:makeMessageBox()
local args = self.args
local image
if args.image then
Line 130 ⟶ 234:
end
 
-- Hardcode tmbox style on the template's page.
-- PS: Needs to be changed if the template is renamed!
local mainTemplatePage = ''
if self.currentTitle.fullText == 'Template:Archive' then
mainTemplatePage = 'talk'
end
local mbox = require('Module:Message box').main('mbox', {
demospace = args.demospace or mainTemplatePage,
image = image,
imageright = args.imageright,
style = args.style or '',
textstyle = args.textstyle or 'text-align:center',
text = self:makeBlurb(),
})
 
Line 216 ⟶ 328:
 
function Navigator:makeArchiveLinksWikitable()
local args = self.args
local lang = mw.language.getContentLanguage()
local nums = self:getArchiveNums()
local noLinks = #nums
-- Skip number processing if |prev and |next are defined.
if args.prev or args.next then
local archives = {}
if args.prev then archives[#archives + 1] = mw.title.new(args.prev) end
archives[#archives + 1] = self.currentTitle
if args.next then archives[#archives + 1] = mw.title.new(args.next) end
local table = makeTable('30em')
for _, title in ipairs(archives) do
if tostring(title) == self.currentTitle.prefixedText then
table:tag("td"):wikitext(string.format(
'<span style="font-size:115%%;">%s</span>',
makeWikilink(title.fullText, title.subpageText)
))
else
table:tag("td"):wikitext(
makeWikilink(title.fullText, title.subpageText)
)
end
end
return tostring(table)
end
if noLinks < 1 then
return ''
Line 262 ⟶ 398:
 
-- Output the wikitable.
local ret = {}
local width
if noLinks <= 3 then
Line 269 ⟶ 404:
width = string.format('%dem', (noLinks + 3) * 5)
else
width = '50em37em'
end
local table = makeTable(width)
ret[#ret + 1] = string.format(
for _, s in ipairs(links) do
'{| style="width:%s;background:transparent;' ..
table:tag("td"):wikitext(s)
'margin:0 auto 0.5em;text-align:center"',
width
)
for i, s in ipairs(links) do
if i % 20 == 1 then
ret[#ret + 1] = '\n|-'
end
ret[#ret + 1] = '\n| '
ret[#ret + 1] = s
end
ret[#ret + 1] = '\n|}'
return table.concattostring(rettable)
end
 
function Navigator:__tostring()
local args = self.args
return self:makeMessageBox() ..
local boxComponents
'\n' ..
self:makeArchiveLinksWikitable() ..
-- Is |omit filled? If not, make the whole box.
' __NONEWSECTIONLINK__ __NOEDITSECTION__ __ARCHIVEDTALK__'
if args.omit == nil then
 
-- Check for annual archives
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()
-- We're omitting the archives, so we should only make the banner.
elseif args.omit == 'archives' then
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
 
Line 307 ⟶ 470:
 
function p._aan(args, cfg, currentTitle)
cfg = cfg or mw.loadData('Module:Automatic archive navigatorArchive/config')
currentTitle = currentTitle or mw.title.getCurrentTitle()
local aan = Navigator.new(args, cfg, currentTitle)
Line 315 ⟶ 478:
function p.aan(frame)
local args = require('Module:Arguments').getArgs(frame, {
wrappers = 'Template:Automatic archive navigatorArchive',
})
return p._aan(args)