Module:Chronology: Difference between revisions

No edit summary
No edit summary
(2 intermediate revisions by the same user not shown)
Line 1: Line 1:
local lang = mw.language.getContentLanguage()
local lang = mw.language.getContentLanguage()
local cargo = mw.ext.cargo


function getNav( year, monthNum )
function getNav( year, monthNum )
Line 29: Line 30:
end
end


function getPosts( year, monthNum )
function getPostsList( year, monthNum )
    local cargo = mw.ext.cargo
     local queryArgs = {
     local queryArgs = {
         format = 'template',
         format = 'template',
Line 42: Line 42:
     for r = 1, #posts do
     for r = 1, #posts do
         local post = posts[r]
         local post = posts[r]
         out = out .. '* ' .. post['date'] .. ' [[' .. post['_pageName'] .. ']]\n\n'
         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 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 .. mw.getCurrentFrame():expandTemplate{ title = 'blog post summary', args = post }
        end
     end
     end
     return out
     return out
Line 57: Line 79:


     -- Figure out the date parts.
     -- Figure out the date parts.
     local parts = mw.text.split( title.text, ' ', true )
     local titleParts = mw.text.split( title.text, ' ', true )
     local year = lang:formatDate( 'Y', title.text )
     local year = false
     local monthNum = false
     local monthNum = false
     if #parts == 2 then
     if #titleParts == 2 and titleParts[2] ~= 'archive' then
         monthNum = lang:formatDate( 'n', title.text )
        -- 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
     end


Line 67: Line 94:
     local out = getNav( year, monthNum )
     local out = getNav( year, monthNum )
     if monthNum then
     if monthNum then
         out = out .. '\n\n' .. getPosts( year, monthNum )
        -- 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
     else
         out = out .. '\n\n@TODO: This is a year.'
        -- Year only
         out = out .. '\n\nSee also the [[' .. year .. ' archive|full archive of ' .. year .. ']].'
     end
     end
     return out
     return out
end
end


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

Revision as of 22:51, 2 April 2020

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 .. ' &middot;&nbsp;'
        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 .. ' &middot; '
    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]
        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 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 .. 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 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