Модуль:TerritorialDivision

Википеди материал

Для документации этого модуля может быть создана страница Модуль:TerritorialDivision/doc

local p = {}

local mRedirect = require('Module:Redirect')

local function Declension(collocation, case)

	local str = string.match(collocation, "([^\(]+)")
	if case == Genitive then
		if string.match(str, "ий район") then
			return string.gsub( str, "ий район", "ого района" )
		elseif string.match(str, "ый район") then
			return string.gsub( str, "ый район", "ого района" )
		elseif string.match(str, "ая автономная область") then
			return string.gsub( str, "ая автономная область", "ой автономной области" )
		elseif string.match(str, "ское сельское поселение") then
			return string.gsub( str, "ское сельское поселение", "ского сельского поселения" )
		elseif string.match(str, "Сельское поселение") then
			return string.gsub( str, "Сельское поселение", "сельского поселения" )
		elseif string.match(str, "сельское поселение") then
			return string.gsub( str, "сельское поселение", "сельского поселения" )
		elseif string.match(str, "Городское поселение") then
			return string.gsub( str, "Городское поселение", "городского поселения" )
		elseif string.match(str, "городское поселение") then
			return string.gsub( str, "городское поселение", "городского поселения" )
		elseif string.match(str, "ий городской округ") then
			return string.gsub( str, "ий городской округ", "ого городского округа" )
		elseif string.match(str, "ый городской округ") then
			return string.gsub( str, "ый городской округ", "ого городского округа" )
		elseif string.match(str, "Городской округ") then
			return string.gsub( str, "Городской округ", "городского округа" )
		elseif string.match(str, "городской округ") then
			return string.gsub( str, "городской округ", "городского округа" )
		end
	end
end

