Modulo:AutoDescriptionFilms

Dokumentado por ĉi tiu modulo povas esti kreata ĉe Modulo:AutoDescriptionFilms/dokumentado

--[[
------------------------------------------
--             AutoDescriptionFilms
-- Модуль для заполнения фильмов. 
------------------------------------------
--]]

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 Country2Adj = require("Module:Country2Adj")

local AutoDescriptionFilms={}

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

local entitiesArray = {drct={}, genre={}, lngT="", member={}, orignCNT={}, prod={}, role={}, scnW={}, title=""}
local entitiesArrayP = {P31="", P57="", P58="", P136="", P161="", P162="", P495="", P577="", P1476=""}


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

-- Последовательно проходим свойства в карточке викиданных, для формирования массива сущностей entitiesArray
function AutoDescriptionFilms.parsing(entity)
	entitiesArrayP["P31"] = AutoDescriptionFilms.parseP31(entity)
	entitiesArrayP["P57"] = AutoDescriptionFilms.parseP57(entity)
	entitiesArrayP["P58"] = AutoDescriptionFilms.parseP58(entity)
	entitiesArrayP["P136"] = AutoDescriptionFilms.parseP136(entity)
	entitiesArrayP["P161"] = AutoDescriptionFilms.parseP161(entity)
	entitiesArrayP["P162"] = AutoDescriptionFilms.parseP162(entity)
	entitiesArrayP["P495"] = AutoDescriptionFilms.parseP495(entity)
	entitiesArrayP["P577"] = AutoDescriptionFilms.parseP577(entity)
	entitiesArrayP["P1476"] = AutoDescriptionFilms.parseP1476(entity)
end

