This is the core of the blog archives system on this wiki. See 2024 for an example of it in use.
See also
local lang = mw.language.getContentLanguage()
local cargo = mw.ext.cargo
function getNav( year, monthNum )
-- Year list.
local y = 1998
local currYear = tonumber( lang:formatDate( 'Y' ) )
local out = '·'
while y <= currYear do
local yearLink = '[[' .. y .. ']]'
if year == y then
yearLink = '<strong>' .. y .. '</strong>'
end
out = out .. yearLink .. ' · '
y = y + 1
end
-- Month list.
out = out .. '\n\n' .. year .. ': '
for m = 1, 12 do
local monthName = lang:formatDate( 'F', '2019-' .. m .. '-01' )
local monthLink = '[[' .. monthName .. ' ' .. year .. '|' .. monthName .. ']]'
if currYear == y and m == monthNum then
monthLink = '<strong>X ' .. monthName .. '</strong>'
end
out = out .. monthLink .. ' · '
end
return out
end
function getPostsList( year, monthNum )
local queryArgs = {
format = 'template',
namedArgs = 'yes',
orderBy = 'posts.date ASC',
moreResultsText = '',
where = 'YEAR(posts.date) = \'' .. year .. '\' AND MONTH(posts.date) = \'' .. monthNum .. '\''
}
local posts = cargo.query( 'posts', '_pageName,date,location,timezone,categories', queryArgs )
local out = ''
for r = 1, #posts do
local post = posts[r]
out = out .. '* ' .. post['date'] .. ' [[' .. post['_pageName'] .. ']]\n'
end
return out
end
function getPostsArchive( year )
local queryArgs = {
format = 'template',
namedArgs = 'yes',
orderBy = 'posts.date DESC',
moreResultsText = '',
where = 'YEAR(posts.date) = \'' .. year .. '\'',
template = 'blog_post_summary'
}
local posts = cargo.query( 'posts', '_pageName,date,location,timezone,categories', queryArgs )
local out = ''
for r = 1, #posts do
local post = posts[r]
if post['_pageName'] ~= year .. ' archive' then
out = out .. '{{blog post summary|_pageName=' .. post['_pageName'] .. ' |categories=' .. post['categories'] .. '}}\n'
end
end
return out
end
local p = {}
p.main = function ( frame )
-- Get the title (current page name or supplied argument).
local title = mw.title.getCurrentTitle()
if frame ~= nil and frame.args ~= nil and frame.args.title ~= nil then
title = { text = frame.args.title }
end
-- Figure out the date parts.
local titleParts = mw.text.split( title.text, ' ', true )
local year = false
local monthNum = false
if #titleParts == 2 and titleParts[2] ~= 'archive' then
-- Month Year
year = lang:formatDate( 'Y', titleParts[2] )
monthNum = lang:formatDate( 'n', titleParts[1] )
else
-- Year only, or year archive
year = lang:formatDate( 'Y', titleParts[1] )
end
-- Put the output together.
local out = getNav( year, monthNum )
if monthNum then
-- Month year
out = out .. '\n\n' .. getPostsList( year, monthNum )
elseif titleParts[2] == 'archive' then
-- Year archive
out = out .. '\n\nThis is the full archive for ' .. year .. '\n\n' .. getPostsArchive( year )
else
-- Year only
out = out .. '\n\nSee also the [[' .. year .. ' archive|full archive of ' .. year .. ']].'
end
return out
end
-- Testing:
-- =p.main({args={title="2012"}})
return p