Difference between revisions of "Module:Article history/Category"

From Wiki.Agency
Jump to: navigation, search
m (Protected Module:Article history/Category: High-risk Lua module ([Edit=Allow only template editors and admins] (indefinite) [Move=Allow only template editors and admins] (indefinite)))
 
m (1 revision imported)
 
(No difference)

Latest revision as of 22:40, 24 October 2018

Documentation for this module may be created at Module:Article history/Category/doc

-------------------------------------------------------------------------------
-- Category class
-- This module makes a Category class for use in [[Module:Article history]].
-- It provides a unified interface for the creation of category links. With
-- this class, categories can passed between objects without concerns about
-- interoperability and still have their values and sort keys easily
-- accessible.
-------------------------------------------------------------------------------

local checkType = require('libraryUtil').checkType
local CATEGORY_NS_TEXT = mw.site.namespaces[14].name

local Category = {}
Category.__index = Category

function Category.new(category, sortKey)
	checkType('Category.new', 1, category, 'string')
	checkType('Category.new', 2, sortKey, 'string', true)
	local obj = setmetatable({}, Category)
	obj.category = category
	obj.sortKey = sortKey
	return obj
end

function Category:__tostring()
	if self.sortKey then
		return string.format(
			'[[%s:%s|%s]]',
			CATEGORY_NS_TEXT,
			self.category,
			self.sortKey
		)
	else
		return string.format(
			'[[%s:%s]]',
			CATEGORY_NS_TEXT,
			self.category
		)
	end
end

return Category