跳转到内容

模組:Article history/Category

被永久保护的模块
维基百科,自由的百科全书

这是本页的一个历史版本,由Xiplus留言 | 贡献2020年2月8日 (六) 01:04 (已保护“模块:Article history/Category”:高風險模板:5031引用([编辑=仅管理员](无限期)[移动=仅管理员](无限期)))编辑。这可能和当前版本存在着巨大的差异。

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