Մոդուլ:Wikidata/P166

Documentation for this module may be created at Մոդուլ:Wikidata/P166/doc

local categorizeByAward = true;

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

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

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

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

	if ( categorizeByAward ) then
		local property = mw.ustring.upper( options.property );
		if ( property == 'P166' ) then
			result = result .. getCategory( 'P2517', entriesToLookupCategory );
		end
	end

	return result;
end

-- append entity id from snak to result
function insertFromSnak( snak, result )
	if ( not categorizeByAward ) 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 _, awardId in pairs( entriesToLookupCategoryFor ) do
		local awardEntity = mw.wikibase.getEntity( awardId );
		local claims = WDS.filter( awardEntity.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 ) then
						if (catEntity.sitelinks[project] and catEntity.sitelinks[project].title) then
							return '[[' .. catEntity.sitelinks[project].title .. ']]';
						else
							return '[[Ստորոգութիւն:Հոդուածներ, որոնք մրցանակակիրների ստորոգութեան կարիք ունեն]]'
						end
					end
				end
			end
		end
	end
	return '';
end

return p;