Difference between revisions of "Module:Current events monthly archive"
Jump to navigation
Jump to search
blackwiki>RossO (Legacy Subroutines removed) |
blackwiki>RossO (Beta 1 version. Ready for review.) |
||
| Line 1: | Line 1: | ||
| + | |||
-- This module renders the introductory content on Monthly archive pages for the [[Portal:Current events]]. | -- This module renders the introductory content on Monthly archive pages for the [[Portal:Current events]]. | ||
| − | + | ||
| − | |||
--[[ | --[[ | ||
| − | + | ||
| − | + | Incoming expected variables: | |
| − | + | frame.args.year = Integer value for year | |
| + | frame.args.month = Integer value for month, 1 based. | ||
| + | |||
| + | Test this against: | ||
| + | Portal:Current events/September 2011/Sandbox | ||
| + | User:RossO/sandbox/Current events monthly intro | ||
| + | |||
--]] | --]] | ||
| Line 11: | Line 17: | ||
function p.main(frame) | function p.main(frame) | ||
| − | + | local isArchive = false | |
| − | local | ||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
if (frame and frame.args and frame.args.year and frame.args.month) then | if (frame and frame.args and frame.args.year and frame.args.month) then | ||
| − | + | isArchive = true | |
| − | |||
| − | |||
| − | |||
end | end | ||
| − | local dateStuff = p.getDateStuff( | + | local argsYear = frame.args.year or os.date( "%Y" ) |
| + | local argsMonth = frame.args.month or os.date( "%m" ) | ||
| + | |||
| + | local dateStuff = p.getDateStuff(argsMonth, argsYear, isArchive) | ||
return p.export(dateStuff) | return p.export(dateStuff) | ||
end | end | ||
| − | |||
--[[ | --[[ | ||
| − | + | Purpose: Construct the dateStuff object. | |
| − | |||
| − | |||
Reference: https://www.mediawiki.org/wiki/Help:Extension:ParserFunctions#.23time | Reference: https://www.mediawiki.org/wiki/Help:Extension:ParserFunctions#.23time | ||
--]] | --]] | ||
| + | |||
| + | function p.getDateStuff(monthNumber, yearNumber, isArchive) | ||
-- Gets date data. | -- Gets date data. | ||
local dateStuff = {} | local dateStuff = {} | ||
| + | local constructedDate = yearNumber .. "-" .. monthNumber .. "-01" -- Construct a date, YYY-M-DD format. | ||
local lang = mw.language.getContentLanguage() | local lang = mw.language.getContentLanguage() | ||
| − | + | ||
| + | local beVerb = "is" | ||
| + | if (isArchive) then | ||
| + | beVerb = "was" | ||
| + | end | ||
dateStuff.beVerb = beVerb | dateStuff.beVerb = beVerb | ||
| − | --Year | + | --Year and Leap Year |
| − | + | dateStuff.year = tonumber(yearNumber) | |
| − | + | local leapBool = tonumber(lang:formatDate('L', constructedDate)) | |
| − | |||
| − | |||
| − | local leapBool = tonumber(lang:formatDate('L', | ||
dateStuff.leapBool = leapBool | dateStuff.leapBool = leapBool | ||
local leapDesc = "common" | local leapDesc = "common" | ||
| Line 57: | Line 58: | ||
-- Month and Name | -- Month and Name | ||
| − | local monthName = lang:formatDate('F', | + | local monthName = lang:formatDate('F', constructedDate) |
dateStuff.monthName = monthName | dateStuff.monthName = monthName | ||
dateStuff.monthNumber = tonumber(monthNumber) | dateStuff.monthNumber = tonumber(monthNumber) | ||
| − | -- Month | + | -- First Day of the Month |
| − | + | local firstOfMonth = lang:formatDate('01-m-Y', constructedDate) | |
| − | local firstOfMonth = lang:formatDate('01-m-Y', | ||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
-- Weekday of the first day of the month | -- Weekday of the first day of the month | ||
| Line 82: | Line 71: | ||
-- Days in month | -- Days in month | ||
local daysInMonth = lang:formatDate('j', firstOfMonth .. ' +1 month -1 day') | local daysInMonth = lang:formatDate('j', firstOfMonth .. ' +1 month -1 day') | ||
| − | daysInMonth = tonumber(daysInMonth) | + | dateStuff.daysInMonth = tonumber(daysInMonth) |
| − | |||
-- Weekday of the last day of the month | -- Weekday of the last day of the month | ||
| Line 93: | Line 81: | ||
function p.export(dateStuff) | function p.export(dateStuff) | ||
| − | + | ||
| + | --[[ | ||
| + | Purpose: Construct WikiMarkup for the Intro paragraph. | ||
| + | Reference: https://www.mediawiki.org/wiki/Help:Extension:ParserFunctions#.23time | ||
| + | --]] | ||
| + | |||
local monthOrdinal = {"first", "second", "third", "fourth", "fifth", "sixth", "seventh", "eighth", "ninth", "tenth", "eleventh", "twelfth and final"} | local monthOrdinal = {"first", "second", "third", "fourth", "fifth", "sixth", "seventh", "eighth", "ninth", "tenth", "eleventh", "twelfth and final"} | ||
local root = mw.html.create('div') | local root = mw.html.create('div') | ||
| − | |||
| − | |||
| − | |||
:tag('p') | :tag('p') | ||
:wikitext("'''''[[" .. dateStuff.monthName .. "]] [[" .. dateStuff.year .. "]]''' " .. dateStuff.beVerb .. " the " .. monthOrdinal[dateStuff.monthNumber] .. " month of the " .. dateStuff.leapDesc .. " year. ") | :wikitext("'''''[[" .. dateStuff.monthName .. "]] [[" .. dateStuff.year .. "]]''' " .. dateStuff.beVerb .. " the " .. monthOrdinal[dateStuff.monthNumber] .. " month of the " .. dateStuff.leapDesc .. " year. ") | ||
:wikitext("The calendar for the month begins on a [[" .. dateStuff.firstDayOfMonthName .. "]] and ends on a [[" .. dateStuff.lastDayOfMonthName .. "]] after " .. dateStuff.daysInMonth .. " days.''") | :wikitext("The calendar for the month begins on a [[" .. dateStuff.firstDayOfMonthName .. "]] and ends on a [[" .. dateStuff.lastDayOfMonthName .. "]] after " .. dateStuff.daysInMonth .. " days.''") | ||
:done() | :done() | ||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
return tostring(root) | return tostring(root) | ||
Revision as of 19:07, 29 September 2017
Usage
{{#invoke:Current events monthly archive|main}}
{{#invoke:Current events monthly archive|main|year=1996|month=12}}
This will eventually be extended to support the layout components for the Portal:Current events archive pages.
-- This module renders the introductory content on Monthly archive pages for the [[Portal:Current events]].
--[[
Incoming expected variables:
frame.args.year = Integer value for year
frame.args.month = Integer value for month, 1 based.
Test this against:
Portal:Current events/September 2011/Sandbox
User:RossO/sandbox/Current events monthly intro
--]]
local p = {}
function p.main(frame)
local isArchive = false
if (frame and frame.args and frame.args.year and frame.args.month) then
isArchive = true
end
local argsYear = frame.args.year or os.date( "%Y" )
local argsMonth = frame.args.month or os.date( "%m" )
local dateStuff = p.getDateStuff(argsMonth, argsYear, isArchive)
return p.export(dateStuff)
end
--[[
Purpose: Construct the dateStuff object.
Reference: https://www.mediawiki.org/wiki/Help:Extension:ParserFunctions#.23time
--]]
function p.getDateStuff(monthNumber, yearNumber, isArchive)
-- Gets date data.
local dateStuff = {}
local constructedDate = yearNumber .. "-" .. monthNumber .. "-01" -- Construct a date, YYY-M-DD format.
local lang = mw.language.getContentLanguage()
local beVerb = "is"
if (isArchive) then
beVerb = "was"
end
dateStuff.beVerb = beVerb
--Year and Leap Year
dateStuff.year = tonumber(yearNumber)
local leapBool = tonumber(lang:formatDate('L', constructedDate))
dateStuff.leapBool = leapBool
local leapDesc = "common"
if (leapBool == 1) then
leapDesc = "leap"
end
dateStuff.leapDesc = leapDesc
-- Month and Name
local monthName = lang:formatDate('F', constructedDate)
dateStuff.monthName = monthName
dateStuff.monthNumber = tonumber(monthNumber)
-- First Day of the Month
local firstOfMonth = lang:formatDate('01-m-Y', constructedDate)
-- Weekday of the first day of the month
local firstDayOfMonthName = lang:formatDate('l', firstOfMonth)
dateStuff.firstDayOfMonthName = firstDayOfMonthName
-- Days in month
local daysInMonth = lang:formatDate('j', firstOfMonth .. ' +1 month -1 day')
dateStuff.daysInMonth = tonumber(daysInMonth)
-- Weekday of the last day of the month
local lastDayOfMonthName = lang:formatDate('l', firstOfMonth .. ' +1 month -1 day')
dateStuff.lastDayOfMonthName = lastDayOfMonthName
return dateStuff
end
function p.export(dateStuff)
--[[
Purpose: Construct WikiMarkup for the Intro paragraph.
Reference: https://www.mediawiki.org/wiki/Help:Extension:ParserFunctions#.23time
--]]
local monthOrdinal = {"first", "second", "third", "fourth", "fifth", "sixth", "seventh", "eighth", "ninth", "tenth", "eleventh", "twelfth and final"}
local root = mw.html.create('div')
:tag('p')
:wikitext("'''''[[" .. dateStuff.monthName .. "]] [[" .. dateStuff.year .. "]]''' " .. dateStuff.beVerb .. " the " .. monthOrdinal[dateStuff.monthNumber] .. " month of the " .. dateStuff.leapDesc .. " year. ")
:wikitext("The calendar for the month begins on a [[" .. dateStuff.firstDayOfMonthName .. "]] and ends on a [[" .. dateStuff.lastDayOfMonthName .. "]] after " .. dateStuff.daysInMonth .. " days.''")
:done()
return tostring(root)
end
return p