function AutoDescriptionFilms.forming()
	
	if(entitiesArrayP["P1476"] ~= "") then
		if(entitiesArray["lngT"] ~= "" and entitiesArray["lngT"] ~= "ru") then  details = details .. "(" .. Langs.launch(entitiesArray.lngT, entitiesArray.title) .. ")"
		else details = details .. "(" .. entitiesArray.title .. ")" end
	end
	
	
	if(entitiesArrayP["P495"] ~= "") then  
		if(entitiesArrayP["P495"] == "full") then
			if(Country2Adj.launch(entitiesArray.orignCNT[1], "1") ~= nil and Country2Adj.launch(entitiesArray.orignCNT[2], "1") ~= nil and entitiesArray.orignCNT[3] == nil) then 
				description = description .. Country2Adj.launch(entitiesArray.orignCNT[1], "1") .. " и " .. Country2Adj.launch(entitiesArray.orignCNT[2], "1") .. " "
			else	
				local cnt_adj
				for k, v in pairs (entitiesArray.orignCNT) do
					cnt_adj = Country2Adj.launch(v, "1")
					if(cnt_adj ~= nil) then description = description .. cnt_adj .. ", " 
					else categories = categories .. "[[Категория:Страны без прилагательного]]" end 	
				end	
				description = description:sub(1, #description - 2) .. " "
			end
		else 
			local cnt_adj = Country2Adj.launch(entitiesArrayP.P495, "1")
			if(cnt_adj ~= nil) then description = description .. cnt_adj .. " " 
			else categories = categories .. "[[Категория:Страны без прилагательного]]" end
		end	
	end
	
	if(entitiesArrayP["P31"] == "фильм") then description = description .. "[[Фильмы|фильм]]." 
	else description = description .. entitiesArrayP["P31"] .. "." 
	end
	
	if(entitiesArrayP["P577"] ~= "") then 
		local data = entitiesArrayP.P577
		if (#data > 4) then
			description = description .. " Дата выхода: " .. "[[" .. data .. " года]]." 
		else
			description = description:sub(1, #description - 1) .. " [[" .. data .. " год]]а." 
		end
		
		local year = data:sub(#data - 4, #data)
		categories = categories .. "[[Категория:Фильмы "..year.." года".."]]" -- Добавляется категория Фильмы * года
	end 
	
	--Описание жанров
	if(entitiesArrayP["P136"] == "full") then
		if(entitiesArray["genre"][2] == nil) then 
			local genr = entitiesArray["genre"][1]
			local indexF = string.find(genr, "фильм-") -- Запрос возвращает не полное вхождение паттерна, а вхождение первых символов
			local indexD = string.find(genr, "-") 
			local indexM = string.find(genr, "мультфильм") -- Первые 2 запроса могут перекрыть слово "мультфильм-"
			if(indexF ~= nil and indexD ~= nil and indexM == nil) then 
				local trash = string.match(genr, '^[^-]*-')
				if(trash ~= nil) then 
					genr = string.gsub(genr, trash, '') 
					genr = genr:sub(3, #genr)
					local capitalWiki = genr:sub(1,1):upper() .. genr:sub(2)
					description = description .. " Жанр: [[" .. capitalWiki .. "|" .. genr .. "]]."
					categories = categories .. "[[Категория:Фильмы в жанре " .. genr .. "]]"
				end					
			else 
				description = description .. " Жанр: [[" .. entitiesArray["genre"][1] .. "]]." 
				if(string.find(genr, "кино") or indexF ~= nil) then
					local capitalName = genr:sub(1,1):upper() .. genr:sub(2)
					categories = categories .. "[[Категория:" .. capitalName .. "]]"
				else categories = categories .. "[[Категория:Фильмы в жанре " .. entitiesArray.genre[1] .. "]]" end
			end
		else
			description = description .. " Жанры: " 
			for k, v in pairs (entitiesArray.genre) do
				local genr = v
				local indexF = string.find(genr, "фильм-")
				local indexD = string.find(genr, "-")
				local indexM = string.find(genr, "мультфильм")
				if(indexF ~= nil and indexD ~= nil) and indexM == nil then 
					local trash = string.match(genr, '^[^-]*-')
					if(trash ~= nil) then 
						genr = string.gsub(genr, trash, '') 
						genr = genr:sub(3, #genr)
						local capitalWiki = genr:sub(1,1):upper() .. genr:sub(2)
						description = description .. " [[" .. capitalWiki .. "|" .. genr .. "]], "
						categories = categories .. "[[Категория:Фильмы в жанре " .. genr .. "]]"
					end					
				else 
					description = description .. " [[" .. v .. "]], " 
					if(string.find(genr, "кино") or indexF ~= nil) then
						local capitalName = genr:sub(1,1):upper() .. genr:sub(2)
						categories = categories .. "[[Категория:" .. capitalName .. "]]"
					else categories = categories .. "[[Категория:Фильмы в жанре " .. v .. "]]" end
				end
			end	
			description=description:sub(1, #description - 2)
        	description=description..". " 
		end	
	end	
	
	if(entitiesArrayP["P57"] == "full") then
		if(entitiesArray["drct"][2] == nil) then description = description .. " Режиссёр: [[" .. entitiesArray["drct"][1] .. "]]."
		else
			description = description .. " Режиссёры: " 
			for k, v in pairs (entitiesArray.drct) do
				description = description .. "[[" .. v.. "]]" .. ", "
			end	
			description=description:sub(1, #description - 2)
        	description=description..". " 
		end	
	end	
	if(entitiesArrayP["P162"] == "full") then
		if(entitiesArray["prod"][2] == nil) then description = description .. " Продюсер: [[" .. entitiesArray["prod"][1] .. "]]."
		else
			description = description .. " Продюсеры: " 
			for k, v in pairs (entitiesArray.prod) do
				description = description .. "[[" .. v.. "]]" .. ", "
			end	
			description=description:sub(1, #description - 2)
        	description=description..". " 
		end	
	end	
	if(entitiesArrayP["P58"] == "full") then
		if(entitiesArray["scnW"][2] == nil) then description = description .. " Сценарист: [[" .. entitiesArray["scnW"][1] .. "]]."
		else
			description = description .. " Сценаристы: " 
			for k, v in pairs (entitiesArray.scnW) do
				description = description .. "[[" .. v.. "]]" .. ", "
			end	
			description=description:sub(1, #description - 2)
        	description=description..". " 
		end	
	end	
	if(entitiesArrayP["P161"] == "full") then
		description = description .. " Актёрский состав: "
     	for k, v in pairs (entitiesArray.member) do
     		if(v ~= " ") then 
     			if (entitiesArray["role"][k] ~= " ") then 
     				description = description .. "[[" .. v  .. "]]" .. ' (в роли [[' .. entitiesArray["role"][k] .. ']])' .. ", "
     			else description = description .. "[[" .. v  .. "]]" .. ", " end
     		end	
     	end	
        description=description:sub(1, #description - 2)
        description=description..". " 
	end	
	
	
	if(entitiesArrayP["P495"] ~= "" and entitiesArrayP["P495"] ~= "full") then  
		local dec = Declension.declineWord(entitiesArrayP.P495, 6)
		if(dec ~= "Logical error Declension.declineWord(arg): word not found") then categories = categories .. "[[Категория:Фильмы "..dec.."]]"
		else categories = categories .. "[[Категория:Фильмы "..entitiesArrayP.P495.."]]"
		end	
	end
	
end


function AutoDescriptionFilms.parseP31(entity)
 local value = entity:formatPropertyValues("P31", mw.wikibase.entity.claimRanks).value
 if(value ~= "") then return value 
 else return ""	end
end	

function AutoDescriptionFilms.parseP57(entity)
 local p57Statements=entity:getAllStatements("P57")
 local i=1
 for ki, vi in pairs (p57Statements) do
 	if(vi['mainsnak'] ~= nil) then
 		entitiesArray['drct'][i]=mw.wikibase.getLabel(vi['mainsnak']['datavalue']['value']['id'])
 		i = i + 1
 	end	
 end	
 if(i > 1) then return "full"
 else return "" end	
end	

function AutoDescriptionFilms.parseP58(entity)
 local p58Statements=entity:getAllStatements("P58")
 local i=1
 for ki, vi in pairs (p58Statements) do
 	if(vi['mainsnak'] ~= nil) then
 		entitiesArray['scnW'][i]=mw.wikibase.getLabel(vi['mainsnak']['datavalue']['value']['id'])
 		i = i + 1
 	end	
 end	
 if(i > 1) then return "full"
 else return "" end	
end	

function AutoDescriptionFilms.parseP136(entity)
 local p136Statements=entity:getAllStatements("P136")
 local i=1
 for ki, vi in pairs (p136Statements) do
 	if(vi['mainsnak'] ~= nil) then
 		entitiesArray['genre'][i]=mw.wikibase.getLabel(vi['mainsnak']['datavalue']['value']['id'])
 		i = i + 1
 	end	
 end	
 if(i > 1) then return "full"
 else return "" end	
end	

function AutoDescriptionFilms.parseP161(entity)
 local p161Statements=mw.wikibase.getBestStatements(entity:getId(), 'P161' )
 local i=1
 for ki, vi in pairs (p161Statements) do
  	local castMember = ""
  	local charRole = ""
  	if(vi['qualifiers'] ~= nil) then
  		if(vi['qualifiers']['P453'] ~= nil) then charRole = vi['qualifiers']['P453'][1]['datavalue']['value']['id'] end	
  		if(vi['mainsnak'] ~= nil) then castMember = vi['mainsnak']['datavalue']['value']['id'] end
     	if (charRole ~= "") then entitiesArray["role"][i] = mw.wikibase.getLabel(charRole) 
     	else entitiesArray["role"][i] = " " end
     	if (castMember ~= "") then entitiesArray["member"][i] = mw.wikibase.getLabel(castMember)
     	else entitiesArray["castMember"][i] = " "	end
  		i=i+1
  	elseif(vi['mainsnak'] ~= nil) then
  		castMember = vi['mainsnak']['datavalue']['value']['id']
  		entitiesArray["role"][i] = " "
  		entitiesArray["member"][i] = mw.wikibase.getLabel(castMember)
  		i = i + 1
  	end	
 end	
 if (i>1) then return "full" 
 else return "" end 
end	

function AutoDescriptionFilms.parseP162(entity)
 local p162Statements=entity:getAllStatements("P162")
 local i=1
 for ki, vi in pairs (p162Statements) do
 	if(vi['mainsnak'] ~= nil) then
 		local label = mw.wikibase.getLabel(vi['mainsnak']['datavalue']['value']['id'])
 		if(label ~= nil) then entitiesArray['prod'][i] = label end
 		i = i + 1
 	end	
 end	
 if(i > 1) then return "full"
 else return "" end	
end	

function AutoDescriptionFilms.parseP495(entity)
 local value = entity:formatPropertyValues("P495", mw.wikibase.entity.claimRanks).value
 if(value ~= "") then 
 	if(string.find(value, ",")) then 
 		local i = 1
 		local p495Statements=entity:getAllStatements("P495")
		for ki, vi in pairs (p495Statements) do
			if(vi['mainsnak'] ~= nil) then
				local label = mw.wikibase.getLabel(vi['mainsnak']['datavalue']['value']['id'])
				if(label ~= "") then entitiesArray.orignCNT[i] = label 
				else entitiesArray.orignCNT[i] = "" end
				i = i + 1
			end	
		end	
 		return "full"
 	else return value end
 else return ""	end
end	

function AutoDescriptionFilms.parseP577(entity)
 local value = entity:formatPropertyValues("P577", mw.wikibase.entity.claimRanks).value
 if(value ~= "") then 
 	if(string.find(value, ",")) then 
 		local trash = string.match(value, ',.*')
 		value = string.gsub(value, trash, '') 
 	end
 	return value 
 else return ""	end
end	

function AutoDescriptionFilms.parseP1476(entity)
 local p1476Statements = entity:getAllStatements("P1476")
 for ki, vi in pairs (p1476Statements) do
 	if(vi['mainsnak'] ~= nil) then 
 		if(vi['mainsnak']['datavalue']['value']['language'] ~= nil) then entitiesArray.lngT = vi['mainsnak']['datavalue']['value']['language']
 		else entitiesArray.lngT = " " end	
 		entitiesArray.title = vi['mainsnak']['datavalue']['value']['text']
 		if(entitiesArray.title ~= nil) then return "full"
 		else return "" end
 	end	
 end
 return ""	 
end	


return AutoDescriptionFilms