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

[перевірена версія][неперевірена версія]
Вилучено вміст Додано вміст
м Захистив «Модуль:Sources»: Превентивно, модуль із великою кількістю включень ([Редагування=Дозволено тільки автопідтвердженим користувач...
Yelysavet (обговорення | внесок)
виніс в окремий модуль
Рядок 1:
local p = {};
local u = require('Module:Sources-utils')
 
local i18nDefaultLanguage = 'uk';
Рядок 61 ⟶ 62:
local options_citetypes = { separator = ' ', conjunction = ' ', format = function( src ) return 'citetype_' .. src end, nolinks = true , preferids = true };
 
local options_commas_authors = { separator = ', ', conjunction = ', ', format = personNameToAuthorName, nolinks = false, preferids = false };
function assertNotNull( argName, arg )
local options_commas_responsible = { separator = ', ', conjunction = ', ', format = personNameToResponsibleName, nolinks = false, preferids = false };
if ( (not arg) or (arg == nil) ) then
error( argName .. ' is not specified' )
end
end
 
function coalescerenderSource( arg1context, arg2, arg3, arg4src )
options_commas_authors.format = personNameToAuthorName;
if ( not isEmpty( arg1 ) ) then return arg1 end
options_commas_responsible.format = personNameToResponsibleName;
if ( not isEmpty( arg2 ) ) then return arg2 end
if ( not isEmpty( arg3 ) ) then return arg3 end
if ( not isEmpty( arg4 ) ) then return arg4 end
return nil;
end
 
function isEmpty( str )
return ( not str ) or ( str == nil ) or ( #str == 0 );
end
 
function isInstanceOf( entity, typeEntityId )
if ( not entity or not entity.claims or not entity.claims.P31 ) then
return false;
end
 
for _, claim in pairs( entity.claims.P31 ) do
if ( claim and claim.mainsnak and claim.mainsnak.datavalue and claim.mainsnak.datavalue.value and claim.mainsnak.datavalue.value["numeric-id"] ) then
local actualTypeId = 'Q' .. claim.mainsnak.datavalue.value["numeric-id"];
if ( actualTypeId == typeEntityId ) then
return true;
end
end
end
 
return false;
end
 
function getEntity( context, entityId )
assertNotNull( 'context', context );
assertNotNull( 'entityId', entityId );
 
local cached = context.cache[ entityId ];
if ( cached ) then return cached; end;
 
local result = mw.wikibase.getEntity( entityId );
if ( result ) then
context.cache[ entityId ] = result;
end
 
return result;
end
 
function toStringSnak( propertyId, strValue )
assertNotNull('propertyId', strValue)
assertNotNull('strValue', strValue)
 
local snak = { snaktype = "value", property = propertyId, datatype = 'string'};
snak["datavalue"] = { value = strValue, type = 'string' };
return snak;
end
 
function toUrlSnak( propertyId, strValue )
assertNotNull('propertyId', strValue)
assertNotNull('strValue', strValue)
 
local snak = { snaktype = "value", property = propertyId, datatype = 'string'};
snak["datavalue"] = { value = strValue, type = 'url' };
return snak;
end
 
function toWikibaseEntityIdSnak( propertyId, entityId )
assertNotNull('propertyId', entityId)
assertNotNull('entityId', entityId)
if ( mw.ustring.sub( entityId, 1, 1 ) ~= 'Q' ) then error( 'Incorrect entity ID: «' .. entityId .. '»' ); end;
 
local value = {};
value["entity-type"] = 'item';
value["numeric-id"] = mw.ustring.sub( entityId , 2);
 
local snak = { snaktype = "value", property = propertyId, datatype = 'wikibase-item'};
snak["datavalue"] = { value = value, type = 'wikibase-entityid' };
return snak;
end
 
function renderSource( context, src )
context.lang = getLangCode( getSingle( src.lang ) ) or i18nDefaultLanguage;
 
Рядок 169 ⟶ 94:
local result;
if ( src.author ) then
result = getPeopleAsAuthorWikitextgetPeopleAsWikitext( context, src.author, options_commasoptions_commas_authors );
end
if ( not isEmpty( result )) then
Рядок 214 ⟶ 139:
if ( src.editor ) then
local prefix = i18nEditors[ context.lang ] or i18nEditors[ i18nDefaultLanguage ];
result = result .. ' / ' .. prefix .. toStringgetPeopleAsWikitext( context, src.editor, options_commasoptions_commas_responsible );
end
 
Рядок 260 ⟶ 185:
if ( src.pages ) then
local letter = i18nPages[ context.lang ] or i18nPages[ i18nDefaultLanguage ];
resultlocal strPages = result .. ' — ' .. letter .. ' ' .. toString( context, src.pages, options_commas ) .. '.';
strPages = mw.ustring.gsub( strPages, '[-—]', '–' );
result = result .. ' — ' .. letter .. ' ' .. strPages .. '.';
end
 
Рядок 334 ⟶ 261:
 
return {text = result, code = src.code};
end
 
function wrapInUrl( urls, text )
local url = getSingle( urls );
if ( string.sub( url, 1, 1 ) == ':' ) then
return '[[' .. url .. '|' .. text .. ']]';
else
return '[' .. url .. ' ' .. text .. ']';
end
end
 
Рядок 381 ⟶ 299:
end
 
function getSinglegetPeopleAsWikitext( context, value, options )
if ( not value ) then
return;
end
if ( type( value ) == 'string' ) then
return value;
elseif ( type( value ) == 'table' ) then
if ( value.id ) then
return value.id;
end
 
for i, tableValue in pairs( value ) do
return getSingle( tableValue );
end
end
 
return '(unknown)';
end
 
function toString( context, value, options )
if ( type( value ) == 'string' ) then
return options.format( value );
elseif ( type( value ) == 'table' ) then
if ( value.id ) then
-- this is link
if ( type( value.label or '' ) ~= 'string' ) then mw.logObject( value ); error('label of table value is not string but ' .. type( value.label ) ) end
 
if ( options.preferids ) then
return options.format( value.id );
else
if ( options.nolinks ) then
return options.format( value.label or mw.wikibase.label( value.id ) or '\'\'(untranslated title)\'\'' );
else
return options.format( renderLink( context, value.id, value.label, options ) );
end
end
end
 
local resultList = {};
for i, tableValue in pairs( value ) do
table.insert( resultList, toString( context, tableValue, options ) );
end
 
return mw.text.listToText( resultList, options.separator, options.conjunction);
else
return options.format( '(unknown type)' );
end
 
return '';
end
 
function renderLink( context, entityId, customTitle, options )
if ( not entityId ) then error("entityId is not specified") end
if ( type( entityId ) ~= 'string' ) then error('entityId is not string, but ' .. type( entityId ) ) end
if ( type( customTitle or '' ) ~= 'string' ) then error('customTitle is not string, but ' .. type( customTitle ) ) end
 
local title = customTitle;
 
if ( isEmpty( title ) ) then
local entity = getEntity( context, entityId );
 
-- ISO 4
if ( isEmpty( title ) ) then
if ( entity.claims and entity.claims.P1160 ) then
for _, claim in pairs( entity.claims.P1160 ) do
if ( claim
and claim.mainsnak
and claim.mainsnak.datavalue
and claim.mainsnak.datavalue.value
and claim.mainsnak.datavalue.value.language == context.lang ) then
title = claim.mainsnak.datavalue.value.text;
mw.log('Got title of ' .. entityId .. ' from ISO 4 claim: «' .. title .. '»' )
break;
end
end
end
end
 
-- official name P1448
-- short name P1813
if ( isEmpty( title ) and options.short ) then
if ( entity.claims and entity.claims.P1813 ) then
for _, claim in pairs( entity.claims.P1813 ) do
if ( claim
and claim.mainsnak
and claim.mainsnak.datavalue
and claim.mainsnak.datavalue.value
and claim.mainsnak.datavalue.value.language == context.lang ) then
title = claim.mainsnak.datavalue.value.text;
mw.log('Got title of ' .. entityId .. ' from short name claim: «' .. title .. '»' )
break;
end
end
end
end
-- person name P1559
-- labels
if ( isEmpty( title ) and entity.labels[ context.lang ] ) then
title = entity.labels[ context.lang ].value;
mw.log('Got title of ' .. entityId .. ' from label: «' .. title .. '»' )
end
end
 
local actualText = title or '\'\'(untranslated)\'\'';
local link = getElementLink( context, entityId, entity);
return wrapInUrl( link, actualText );
end
 
function getElementLink( context, entityId, entity )
-- fast sitelink lookup, not an expensive operation
local link = mw.wikibase.sitelink( entityId )
if ( link ) then return ':' .. link end
 
if ( not entity and entityId ) then
entity = getEntity( context, entityId )
end
 
if ( entity ) then
-- link to entity in source context language
local projectToCheck = context.lang .. 'wiki';
if ( entity.sitelinks and entity.sitelinks[ projectToCheck ] ) then
return ':' .. context.lang .. ':' .. entity.sitelinks[ projectToCheck ].title;
end
end
 
if ( entityId ) then return ':d:' .. entityId end;
-- if ( entityId ) then return 'https://tools.wmflabs.org/reasonator/?q=' .. entityId .. '&lang=uk' end;
return nil;
end
 
function getPeopleAsAuthorWikitext( context, value, options )
if ( type( value ) == 'string' ) then
return personNameToAuthorName( value );
elseif ( type( value ) == 'table' ) then
if ( value.id ) then
Рядок 521 ⟶ 309:
else
if ( options.nolinks ) then
return getPersonNameAsAuthorLabelgetPersonNameAsLabel( context, value.id, value.label, options );
else
return getPersonNameAsAuthorWikitextgetPersonNameAsWikitext( context, value.id, value.label, options );
end
end
Рядок 530 ⟶ 318:
local resultList = {};
for i, tableValue in pairs( value ) do
local nextWikitext = getPeopleAsAuthorWikitextgetPeopleAsWikitext( context, tableValue, options );
if ( not isEmpty( nextWikitext ) ) then
table.insert( resultList, nextWikitext );
Рядок 558 ⟶ 346:
end
 
function getPersonNameAsAuthorWikitextgetPersonNameAsWikitext( context, entityId, customLabel, options )
local personNameAsAuthorpersonName = getPersonNameAsAuthorLabelgetPersonNameAsLabel( context, entityId, customLabel, options);
if ( personNameAsAuthorpersonName == nil ) then
return nil;
end
 
local link = getElementLink( context, entityId, nil );
return wrapInUrl( link, personNameAsAuthorpersonName );
end
 
function getPersonNameAsAuthorLabelgetPersonNameAsLabel( context, entityId, providedLabel, options )
-- would custom label provided we don't need to check entity at all
if ( not isEmpty( providedLabel ) ) then
mw.log( 'Custom label provided for ' .. entityId );
return personNameToAuthorNameoptions.format( providedLabel );
end
 
local entity = getEntity( context, entityId );
if ( not entity ) then return '\'\'(entity ' .. entityId .. ' is missing)\'\'' end;
if ( not isInstanceOf( entity, 'Q5' ) ) then
mw.log( 'Entity ' .. entityId .. ' is not a person' );
return nil;
end
 
local personName = nil;
Рядок 587 ⟶ 371:
personName = entity.labels[ context.lang ].value;
mw.log('Got person name of ' .. entityId .. ' from label: «' .. personName .. '»' )
end
 
if ( not isInstanceOf( entity, 'Q5' ) ) then
mw.log( 'Entity ' .. entityId .. ' is not a person' );
return personName;
end
 
Рядок 592 ⟶ 381:
return '\'\'(not translated to ' .. context.lang .. ')\'\'';
else
return personNameToAuthorNameoptions.format( personName );
end
end
Рядок 637 ⟶ 426:
mw.log( 'personNameToAuthorName: «' .. fullName .. '»: have «Im O. Fa» match' );
return f .. ' ' .. mw.ustring.sub( i, 1, 1 ) .. '. ' .. o .. '.';
end
 
local i1, i2, i3, f = mw.ustring.match( fullName, '^%s*(%a[%a\-]*)%s(%a)\.%s(%a)\.%s(%a[%a\-]*)%s*$');
if ( f ) then
mw.log( 'personNameToAuthorName: «' .. fullName .. '»: have «Im I. I. Fa» match' );
return f .. ' ' .. mw.ustring.sub( i1, 1, 1 ) .. '. ' .. i2 .. '. ' .. i3 .. '.';
end
 
Рядок 651 ⟶ 446:
end
 
local ii1, i2, f = mw.ustring.match( fullName, '^%s*(%a[%a\-]+)%s(%a[%a\-]+)%s+de%s+(%a[%a\-]+)%s*$');
if ( f ) then
mw.log( 'personNameToAuthorName: «' .. fullName .. '»: have «I1 I2 de Fa» match' );
return f .. ' ' .. mw.ustring.sub( i1, 1, 1 ) .. '. ' .. mw.ustring.sub( i2, 1, 1 ) .. '.';
end
 
local i, f = mw.ustring.match( fullName, '^%s*(%a[%a\-\']+)%s(%a[%a\-\']+)%s*$');
if ( f ) then
mw.log( 'personNameToAuthorName: «' .. fullName .. '»: have «Im Fa» match' );
Рядок 661 ⟶ 462:
end
 
function personNameToResponsibleName( fullName )
-- Expand special types of references when additional data could be found in OTHER entity properties
if ( not fullName ) then return fullName; end
function expandSpecials( currentEntity, reference, data )
if ( reference.snaks.P248
and reference.snaks.P248[1]
and reference.snaks.P248[1].datavalue
and reference.snaks.P248[1].datavalue.value["numeric-id"]) then
local sourceId = "Q" .. reference.snaks.P248[1].datavalue.value["numeric-id"];
data.sourceId = sourceId;
 
local f, i, o = mw.ustring.match( fullName, '^%s*(%a[%a\-]*)\,%s(%a[%a\-]*)%s(%a[%a\-]*)%s*$' );
-- Gemeinsame Normdatei -- specified by P227
if ( sourceId == 'Q36578'f ) then
mw.log( 'personNameToResponsibleName: «' .. fullName .. '»: have «Fa, I. O.» match' );
appendSnaks( currentEntity.claims, 'P227', data, 'part', { format = function( gnd ) return 'Record #' .. gnd; end } );
return mw.ustring.sub( i, 1, 1 ) .. '. ' .. mw.ustring.sub( o, 1, 1 ) .. '. ' .. f;
appendSnaks( currentEntity.claims, 'P227', data, 'url', { format = function( gnd ) return 'http://d-nb.info/gnd/' .. gnd .. '/'; end } );
data.year = '2012—2016'
expandSpecialsQualifiers( currentEntity, 'P227', data );
end
 
-- BNF -- specified by P268
if ( sourceId == 'Q15222191' ) then
appendSnaks( currentEntity.claims, 'P268', data, 'part', { format = function( id ) return 'Record #' .. id; end } );
appendSnaks( currentEntity.claims, 'P268', data, 'url', { format = function( id ) return 'http://catalogue.bnf.fr/ark:/12148/cb' .. id; end } );
expandSpecialsQualifiers( currentEntity, 'P268', data );
end
 
-- Union List of Artist Names -- specified by P245
if ( sourceId == 'Q2494649' ) then
appendSnaks( currentEntity.claims, 'P245', data, 'url', { format = function( id ) return 'http://www.getty.edu/vow/ULANFullDisplay?find=&role=&nation=&subjectid=' .. id end } );
expandSpecialsQualifiers( currentEntity, 'P245', data );
end
 
-- imdb.com -- specified by P345
if ( sourceId == 'Q37312' ) then
appendSnaks( currentEntity.claims, 'P345', data, 'part', { format = function( id ) return 'Person Profile' end } );
appendSnaks( currentEntity.claims, 'P345', data, 'url', { format = function( id ) return 'http://www.imdb.com/name/' .. id .. '/' end } );
expandSpecialsQualifiers( currentEntity, 'P345', data );
end
 
-- wtatennis.com -- specified by P597
if ( sourceId == 'Q14580067' ) then
appendSnaks( currentEntity.claims, 'P597', data, 'part', { format = function( id ) return 'Player Profile' end } );
appendSnaks( currentEntity.claims, 'P597', data, 'url', { format = function( id ) return 'http://www.wtatennis.com/players/player/' .. id end } );
expandSpecialsQualifiers( currentEntity, 'P597', data );
end
 
-- Find a Grave -- specified by P535
if ( sourceId == 'Q63056' ) then
appendSnaks( currentEntity.claims, 'P535', data, 'url', { format = function( id ) return 'http://www.findagrave.com/cgi-bin/fg.cgi?page=gr&GRid=' .. id; end } );
expandSpecialsQualifiers( currentEntity, 'P535', data );
end
 
-- Gran Enciclopèdia Catalana -- specified by P1296
if ( sourceId == 'Q2664168' ) then
appendSnaks( currentEntity.claims, 'P1296', data, 'url', { format = function( id ) return 'http://www.enciclopedia.cat/enciclop%C3%A8dies/gran-enciclop%C3%A8dia-catalana/EC-GEC-' .. id .. '.xml'; end } );
expandSpecialsQualifiers( currentEntity, 'P1296', data );
end
 
-- Encyclopædia Britannica online -- specified by P1417
if ( sourceId == 'Q5375741' ) then
appendSnaks( currentEntity.claims, 'P1417', data, 'url', { format = function( id ) return 'http://global.britannica.com/' .. id; end } );
expandSpecialsQualifiers( currentEntity, 'P1417', data );
end
 
-- do we have appropriate record in P1433 ?
local claims = findClaimsByValue( currentEntity, 'P1343', sourceId );
if ( claims and #claims ~= 0 ) then
for _, claim in pairs( claims ) do
populateDataFromClaims( sourceId, claim.qualifiers, data );
end
end
 
-- Electronic Jewish Encyclopedia (Elektronnaja Evrejskaja Entsiklopedia) -- specified by P1438
if ( sourceId == 'Q1967250' ) then
appendSnaks( currentEntity.claims, 'P1438', data, 'url', { format = function( id ) return 'http://www.eleven.co.il/article/' .. id; end } );
expandSpecialsQualifiers( currentEntity, 'P1438', data );
end
 
-- sports-reference.com -- specified by P1447
if ( sourceId == 'Q18002875' ) then
appendSnaks( currentEntity.claims, 'P1447', data, 'url', { format = function( id ) return 'http://www.sports-reference.com/olympics/athletes/' .. id .. '.html'; end } );
expandSpecialsQualifiers( currentEntity, 'P1447', data );
end
 
-- Dizionario Biografico degli Italiani -- specified by P1986
if ( sourceId == 'Q1128537' ) then
if ( not data.lang ) then data.lang = { id = 'Q652' } end;
appendSnaks( currentEntity.claims, 'P1986', data, 'url', { format = function( id ) return 'http://www.treccani.it/enciclopedia/' .. id .. '_%28Dizionario_Biografico%29/' end } );
expandSpecialsQualifiers( currentEntity, 'P1986', data );
end
end
end
 
local f1, f2, i = mw.ustring.match( fullName, '^%s*(%a[%a\-]*)%s(%a[%a\-]*)\,%s(%a[%a\-]*)%s*$' );
function expandSpecialsQualifiers( entity, propertyId, data )
if ( f1 ) then
if ( entity.claims ~= nil and entity.claims[propertyId] ~= nil ) then
mw.log( 'personNameToResponsibleName: «' .. fullName .. '»: have «Fa Fa, I» match' );
for _, claim in pairs( entity.claims[propertyId] ) do
return mw.ustring.sub( i, 1, 1 ) .. ' ' .. f1 .. ' ' .. f2;
populateDataFromClaims( nil, claim.qualifiers, data );
end
end
end
 
local i, o, f = mw.ustring.match( fullName, '^%s*(%a)\.%s(%a)\.%s(%a[%a\-]+)%s*$');
function findClaimsByValue( entity, propertyId, value )
if ( f ) then
local result = {};
mw.log( 'v: «' .. fullName .. '»: have «I. O. Fa» match' );
if ( entity and entity.claims and entity.claims[propertyId] ) then
return i .. '. ' .. o .. '. ' .. f;
for i, claim in pairs( entity.claims[propertyId] ) do
if ( claim.mainsnak and claim.mainsnak.datavalue ) then
local datavalue = claim.mainsnak.datavalue;
if ( datavalue.type == "string" and datavalue.value == value
or datavalue.type == "wikibase-entityid" and datavalue.value["entity-type"] == "item" and tostring( datavalue.value["numeric-id"] ) == mw.ustring.sub( value, 2 ) ) then
table.insert( result, claim );
end
end
end
end
return result;
end
 
local i1, i2, i3, f = mw.ustring.match( fullName, '^%s*(%a)\.%s(%a)\.%s(%a)\.%s(%a[%a\-]+)%s*$');
function appendSnaks( allSnaks, snakPropertyId, result, property, options )
if ( f ) then
-- do not populate twice
mw.log( 'personNameToResponsibleName: «' .. fullName .. '»: have «I. O. ?. Fa» match' );
if ( result[property] ) then return result end;
return i1 .. '. ' .. i2 .. '. ' .. i3 .. '. ' .. f;
if ( not allSnaks ) then return result; end;
local selectedSnakes = allSnaks[ snakPropertyId ];
if ( not selectedSnakes ) then return result; end;
 
local hasPreferred = false;
for k, snak in pairs( selectedSnakes ) do
if ( snak and snak.mainsnak and snak.mainsnak.datavalue and snak.rank == 'preferred' ) then
--it's a preferred claim
appendImpl( snak.mainsnak.datavalue, snak.qualifiers, result, property, options );
hasPreferred = true;
end
end
if ( hasPreferred ) then return result; end;
 
-- Joel J. P. C. Rodrigues
for k, snak in pairs( selectedSnakes ) do
local i1, i2, i3, i4, f = mw.ustring.match( fullName, '^%s*(%a)[%a\-]+%s(%a)\.%s(%a)\.%s(%a)\.%s(%a[%a\-]+)%s*$');
if ( snak and snak.mainsnak and snak.mainsnak.datavalue and snak.rank ~= 'deprecated' ) then
if ( f ) then
--it's a claim
mw.log( 'personNameToResponsibleName: «' .. fullName .. '»: have «I. O. ?. Fa» match' );
appendImpl( snak.mainsnak.datavalue, snak.qualifiers, result, property, options );
return i1 .. '. ' .. i2 .. '. ' .. i3 .. '. ' .. i4 .. '. ' .. f;
elseif ( snak and snak.datavalue ) then
-- it's a snak
appendImpl( snak.datavalue, nil, result, property, options );
end
end
end
 
local i, o, f = mw.ustring.match( fullName, '^%s*(%a[%a\-]*)%s(%a)\.%s(%a[%a\-]*)%s*$');
function appendQualifiers( claims, qualifierPropertyId, result, resultProperty, options )
if ( f ) then
-- do not populate twice
mw.log( 'personNameToResponsibleName: «' .. fullName .. '»: have «Im O. Fa» match' );
if ( not claims ) then return result end;
return mw.ustring.sub( i, 1, 1 ) .. '. ' .. o .. '. ' .. f;
if ( result[resultProperty] ) then return result end;
 
for i, claim in pairs( claims ) do
if ( claim.qualifiers and claim.qualifiers[ qualifierPropertyId ] ) then
for k, qualifier in pairs( claim.qualifiers[ qualifierPropertyId ] ) do
if ( qualifier and qualifier.datavalue ) then
appendImpl( qualifier.datavalue, nil, result, resultProperty, options );
end
end
end
end
end
 
local i, o, f = mw.ustring.match( fullName, '^%s*(%a[%a\-]*)%s(%a[%a\-]*)%s(%a[%a\-]*)%s*$');
function appendImpl( datavalue, qualifiers, result, property, options )
if ( datavalue.type == 'string'f ) then
mw.log( 'personNameToResponsibleName: «' .. fullName .. '»: have «Im Ot Fa» match' );
local statedAs = getSingleStringQualifierValue(qualifiers, 'P1932');
return mw.ustring.sub( i, 1, 1 ) .. '. ' .. mw.ustring.sub( o, 1, 1 ) .. '. ' .. f;
local value;
if ( statedAs ) then
value = statedAs;
else
value = datavalue.value;
if ( options.format ) then
value = options.format( value );
end
end
appendImpl_toTable( result, property );
table.insert( result[property], value);
elseif ( datavalue.type == 'monolingualtext' ) then
local value = datavalue.value.text;
if ( options.format ) then
value = options.format( value );
end
appendImpl_toTable( result, property );
table.insert( result[property], value);
elseif ( datavalue.type == 'quantity' ) then
local value = datavalue.value.amount;
if ( mw.ustring.sub( value , 1, 1 ) == '+' ) then
value = mw.ustring.sub( value , 2 );
end
if ( options.format ) then
value = options.format( value );
end
appendImpl_toTable( result, property );
table.insert( result[property], value);
elseif ( datavalue.type == 'wikibase-entityid' ) then
local value = datavalue.value;
appendImpl_toTable( result, property );
local toInsert = {
id = 'Q' .. value["numeric-id"],
label = getSingleStringQualifierValue(qualifiers, 'P1932') -- stated as
};
table.insert( result[property], toInsert );
elseif datavalue.type == 'time' then
local value = datavalue.value;
if ( options.format ) then
value = options.format( value );
end
appendImpl_toTable( result, property );
table.insert( result[property], tostring( value.time ));
end
end
 
function appendImpl_toTable(result, resultProperty)
if ( not result[resultProperty] ) then
result[resultProperty] = {};
elseif ( type( result[resultProperty] ) == 'string' or ( type( result[resultProperty] ) == 'table' and type( result[resultProperty].id ) == 'string' ) ) then
result[resultProperty] = { result[resultProperty] };
end
end
 
local i, o, f = mw.ustring.match( fullName, '^%s*(%a[%a\-]+)%s(%a[%a\-]+)%s+оглы%s+(%a[%a\-]+)%s*$');
function getSingleStringQualifierValue( allQualifiers, qualifierPropertyId )
if ( not allQualifiersf ) then return end
mw.log( 'personNameToResponsibleName: «' .. fullName .. '»: have «Im Ot оглы Fa» match' );
if ( not allQualifiers[qualifierPropertyId] ) then return end
return mw.ustring.sub( i, 1, 1 ) .. '. ' .. mw.ustring.sub( o, 1, 1 ) .. '. ' .. f;
 
for k, qualifier in pairs( allQualifiers[qualifierPropertyId] ) do
if ( qualifier and qualifier.datatype == 'string'
and qualifier.datavalue and qualifier.datavalue.type == 'string' and not isEmpty( qualifier.datavalue.value ) ) then
return qualifier.datavalue.value;
end
end
 
local i, f = mw.ustring.match( fullName, '^%s*(%a[%a\-\']+)%s(%a[%a\-\']+)%s*$');
return;
if ( f ) then
end
mw.log( 'personNameToResponsibleName: «' .. fullName .. '»: have «Im Fa» match' );
 
return mw.ustring.sub( i, 1, 1 ) .. '. ' .. f;
function expandPublication( context, sourceEntity, data )
local publication = data.publication;
 
-- use only first one
if ( type( publication ) == 'table' and publication[1] and publication[1].id ) then
data.publication = publication[1];
publication = data.publication;
end
 
mw.log( 'Unmatched any pattern: «' .. fullName .. '»' );
if ( not publication ) then return end;
return fullName;
if ( not publication.id ) then return end;
 
if ( sourceEntity ) then
-- do we have appropriate record in P1433 ?
local claims = findClaimsByValue( sourceEntity, 'P1433', publication.id );
if ( claims and #claims ~= 0 ) then
for _, claim in pairs( claims ) do
populateDataFromClaims( sourceEntity, claim.qualifiers, data );
break;
end
end
end
 
local titleWerePresent = not (not data.title);
local pubEntity = getEntity( context, publication.id );
populateSourceDataImpl( pubEntity, data );
if ( titleWerePresent and isEmpty( data.publication.label ) ) then
appendSnaks( pubEntity.claims, 'P1160', data, 'publication-title', {} ); -- obsolete
data.publication.label = getSingle( data['publication-title'] );
end
if ( titleWerePresent and isEmpty( data.publication.label ) ) then
appendSnaks( pubEntity.claims, 'P357', data, 'publication-title', {} ); -- obsolete
appendSnaks( pubEntity.claims, 'P1476', data, 'publication-title', {} );
appendSnaks( pubEntity.claims, 'P1680', data, 'publication-subtitle', {} );
data.publication.label = getSingle( data['publication-title'] );
data.publication.subtitle = getSingle( data['publication-subtitle'] );
end
 
if ( pubEntity.claims and pubEntity.claims.P361 ) then
for c, claim in pairs( pubEntity.claims.P361 ) do
if ( claim and claim.mainsnak and claim.mainsnak.datavalue and claim.mainsnak.datavalue.value and claim.mainsnak.datavalue.value["numeric-id"] ) then
local possibleBookSeriesEntityId = 'Q' .. claim.mainsnak.datavalue.value["numeric-id"];
local possibleBookSeriesEntity = getEntity( context, possibleBookSeriesEntityId );
if ( isInstanceOf( possibleBookSeriesEntity, 'Q277759' ) ) then
appendImpl_toTable( data, 'bookSeries' );
table.insert( data.bookSeries, { id = possibleBookSeriesEntityId } );
 
appendQualifiers( { claim }, 'P478', data, 'bookSeriesVolume', {} );
appendQualifiers( { claim }, 'P433', data, 'bookSeriesIssue', {} );
end
end
end
end
 
expandBookSeries( context, data );
end
 
function expandBookSeries( context, data )
local bookSeries = data.bookSeries;
if ( not bookSeries ) then return end;
 
-- use only first one
if ( type( bookSeries ) == 'table' and bookSeries[1] and bookSeries[1].id ) then
data.bookSeries = bookSeries[1];
bookSeries = data.bookSeries;
end
 
if ( not bookSeries ) then return end;
if ( not bookSeries.id ) then return end;
 
local bookSeriesEntity = getEntity( context, bookSeries.id );
appendSnaks( bookSeriesEntity.claims, 'P123', data, 'publisher', {} );
appendSnaks( bookSeriesEntity.claims, 'P291', data, 'place', {} );
appendSnaks( bookSeriesEntity.claims, 'P236', data, 'issn', {} );
end
 
function populateSourceDataImpl( entity, plainData )
populateDataFromClaims( entity.id, entity.claims, plainData );
 
local normativeTitle = getNormativeTitle( entity )
if ( normativeTitle ) then
local y, m, d = mw.ustring.match( getSingle( plainData.dateOfCreation ) , "(%-?%d+)%-(%d+)%-(%d+)T" );
y,m,d = tonumber(y),tonumber(m),tonumber(d);
local title = toString( { lang='uk' }, plainData.title, options_commas_nolinks );
plainData.title = { normativeTitle .. " от " .. tostring(d) .. " " .. monthg[m] .. " " .. tostring(y) .. " г. № " .. getSingle( plainData.docNumber ) .. ' «' .. title.. '»' }
end
 
if ( not plainData.title ) then
if ( entity.labels and entity.labels.uk and entity.labels.uk.value ) then
plainData.title = { entity.labels.uk.value };
end
end
 
return plainData;
end
 
function populateDataFromClaims( entityId, claims, data )
appendSnaks( claims, 'P50', data, 'author', {} );
appendSnaks( claims, 'P2093', data, 'author', {} );
appendSnaks( claims, 'P407', data, 'lang', {} );
appendSnaks( claims, 'P364', data, 'lang', {} );
appendSnaks( claims, 'P958', data, 'part', {} );
if ( not data.title ) then
if ( not isEmpty( entityId ) ) then
local optionsAsLinks = { format = function( text ) return { id = entityId, label = text } end };
appendSnaks( claims, 'P357', data, 'title', optionsAsLinks ); -- obsolete
appendSnaks( claims, 'P1476', data, 'title', optionsAsLinks );
else
appendSnaks( claims, 'P357', data, 'title', {} ); -- obsolete
appendSnaks( claims, 'P1476', data, 'title', {} );
end
appendSnaks( claims, 'P1680', data, 'subtitle', {} );
end
appendSnaks( claims, 'P953', data, 'url', {} );
appendSnaks( claims, 'P1065', data, 'url', {} );
appendSnaks( claims, 'P854', data, 'url', {} );
-- temp disable, use only for current entity, see Q22338048 for example of incorrect work
-- appendSnaks( claims, 'P856', data, 'url', {} );
appendSnaks( claims, 'P1433', data, 'publication', {} );
appendSnaks( claims, 'P393', data, 'edition', {} );
appendSnaks( claims, 'P123', data, 'publisher', {} );
appendSnaks( claims, 'P291', data, 'place', {} );
appendSnaks( claims, 'P304', data, 'pages', {} );
appendSnaks( claims, 'P1104', data, 'numberOfPages', {} );
appendSnaks( claims, 'P478', data, 'volume', {} );
appendSnaks( claims, 'P433', data, 'issue', {} );
appendSnaks( claims, 'P571', data, 'dateOfCreation', {} );
appendSnaks( claims, 'P577', data, 'dateOfPublication', {} );
appendSnaks( claims, 'P212', data, 'isbn', {} ); -- ISBN-13
appendSnaks( claims, 'P957', data, 'isbn', {} ); -- ISBN-10
appendSnaks( claims, 'P236', data, 'issn', {} );
 
-- web
-- appendSnaks( claims, 'P813', data, 'accessdate', {} );
 
-- docs
appendSnaks( claims, 'P1545', data, 'docNumber', {} );
 
-- other
appendSnaks( claims, 'P31', data, 'type', {} );
 
appendSnaks( claims, 'P818', data, 'arxiv', {} );
appendSnaks( claims, 'P356', data, 'doi', {} );
-- JSTOR
appendSnaks( claims, 'P888', data, 'url', { format = function( id ) return 'http://www.jstor.org/stable/' .. id end } );
 
return src;
end
 
Рядок 1054 ⟶ 537:
 
return p.renderSourceImpl( mw.text.trim( arg ), args );
end
 
function copyArgsToSnaks( args, snaks )
if ( not isEmpty( args.part ) ) then snaks.P958 = { toStringSnak( 'P958', tostring( args.part ) ) } end
if ( not isEmpty( args.pages ) ) then snaks.P304 = { toStringSnak( 'P304', tostring( args.pages ) ) } end
if ( not isEmpty( args.issue ) ) then snaks.P433 = { toStringSnak( 'P433', tostring( args.issue ) ) } end
if ( not isEmpty( args.volume ) ) then snaks.P478 = { toStringSnak( 'P478', tostring( args.volume ) ) } end
if ( not isEmpty( args.url ) ) then snaks.P953 = { toUrlSnak( 'P953', tostring( args.url ) ) } end
end
 
Рядок 1123 ⟶ 598:
 
-- забрать данные из reference
populateDataFromClaims( context, nil, reference.snaks, data )
 
-- update ref name with ref-specific properties
Рядок 1134 ⟶ 609:
end
 
expandSpecials( context, currentEntity, reference, data );
 
local sourceEntity = nil;
Рядок 1140 ⟶ 615:
sourceEntity = getEntity( context, data.sourceId );
if ( sourceEntity ) then
populateSourceDataImpl( context, sourceEntity, data );
end
end
Рядок 1147 ⟶ 622:
expandPublication( context, sourceEntity, data );
end
 
expandBookSeries( context, data );
 
if ( next( data ) == nil ) then
Рядок 1172 ⟶ 649:
 
return rendered;
end
 
function getNormativeTitle( entity )
if ( not entity or not entity.claims or not entity.claims.P31 ) then
return;
end
 
for _, claim in pairs( entity.claims.P31 ) do
if ( claim and claim.mainsnak and claim.mainsnak.datavalue and claim.mainsnak.datavalue.value and claim.mainsnak.datavalue.value["numeric-id"] ) then
local classId = 'Q' .. claim.mainsnak.datavalue.value["numeric-id"];
local title = nil;
if ( title ) then
return title;
end
end
end
 
return;
end
 
local LANG_CACHE = {
Q150 = 'fr',
Q188 = 'de',
Q1321 = 'es',
Q1860 = 'en',
Q652 = 'it'
}
 
function getLangCode( langEntityId )
if ( not langEntityId ) then
return;
end
 
-- small optimization
local cached = LANG_CACHE[ langEntityId ];
if ( cached ) then return cached; end
 
local langEntity = mw.wikibase.getEntity( langEntityId );
if ( not langEntity ) then
mw.log( '[getLangCode] Missing entity ' .. langEntityId );
else
if ( langEntity.claims and langEntity.claims.P424 ) then
for _, claim in pairs( langEntity.claims.P424 ) do
if ( claim
and claim.mainsnak
and claim.mainsnak.datavalue
and claim.mainsnak.datavalue.value ) then
return '' .. claim.mainsnak.datavalue.value;
end
end
end
end
 
return;
end
 
function preprocessPlaces( data, lang )
if ( not data.place ) then
return;
end;
 
local newPlaces = {};
for index, place in pairs( data.place ) do
if ( place.id ) then
local newPlaceStr = getPlaceName(lang, place.id)
if ( newPlaceStr ) then
newPlaces[index] = newPlaceStr;
else
newPlaces[index] = place;
end
else
newPlaces[index] = place;
end
end
data.place = newPlaces;
end
 
function getPlaceName( lang, placeId )
return nil;
end
 
function toTextWithTip( text, tip )
return '<span title="' .. tip .. '" style="border-bottom: 1px dotted; cursor: help; white-space: nowrap">' .. text .. '</span>';
end