Модуль:Wikidata/Places: відмінності між версіями
[неперевірена версія] | [перевірена версія] |
Вилучено вміст Додано вміст
Shmurak (обговорення | внесок) Немає опису редагування |
Shmurak (обговорення | внесок) Немає опису редагування |
||
Рядок 1:
local
local WDS = require( 'Module:WikidataSelectors' );
local Flags = require( 'Module:Wikidata/Flags' );
local p = {
config = {
hideSameLabels = false,
hidePartOfLabels = false,
hideUnitsForCapitals = true,
reverseOrder = false,
}
};
local function min( prev, next )
if
return next; elseif
return next; else
return prev; end end
local function max( prev, next )
if
return next; elseif
return next; else
return prev; end end
local function getTimeBoundariesFromProperty( context, propertyId )
local dateClaims = WDS.filter( context.entity.claims, propertyId );
if
return nil; end -- only support exact date so far, but need improvment
Рядок 29 ⟶ 42:
local right = nil;
for _, claim in pairs( dateClaims ) do
if
local boundaries = context.parseTimeBoundariesFromSnak( claim.mainsnak );
if
left = min( left, boundaries[ 1 ] );
right = max( right, boundaries[ 2 ] );
end
if
return { left, right };
end
Рядок 57 ⟶ 69:
local left = nil;
local right = nil;
if
for _, qualifier in pairs( statement.qualifiers[qualifierId] ) do
local boundaries = context.parseTimeBoundariesFromSnak( qualifier );
if
left = min( left, boundaries[1] );
right = max( right, boundaries[2] );
Рядок 66 ⟶ 78:
end
if
return nil;
end
Рядок 73 ⟶ 85:
end
local function getParentsInBoundariesSnakImpl( context,
local results = {};
Рядок 79 ⟶ 91:
return results;
end
for _, propertyId in ipairs( propertyIds ) do
if (not string.match( propertyId, '^P%d+$' )) then error('Incorrect propertyId: ' + propertyId); end
local selector;
if (selectors ~= nil) then
selector = selectors[propertyId] or propertyId;
else
selector = propertyId;
end
local entityClaims = {};
local filteredClaims = WDS.filter( entityClaims, selector .. '[rank:preferred, rank:normal]' );
if filteredClaims then
for _, claim in pairs( filteredClaims ) do table.insert( results, claim.mainsnak );
else
local startBoundaries = getTimeBoundariesFromQualifiers( context, claim, 'P580' );
local endBoundaries = getTimeBoundariesFromQualifiers( context, claim, 'P582' );
if ( startBoundaries == nil or startBoundaries[2] <= boundaries[1] ) and
( endBoundaries == nil or endBoundaries[1] >= boundaries[2] )
then
table.insert( results, claim.mainsnak );
end
end
end
end
end
Рядок 108 ⟶ 130:
end
local function getParentsInBoundariesSnak( context,
if
if
if
if
local results = getParentsInBoundariesSnakImpl( context,
for r, result in pairs( results ) do
Рядок 121 ⟶ 143:
end
local resultId = result.datavalue.value.id;
if
return nil;
end
Рядок 143 ⟶ 165:
local isCountry = false;
local isUnion = false;
if
then for c, claim in pairs( entity.claims.P31 ) do
if
then local typeId =
isCountry = isCountry or countries[ typeId ];
isUnion = isUnion or unions[ typeId ];
end
end
Рядок 161 ⟶ 185:
end
local function isPartOfNext( prevLabel, nextLabel )
return ( mw.ustring.len( prevLabel ) > mw.ustring.len( nextLabel ) )
and ( mw.ustring.sub( prevLabel, mw.ustring.len( prevLabel ) - mw.ustring.len( nextLabel ) + 1 ) == nextLabel );
end
Рядок 169 ⟶ 193:
function p.formatPlaceWithQualifiers( context, options, statement )
local property = mw.ustring.upper( options.property );
local actualDateBoundariesProperties = nil;
if property == 'P19' then actualDateBoundariesProperties = {'P569','P570'}; end
if property == 'P20' then actualDateBoundariesProperties = {'P570','P569'}; end
if property == 'P119' then actualDateBoundariesProperties = {'P570','P569'}; end
local boundaries = nil;
if actualDateBoundariesProperties ~= nil then
boundaries = getTimeBoundariesFromProperties( context, actualDateBoundariesProperties );
end
local entriesToLookupCategory = {};
Рядок 176 ⟶ 209:
local result = '';
local baseResult = context.formatSnak( options, statement.mainsnak, circumstances );
if not baseResult then
return nil;
end
insertFromSnak( statement.mainsnak, entriesToLookupCategory )
local hasAdditionalQualifiers = false;
if
--country
if
for i, qualifier in ipairs( statement.qualifiers.P17 ) do
local qualifierEntityId = qualifier.datavalue.value.id;
parentOptions[ 'text' ] = getLabel( context, qualifierEntityId, boundaries );
local link = context.formatSnak( parentOptions, qualifier );
if p.config.reverseOrder then
result = link .. ', ' .. result;
else
result = result .. ', ' .. link;
end
insertFromSnak( qualifier, entriesToLookupCategory )
hasAdditionalQualifiers = true;
Рядок 190 ⟶ 236:
end
if
then local
local parentSnaks = { statement.mainsnak };
local
if actualDateBoundariesProperties ~= nil then
local filterCapitalOf = {
[ entityId ] = getParentsInBoundariesSnakImpl( context, entityId, boundaries, {'P1376'} )
};
if boundaries then
local entityOptions = context.cloneOptions( options );
baseResult = context.formatSnak( entityOptions, statement.mainsnak, circumstances );
local
while
-- get parent
local newParentSnaks = getParentsInBoundariesSnak( context,
if
elseif
local parentSnak = newParentSnaks[ 1 ];
table.insert( parentSnaks, parentSnak );
table.insert(
filterCapitalOf[ parentId ] = getParentsInBoundariesSnakImpl( context, parentId, boundaries, { 'P1376' } );
else
end
end
if
for i = 2, #parentSnaks, 1 do
local parentSnak = parentSnaks[ i ];
insertFromSnak( parentSnak, entriesToLookupCategory )
end
end
do
local i = #parentSnaks;
while
local
-- TODO: use English labels, if there is no current language labels
local prevLabel = getLabel( context,
local
local nextLabel = getLabel( context,
if
-- do not output same label twice (NY, NY, USA)
table.remove( parentSnaks, i );
table.remove(
elseif
-- do not output same label if it's part of previos
table.remove( parentSnaks, i - 1 );
table.remove(
elseif p.config and p.config.hideUnitsForCapitals == true then
-- do not ouput items whose capital is the first item
local capitalId = nil;
for _capitalId, capitalSnaks in pairs( filterCapitalOf ) do
if #capitalSnaks > 0 then
for __, capitalSnak in pairs( capitalSnaks ) do
if parentSnaks[ i ].datavalue.value.id == capitalSnak.datavalue.value.id then
capitalId = _capitalId;
break;
end
end
end
end
if capitalId ~= nil then
if i == #parentSnaks then
i = i - 1;
end
while i > 1 and parentEntityIds[ i ] ~= capitalId do
table.remove( parentSnaks, i );
table.remove( parentEntityIds, i );
i = i - 1;
end
end
end
i = i - 1;
Рядок 256 ⟶ 323:
end
if
table.remove( parentSnaks, #
table.remove(
end
if
for i = 2, #parentSnaks, 1 do
local parentSnak = parentSnaks[ i ];
local parentOptions = context.cloneOptions( options );
if p.config.reverseOrder then
result = context.formatSnak( parentOptions, parentSnak ) .. ', ' .. result;
else
result = result .. ', ' .. context.formatSnak( parentOptions, parentSnak );
end
end
end
Рядок 276 ⟶ 346:
end
if
result = baseResult .. context.formatRefs( options, statement );
elseif p.config.reverseOrder then
result = result .. baseResult .. context.formatRefs( options, statement );
else
result = baseResult .. result .. context.formatRefs( options, statement );
end
if categorizeByPlace then
if
if
if
end
Рядок 293 ⟶ 365:
-- append entity id from snak to result
function insertFromSnak( snak, result )
if
return;
end
if
then table.insert( result,
end
end
function getCategory(
for _, placeId in pairs( entriesToLookupCategoryFor ) do
local
if
for _, claim in pairs( claims ) do
if
then local catEntityId =
local
if (catSitelink) then
return '[[' ..
end
end
Рядок 327 ⟶ 400:
return '';
end
local historicNamesProperties = { 'P1813', 'P1448', 'P1705' };
local langCode = mw.language.getContentLanguage():getCode();
local historicNamesPropertySelectors = {
P1813 = 'P1813[language:' .. langCode .. ']',
P1448 = 'P1448[language:' .. langCode .. ']',
P1705 = 'P1705[language:' .. langCode .. ']'
};
-- get current of historic name of place
function getLabel( context,
if not
return nil;
end
if (type(entityId) ~= 'string') then error('incorrect type of entityId argument'); end;
-- name from label
local label = mw.wikibase.getLabel( entityId );
-- name from properties
local results = getParentsInBoundariesSnakImpl( context,
historicNamesProperties, historicNamesPropertySelectors);
for r, result in pairs( results ) do
if result.datavalue and
then label = result.datavalue.value.text;
break;
Рядок 370 ⟶ 441:
local function calculateEndDateTimestamp( context, options, statement )
if
if
if
if
if
for i, qualifier in ipairs( statement.qualifiers.P582 ) do
local parsedTime = context.parseTimeFromSnak( qualifier );
if
return parsedTime;
end
Рядок 385 ⟶ 456:
-- check death day... do we have it at all?
for h, propertyId in pairs( { "P570", "P577", "
local dateClaims = context.selectClaims( options, propertyId );
if
for i, statement in ipairs( dateClaims ) do
local parsedTime = context.parseTimeFromSnak( statement.mainsnak );
if
return parsedTime;
end
Рядок 404 ⟶ 475:
function p.formatCountryClaimWithFlag( context, options, statement )
if
if
if
if
if not statement.mainsnak or
not statement.mainsnak.datavalue or
then
local result = context.formatStatementDefault( context, options, statement );
if not result then
return '';
end
return '<span class="country-name">' .. result .. '</span>';
end
local countryEntityId = statement.mainsnak.datavalue.value.id;
local endDateTimestamp = calculateEndDateTimestamp( context, options, statement );
local boundaries = getTimeBoundariesFromProperties( context, {'P570', 'P577', 'P571'} );
local countryOptions = context.cloneOptions( options );
if not countryOptions['text'] or countryOptions['text'] == '' then
countryOptions['text'] = getLabel( context,
end
local flag = Flags.getFlag( context, countryEntityId, endDateTimestamp );
if
return flag .. ' <span class="country-name">' ..
context.formatStatementDefault( context, countryOptions, statement ) .. '</span>'; end
return '<span class="country-name">' ..
context.formatStatementDefault( context, countryOptions, statement ) .. '</span>'; end
|