Документация
--[[
------------------------------------------
--             AutoDescriptionHoliday/DonSimon
-- Модуль для заполнения праздников. 
------------------------------------------
--]]

local Dates = require("Module:Dates")
local Declension = require("Module:Declension")
local Langs = require("Module:Langs")
local MathTonumber = require("Module:Math/tonumber")
local ReturnGendered = require("Module:ReturnGendered")
local SerializeTable = require("Module:SerializeTable")
local Math = require("Module:Math")

local AutoDescriptionHoliday={}

-- Формируем и возвращаем три переменных
local description = "" -- строка описания
local details = "" -- уточнение
local categories = "[[Категория:Праздники по алфавиту]]" -- категории-- Промежуточный масив сущностей, в который помещаются результаты парсинга перед формированием описания

local entitiesArray = {country={}, dateP={}, dateCNT={}, hol={}}
local entitiesArrayP = {P17="", P31="", P837=""}

-- Точка входа
function AutoDescriptionHoliday.launch(entity)
 AutoDescriptionHoliday.parsing(entity) -- парсим карточку викиданных
 AutoDescriptionHoliday.forming() -- формируем переменные
 return details, description, categories
end	

-- Последовательно проходим свойства в карточке викиданных, для формирования массива сущностей entitiesArray
function AutoDescriptionHoliday.parsing(entity)
	entitiesArrayP["P17"] = AutoDescriptionHoliday.parseP17(entity)
	entitiesArrayP["P31"] = AutoDescriptionHoliday.parseP31(entity)
	entitiesArrayP["P837"] = AutoDescriptionHoliday.parseP837(entity)
end

function AutoDescriptionHoliday.forming()
	
	if(entitiesArrayP["P31"] == "full") then 
		if(entitiesArray["hol"][3] == nil) then description = description .. entitiesArray["hol"][1] .. " и " .. entitiesArray["hol"][2] .. "."
		else
			for k, v in pairs (entitiesArray.hol) do
				description = description .. "[[" .. v.. "]]" .. ", "
			end	
			description=description:sub(1, #description - 2)
        	description=description..". " 
		end
	else
		description = description .. entitiesArrayP.P31 .. "." 
	end
	
	if(entitiesArrayP["P17"] ~= "") then
		if(entitiesArrayP["P17"] == "full") then
			description = description .. " Отмечается в странах: "
			for k, v in pairs (entitiesArray.country) do
				description = description .. "[[" .. v.. "]]" .. ", "
			end	
			description=description:sub(1, #description - 2)
        	description=description..". " 
		else description = description .. " Отмечается в стране [[" ..	entitiesArrayP.P17 .. "]]." end	
	end	
	
	if(entitiesArrayP["P837"] ~= "") then
		if(entitiesArrayP["P837"] == "full") then
			description = description .. " Отмечается: "
			for k, v in pairs (entitiesArray.dateP) do
     			if(v ~= " ") then 
     				if (entitiesArray["dateCNT"][k] ~= " ") then 
     					description = description .. "[[" .. v  .. "]]" .. ' (в cтране [[' .. entitiesArray.dateCNT[k] .. ']])' .. ", "
     				else description = description .. "[[" .. v  .. "]]" .. ", " end
     			end	
			end	
			description=description:sub(1, #description - 2)
        	description=description..". " 
		else description = description .. " Отмечается [[" ..	entitiesArrayP.P837 .. "]]." end	
	end	
	
	
	if(entitiesArrayP["P17"] ~= "" and entitiesArrayP["P17"] ~= "full") then  
		local dec = Declension.declineWord(entitiesArrayP.P17, 2)
		if(dec ~= "Logical error Declension.declineWord(arg): word not found") then categories = categories .. "[[Категория:Праздники "..dec.."]]"
		else categories = categories .. "[[Категория:Праздники ("..entitiesArrayP.P17..")]]"
		end	
	end
	
end


function AutoDescriptionHoliday.parseP17(entity)
 local value = entity:formatPropertyValues("P17", mw.wikibase.entity.claimRanks).value
 if(value ~= "") then 
 	if(string.find(value, ",")) then
 		local i = 1
 		local p17Statements=entity:getAllStatements("P17")
 		for ki, vi in pairs (p17Statements) do
 			if(vi['mainsnak'] ~= nil) then
 				entitiesArray.country[i] = mw.wikibase.getLabel(vi['mainsnak']['datavalue']['value']['id'])
 				i = i + 1
 			end	
 		end	
 		return "full" 
 	else return value end	
 else return ""	end
end	

function AutoDescriptionHoliday.parseP31(entity)
 local value = entity:formatPropertyValues("P31", mw.wikibase.entity.claimRanks).value
 if(value ~= "") then 
 	if(string.find(value, ",")) then
 		local i = 1
 		local p31Statements=entity:getAllStatements("P31")
 		for ki, vi in pairs (p31Statements) do
 			if(vi['mainsnak'] ~= nil) then
 				entitiesArray.hol[i] = mw.wikibase.getLabel(vi['mainsnak']['datavalue']['value']['id'])
 				i = i + 1
 			end	
 		end	
 		return "full" 
 	else return value end	
 else return ""	end
end	

function AutoDescriptionHoliday.parseP837(entity)
 local value = entity:formatPropertyValues("P837", mw.wikibase.entity.claimRanks).value
 if(value ~= "") then 
 	if(string.find(value, ",")) then
 		local p837Statements=mw.wikibase.getBestStatements(entity:getId(), 'P837' )
		local i=1
		for ki, vi in pairs (p837Statements) do
			local dates = ""
			local cnt = ""
			if(vi['qualifiers'] ~= nil) then
				if(vi['qualifiers']['P17'] ~= nil) then cnt = vi['qualifiers']['P17'][1]['datavalue']['value']['id'] end	
  				if(vi['mainsnak'] ~= nil) then dates = vi['mainsnak']['datavalue']['value']['id'] end
     			if (cnt ~= "") then entitiesArray["dateCNT"][i] = mw.wikibase.getLabel(cnt)
     			else entitiesArray["cnt"][i] = " " end
     			if (dates ~= "") then entitiesArray["dateP"][i] = mw.wikibase.getLabel(dates)
     			else entitiesArray["dates"][i] = " "	end
				i = i + 1
			end
		end	
 		return "full" 
 	else return value end	
 else return ""	end
end	


return AutoDescriptionHoliday