Documentation for this module may be created at Module:etymology languages/doc

local export = {}

local EtymologyLanguage = {}


function EtymologyLanguage:getCode()
	return self._code
end


function EtymologyLanguage:getCanonicalName()
	return self._rawData.canonicalName
end


function EtymologyLanguage:getDisplayForm()
	return self:getCanonicalName()
end


function EtymologyLanguage:getOtherNames(onlyOtherNames)
	return require("Module:language-like").getOtherNames(self, onlyOtherNames)
end


function EtymologyLanguage:getAliases()
	return self._rawData.aliases or {}
end


function EtymologyLanguage:getVarieties(flatten)
	return require("Module:language-like").getVarieties(self, flatten)
end


--function EtymologyLanguage:getAllNames()
--	return self._rawData.names
--end


function EtymologyLanguage:getCategoryName(nocap)
	local name = self:getCanonicalName()
	if not nocap then
		name = mw.getContentLanguage():ucfirst(name)
	end
	return name
end


function EtymologyLanguage:makeCategoryLink()
	return "[[:Category:" .. self:getCategoryName() .. "|" .. self:getDisplayForm() .. "]]"
end


function EtymologyLanguage:getType()
	return "etymology language"
end


function EtymologyLanguage:getParentCode()
	return self._rawData.parent
end

function EtymologyLanguage:getParent()
	if self._parentObject then
		return self._parentObject
	end
	
	local parent = self._rawData.parent
	if parent then
		parent = export.getByCode(parent) or require("Module:languages").getByCode(parent)
	end
	
	return parent
end


function EtymologyLanguage:getAncestors()
	if not self._ancestorObjects then
		self._ancestorObjects = {}
		
		if self._rawData.ancestors then
			for _, ancestor in ipairs(self._rawData.ancestors or {}) do
			table.insert(self._ancestorObjects, export.getByCode(ancestor) or require("Module:languages").getByCode(ancestor))
			end
		else
			local parent = self:getParent()
			table.insert(self._ancestorObjects, parent)
			while parent:getType() == "etymology language" do
				parent = self:getParent()
				table.insert(self._ancestorObjects, parent)
			end
			
			local fam = parent:getFamily()
			local protoLang = fam and fam:getProtoLanguage() or nil
			
			while not protoLang and not (not fam or fam:getCode() == "qfa-not") do
				fam = fam:getFamily()
				protoLang = fam and fam:getProtoLanguage() or nil
			end
			
			table.insert(self._ancestorObjects, protoLang)
		end
	end
	
	return self._ancestorObjects
end

local function iterateOverAncestorTree(node, func)
	for _, ancestor in ipairs(node:getAncestors()) do
		if ancestor then
			local ret = func(ancestor) or iterateOverAncestorTree(ancestor, func)
			if ret then
				return ret
			end
		end
	end
end

function EtymologyLanguage:getAncestorChain()
	if not self._ancestorChain then
		self._ancestorChain = {}
		local step = #self:getAncestors() == 1 and self:getAncestors()[1] or nil
		
		while step do
			table.insert(self._ancestorChain, 1, step)
			step = #step:getAncestors() == 1 and step:getAncestors()[1] or nil
		end
	end
	
	return self._ancestorChain
end

function EtymologyLanguage:hasAncestor(otherlang)
	local function compare(ancestor)
		return ancestor:getCode() == otherlang:getCode()
	end
	
	return iterateOverAncestorTree(self, compare) or false
end

function EtymologyLanguage:isAncestralToParent()
	return self._rawData.ancestral_to_parent
end

function EtymologyLanguage:getWikidataItem()
	local item = self._rawData.wikidata_item
	
	if type(item) == "number" then
		return "Q" .. item
	else
		return item
	end
end

function EtymologyLanguage:getWikipediaArticle()
	return self._rawData.wikipedia_article or 
		(self:getWikidataItem() and mw.wikibase and
	     mw.wikibase.sitelink(self:getWikidataItem(), 'enwiki')) or
	     self._rawData.canonicalName
end

function EtymologyLanguage:makeWikipediaLink()
	return "[[w:" .. self:getWikipediaArticle() .. "|" .. self:getCanonicalName() .. "]]"
end


function EtymologyLanguage:toJSON()
	local ret = {
		canonicalName = self:getCanonicalName(),
		categoryName = self:getCategoryName("nocap"),
		code = self._code,
		otherNames = self:getOtherNames(true),
		aliases = self:getAliases(),
		varieties = self:getVarieties(),
		parent = self._rawData.parent,
		type = self:getType(),
	}
	
	return require("Module:JSON").toJSON(ret)
end


function EtymologyLanguage:getRawData()
	return self._rawData
end


EtymologyLanguage.__index = EtymologyLanguage


function export.makeObject(code, data)
	return data and setmetatable({ _rawData = data, _code = code }, EtymologyLanguage) or nil
end


function export.getByCode(code)
	return export.makeObject(code, mw.loadData("Module:etymology languages/data")[code])
end


function export.getByCanonicalName(name)
	local code = mw.loadData("Module:etymology languages/by name")[name]
	
	if not code then
		return nil
	end
	
	return export.makeObject(code, mw.loadData("Module:etymology languages/data")[code])
end


return export
"https://ta.wiktionary.org/w/index.php?title=Module:etymology_languages&oldid=1984309" இலிருந்து மீள்விக்கப்பட்டது