Module:Article history

This is an old revision of this page, as edited by Mr. Stradivarius (talk | contribs) at 06:33, 15 October 2014 (move the action parameter values to the config). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

-------------------------------------------------------------------------------
--                            Article history
--
-- This module allows editors to link to all the significant events in an
-- article's history, such as good article nominations and featured article
-- nominations. It also displays its current status or statuses, as well as
-- other information such as the date it was featured on the main page.
-------------------------------------------------------------------------------

local CONFIG_PAGE = 'Module:Article history/config'

-------------------------------------------------------------------------------
-- Helper functions
-------------------------------------------------------------------------------

local function isPositiveInteger(num)
	return type(num) == 'number'
		and math.floor(num) == num
		and num > 0
		and num < math.huge
end

-------------------------------------------------------------------------------
-- Action class
-- Action objects deal with a single action in the history of the article.
-------------------------------------------------------------------------------

local Action = {}
Action.__index = Action

-- Properties to implement:
-- * id
-- * name
-- * title
-- * oldid
-- * paramNum

function Action.new(data)
	-- Valid data table keys:
	-- code, result, link, paramNum, rowCfg, articleHistoryObj
	local obj = setmetatable({}, Action)
	return obj
end

function Action:getResult()
end

function Action:getText()
end

function Action:getCurrentStatus()
end

-------------------------------------------------------------------------------
-- ImageRow
-- ImageRow objects represent a row with its own image and blurb.
-------------------------------------------------------------------------------

local ImageRow = {}
ImageRow.__index = ImageRow

-------------------------------------------------------------------------------
-- Status class
-- Status objects deal with possible current statuses of the article.
-------------------------------------------------------------------------------

local Status = {}
Status.__index = Status

-------------------------------------------------------------------------------
-- DoubleStatus class
-- For when an article can have two distinct statuses, e.g. former featured
-- article status and good article status.
-------------------------------------------------------------------------------

local DoubleStatus = {}
DoubleStatus.__index = DoubleStatus

-------------------------------------------------------------------------------
-- Notice class
-- Notice objects contain notices about an article that aren't time-dependent
-- and aren't part of its current status, e.g. the topic area of a good
-- article, or the date the article was featured on DYK.
-------------------------------------------------------------------------------

local Notice = {}
Notice.__index = Notice

-------------------------------------------------------------------------------
-- ArticleHistory class
-- This class represents the whole template.
-------------------------------------------------------------------------------

local ArticleHistory = {}
ArticleHistory.__index = ArticleHistory

function ArticleHistory.new(args, cfg, title)
	local obj = setmetatable({}, ArticleHistory)

	-- Set input
	obj.args = args or {}
	obj.cfg = cfg or require(CONFIG_PAGE)
	obj.title = title or mw.title.getCurrentTitle()

	-- Define object structure
	obj.actions = {}
	obj.categories = {}

	-- Initialise the action objects
	do
		-- Filter the arguments for actions.
		local actionParams = {}
		local pattern = '^' .. obj.cfg.actionParamPrefix .. '([1-9][0-9]*)(.-)$'
		local suffixes = obj.cfg.actionParamSuffixes
		for k, v in pairs(args) do
			if type(k) == 'string' then
				local num, suffix = key:match(pattern)
				if num and suffix and suffixes[suffix] then
					num = tonumber(num)
					local t = actionParams[num] or {}
					t[suffixes[suffix]] = v
					t.num = num
					actionParams[num] = t
				end
			end
		end
		
		-- Sort the action parameters.
		local actionParamsSorted = {}
		for num, t in pairs(actionParams) do
			table.insert(actionParamsSorted, t)
		end
		table.sort(actionParamsSorted, function (t1, t2)
			return t1.num < t2.num
		end)
		
		-- Create the action objects.
		for _, t in ipairs(actionParamsSorted) do
			t.title = obj.title
			t.cfg = obj.cfg
			table.insert(obj.actions, Action.new(t))
		end
	end

	return obj
end

function ArticleHistory:getCurrentStatus()
end

function ArticleHistory:renderBox()
end

function ArticleHistory:addCategory(category, sortKey)
	if category then
		table.insert(self.categories, {category = category, sortKey = sortKey})
	end
end

function ArticleHistory:renderCategories()
	local ret = {}
	local categoryNsText = mw.site.namespaces[14].name
	for _, t in ipairs(self.categories) do
		local cat
		if t.sortKey then
			cat = string.format(
				'[[%s:%s|%s]]',
				categoryNsText, t.category,	t.sortKey
			)
		else
			cat = string.format('[[%s:%s]]', categoryNsText, category)
		end
		table.insert(ret, cat)
	end
	return table.concat(ret)
end

function ArticleHistory:__tostring()
end

-------------------------------------------------------------------------------
-- Exports
-- These functions are called from Lua and from wikitext.
-------------------------------------------------------------------------------

local p = {}

function p._main(args)
end

function p.main(frame)
	local args = require('Module:Arguments').getArgs(frame, {
		wrappers = 'Template:Article history'
	})
	return p._main(args)
end

function p._exportClasses()
	return {
		Action = Action,
		ImageRow = ImageRow,
		Status = Status,
		Notice = Notice,
		ArticleHistory = ArticleHistory
	}
end

return p