Jump to content

Module:Signpost/index: Difference between revisions

From Wikipedia, the free encyclopedia
Content deleted Content added
remove unused variable
try and get next year's index module as well so that things work smoothly near the end of the year
Line 27: Line 27:
local function getYearIndexes()
local function getYearIndexes()
local ret = {}
local ret = {}
for i = INDEX_START_YEAR, tonumber(lang:formatDate('Y')) do
for i = INDEX_START_YEAR, tonumber(lang:formatDate('Y')) + 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:13, 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 lang = mw.language.getContentLanguage()

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, tonumber(lang:formatDate('Y')) + 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()