Модуль:Wikidata/SisterCities

Материал из Томская энциклопедии

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

local p = {}
local flags = require( 'Module:Wikidata/Flags' );
local places = require( 'Module:Wikidata/Places' );

function p.formatSisterCitiesProperty( context, options )
	if ( not context ) then error( 'context not specified' ); end;
	if ( not options ) then error( 'options not specified' ); end;
	if ( not options.entity ) then error( 'options.entity missing' ); end;

    local claims = context.selectClaims( options, options.property );
    if (claims == nil) then
        return '' --TODO error?
    end

	local cityNames = {};
    for i, claim in ipairs(claims) do
    	if (claim.mainsnak and claim.mainsnak.datavalue and claim.mainsnak.datavalue.value) then
    		cityNames[ claim.id ] = mw.wikibase.label( 'Q' .. claim.mainsnak.datavalue.value['numeric-id'] );
    	else
    		cityNames[ claim.id ] = '(none)';
    	end
	end

	local comparator = function(c1, c2)
		local n1 = cityNames[c1.id] or '';
		local n2 = cityNames[c2.id] or '';
		return n1 < n2;
	end
	table.sort( claims, comparator )

    -- Обход всех заявлений утверждения и с накоплением оформленых предпочтительных 
    -- заявлений в таблице
    local formattedClaims = {}

    for i, claim in ipairs(claims) do
        local formattedStatement = context.formatStatement( options, claim )
        -- здесь может вернуться либо оформленный текст заявления
        -- либо строка ошибки nil похоже никогда не возвращается
        if (formattedStatement) then
            formattedStatement = '<span class="wikidata-claim" data-wikidata-property-id="' .. string.upper( options.property ) .. '" data-wikidata-claim-id="' .. claim.id .. '">' .. formattedStatement .. '</span>'
            table.insert( formattedClaims, formattedStatement )
        end
    end

	-- создание текстовой строки со списком оформленых заявлений из таблицы  
    local out = mw.text.listToText( formattedClaims, '\n* ', '\n* ' )
    if out ~= '' then
    	out = '* ' .. out;
	    if options.before then
	    	out = options.before .. out
		end
	    if options.after then
	    	out = out .. options.after
		end
	end

    return out
end

function findCountryStatements( placeId )
	local countryStatements = {}

	local placeEntity = mw.wikibase.getEntity( placeId );
	if ( placeEntity and placeEntity.claims and placeEntity.claims.P17 ) then
		for _, countryStatement in pairs( placeEntity.claims.P17 ) do
			if ( countryStatement.rank ~= 'deprecated' ) then
				if ( countryStatement
						and countryStatement.mainsnak and countryStatement.mainsnak.datavalue and countryStatement.mainsnak.datavalue.value 
						and not ( countryStatement.qualifiers and countryStatement.qualifiers.P582 ) ) then
					table.insert( countryStatements, countryStatement );
				end
			end
		end
	end
	return countryStatements;
end

function p.formatSisterCityClaim( context, options, statement )
	local result = '';
	if not statement or statement.mainsnak.snaktype ~= 'value' then
		return result;
	end

	local countryStatements = findCountryStatements( statement.mainsnak.datavalue.value.id );
	local flagImages = {};
	for _, countryStatement in pairs( countryStatements ) do
		local countryId = 'Q' .. countryStatement.mainsnak.datavalue.value['numeric-id'];
		local flagImage = flags.getFlag( context, countryId, os.time() * 1000);
		if ( flagImage ) then
			table.insert( flagImages, flagImage );
		end
	end

	if ( #flagImages ) then
		result = result .. mw.text.listToText( flagImages, '&nbsp;/&nbsp;', '&nbsp;/&nbsp;' ) .. '&nbsp;';
	end

	result = result .. context.formatSnak( options, statement.mainsnak, {} )

	local countryLinks = {};
	for _, countryStatement in pairs( countryStatements ) do
		local countryId = 'Q' .. countryStatement.mainsnak.datavalue.value['numeric-id'];
		local countryEntity = mw.wikibase.getEntity( countryId );
		local countryOptions = {};
		if ( countryEntity ) then
			countryOptions.text = places.getLabel( context, countryEntity, nil );
		end
		table.insert( countryLinks, context.formatSnak( countryOptions, countryStatement.mainsnak, {} ) );
	end

	if ( #countryLinks ) then
		result = result .. ', ' .. mw.text.listToText( countryLinks, ' / ', ' / ' );
	end

	if ( statement.qualifiers ) then
		if ( statement.qualifiers.P580 ) then
			result = result .. ' (' .. context.formatSnak( options, statement.qualifiers.P580[1] ) .. ')'
		end
	end

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

	return result;
end

return p;