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

From blackwiki
Jump to navigation Jump to search
blackwiki>RossO
(HTMLified the Wikimarkup)
blackwiki>RossO
(Ordinal Month)
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]].
 
+
-- Test this against Portal:Current events/September 2011/Sandbox
 
--[[
 
--[[
 
Incoming expected variables:
 
Incoming expected variables:
Line 16: Line 16:
 
argsDate = frame.args.year .. "-" .. frame.args.month .. "-01" -- Construct a date, YYY-M-DD format.
 
argsDate = frame.args.year .. "-" .. frame.args.month .. "-01" -- Construct a date, YYY-M-DD format.
 
end
 
end
local dateStuff = p.getDateStuff(argsDate)
+
local dateStuff = p.getDateStuff(argsDate, frame.args.month, frame.args.year)
 
local dayStrings = p.makeDayStrings(dateStuff)
 
local dayStrings = p.makeDayStrings(dateStuff)
 
return p.export(dayStrings, dateStuff)
 
return p.export(dayStrings, dateStuff)
Line 29: Line 29:
 
end
 
end
  
function p.getDateStuff(argsDate)
+
function p.getDateStuff(argsDate, monthNumber, yearNumber)
  
 
--[[
 
--[[
Line 48: Line 48:
 
local month = lang:formatDate('F', argsDate)
 
local month = lang:formatDate('F', argsDate)
 
dateStuff.month = month
 
dateStuff.month = month
 +
dateStuff.monthNumber = tonumber(monthNumber)
 
-- Month and year
 
-- Month and year
 
local monthAndYear = lang:formatDate('F Y', argsDate)
 
local monthAndYear = lang:formatDate('F Y', argsDate)
Line 105: Line 106:
  
 
function p.export(dayStrings, dateStuff)
 
function p.export(dayStrings, dateStuff)
-- Generates the calendar HTML.
+
-- Intro paragraph
 
local monthAndYear = dateStuff.monthAndYear
 
local monthAndYear = dateStuff.monthAndYear
local root = mw.html.create('table')
+
local ordinal = {"first", "second", "third", "fourth", "fifth", "sixth", "seventh", "eighth", "ninth", "tenth", "eleventh", "twelfth"}
-- The next two lines help to make the table-layout-based Archive pages look good. When the
+
local root = mw.html.create('div')
-- Archives have been converted to a grid-based layout, this logic can be removed, and the
 
-- corressponding CSS margin attribute can be simplified.
 
local temporaryMarginAdjustment = "auto !important"
 
if dateStuff.argsDate then temporaryMarginAdjustment = "8px 0 0 8px" end
 
 
root
 
root
-- Intro paragraph
 
 
:tag('p')
 
:tag('p')
:wikitext("'''[[" .. dateStuff.month .. "]] [[" .. dateStuff.year .. "]]''' was the ninth month of that common year. The month, which began on a [[Thursday]], ended on a [[Friday]] after " .. dateStuff.daysInMonth .. " days.")
+
:wikitext("'''[[" .. dateStuff.month .. "]] [[" .. dateStuff.year .. "]]''' was the " .. ordinal[dateStuff.monthNumber] .. " month of that common year. The month, which began on a [[Thursday]], ended on a [[Friday]] after " .. dateStuff.daysInMonth .. " days.")
 
:done()
 
:done()
 
:tag('h2')
 
:tag('h2')

Revision as of 23:30, 26 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]].
-- Test this against Portal:Current events/September 2011/Sandbox
--[[
	Incoming expected variables:
		frame.args.year = Integer value for year
		frame.args.month = Integer value for month, 1 based.
--]]

local p = {}

function p.main(frame)
	local argsDate = nil
	if (frame and frame.args and frame.args.year and frame.args.month) then
		-- If a date is passed in, assume that the display page is an Archive page.
		-- If no date passed in, assume that the display page is the current Current Events page
		argsDate = frame.args.year .. "-" .. frame.args.month .. "-01" -- Construct a date, YYY-M-DD format.
	end
	local dateStuff = p.getDateStuff(argsDate, frame.args.month, frame.args.year)
	local dayStrings = p.makeDayStrings(dateStuff)
	return p.export(dayStrings, dateStuff)
end

local function makeWikilink(link, display)
	if display then
		return string.format('[[%s|%s]]', link, display)
	else
		return string.format('[[%s]]', link)
	end
end

function p.getDateStuff(argsDate, monthNumber, yearNumber)

--[[
	Note: This function takes advantage of the formatDate's second argument to
	create data for the archival calendars. If the second arg (argsDate) is nil,
	then formatDate assumes the current date/time.
--]]

	-- Gets date data.
	local dateStuff = {}
	local lang = mw.language.getContentLanguage()
	dateStuff.argsDate = argsDate
	--Year
	local year = lang:formatDate('Y', argsDate)
	year = tonumber(year)
	dateStuff.year = year
	-- Month
	local month = lang:formatDate('F', argsDate)
	dateStuff.month = month
	dateStuff.monthNumber = tonumber(monthNumber)
	-- Month and year
	local monthAndYear = lang:formatDate('F Y', argsDate)
	local firstOfMonth = lang:formatDate('01-m-Y', argsDate)
	dateStuff.monthAndYear = monthAndYear
	-- Previous month and year
	dateStuff.previousMonthAndYear = lang:formatDate('F Y', firstOfMonth .. ' -1 month')
	-- Next month and year
	dateStuff.nextMonthAndYear = lang:formatDate('F Y', firstOfMonth .. ' +1 month')
	-- Day
	local day = lang:formatDate('j', argsDate)
	day = tonumber(day)
	dateStuff.day = day
	-- Days in month
	local daysInMonth = lang:formatDate('j', firstOfMonth .. ' +1 month -1 day')
	daysInMonth = tonumber(daysInMonth)
	dateStuff.daysInMonth = daysInMonth
	-- Weekday of the first day of the month
	local firstWeekday = lang:formatDate('w', firstOfMonth) -- Sunday = 0, Saturday = 6
	firstWeekday = tonumber(firstWeekday)
	firstWeekday = firstWeekday + 1 -- Make compatible with Lua tables. Sunday = 1, Saturday = 7.
	dateStuff.firstWeekday = firstWeekday
	return dateStuff
end

function p.makeDayStrings(dateStuff)
	local calStrings = {}
	local currentDay = dateStuff.day
	local isLinkworthy = p.isLinkworthy
	local currentMonth = dateStuff.month
	local currentYear = dateStuff.year
	local makeDayLink = p.makeDayLink
	for day = 1, dateStuff.daysInMonth do
		if dateStuff.argsDate or isLinkworthy(day, currentDay) then
			calStrings[#calStrings + 1] = makeDayLink(day, currentMonth, currentYear)
		else
			calStrings[#calStrings + 1] = tostring(day)
		end
	end
	return calStrings
end

function p.isLinkworthy(day, currentDay)
	-- Returns true if the calendar day should be linked, and false if not.
	-- Days should be linked if they are the current day or if they are within the six
	-- preceding days, as that is the number of items on the current events page.
	if currentDay - 6 <= day and day <= currentDay then
		return true
	else
		return false
	end
end

function p.makeDayLink(day, month, year)
	return string.format("'''[[#%d %s %d|&nbsp;&nbsp;%d&nbsp;&nbsp;]]'''", year, month, day, day)
end

function p.export(dayStrings, dateStuff)
	-- Intro paragraph
	local monthAndYear = dateStuff.monthAndYear
	local ordinal = {"first", "second", "third", "fourth", "fifth", "sixth", "seventh", "eighth", "ninth", "tenth", "eleventh", "twelfth"}
	local root = mw.html.create('div')
	root
		:tag('p')
			:wikitext("'''[[" .. dateStuff.month .. "]] [[" .. dateStuff.year .. "]]''' was the " .. ordinal[dateStuff.monthNumber] .. " month of that common year. The month, which began on a [[Thursday]], ended on a [[Friday]] after " .. dateStuff.daysInMonth .. " days.")
			:done()
		:tag('h2')
			:wikitext("[[Portal:Current events]]")
			:done()
		:tag('p')
			:wikitext("''This is an [[Portal:Current events/How to archive the portal|archived version]] of Wikipedia's [[Portal:Current events|Current events Portal]] from September 2011.''")
			:done()
		:tag('div')
			:css{border='solid 1px black',display='flex'}
			:tag('div')
				:css{border='solid 1px black',flex='100%'}
				:tag('p')
					:wikitext("{{Portal:Current events/Month Inclusion|2011 September}}")
					:done()
				:done()
			:tag('div')
				:css{border='solid 1px black',flex='300px'}
				:tag('div')
					:wikitext("{{Portal:Current events/September 2011/Calendar}}")
					:done()
				:tag('div')
					:wikitext("{{Portal:Current events/September 2011/Sidebar}}")
					:done()
				:done()
			:done()
		:tag('h2')
			:wikitext("References")
			:done()
		:tag('div')
			:wikitext("{{reflist}}")
			:done()
		:tag('div')
			:wikitext("{{commons category|September 2011}}")
			:done()
		:tag('div')
			:wikitext("{{Portal:Current events/Events by month}}")
			:done()
		:tag('div')
			:wikitext("[[Category:September|2011]] [[Category:2011|*2011-09]] [[Category:Current events archives]]")
			:done()
	
	return tostring(root)
end

return p