وحدة:category tree/poscatboiler

من ويكاموس، القاموس الحر

يمكن إنشاء صفحة توثيق الوحدة في وحدة:category tree/poscatboiler/شرح

local export = {}

local labels = require("Module:category tree/poscatboiler/data")


-- Category object

local Category = {}
Category.__index = Category


function Category.new(info, auto)
	local self = setmetatable({}, Category)
	assert(type(info) == "table", "The \"info\" parameter must be a table.")
	self._info = {}
	
	for key, val in pairs(info) do
		if key == "code" then
			self._info.code = val
			self._lang = require("Module:languages").getByCode(val) or error("The language code \"" .. val .. "\" is not valid.")
		elseif key == "label" then
			self._info.label = val
		elseif key == "sc" then
			self._info.sc = val
			self._sc = require("Module:scripts").getByCode(val) or error("The script code \"" .. val .. "\" is not valid.")
		else
			error("The parameter \"" .. key .. "\" was not recognized.")
		end
	end
	
	-- Was no label given?
	if not self._info.label then
		if auto then
			-- Try to extract the label from the page name.
			local pagename = mw.title.getCurrentTitle().text
			
			if not mw.ustring.find(pagename, "^" .. self._lang:getCanonicalName() .. " ") then
				return nil
			end
			
			self._info.label = mw.ustring.gsub(pagename, "^" .. self._lang:getCanonicalName() .. " ", "")
		else
			error("No label was specified.")
		end
	end
	
	-- Check if the label exists
	self._data = labels[self._info.label]
	
	if not self._data then
		return nil
	end
	
	-- Umbrella categories cannot have a script
	if self._sc and not self._lang then
		error("Umbrella categories cannot have a script specified.")
	end
	
	return self
end

export.new = Category.new


function Category:getBreadcrumbName()
	local ret = self._info.label
	
	if self._sc then
		ret = ret .. " in " .. self._sc:getCategoryName()
	end
	
	return ret
end


function Category:getDataModule()
	return self._data["edit"]
end


function Category:canBeEmpty()
	return self._data["can_be_empty"]
end


function Category:getCategoryName()
	if self._lang then
		local ret = self._lang:getCanonicalName() .. " " .. self._info.label
		
		if self._sc then
			ret = ret .. " in " .. self._sc:getCategoryName()
		end
		
		return mw.getContentLanguage():ucfirst(ret)
	else
		return mw.getContentLanguage():ucfirst(self._info.label .. " حسب لغة")
	end
end


function Category:getDescription()
	if self._lang then
		if self._sc then
			return self:getCategoryName() .. "."
		else
			local ret = self._data["description"]
			
			if ret then
				ret = ret:gsub("{{PAGENAME}}", mw.title.getCurrentTitle().text)
				ret = ret:gsub("{{{langname}}}", self._lang:getCanonicalName())
				ret = ret:gsub("{{{langcat}}}", self._lang:getCategoryName())
			end
			
			return ret
		end
	else
		return "Categories with " .. self._info.label .. " in various specific languages."
	end
end


function Category:getParents()
	if self._lang then
		if self._sc then
			local pinfo = mw.clone(self._info)
			pinfo.sc = nil
			local parent = Category.new(pinfo)
			
			return {{name = parent, sort = self._sc:getCanonicalName()}}
		else
			local parents = self._data["parents"]
			
			if not parents or #parents == 0 then
				return nil
			end
			
			local ret = {}
			
			for _, parent in ipairs(parents) do
				local parent = mw.clone(parent)
				
				if type(parent) == "table" then
					parent.sort = parent.sort:gsub("{{{langname}}}", self._lang:getCanonicalName())
					parent.sort = parent.sort:gsub("{{{langcat}}}", self._lang:getCategoryName())
				else
					parent = {name = parent, sort = self._info.label}
				end
				
				if parent.name:find("^Category:") then
					parent.name = parent.name:gsub("{{{langname}}}", self._lang:getCanonicalName())
					parent.name = parent.name:gsub("{{{langcat}}}", self._lang:getCategoryName())
				else
					local pinfo = mw.clone(self._info)
					pinfo.label = parent.name
					parent.name = Category.new(pinfo)
				end
				
				table.insert(ret, parent)
			end
			
			return ret
		end
	else
		if self._data["fundamental"] then
			return {{name = "Category:" .. self._data["fundamental"], sort = self._data["sortparentumbrella"] or self._info.label}}
		else
			return nil
		end
	end
end


function Category:getChildren()
	local children = self._data["children"]
	
	if not self._lang or not children or #children == 0 then
		return nil
	end
	
	local ret = {}
	
	for _, child in ipairs(children) do
		child = mw.clone(child)
		
		if type(child) ~= "table" then
			child = {name = child, sort = child}
		end
		
		local cinfo = mw.clone(self._info)
		cinfo.label = child.name
		child.name = Category.new(cinfo)
		
		table.insert(ret, child)
	end
	
	return ret
end


function Category:getUmbrella()
	if not self._lang or self._sc then
		return nil
	end
	
	local uinfo = mw.clone(self._info)
	uinfo.code = nil
	return Category.new(uinfo)
end


return export