Module:Chronology: Difference between revisions

No edit summary
No edit summary
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'
         if post['_pageName'] ~= year .. ' archive' then
            out = out .. '* ' .. post['date'] .. ' [[' .. post['_pageName'] .. ']]\n'
        end
     end
     end
     return out
     return out
Line 61: Line 63:
         local post = posts[r]
         local post = posts[r]
         if post['_pageName'] ~= year .. ' archive' then
         if post['_pageName'] ~= year .. ' archive' then
            -- out = out .. '{{blog post summary |_pageName=' .. post['_pageName'] .. ' |categories=' .. post['categories'] .. '}}\n'
             out = out .. mw.getCurrentFrame():expandTemplate{ title = 'blog post summary', args = post }
             out = out .. mw.getCurrentFrame():expandTemplate{ title = 'blog post summary', args = post }
         end
         end

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