Jump to content

Module:Signpost/index

Permanently protected module
From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Mr. Stradivarius (talk | contribs) at 15:17, 15 March 2015 (get the current year using os.date - should be quicker than using a language object). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

-- 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()