Изменения

Перейти к навигации Перейти к поиску
ляп
Строка 9: Строка 9:  
hideUnitsForCapitals = true,
 
hideUnitsForCapitals = true,
 
reverseOrder = false,
 
reverseOrder = false,
catAmbiguousGeoChains = '[[Категория:Википедия:Страницы с неоднозначными геоцепочками]]',
+
catAmbiguousGeoChains = 'Категория:Википедия:Страницы с неоднозначными геоцепочками',
catLoopInGeoChains = '[[Категория:Википедия:Страницы с зацикливающимися геоцепочками]]',
+
catQualifierGeoChains = 'Категория:Википедия:Страницы с геоцепочками в квалификаторах',
catWikibaseError = '[[Категория:Википедия:Страницы с ошибками скриптов, использующих Викиданные]]'
+
catLoopInGeoChains = 'Категория:Википедия:Страницы с зацикливающимися геоцепочками',
 
}
 
}
 
};
 
};
Строка 35: Строка 35:  
end
 
end
   −
local function getParentsInBoundariesSnak( context, entityId, boundaries )
+
local function getParentsInBoundariesSnak( context, entityId, boundaries, propertyId )
 
if not entityId then error('entityId must be specified'); end
 
if not entityId then error('entityId must be specified'); end
 
if type(entityId) ~= 'string' then error('entityId must be string'); end
 
if type(entityId) ~= 'string' then error('entityId must be string'); end
Строка 41: Строка 41:  
if type(boundaries) ~= 'table' then error('boundaries must be table'); end
 
if type(boundaries) ~= 'table' then error('boundaries must be table'); end
   −
local results = context.getPropertyInBoundaries( context, entityId, boundaries, {'P131'} ) -- located in
+
local results = context.getPropertyInBoundaries( context, entityId, boundaries, { propertyId } )
if not results or #results == 0 then
  −
results = context.getPropertyInBoundaries( context, entityId, boundaries, {'P17'} ) -- country
  −
end
      
for r, result in pairs( results ) do
 
for r, result in pairs( results ) do
 
if result.snaktype ~= 'value' then
 
if result.snaktype ~= 'value' then
return nil;
+
return nil
 
end
 
end
local resultId = result.datavalue.value.id;
+
local resultId = result.datavalue.value.id
 
if resultId == entityId then
 
if resultId == entityId then
return nil;
+
return nil
 
end
 
end
 
end
 
end
return results;
+
 
 +
return results
 
end
 
end
   Строка 93: Строка 91:  
return isUnion and not isCountry;
 
return isUnion and not isCountry;
 
end
 
end
 +
    
local function isPartOfNext( prevLabel, nextLabel )
 
local function isPartOfNext( prevLabel, nextLabel )
Строка 98: Строка 97:  
and ( mw.ustring.sub( prevLabel, mw.ustring.len( prevLabel ) - mw.ustring.len( nextLabel ) + 1 ) == nextLabel );
 
and ( mw.ustring.sub( prevLabel, mw.ustring.len( prevLabel ) - mw.ustring.len( nextLabel ) + 1 ) == nextLabel );
 
end
 
end
 +
 +
 +
local function concatChain( context, options, chain )
 +
if p.config.reverseOrder then
 +
table.sort( chain, function( a, b ) return a > b end )
 +
end
 +
 +
return table.concat( chain, ', ' )
 +
end
 +
 +
 +
local function formatPlaceFromQualifiers( context, options, qualifiers, entriesToLookupCategory )
 +
local allQualifiers = {}
 +
 +
-- parent divisions
 +
if qualifiers.P131 then
 +
for i, qualifier in ipairs( qualifiers.P131 ) do
 +
if qualifier.datavalue then
 +
table.insert( allQualifiers, qualifier )
 +
end
 +
end
 +
end
 +
 +
-- country
 +
if qualifiers.P17 then
 +
for i, qualifier in ipairs( qualifiers.P17 ) do
 +
if qualifier.datavalue then
 +
table.insert( allQualifiers, qualifier )
 +
end
 +
end
 +
end
 +
 +
if #allQualifiers > 0 then
 +
local chain = {}
 +
if #allQualifiers then
 +
for i, qualifier in ipairs( allQualifiers ) do
 +
if qualifier.datavalue then
 +
local parentOptions = context.cloneOptions( options )
 +
local qualifierEntityId = qualifier.datavalue.value.id
 +
