Difference between revisions of "Module:Current events monthly archive"

From blackwiki
Jump to navigation Jump to search
blackwiki>RossO
(Tiny whitespace fix)
blackwiki>RossO
(Proper Millennium and Century start/end months.)
Line 32: Line 32:
 
Purpose: Construct the dateStuff object.
 
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
 +
NOTE: HTML/Wikitext string construction will always end with a blank space.
 
--]]
 
--]]
  
Line 45: Line 46:
 
beVerb = "was"
 
beVerb = "was"
 
end
 
end
 +
-- Support for future tense 'will be' can go here.
 
dateStuff.beVerb = beVerb
 
dateStuff.beVerb = beVerb
  
Line 64: Line 66:
 
dateStuff.monthName = monthName
 
dateStuff.monthName = monthName
 
dateStuff.monthNumber = tonumber(monthNumber)
 
dateStuff.monthNumber = tonumber(monthNumber)
 
--Moon Note
 
local moonNote = ""
 
if (monthNumber == 2) then
 
-- https://www.quora.com/When-was-the-last-time-the-entire-month-of-February-passed-without-a-Full-Moon/answer/Alan-Marble
 
if (yearNumber == 1961 or yearNumber == 1999 or yearNumber == 2018 or yearNumber == 2037 or yearNumber == 2067 or yearNumber == 2094) then
 
moonNote = "This month had no full moon. "
 
end
 
end
 
dateStuff.moonNote = moonNote
 
  
 
--Decade/Century/Millennium Note
 
--Decade/Century/Millennium Note
Line 79: Line 71:
 
local decadeNote = ""
 
local decadeNote = ""
 
local decadeNumber = math.floor(yearNumber / 10) * 10
 
local decadeNumber = math.floor(yearNumber / 10) * 10
local centuryNumber = math.floor(yearNumber / 100) + 1
+
local centuryCount = math.floor((yearNumber - 1) / 100) + 1 -- Fenceposts
local millenniumNumber = math.floor(yearNumber / 1000) + 1
+
local millenniumCount = math.floor((yearNumber - 1) / 1000) + 1 -- Fenceposts
 
local notePrefix = "It " .. beVerb .. " the "
 
local notePrefix = "It " .. beVerb .. " the "
 
local noteInfix = "month of the "
 
local noteInfix = "month of the "
Line 87: Line 79:
 
noteFirstOrLast = "last "
 
noteFirstOrLast = "last "
 
end
 
end
local ordinalSuffix = {"1st", "2nd", "3rd", "4th", "5th", "6th", "7th", "8th", "9th", "10th", "11th", "12th", "13th", "14th", "15th", "16th", "17th", "18th", "19th", "20th", "21st", "22nd", "23rd", "24th", "25th", "26th", "27th", "28th", "29th", "30th"}
+
local ordinalSuffix = {
 +
"1st", "2nd", "3rd", "4th", "5th", "6th", "7th", "8th", "9th", "10th",
 +
"11th", "12th", "13th", "14th", "15th", "16th", "17th", "18th", "19th", "20th",
 +
"21st", "22nd", "23rd", "24th", "25th", "26th", "27th", "28th", "29th", "30th"
 +
}
 +
if (math.fmod(yearNumber, 1000) == 0 and monthNumber == 12) then
 +
decadeNote = "[[" .. ordinalSuffix[millenniumCount] .. " millennium]] "
 +
decadeNote = decadeNote .. "and the " --Millenniums always overlap centuries.
 +
decadeFlag = true
 +
end
 +
if (math.fmod(yearNumber, 1000) == 1 and monthNumber == 1) then
 +
decadeNote = "[[" .. ordinalSuffix[millenniumCount] .. " millennium]] "
 +
decadeNote = decadeNote .. "and the " --Millenniums always overlap centuries.
 +
decadeFlag = true
 +
end
 +
if (math.fmod(yearNumber, 100) == 0 and monthNumber == 12) then
 +
decadeNote = decadeNote .. "[[" .. ordinalSuffix[centuryCount] .. " century]]. "
 +
decadeFlag = true
 +
end
 +
if (math.fmod(yearNumber, 100) == 1 and monthNumber == 1) then
 +
decadeNote = decadeNote .. "[[" .. ordinalSuffix[centuryCount] .. " century]]. "
 +
decadeFlag = true
 +
end
 +
 
 
if (math.fmod(yearNumber, 10) == 9 and monthNumber == 12) then
 
if (math.fmod(yearNumber, 10) == 9 and monthNumber == 12) then
 
decadeNote = "[[" .. decadeNumber .. "s]] decade. "
 
decadeNote = "[[" .. decadeNumber .. "s]] decade. "
Line 96: Line 111:
 
decadeFlag = true
 
decadeFlag = true
 
end
 
end
if (math.fmod(yearNumber, 100) == 99 and monthNumber == 12) then
+
 
decadeNote = "[[" .. ordinalSuffix[centuryNumber] .. " century]] and the " .. decadeNote
 
end
 
if (math.fmod(yearNumber, 100) == 0 and monthNumber == 1) then
 
decadeNote = "[[" .. ordinalSuffix[centuryNumber] .." century]] and the " .. decadeNote
 
end
 
if (math.fmod(yearNumber, 1000) == 999 and monthNumber == 12) then
 
