Jump to content

Module:Signpost/index: Difference between revisions

From Wikipedia, the free encyclopedia
Content deleted Content added
get the current year using os.date - should be quicker than using a language object
m use local time where available (although it shouldn't make any difference)
Line 26: Line 26:
local function getYearIndexes()
local function getYearIndexes()
local ret = {}
local ret = {}
for i = INDEX_START_YEAR, os.date('!*t').year + 1 do
for i = INDEX_START_YEAR, os.date('*t').year + 1 do
local module = maybeRequire(INDEX_BASE .. tostring(i))
local module = maybeRequire(INDEX_BASE .. tostring(i))
insert(ret, module)
insert(ret, module)

Revision as of 15:20, 15 March 2015

-- This module processes data from [[Module:Signpost/index]], to be loaded from
-- [[Module:Signpost]] with mw.loadData.

local PAGE_FORMAT = 'Wikipedia:Wikipedia Signpost/%s/%s'
local INDEX_START_YEAR = 2005
local INDEX_BASE = 'Module:Signpost/index/'
local insert = table.insert
local format = string.format

local function makePageName(date, subpage)
	return format(PAGE_FORMAT, date, subpage)
end

local function addSubtable(tIn, tOut, key)
	tOut[key] = tOut[key] or {}
	insert(tOut[key], tIn)
end

local function maybeRequire(page)
	local success, module = pcall(require, page)
	if success then
		return module
	end
end

local function getYearIndexes()
	local ret = {}
	for i = INDEX_START_YEAR, os.date('*t').year + 1 do
		local module = maybeRequire(INDEX_BASE .. tostring(i))
		insert(ret, module)
	end
	return ret
end

local function main()
	local dates, tags, pages = {}, {}, {}
	for i, index in ipairs(getYearIndexes()) do
		for j, t in ipairs(index) do
			local date = t.date
			local page = makePageName(date, t.subpage)
			t.page = page
			addSubtable(t, dates, date)
			for k, tag in ipairs(t.tags) do
				addSubtable(t, tags, tag)
			end
			pages[page] = t
		end
	end
	return {
		dates = dates,
		tags = tags,
		pages = pages
	}
end

return main()