Modul:category tree/derived cat

A modult a Modul:category tree/derived cat/doc lapon tudod dokumentálni

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:families").getByCode(self._info.label) or
		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, family 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()
	local ret = nil
	
	if self._source:getType() == "family" then
		ret = self._source:getCategoryName()
	else
		ret = self._source:getCanonicalName()
	end
	
	return (not self._lang and "terms derived from " or "") .. ret
end


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


function Category:canBeEmpty()
	return false
end


function Category:isHidden()
	return false
end


function Category:getCategoryName()
	local ret = nil

	if self._source:getType() == "family" then
		ret = self._source:getCategoryName()
	else
		if self._source:getCode() == "mul-tax" then
			ret = "taxonomic names"
		else
			ret = self._source:getCanonicalName()
		end
	end
	
	if self._lang then
		return mw.getContentLanguage():ucfirst(self._lang:getCanonicalName() .. " terms derived from " .. ret)
	else
		return "Terms derived from " .. ret
	end
end


function Category:getDescription()
	local ret = nil
	
	if not self._lang then
		if self._source:getType() == "family" then
			return "Categories with terms derived from " .. self._source:getCategoryName() .. " in various specific languages."
		else
			return "Categories with terms derived from " .. self._source:getCanonicalName() .. " in various specific languages."
		end
	else
		if self._source:getType() == "family" then
			return
				"Terms in " .. self._lang:getCanonicalName() .. " that originate from " ..
				"[[:Category:" .. mw.getContentLanguage():ucfirst(self._source:getCategoryName()) .. "|" .. self._source:getCategoryName() .. "]].\n\n" ..
				"This category should, ideally, contain only other categories. Entries can be categorized here, too, when the proper subcategory is unclear. " ..
				"If you know the exact language from which an entry categorized here is derived, please edit its respective entry."
		elseif self._source:getType() == "etymology language" then
			return "Terms in " .. self._lang:getCanonicalName() .. " that originate from " .. self._source:getCanonicalName() .. "."
		else
			return "Terms in " .. self._lang:getCanonicalName() .. " that originate from the [[:Category:" .. mw.getContentLanguage():ucfirst(self._source:getCategoryName()) .. "|" .. self._source:getCategoryName() .. "]]."
		end
	end
end


function Category:getParents()
	local derived_from_subvariety_of_self = false
	if self._lang then
		local parent = {}
		
		local pinfo = {
			code = self._lang:getCode()
		}
		local sortkey = nil
		
		if self._source:getType() == "family" then
			local fam = self._source:getFamily()
			
			if not fam or fam:getCode() == "qfa-iso" or fam:getCode() == "qfa-not" then
				pinfo.label = nil
			else
				pinfo.label = fam:getCode()
			end
			
			sortkey = self._source:getCategoryName()
		elseif self._source:getType() == "etymology language" then
			if self._source:getParentCode() == "qfa-iso" or self._source:getParentCode() == "qfa-not" or self._source:getParentCode() == "qfa-und" then
				pinfo.label = nil
			elseif self._source:getParentCode() == self._lang:getCode() then
				-- error("A language can't derive terms from a subvariety of itself.")
				derived_from_subvariety_of_self = true
				pinfo.label = self._lang:getFamily():getCode()
			else
				pinfo.label = self._source:getParentCode()
			end
			
			sortkey = self._source:getCanonicalName()
		else
			local fam = self._source:getFamily()
			
			if fam and not (fam:getCode() == "qfa-iso" or fam:getCode() == "qfa-not") then
				pinfo.label = fam:getCode()
			end -- else label is nil
			
			sortkey = self._source:getCanonicalName()
		end
		
		local additional_category = derived_from_subvariety_of_self and {
				name = "Category:Categories for terms in a language derived from a term in a subvariety of that language",
				sort = self._lang:getCanonicalName()
			} or nil
		
		if not pinfo.label then
			return {
				{
					name = require("Module:category tree/poscatboiler")
						.new {
							code = self._info.code,
							label = "terms derived from other languages"
						},
					sort = sortkey
				},
				additional_category,
			}
		else
			return {{name = Category.new(pinfo), sort = sortkey}, additional_category}
		end
	else
		if self._source:getType() == "family" then
			return {{name = require("Module:category tree/family cat").new({code = self._info.label}), sort = " "}}
		elseif self._source:getType() == "etymology language" then
			return {{name = "Category:" .. mw.getContentLanguage():ucfirst(self._source:getCategoryName()), sort = "terms derived from"}}
		else
			return {{name = require("Module:category tree/langcatboiler").new({code = self._info.label}), sort = "terms derived from"}}
		end
	end
end

function Category:getChildren()
	return nil
end


function Category:getUmbrella()
	if not self._lang then
		return nil
	end
	
	return Category.new({label = self._source:getCode()})
end


return export