decadeNote = "[[" .. ordinalSuffix[millenniumNumber] .." millenium]] and the " .. decadeNote
 
end
 
if (math.fmod(yearNumber, 1000) == 0 and monthNumber == 1) then
 
decadeNote = "[[" .. ordinalSuffix[millenniumNumber] .." millenium]] and the " .. decadeNote
 
end
 
 
dateStuff.decadeNote = notePrefix .. noteFirstOrLast .. noteInfix .. decadeNote
 
dateStuff.decadeNote = notePrefix .. noteFirstOrLast .. noteInfix .. decadeNote
 
if (decadeFlag == false) then
 
if (decadeFlag == false) then
 
dateStuff.decadeNote = ""
 
dateStuff.decadeNote = ""
 
end
 
end
 +
 +
 +
--Moon Note
 +
local moonNote = ""
 +
if (monthNumber == 2) then
 +
-- https://www.quora.com/When-was-the-last-time-the-entire-month-of-February-passed-without-a-Full-Moon/answer/Alan-Marble
 +
if (yearNumber == 1961 or yearNumber == 1999 or yearNumber == 2018 or yearNumber == 2037 or yearNumber == 2067 or yearNumber == 2094) then
 +
moonNote = "This month had no full moon. "
 +
end
 +
end
 +
dateStuff.moonNote = moonNote
  
 
-- First Day of the Month
 
-- First Day of the Month

Revision as of 18:07, 2 October 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.

Preview 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 = tonumber(frame.args.year or os.date( "%Y" ))
	local argsMonth = tonumber(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
	NOTE: HTML/Wikitext string construction will always end with a blank space.
--]]

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
	-- Support for future tense 'will be' can go here.
	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"
		if (math.fmod(yearNumber, 400) == 0) then
			leapDesc = "century leap"
		end
	end
	dateStuff.leapDesc = leapDesc

	-- Month and Name
	local monthName = lang:formatDate('F', constructedDate)
	dateStuff.monthName = monthName
	dateStuff.monthNumber = tonumber(monthNumber)

	--Decade/Century/Millennium Note
	local decadeFlag = false
	local decadeNote = ""
	local decadeNumber = math.floor(yearNumber / 10) * 10
	local centuryCount = math.floor((yearNumber - 1) / 100) + 1 -- Fenceposts
	local millenniumCount = math.floor((yearNumber - 1) / 1000) + 1 -- Fenceposts
	local notePrefix = "It " .. beVerb .. " the "
	local noteInfix = "month of the "
	local noteFirstOrLast = "first "
	if (monthNumber == 12) then
		noteFirstOrLast = "last "
	end
	local ordinalSuffix = {
		"1st", "2nd", "3rd", "4th", "5th", "6th", "7th", "8th", "9th", "10th",
		"11th", "12th", "13th", "14th", "15th", "16th", "17th", "18th", "19th", "20th",
		"21st", "22nd", "23rd", "24th", "25th", "26th", "27th", "28th", "29th", "30th"
	}
	if (math.fmod(yearNumber, 1000) == 0 and monthNumber == 12) then
		decadeNote = "[[" .. ordinalSuffix[millenniumCount] .. " millennium]] "
		decadeNote = decadeNote .. "and the " --Millenniums always overlap centuries.
		decadeFlag = true
	end
	if (math.fmod(yearNumber, 1000) == 1 and monthNumber == 1) then
		decadeNote = "[[" .. ordinalSuffix[millenniumCount] .. " millennium]] "
		decadeNote = decadeNote .. "and the " --Millenniums always overlap centuries.
		decadeFlag = true
	end
	if (math.fmod(yearNumber, 100) == 0 and monthNumber == 12) then
		decadeNote = decadeNote .. "[[" .. ordinalSuffix[centuryCount] .. " century]]. "
		decadeFlag = true
	end
	if (math.fmod(yearNumber, 100) == 1 and monthNumber == 1) then
		decadeNote = decadeNote .. "[[" .. ordinalSuffix[centuryCount] .. " century]]. "
		decadeFlag = true
	end

	if (math.fmod(yearNumber, 10) == 9 and monthNumber == 12) then
		decadeNote = "[[" .. decadeNumber .. "s]] decade. "
		decadeFlag = true
	end
	if (math.fmod(yearNumber, 10) == 0 and monthNumber == 1) then
		decadeNote = "[[" .. decadeNumber .. "s]] decade. "
		decadeFlag = true
	end

	dateStuff.decadeNote = notePrefix .. noteFirstOrLast .. noteInfix .. decadeNote
	if (decadeFlag == false) then
		dateStuff.decadeNote = ""
	end


	--Moon Note
	local moonNote = ""
	if (monthNumber == 2) then
		-- https://www.quora.com/When-was-the-last-time-the-entire-month-of-February-passed-without-a-Full-Moon/answer/Alan-Marble
		if (yearNumber == 1961 or yearNumber == 1999 or yearNumber == 2018 or yearNumber == 2037 or yearNumber == 2067 or yearNumber == 2094) then
			moonNote = "This month had no full moon. "
		end
	end
	dateStuff.moonNote = moonNote

	-- 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("" .. dateStuff.moonNote .. "" .. dateStuff.decadeNote .. "")
			: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