parentOptions['text'] = getLabel( context, qualifierEntityId, boundaries )
 +
local link = context.formatSnak( parentOptions, qualifier )
 +
 +
table.insert( chain, link )
 +
 +
insertFromSnak( qualifier, entriesToLookupCategory )
 +
end
 +
end
 +
end
 +
 +
local result = ''
 +
if #chain then
 +
result = concatChain( context, options, chain )
 +
if p.config and p.config.catQualifierGeoChains then
 +
result = result .. '[[' .. p.config.catQualifierGeoChains .. ']]'
 +
end
 +
end
 +
return result
 +
end
 +
return ''
 +
end
 +
    
--Property:P19, Property:P20, Property:P119
 
--Property:P19, Property:P20, Property:P119
Строка 124: Строка 184:  
local entriesToLookupCategory = {};
 
local entriesToLookupCategory = {};
   −
local circumstances = context.getSourcingCircumstances( statement );
+
local circumstances = context.getSourcingCircumstances( statement )
local result = '';
+
local result = ''
local baseResult = context.formatSnak( options, statement.mainsnak, circumstances );
+
local baseResult = context.formatSnak( options, statement.mainsnak, circumstances )
 +
local categories = ''
 
if not baseResult then
 
if not baseResult then
 
return nil;
 
return nil;
Строка 133: Строка 194:  
insertFromSnak( statement.mainsnak, entriesToLookupCategory )
 
insertFromSnak( statement.mainsnak, entriesToLookupCategory )
   −
local hasAdditionalQualifiers = false;
+
local hasAdditionalQualifiers = false
 
if statement.qualifiers then
 
if statement.qualifiers then
--parent divisions
+
result = formatPlaceFromQualifiers( context, options, statement.qualifiers, entriesToLookupCategory )
if statement.qualifiers.P131 then
+
if result ~= '' then
for i, qualifier in ipairs( statement.qualifiers.P131 ) do
+
hasAdditionalQualifiers = true
if qualifier.datavalue then
  −
local parentOptions = context.cloneOptions( options );
  −
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;
  −
end
  −
end
  −
end
  −
 
  −
--country
  −
if statement.qualifiers.P17 then
  −
for i, qualifier in ipairs( statement.qualifiers.P17 ) do
  −
if qualifier.datavalue then
  −
local parentOptions = context.cloneOptions( options );
  −
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;
  −
end
  −
end
   
end
 
end
 
end
 
end
Строка 194: Строка 220:  
baseResult = context.formatSnak( entityOptions, statement.mainsnak, circumstances );
 
baseResult = context.formatSnak( entityOptions, statement.mainsnak, circumstances );
   −
local parentId = entityId;
+
local parentId = entityId
 +
local parentIsCountry = false
 +
local isAmbiguous = false
 
while parentId ~= nil do
 
while parentId ~= nil do
 
-- get parent
 
-- get parent
local newParentSnaks = getParentsInBoundariesSnak( context, parentId, boundaries );
+
local propertyId = parentIsCountry and 'P17' or 'P131' -- country / located in
 +
local newParentSnaks = getParentsInBoundariesSnak( context, parentId, boundaries, propertyId )
 +
 
 
if not newParentSnaks or #newParentSnaks == 0 then
 
if not newParentSnaks or #newParentSnaks == 0 then
parentId = nil;
+
if not parentIsCountry then
 +
parentIsCountry = true
 +
else
 +
parentId = nil
 +
end
 
elseif #newParentSnaks == 1 then
 
elseif #newParentSnaks == 1 then
local parentSnak = newParentSnaks[ 1 ];
+
local parentSnak = newParentSnaks[ 1 ]
parentId = parentSnak.datavalue.value.id;
+
parentId = parentSnak.datavalue.value.id
 
 
 
local hasLoop = false
 
local hasLoop = false
Строка 213: Строка 247:  
if hasLoop then
 
if hasLoop then
 
if p.config and p.config.catLoopInGeoChains then
 
if p.config and p.config.catLoopInGeoChains then
result = result .. p.config.catLoopInGeoChains;
+
categories = categories .. '[[' .. p.config.catLoopInGeoChains .. ']]';
 
end
 
end
 
break -- while parentId ~= nil do
 
break -- while parentId ~= nil do
 
end
 
end
+
 
