Jump to content

Module:category tree/inherited cat

Hali sa Wiksyunaryo

This module implements {{inherited cat}}. For information on how to use the template itself, see its documentation.


local export = {}

-- Category object

local Category = {}
Category.__index = Category


function Category.new_main(frame)
	local params = {
		[1] = {},
		[2] = {required = true},
	}
	
	args = require("Module:parameters").process(frame:getParent().args, params)
	
	return Category.new({code = args[1], label = args[2]})
end


function Category.new(info)
	for key, val in pairs(info) do
		if not (key == "code" or key == "label") then
			error("The parameter \"" .. key .. "\" was not recognized.")
		end
	end
	
	local self = setmetatable({}, Category)
	self._info = info
	
	if not self._info.label then
		error("No label was specified.")
	end
	
	self._lang = self._info.code and (require("Module:languages").getByCode(self._info.code) or error("The language code \"" .. self._info.code .. "\" is not valid.")) or nil
	self._source =
		require("Module:etymology languages").getByCode(self._info.label) or
		require("Module:languages").getByCode(self._info.label) or 
		error("\"" .. self._info.label .. "\" is not a valid language or etymology-only language code.")
	
	return self
end

export.new = Category.new
export.new_main = Category.new_main


function Category:getInfo()
	return self._info
end


function Category:getBreadcrumbName()
	return (not self._lang and "terms inherited from " or "") .. self._source:getCanonicalName()
end


function Category:getDataModule()
	return "Module:category tree/inherited cat"
end


function Category:canBeEmpty()
	return false
end


function Category:isHidden()
	return false
end


function Category:getCategoryName()
	if self._lang then
		return mw.getContentLanguage():ucfirst(self._lang:getCanonicalName() .. " terms inherited from " .. self._source:getCanonicalName())
	else
		return "Terms inherited from " .. self._source:getCanonicalName()
	end
end


function Category:getDescription()
	if not self._lang then
		return "Categories with terms inherited from " .. self._source:getCanonicalName() .. " in various specific languages."
	else
		if self._source:getType() == "etymology language" then
			return "Terms in " .. self._lang:getCanonicalName() .. " that were inherited from " .. self._source:getCanonicalName() .. "."
		else
			return "Terms in " .. self._lang:getCanonicalName() .. " that were inherited from the [[:Category:" .. mw.getContentLanguage():ucfirst(self._source:getCategoryName()) .. "|" .. self._source:getCategoryName() .. "]]."
		end
	end
end


function Category:getParents()
	if self._lang then
		return {
			{name = require("Module:category tree/poscatboiler").new({code = self._info.code, label = "inherited terms"}), sort = self._source:getCanonicalName()},
			{name = require("Module:category tree/derived cat").new({code = self._info.code, label = self._source:getCode()}), sort = " "},
			}
	else
		return {{name = require("Module:category tree/derived cat").new({label = self._source:getCode()}), sort = " "}}
	end
end

function Category:getChildren()
	return nil
end


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


return export