function p.MakeMunicipality(frame)
    local args = frame:getParent().args
	if args[1]==nil then 
    	CallSite = mw.wikibase.getEntityObject() 
    else
    	CallSite = mw.wikibase.getEntityObject(string.match( args[1], "Q%d+")) 
    end
    if CallSite==nil then do return "Не удалось получить свойства района из викидаты" end end
    local District

	if CallSite.claims.P31 and CallSite.claims.P31[1].mainsnak.datavalue.value['id'] == "Q2198484" then 
		District = CallSite
	elseif (CallSite.claims.P31 and CallSite.claims.P31[1].mainsnak.datavalue.value['id'] == "Q634099") or
		(CallSite.claims.P31 and CallSite.claims.P31[1].mainsnak.datavalue.value['id'] == "Q2661988") then 
		District = mw.wikibase.getEntityObject( CallSite.claims.P131 and CallSite.claims.P131[1].mainsnak.datavalue.value['id'] )
	else
		do return "Неизвестный тип поселения. Сообщите, пожалуйста, на страницу обсуждения шаблона" end
    end
    if District == nil then do return "Не удалось получить свойства района из викидаты" end end

    DistrictName = District:getSitelink() or District:getLabel()
	
	--local Colour   = args['цвет'] or "{{Цвет|Россия}}"
	local Titul    = args['заглавие'] or "Муниципальные образования [[" .. DistrictName .. "|" ..  Declension(DistrictName, Genitive) .. "]]"
	local State    = args['state'] or (#District.claims.P150 > 20 and "<includeonly>collapsed</includeonly>")
	local Picture  = args['изображение'] or (District.claims.P94 and "[[Файл:" .. District.claims.P94[1].mainsnak.datavalue['value'] .. "|x80px]]")
	local Picture2 = args['изображение2'] or (District.claims.P41 and "[[Файл:" .. District.claims.P41[1].mainsnak.datavalue['value'] .. "|x60px|border]]") 

	local frameTable={}
	table.insert( frameTable,  "{{навигационная полоса")
	table.insert( frameTable, "|имя = Муниципальные образования")
	--table.insert( frameTable,  "|цвет = " .. Colour)
	table.insert( frameTable, "|заглавие = " .. Titul)
	if State then table.insert( frameTable, "|state = " .. State) end
	if Picture then table.insert( frameTable, "|изображение = " .. Picture) end
	if Picture2 then table.insert( frameTable, "|изображение2 = " .. Picture2) end
	table.insert( frameTable, "|содержание = ")
	local GorodMO={}
	local SeloMO={}
	for c, claim in pairs( District.claims.P150 ) do
		if ( claim['rank'] ~= "deprecated" and claim.mainsnak.datavalue.value['id'] ) then
			local childQ = claim.mainsnak.datavalue.value['id']
		    local Child = mw.wikibase.getEntityObject( childQ )
			if Child.claims.P31 and Child.claims.P31[1].mainsnak.datavalue.value['id'] == "Q634099" then 
		    	table.insert (SeloMO, Child:getSitelink()  or Child:getLabel())
		    elseif Child.claims.P31 and Child.claims.P31[1].mainsnak.datavalue.value['id'] == "Q2661988" then 
		    	table.insert (GorodMO, Child:getSitelink()  or Child:getLabel())
		    else			
		    	do return "Неизвестный тип поселения" end
		    end
		end
	end

	table.sort(GorodMO)
	for key, val in pairs(GorodMO) do
   		table.insert( frameTable, "{{s|[[" .. val .. "|" .. string.match(val, "([^\(]+)") .. "]]}}{{•}}" )
	end	
	frameTable[#frameTable] = string.gsub( frameTable[#frameTable], "}}{{•}}$", "}}<br>") 

	table.sort(SeloMO)
	SeloStr=""
	for key, val in pairs(SeloMO) do
   		table.insert( frameTable, "{{s|[[" .. val)
   		if string.match(val, "%(") then table.insert( frameTable, "|" .. string.match(val, "([^\(]+) %(")) end
   		table.insert( frameTable, "]]}}{{•}}")
	end	
	frameTable[#frameTable] = string.gsub( frameTable[#frameTable], "}}{{•}}$", "}}") 
	table.insert( frameTable, "}}")
	frameStr=table.concat(frameTable)

	return frame:preprocess( frameStr ) 
end

-- ================================================================================================================================
function p.MakeSettlements (frame)
    local args = frame:getParent().args
	if args[1]==nil then 
    	CallSite = mw.wikibase.getEntityObject() 
    else
    	CallSite = mw.wikibase.getEntityObject(string.match( args[1], "Q%d+")) 
    end
    if CallSite==nil then do return "Не удалось получить свойства района из викидаты" end end
    local District

	if CallSite.claims.P31 and CallSite.claims.P31[1].mainsnak.datavalue.value['id'] == "Q2198484" then 
		District = CallSite
	else
		Parent = mw.wikibase.getEntityObject( CallSite.claims.P131 and CallSite.claims.P131[1].mainsnak.datavalue.value['id'] )
		District = mw.wikibase.getEntityObject( Parent.claims.P131 and Parent.claims.P131[1].mainsnak.datavalue.value['id'] )
		if District.claims.P31 and District.claims.P31[1].mainsnak.datavalue.value['id'] ~= "Q2198484" then District = nil end
    end
    if District == nill then do return "Не удалось получить свойства района из викидаты" end end

    DistrictName = District:getSitelink() or District:getLabel()
    AdmCenterQ = District.claims.P36 and District.claims.P36[1].mainsnak.datavalue.value['id']
	AdmCenter = mw.wikibase.getEntityObject( AdmCenterQ )
	AdmCenterName = AdmCenter:getSitelink() or AdmCenter:getLabel()
	
	--local Colour   = args['цвет'] or "{{Цвет|Россия}}"
	local Titul    = args['заглавие'] or "Населённые пункты [[" .. DistrictName .. "|" ..  Declension(DistrictName, Genitive) .. "]]"
	local State    = args['state'] or (#District.claims.P150 > 20 and "<includeonly>collapsed</includeonly>")
	local Picture  = args['изображение'] or (District.claims.P94 and "[[Файл:" .. District.claims.P94[1].mainsnak.datavalue['value'] .. "|x80px]]")
	local Picture2 = args['изображение2'] or (District.claims.P41 and "[[Файл:" .. District.claims.P41[1].mainsnak.datavalue['value'] .. "|x60px|border]]") 

	local frameTable={}
	table.insert( frameTable, "{{навигационная полоса")
	table.insert( frameTable, "|имя = Муниципальные образования")
	--table.insert( frameTable, "|цвет = " .. Colour)
	table.insert( frameTable, "|заглавие = " .. Titul)
	if State then table.insert( frameTable, "|state = " .. State) end
	if Picture then table.insert( frameTable, "|изображение = " .. Picture) end
	if Picture2 then table.insert( frameTable, "|изображение2 = " .. Picture2) end
	table.insert( frameTable, "|содержание = ")

	local AdmCenterExist = false
	local Settlements={}
	for d, DistrictClaim in pairs( District.claims.P150 ) do
		if ( DistrictClaim['rank'] ~= "deprecated" and DistrictClaim.mainsnak.datavalue.value['id'] ) then
			local MunicipalityQ = DistrictClaim.mainsnak.datavalue.value['id']
		    local Municipality = mw.wikibase.getEntityObject( MunicipalityQ )
			for m, MunicipalityClaim in pairs( Municipality.claims.P150 ) do
				if ( MunicipalityClaim['rank'] ~= "deprecated" and MunicipalityClaim.mainsnak.datavalue.value['id'] ) then
					local SettlementQ = MunicipalityClaim.mainsnak.datavalue.value['id']
				    local Settlement = mw.wikibase.getEntityObject( SettlementQ )
			    	table.insert (Settlements, Settlement:getSitelink()  or Settlement:getLabel())
			    	AdmCenterExist = AdmCenterExist or (AdmCenterQ == SettlementQ)
				end
			end
		end
	end
	table.insert( frameTable, 	"'''Районный центр:''' [[" .. AdmCenterName)
   	if string.match(AdmCenterName, "%(") then table.insert( frameTable, "|" .. string.match(AdmCenterName, "([^\(]+) %(")) end
   	table.insert( frameTable, "]]")
	if not AdmCenterExist then table.insert( frameTable, " (в состав района не входит)") end
	table.insert( frameTable, 	"<br>")

	table.sort(Settlements)
	for key, val in pairs(Settlements) do
		if val ~= AdmCenterName then
   			table.insert( frameTable, "{{s|[[" .. val)
   			if string.match(val, "%(") then table.insert( frameTable, "|" .. string.match(val, "([^\(]+) %(")) end
   			table.insert( frameTable, "]]}}{{•}}")
   		end
	end	
	frameTable[#frameTable] = string.gsub( frameTable[#frameTable], "}}{{•}}$", "}}") 

	table.insert( frameTable, "}}")
	frameStr=table.concat(frameTable)

	return frame:preprocess( frameStr ) 
end

-- ================================================================================================================================

function p.MakeMunicipalityDB(frame)
	local args = {}
	if #frame:getParent().args > 0 then
		args = frame:getParent().args
	else
		args = frame.args
	end
	if args==nil then do return "Не удалось получить код ОКТМО для данной страницы из викидаты" end end
    local CodePage = mw.text.trim(args[1])
	local RegionSet = mw.loadData("Модуль:TerritorialDivision/" .. string.sub(CodePage, 1, 2))
	local RayonSet = RegionSet[string.sub(CodePage, 1, 5)..'000']
    DistrictName = RayonSet[0][1]
    -- На случай, если переадресация
	local DistrictQ = mw.wikibase.getEntityIdForTitle(mRedirect.luaMain(DistrictName, bracket))
	local District = mw.wikibase.getEntity(DistrictQ)
	local GorodMO={}
	local SeloMO={}
	for k, var in pairs(RayonSet) do
		if k ~= 0 then
			if var[0][2] == 'сельское поселение' then
		    	table.insert (SeloMO, var[0][1])
		    elseif var[0][2] == 'городское поселение' then
			    table.insert (GorodMO, var[0][1])
		    else			
		    	do return var[0][1] .. " -> Неизвестный тип поселения" end
		    end
		end
	end
	local Titul    = args['заглавие'] or "Муниципальные образования [[" .. DistrictName .. "|" ..  Declension(string.gsub(DistrictName, " *%(.+", ""), Genitive) .. "]]"
	local State = args['state'] or (#GorodMO + #SeloMO > 20 and "<includeonly>collapsed</includeonly>")
	local Picture  = args['изображение'] or (District.claims.P94 and "[[Файл:" .. District.claims.P94[1].mainsnak.datavalue['value'] .. "|x80px]]")
	local Picture2 = args['изображение2'] or (District.claims.P41 and "[[Файл:" .. District.claims.P41[1].mainsnak.datavalue['value'] .. "|x60px|border]]") 

	local frameTable={}
	table.insert( frameTable,  "{{навигационная полоса")
	table.insert( frameTable, "|имя = Муниципальные образования района")
	--table.insert( frameTable,  "|цвет = " .. Colour)
	table.insert( frameTable, "|заглавие = " .. Titul)
	if State then table.insert( frameTable, "|state = " .. State) end
	if Picture then table.insert( frameTable, "|изображение = " .. Picture) end
	if Picture2 then table.insert( frameTable, "|изображение2 = " .. Picture2) end
	table.insert( frameTable, "|содержание = ")

	table.sort(GorodMO)
	for key, val in pairs(GorodMO) do
   		table.insert( frameTable, "{{s|[[" .. val)
   		if string.match(val, "%(") then table.insert( frameTable, "|" .. string.gsub(val, " *%(.+", "")) end
   		table.insert( frameTable, "]]}}{{•}}")
	end	
	frameTable[#frameTable] = string.gsub( frameTable[#frameTable], "}}{{•}}$", "}}<br>") 

	table.sort(SeloMO)
	SeloStr=""
	for key, val in pairs(SeloMO) do
   		table.insert( frameTable, "{{s|[[" .. val)
   		if string.match(val, "%(") then table.insert( frameTable, "|" .. string.gsub(val, " *%(.+", "")) end
   		table.insert( frameTable, "]]}}{{•}}")
	end	
	frameTable[#frameTable] = string.gsub( frameTable[#frameTable], "}}{{•}}$", "}}") 
	table.insert( frameTable, "}}")
	frameStr=table.concat(frameTable)

	return frame:preprocess( frameStr ) 
end


-- ================================================================================================================================
function p.MakeSettlementsDB (frame)
	local args = {}
	if #frame:getParent().args > 0 then
		args = frame:getParent().args
	else
		args = frame.args
	end
	if args==nil then do return "Не удалось получить код ОКТМО для данной страницы из викидаты" end end
    local CodePage = mw.text.trim(args[1])
	local RegionSet = mw.loadData("Модуль:TerritorialDivision/"..string.sub(CodePage, 1, 2))
	local Poselenie = false
	if string.len(CodePage) == 8 and string.find(CodePage, "^(%d%d%d%d%d000)") == nil then Poselenie = true end
	local MOSet
	if Poselenie then
		MOSet = RegionSet[string.sub(CodePage, 1, 5)..'000'][CodePage]
	else 
		MOSet = RegionSet[string.sub(CodePage, 1, 5)..'000']
	end
    local DistrictName = MOSet[0][1]
    local AdmCenterName = MOSet[0][3] 
    -- На случай, если переадресация
	local DistrictQ = mw.wikibase.getEntityIdForTitle(mRedirect.luaMain(DistrictName, bracket))
	local District = mw.wikibase.getEntity(DistrictQ)
    if District == nill then do return "Не удалось получить свойства района из викидаты" end end

	local AdmCenterExist = false
	local Settlements={}
	for k, var in pairs(MOSet) do
		if k ~= 0 then
			if var[0] == nil then
				if var[1] == AdmCenterName then
					AdmCenterExist = true
				else
					table.insert (Settlements, var[1])
				end
			else
				for s, mo in pairs(var) do
					if s ~= 0 then 				
						if mo[1] == AdmCenterName then
							AdmCenterExist = true
						else
							table.insert (Settlements, mo[1])
						end
					end
				end
			end
		end
	end

	--local Colour   = args['цвет'] or "{{Цвет|Россия}}"
	local Titul    = args['заглавие'] or "Населённые пункты [[" .. DistrictName .. "|" ..  Declension(string.gsub(DistrictName, " *%(.+", ""), Genitive) .. "]]"
	local State    = args['state'] or (#Settlements > 30 and "<includeonly>collapsed</includeonly>")
	local Picture  = args['изображение'] or (District.claims.P94 and "[[Файл:" .. District.claims.P94[1].mainsnak.datavalue['value'] .. "|x80px]]")
	local Picture2 = args['изображение2'] or (District.claims.P41 and "[[Файл:" .. District.claims.P41[1].mainsnak.datavalue['value'] .. "|x60px|border]]") 

	local frameTable={}
	table.insert( frameTable, "{{навигационная полоса")
	table.insert( frameTable, "|имя = Населённые пункты муниципального образования")
	--table.insert( frameTable, "|цвет = " .. Colour)
	table.insert( frameTable, "|заглавие = " .. Titul)
	if State then table.insert( frameTable, "|state = " .. State) end
	if Picture then table.insert( frameTable, "|изображение = " .. Picture) end
	if Picture2 then table.insert( frameTable, "|изображение2 = " .. Picture2) end
	table.insert( frameTable, "|содержание = ")

	table.insert( frameTable, 	"'''Административный центр:''' [[" .. AdmCenterName)
   	if string.match(AdmCenterName, "%(") then table.insert( frameTable, "|" .. string.gsub(AdmCenterName, " *%(.+", "")) end
   	table.insert( frameTable, "]]")
	if not AdmCenterExist then table.insert( frameTable, " (в состав муниципального образования не входит)") end
	table.insert( frameTable, 	"<br>")

	table.sort(Settlements)
	for key, val in pairs(Settlements) do
		if val ~= AdmCenterName then
   			table.insert( frameTable, "{{s|[[" .. val)
   			if string.match(val, "%(") then table.insert( frameTable, "|" .. string.gsub(val, " *%(.+", "")) end
   			table.insert( frameTable, "]]}}{{•}}")
   		end
	end	
	frameTable[#frameTable] = string.gsub( frameTable[#frameTable], "}}{{•}}$", "}}") 

	table.insert( frameTable, "}}")
	frameStr=table.concat(frameTable)

	return frame:preprocess( frameStr ) 
end


-- ================================================================================================================================
function p.MakeSettlementsTable(frame)
	local args = {}
	if #frame:getParent().args > 0 then
		args = frame:getParent().args
	else
		args = frame.args
	end
	if args==nil then do return "Не удалось получить код ОКТМО для данной страницы из викидаты" end end
    local CodePage = mw.text.trim(args[1])
	local RegionSet = mw.loadData("Модуль:TerritorialDivision/"..string.sub(CodePage, 1, 2))
	local RegionName = RegionSet[string.sub(CodePage, 1, 2)..'000000'][1]
	local Poselenie = false
	if string.len(CodePage) == 8 and string.find(CodePage, "^(%d%d%d%d%d000)") == nil then Poselenie = true end
	local MOSet
	if Poselenie then
		MOSet = RegionSet[string.sub(CodePage, 1, 5)..'000'][CodePage]
	else 
		MOSet = RegionSet[string.sub(CodePage, 1, 5)..'000']
	end
    local DistrictName = MOSet[0][1]
    local DistrictType = MOSet[0][2]
    local AdmCenterName = MOSet[0][3] 

	local Settlements={}
	local Settlement = ''
	for k, var in pairs(MOSet) do
		if k ~= 0 then
			--do return var[0][1] end
			if var[0] == nil then
				Settlement = '| [[' .. var[1]
				if string.match(var[1], "%(") then Settlement = Settlement .. "|" .. string.gsub(var[1], " *%(.+", "") end
   				Settlement = Settlement .. "]] | " .. var[2]
   				if var[1] == AdmCenterName then 
   					Settlement = '|%' .. Settlement .. ", {{s|административный центр}}" 
   				else
   					Settlement = '|' .. Settlement
   				end
   				if var[3] == nil then
   					Settlement = Settlement .. " | {{ Население | " .. var[1] .. " | Регион = " .. RegionName .. " | тс }}"
   				else
   					Settlement = Settlement .. " | {{ Население | " .. var[3] .. " | Регион = " .. RegionName .. " | тс }}"
   				end
				table.insert (Settlements, Settlement)
			else
				MOName = var[0][1]
				for s, mo in pairs(var) do
					if s ~= 0 then 
						Settlement = '| [[' .. mo[1]
						if string.match(mo[1], "%(") then Settlement = Settlement .. "|" .. string.gsub(mo[1], " *%(.+", "") end
   						Settlement = Settlement .. "]] | " .. mo[2]
   						if mo[1] == AdmCenterName then 
		   					Settlement = '|%' .. Settlement .. ", {{s|административный центр}}" 
		   				else
		   					Settlement = '|' .. Settlement
   						end
		   				if mo[3] == nil then
		   					Settlement = Settlement .. " | {{ Население | " .. mo[1] .. " | Регион = " .. RegionName .. " | тс }} | [[" .. MOName
		   				else
		   					Settlement = Settlement .. " | {{ Население | " .. mo[3] .. " | Регион = " .. RegionName .. " | тс }} | [[" .. MOName
		   				end
						if string.match(MOName, "%(") then Settlement = Settlement .. "|" .. string.gsub(MOName, " *%(.+", "") end
   						Settlement = Settlement .. "]]"
						table.insert (Settlements, Settlement)
					end
				end
			end
		end
	end

	local MOName = ''
	local frameTable={}
	table.insert( frameTable, "{{Автонумерация")
	if DistrictType == "муниципальный район" then
		table.insert( frameTable, " | Столбцов   = 5")
	else
		table.insert( frameTable, " | Столбцов   = 4")
	end
	table.insert( frameTable, "  | Оформление = standard sortable")
	if #Settlements > 29 then 
		table.insert( frameTable, " collapsible collapsed") 
		if DistrictType == "муниципальный округ" then
			table.insert( frameTable, " | Название = В округ ") 
		elseif DistrictType == "муниципальный район" then 
			table.insert( frameTable, " | Название = В район ") 
		else
			table.insert( frameTable, " | Название = " .. "В состав " .. Declension(DistrictType, Genitive) .. "") 
		end
		table.insert( frameTable, "\n")
        if (math.fmod(#Settlements, 10) == 1) and not (math.fmod(#Settlements, 100) == 11) then
            table.insert( frameTable, " входит " .. #Settlements .. " населённый пункт")
        elseif (math.fmod(#Settlements, 10) == 2) and not (math.fmod(#Settlements, 100) == 12) then
            table.insert( frameTable, " входят " .. #Settlements .. " населённых пункта")
        elseif (math.fmod(#Settlements, 10) == 3) and not (math.fmod(#Settlements, 100) == 13) then
            table.insert( frameTable, " входят " .. #Settlements .. " населённых пункта")
        elseif (math.fmod(#Settlements, 10) == 4) and not (math.fmod(#Settlements, 100) == 14) then
            table.insert( frameTable, " входят " .. #Settlements .. " населённых пункта")
        else
            table.insert( frameTable, " входят " .. #Settlements .. " населённых пунктов")
        end 
	end
	table.insert( frameTable, " | Заголовок2 = Населённый пункт")
	table.insert( frameTable, " | Заголовок3 = Тип населённого пункта")
	table.insert( frameTable, " | Заголовок4 = Население")
	if DistrictType == "муниципальный район" then table.insert( frameTable, " | Заголовок5 = Муниципальное образование") end
	table.insert( frameTable, " | Сортировка = 2")
	table.insert( frameTable, " | Сортировка4 = число")
	table.insert( frameTable, " | Выравнивание4 = right")
	
--	table.sort(Settlements)
	for key, val in pairs(Settlements) do
   		table.insert( frameTable, val)
	end	
	table.insert( frameTable, "}}")
	frameStr=table.concat(frameTable)

	return frame:preprocess( frameStr ) 
end

return p