Модул за автоматично запълване на категории.



--[[
------------------------------------------
--              CategoryAutoDescription
-- Модул за автоматично попълване на категории.
------------------------------------------
--]]

local CategoryAutoDescription = {}

local AutoDescriptionAsteroid = require("Module:AutoDescriptionAsteroid")
local AutoDescriptionLocality = require("Module:AutoDescriptionLocality")
local AutoDescriptionHuman = require("Module:AutoDescriptionHuman")
local AutoDescriptionStars = require("Module:AutoDescriptionStars")
local AutoDescriptionGalaxy = require("Module:AutoDescriptionGalaxy")

local essence = {Q515 = "locality" --[[ city --]], Q532 = "locality" --[[ village --]], Q5084 = "locality" --[[ hamlet --]], 
 Q5084 = "locality" --[[ hamlet --]], Q486972 = "locality" --[[ human settlement --]], Q2514025 = "locality" --[[ small settlement --]], Q3744870 = "locality" --[[ township --]], 
 Q5123999 = "locality" --[[ city of regional significance of Ukraine --]], Q12131624 = "locality" --[[ city in Ukraine --]],
 Q747074 = "locality" --[[ comune of Italy --]], Q1637706 = "locality" --[[ city with a population of more than 1,000,000 --]], 
 Q3957 = "locality" --[[ settlement that is bigger than a village but smaller than a city --]], Q482821 =  "locality" --[[ metropolitan city of South Korea --]], 
 Q13539802 =  "locality" --[[ place with town rights and privileges --]], Q262882 = "locality" --[[ statutory city of Austria --]], Q667509 = "locality" --[[ municipality of Austria --]], 
 Q1906268 = "locality" --[[ municipality of Bulgaria --]], Q50330360 = "locality" --[[ second largest city --]], Q1549591 = "locality" --[[ big city --]], Q12813115 = "locality" --[[ urban area of Sweden --]], 
 Q70208 = "locality" --[[ municipality of Switzerland --]], Q14770218 = "locality" --[[ cantonal capital of Switzerland --]], 
 Q537250 = "locality" --[[ federal city --]], Q1187811 = "locality" --[[ college town --]], Q5119 = "locality" --[[ capital --]], Q317548 = "locality" --[[ resort town --]], 
 Q13218391 = "locality" --[[ charter city --]], Q484170 = "locality" --[[ commune of France --]], Q2989398 = "locality" --[[ municipality of Algeria --]], Q25412763 = "locality" --[[ city in Chile --]],
 Q15661340 = "locality" --[[ ancient city --]], Q2590631 = "locality" --[[ municipality of Hungary --]], Q12134916 = "locality" --[[ center of oblast --]], 
 Q3184121 = "locality" --[[ municipality of Brazil --]], Q15127838 = "locality" --[[ city under state jurisdiction in Latvia --]], Q51049922 = "locality" --[[type of settlement in Estonia --]],
 Q33146843 = "locality" --[[ municipality of Catalania --]], Q2989457 = "locality" --[[ urban settlement --]], Q7216840 = "locality" --[[ urban settlement of Ukraine --]], Q15078955 = "locality" --[[ urban settlement of Russia --]], 
 Q21672098 = "locality" --[[ village of Ukraine --]], Q7930989 = "locality" --[[ city/town --]], Q755707 = "locality" --[[ municipality of Norway --]], Q2074737 = "locality" --[[ municipality of Spain --]],
 Q12131640 = "locality" --[[ city of district significance --]], Q3199141 = "locality" --[[ city of Indonesia --]], Q1093829 = "locality" --[[ city of the United States --]], Q2039348 = "locality" --[[ municipality of the Netherlands --]],
 
 Q5 = "human", 
 
 Q43185532 = "star" --[[ B-type supergiant star --]], Q523="star" --[[ star --]], Q43185588="star" --[[ A-type supergiant star --]], Q5898="star" --[[ red supergiant --]], 
 Q1353952="star" --[[ F-type main-sequence star --]], Q285400="star" --[[ flare star --]], Q50053="star" --[[ binary star --]], Q244393="star" --[[ dwarf star --]], Q101600 = "star" --[[ brown dwarf --]], 
 Q2088753 = "star" --[[ triple star system --]], Q878367="star" --[[ multiple star --]], Q13890 = "star" --[[ double star --]], Q5875 = "star" --[[ giant star --]], Q1048372 = "star" --[[ blue supergiant --]], 
 
 Q3863="asteroid", 
 
 Q318="galaxy" --[[galaxy --]], Q2488="galaxy" --[[spiral galaxy --]]	
}

--[[
-- Главная функция - точка входа
--]]
function CategoryAutoDescription.launch(frame)
 --if(frame.args[2] == "{{{Описание}}}") then return "true: " .. frame.args[2] end
 local id = mw.wikibase.getEntityIdForCurrentPage() -- Q...
 local name = mw.wikibase.label() -- имя категории
 if(name == nil) then name = frame.args[1] end
 local entity = mw.wikibase.getEntity()
 local details, description, categories = CategoryAutoDescription.parseInstanceOf(entity) -- берём детали (то, что в скобочках), описание и категории
 local out = ""
 if(frame.args[2] ~= nil and frame.args[2] ~= "" and frame.args[2] ~= " " and frame.args[2] ~= "{{{Описание}}}") then out = out .. frame.args[2] .. categories
 elseif (entity == nil) then do
  if(frame.args[1] ~= nil) then out = out .. "'''" .. name .. "''' " .. " — ."
  elseif(name ~= nil) then out = out .. "'''" .. name .. "''' " .. " — ."
  end
 end
 else out = "'''" .. name .. "''' " .. details .. " — " .. description .. categories 
 end
 if(out == "") then out = "[[Категория:Категория без описание]]" end
 return out 
end

--[[
-- Вызывает для разных типов объектов (instance of) собственный обработчик для 
-- создания описания и категории
--]]
function CategoryAutoDescription.parseInstanceOf(entity) 
 if(entity == nil) then return "", "", "" end	
 local p31Statements = entity:getAllStatements("P31")
 for ki, vi  in pairs(p31Statements) do
  local idP31=vi['mainsnak']['datavalue']['value']['id'] 
  local upP31={}
  local p279Statements=mw.wikibase.getBestStatements(idP31, 'P279' )
  local j=1
  for kj, vj in pairs (p279Statements) do
		upP31[j] = vj['mainsnak']['datavalue']['value']['id'] 
		j=j+1
  end	
  for kj, vj  in pairs(essence) do
   if (vi['mainsnak']['datavalue']['value']['id'] == kj) then 
   	if (vj == "locality") then return AutoDescriptionLocality.launch(entity) end
   	if (vj == "human") then return AutoDescriptionHuman.launch(entity) end
   	if (vj == "star") then return AutoDescriptionStars.launch(entity) end
   	if (vj == "asteroid") then return AutoDescriptionAsteroid.launch(entity) end
   	if (vj == "galaxy") then return AutoDescriptionGalaxy.launch(entity) end
   end
   for kk, vk in pairs(upP31) do
   	if (kj == vk) then
   	 if (vj == "locality") then return AutoDescriptionLocality.launch(entity) end
   	 if (vj == "human") then return AutoDescriptionHuman.launch(entity) end
     if (vj == "star") then return AutoDescriptionStars.launch(entity) end
   	 if (vj == "asteroid") then return AutoDescriptionAsteroid.launch(entity) end
   	 if (vj == "galaxy") then return AutoDescriptionGalaxy.launch(entity) end
   	end	
   end	
  end	
 end	
 return "", "", ""
end	

return CategoryAutoDescription