Module:Chronology

All modules: book, chronology, kbd, module, photos, post, todo, wikidata link, yesno.


This is the core of the blog archives system on this wiki. See 2026 for an example of it in use.


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( where, order_by, limit )
    local queryArgs = {
        format = 'template',
        namedArgs = 'yes',
        orderBy = order_by,
        moreResultsText = '',
        where = where,
        limit = limit,
        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]
        out = out .. '<div class="mdl-chronology-archive">'
        	.. '<div class="mdl-chronology-archive-header"></div>'
        	.. '<hr>'
        	.. '<h2>[[' .. post['_pageName'] .. ']]</h2>'
        	.. '<div>' .. mw.getCurrentFrame():expandTemplate{ title = ':' .. post['_pageName'] } .. '</div>'
        	.. '</div>'
    end
	out = out .. mw.getCurrentFrame():extensionTag( 'templatestyles', '', { src = "Module:Chronology/style.css" } )
    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 = '&middot;'
    while y <= currYear do
        local yearLink = '[[' .. y .. ']]'
        if year == y then
            yearLink = '<strong>' .. y .. '</strong>'
        end
        out = out .. yearLink .. ' &middot;&nbsp;'
        y = y + 1
    end

	-- Output everything.
    if isArchive then
        -- Year archive.
        mw.ext.displaytitle.set( year .. ' archive' )
        out = out .. '\n\nThis is the full archive for ' .. year .. '\n\n'
        	.. getPostsArchive( 'YEAR(posts.date) = \'' .. year .. '\'', 'posts.date ASC', 10000 )
    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

p.recent = function ( frame )
    return getPostsArchive( '', 'posts.date desc', 15 )
end

-- Testing:
--   =p.main({args={title="2012"}})
return p