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 getPostsList( year )
local queryArgs = {
format = 'template',
namedArgs = 'yes',
orderBy = 'posts.date ASC',
moreResultsText = '',
limit = 1000,
where = 'YEAR(posts.date) = \'' .. year .. '\''
}
local posts = cargo.query( 'posts', '_pageName,date,location,timezone,keywords', queryArgs )
local out = ''
for r = 1, #posts do
local post = posts[r]
if post['_pageName'] ~= year .. ' archive' then
out = out .. '* ' .. post['date'] .. ' [[' .. post['_pageName'] .. ']]\n'
end
end
return out
end
function getPostsArchive( year )
local queryArgs = {
format = 'template',
namedArgs = 'yes',
orderBy = 'posts.date ASC',
moreResultsText = '',
where = 'YEAR(posts.date) = \'' .. year .. '\'',
template = 'blog_post_summary'
}
local posts = cargo.query( 'posts', '_pageName,date,location,timezone,keywords', queryArgs )
local out = ''
for r = 1, #posts do
local post = posts[r]
if post['_pageName'] ~= year .. ' archive' then
out = out .. mw.getCurrentFrame():expandTemplate{ title = 'blog post summary', args = post }
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 year.
local titleParts = mw.text.split( title.text, ' ', true )
local isArchive = #titleParts == 2 and titleParts[2] == 'archive'
local year = tonumber( titleParts[1] )
-- 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
-- Output everything.
if isArchive then
-- Year archive.
out = out .. '\n\nThis is the full archive for ' .. year .. '\n\n' .. getPostsArchive( year )
elseif year then
-- Year ToC.
out = out .. '\n\n' .. getPostsList( year )
out = out .. '\n\nSee also the [[' .. year .. ' archive|full archive of ' .. year .. ']].'
end
return out
end
-- Testing:
-- =p.main({args={title="2012"}})
return p