table.insert( parentSnaks, parentSnak );
+
table.insert( parentSnaks, parentSnak )
table.insert( parentEntityIds, parentId );
+
table.insert( parentEntityIds, parentId )
 
filterCapitalOf[ parentId ] = context.getPropertyInBoundaries( context, parentId, boundaries, { 'P1376' } );
 
filterCapitalOf[ parentId ] = context.getPropertyInBoundaries( context, parentId, boundaries, { 'P1376' } );
 +
parentIsCountry = false
 
else
 
else
parentId = nil;
+
isAmbiguous = true
if p.config and p.config.catAmbiguousGeoChains then
+
 
result = result .. p.config.catAmbiguousGeoChains;
+
if not parentIsCountry then
 +
parentIsCountry = true
 +
else
 +
parentId = nil
 
end
 
end
 +
end
 +
end
 +
 +
if isAmbiguous and p.config and p.config.catAmbiguousGeoChains then
 +
local countryId = parentEntityIds[ #parentEntityIds ]
 +
local countryName = getLabel( context, countryId, boundaries ) or countryId
 +
local countryCategoryName = p.config.catAmbiguousGeoChains .. ' (' .. countryName .. ')'
 +
local categoryTitle = mw.title.new( countryCategoryName )
 +
if categoryTitle.exists then
 +
categories = categories .. '[[' .. countryCategoryName .. ']]'
 +
else
 +
categories = categories .. '[[' .. p.config.catAmbiguousGeoChains .. ']]'
 
end
 
end
 
end
 
end
Строка 400: Строка 450:  
parentOptions['text'] = getLabel( context, parentEntityIds[ i ], boundaries );
 
parentOptions['text'] = getLabel( context, parentEntityIds[ i ], boundaries );
   −
local comma;
+
local comma = ''
if ((baseResult == '') and (i == 2)) then
+
local subResult = context.formatSnak( parentOptions, parentSnak )
comma = '';
+
if subResult ~= '' and result ~= '' then
else
+
comma = ', '
comma = ', ';
+
end
 +
 
 +
if i == #parentSnaks and parentEntityIds[ i ] == 'Q183' then
 +
if string.sub(subResult,-34) == '[[Германия|ФРГ]]</span>' then
 +
subResult = '[[Федеративная Республика Германии (1949—1990)|ФРГ]]'
 +
else
 +
subResult = subResult
 +
end
 
end
 
end
    
if p.config.reverseOrder then
 
if p.config.reverseOrder then
result = context.formatSnak( parentOptions, parentSnak ) .. comma .. result;
+
result = subResult .. comma .. result
 
else
 
else
result = result .. comma .. context.formatSnak( parentOptions, parentSnak );
+
result = result .. comma .. subResult
 
end
 
end
 
end
 
end
Строка 418: Строка 475:  
end
 
end
    +
local comma = ''
 +
if baseResult ~= '' and result ~= '' then
 +
comma = ', '
 +
end
 +
 
if options[ 'thisLocationOnly' ] then
 
if options[ 'thisLocationOnly' ] then
result = baseResult;
+
result = baseResult
 
elseif p.config.reverseOrder then
 
elseif p.config.reverseOrder then
result = result .. baseResult;
+
result = result .. comma .. baseResult
 
else
 
else
result = baseResult .. result;
+
result = baseResult .. comma .. result
 
end
 
end
 
if options.references then
 
if options.references then
result = result .. context.formatRefs( options, statement );
+
result = result .. context.formatRefs( options, statement )
 
end
 
end
 
 
if not options.nocat and options.nocat ~= '' and categorizeByPlace then
+
if not options.nocat and options.nocat ~= '' then
if property == 'P19' then result = result .. getCategory( 'P1464', entriesToLookupCategory ); end
+
if categorizeByPlace then
if property == 'P20' then result = result .. getCategory( 'P1465', entriesToLookupCategory ); end
+
if property == 'P19' then categories = categories .. getCategory( 'P1464', entriesToLookupCategory ); end
if property == 'P119' then result = result .. getCategory( 'P1791', entriesToLookupCategory ); end
+
if property == 'P20' then categories = categories .. getCategory( 'P1465', entriesToLookupCategory ); end
 +
if property == 'P119' then categories = categories .. getCategory( 'P1791', entriesToLookupCategory ); end
 +
end
 +
result = result .. categories
 
end
 
end
   −
return result;
+
return result
 
end
 
end
  
Анонимный участник

Реклама:

Навигация