Модуль:Wikidata/Places: відмінності між версіями

Вилучено вміст Додано вміст
Створена сторінка: local categorizeByPlaceOfBirthAndDeath = true; local WDS = require('Module:WikidataSelectors') local p = {} local project = 'ruwiki'; --Property:P19, Property...
(Немає відмінностей)

Версія за 12:25, 23 січня 2016

{{i}} Документація модуля[перегляд] [редагувати] [історія] [очистити кеш]

{{Модуль:Wikidata/Places}} (обг. · викор. · ред.) містить функції форматування для Властивостей (claims), які є посиланнями на географічні сутності (зазвичай — адміністративні одиниці).


Вызов всех функций данного шаблона осуществляется из шаблона {{wikidata}}. Функции модуля нельзя вызывать напрямую — они передаются в виде аргументов из соответствующих связанных шаблонов (и только из них, в другие шаблоны их также вставлять не нужно).

formatPlaceWithQualifiers

...

Категорії підтримки

Цей модуль містить код, запозичений з модуля «Wikidata/Places» російської Вікіпедії.

Переклад
local categorizeByPlaceOfBirthAndDeath = true;

local WDS = require('Module:WikidataSelectors')
local p = {}
local project = 'ruwiki';

--Property:P19, Property:P20, Property:P119
function p.formatPlaceWithQualifiers( context, options, statement )
	local entriesToLookupCategory = {};

	local circumstances = context.getSourcingCircumstances( statement );
	local result = context.formatSnak( options, statement.mainsnak, circumstances );
	insertFromSnak( statement.mainsnak, entriesToLookupCategory )

	if ( statement.qualifiers ) then
		--parent divisions
		if ( statement.qualifiers.P131 ) then
			for i, qualifier in ipairs( statement.qualifiers.P131 ) do
				result = result .. ', ' .. context.formatSnak( options, qualifier );
				insertFromSnak( qualifier, entriesToLookupCategory )
			end
		end

		--country
		if ( statement.qualifiers.P17 ) then
			for i, qualifier in ipairs( statement.qualifiers.P17 ) do
				result = result .. ', ' .. context.formatSnak( options, qualifier );
				insertFromSnak( qualifier, entriesToLookupCategory )
			end
		end
	end

	result =  result .. context.formatRefs( options, statement );

	if ( categorizeByPlaceOfBirthAndDeath ) then
		local property = mw.ustring.upper( options.property );
		if ( property == 'P19' ) then
			result = result .. getCategory( 'P1464', entriesToLookupCategory );
		end
		if ( property == 'P20' ) then
			result = result .. getCategory( 'P1465', entriesToLookupCategory );
		end
		if ( property == 'P119' ) then
			result = result .. getCategory( 'P1791', entriesToLookupCategory );
		end
	end

	return result;
end

-- append entity id from snak to result
function insertFromSnak( snak, result )
	if ( not categorizeByPlaceOfBirthAndDeath ) then
		return;
	end
	if ( snak 
			and snak.datavalue
			and snak.datavalue.type == 'wikibase-entityid'
			and snak.datavalue.value
			and snak.datavalue.value['entity-type'] == 'item' ) then
		table.insert( result, 'Q' .. snak.datavalue.value['numeric-id'] );
	end
end

function getCategory( propertyToSearch, entriesToLookupCategoryFor )
	for _, placeId in pairs( entriesToLookupCategoryFor ) do
		local placeEntity = mw.wikibase.getEntity( placeId );
		local claims = WDS.filter( placeEntity.claims, propertyToSearch );

		if ( claims ) then
			for _, claim in pairs( claims ) do
				if ( claim.mainsnak
						and claim.mainsnak
						and claim.mainsnak.datavalue
						and claim.mainsnak.datavalue.type == "wikibase-entityid" ) then
					local catEntityId =  'Q' .. claim.mainsnak.datavalue.value["numeric-id"];
					local catEntity = mw.wikibase.getEntity( catEntityId );
					if ( catEntity and catEntity.sitelinks and catEntity.sitelinks[project] and catEntity.sitelinks[project].title ) then
						return '[[' .. catEntity.sitelinks[project].title .. ']]';
					end
				end
			end
		end
	end
	return '';
end

return p;