Модуль:Sources-authors
Поделись знанием:
Документация
Возвращает перечисление авторов источника для включения в виде параметра ref в шаблоне {{Source}} гаджете вставки источника (см. MediaWiki:Gadget-wefsources.js)
Тесты [ править ]
5 tests failed.
test_Sources:
Во избежание поломок страниц, использующих данный модуль, желательно экспериментировать в Песочнице для модулей.
local p = {}; local u = require('Module:Sources-utils') local i18nEtAlDefault = ' et al.'; local i18nEtAl = { ru = ' и др.', } function p.renderSourceAuthors( frame ) p.currentFrame = frame; return p.renderSourceAuthorsImpl( mw.text.trim( frame.args[1] ) ); end function p.renderSourceAuthorsImpl( entityId ) local snaks = {}; snaks.P248 = { toWikibaseEntityIdSnak( 'P248', entityId ) }; local rendered = renderSourceAuthors_1( mw.wikibase.getEntity(), { snaks = snaks } ); if ( rendered ) then return rendered.text end; end function renderSourceAuthors_1( currentEntity, reference ) if ( not reference.snaks ) then return nil; end -- контекст, содержит также кеш элементов local context = { cache = {}, } -- данные в простом формате, согласованном с модулями формирования библиографического описания local data = {}; -- забрать данные из reference populateDataFromClaims( context, nil, reference.snaks, data ) expandSpecials( context, currentEntity, reference, data ); local sourceEntity = nil; if ( data.sourceId ) then sourceEntity = getEntity( context, data.sourceId ); if ( sourceEntity ) then populateSourceDataImpl( context, sourceEntity, data ); end end if ( data.publication ) then expandPublication( context, sourceEntity, data ); end if ( next( data ) == nil ) then return nil; end local rendered = renderSourceAuthors_2( context, data ); if ( mw.ustring.len( rendered.text ) == 0 ) then return nil; end return rendered; end local options_commas = { separator = ', ', conjunction = ', ', format = function( src ) return src end, nolinks = true, preferids = false }; function renderSourceAuthors_2( context, src ) context.lang = getLangCode( getSingle( src.lang ) ) or u.i18nDefaultLanguage; local result = ''; if ( src.author ) then result = getPeopleAsAuthorWikitext( context, src.author, options_commas ); end return {text = result, code = src.code}; end function getPeopleAsAuthorWikitext( context, value, options ) if ( type( value ) == 'string' ) then return personNameToAuthorName( value ); elseif ( type( value ) == 'table' ) then if ( value.id ) then -- this is link if ( options.preferids ) then return value.id; else return getPersonNameAsAuthorLabel( context, value.id, value.label, options ); end end local resultList = {}; for i, tableValue in pairs( value ) do local nextWikitext = getPeopleAsAuthorWikitext( context, tableValue, options ); if ( not isEmpty( nextWikitext ) ) then table.insert( resultList, nextWikitext ); if ( #resultList == 4 ) then -- even 4 is too much, but we preserve 4th to mark that "it's more than 3" break; end end end local resultWikitext = ''; for i, wikitext in pairs( resultList ) do if ( i == 4 ) then resultWikitext = resultWikitext .. ( i18nEtAl[ context.lang ] or i18nEtAlDefault ); break; end if ( i ~= 1 ) then resultWikitext = resultWikitext .. ', '; end resultWikitext = resultWikitext .. wikitext; end return resultWikitext; end return options.format( '(unknown type)' ); end function getPersonNameAsAuthorLabel( 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 personNameToAuthorName( 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; -- support only labels so far if ( entity.labels[ context.lang ] ) then personName = entity.labels[ context.lang ].value; mw.log('Got person name of ' .. entityId .. ' from label: «' .. personName .. '»' ) end if ( isEmpty( personName ) ) then return '\'\'(not translated to ' .. context.lang .. ')\'\''; else return personNameToAuthorName( personName ); end end function personNameToAuthorName( fullName ) if ( not fullName ) then return fullName; end local f, i, o = mw.ustring.match( fullName, '^%s*(%a[%a\-]*)\,%s(%a[%a\-]*)%s(%a[%a\-]*)%s*$' ); if ( f ) then mw.log( 'personNameToAuthorName: «' .. fullName .. '»: have «Fa, I. O.» match' ); return f; end local f1, f2, i = mw.ustring.match( fullName, '^%s*(%a[%a\-]*)%s(%a[%a\-]*)\,%s(%a[%a\-]*)%s*$' ); if ( f1 ) then mw.log( 'personNameToAuthorName: «' .. fullName .. '»: have «Fa Fa, I» match' ); return f1 .. ' ' .. f2; end local i, o, f = mw.ustring.match( fullName, '^%s*(%a)\.%s(%a)\.%s(%a[%a\-]+)%s*$'); if ( f ) then mw.log( 'personNameToAuthorName: «' .. fullName .. '»: have «I. O. Fa» match' ); return f; end local i1, i2, i3, f = mw.ustring.match( fullName, '^%s*(%a)\.%s(%a)\.%s(%a)\.%s(%a[%a\-]+)%s*$'); if ( f ) then mw.log( 'personNameToAuthorName: «' .. fullName .. '»: have «I. O. ?. Fa» match' ); return f; end -- Joel J. P. C. Rodrigues local i1, i2, i3, i4, f = mw.ustring.match( fullName, '^%s*(%a)[%a\-]+%s(%a)\.%s(%a)\.%s(%a)\.%s(%a[%a\-]+)%s*$'); if ( f ) then mw.log( 'personNameToAuthorName: «' .. fullName .. '»: have «I. O. ?. Fa» match' ); return f; end local i, o, f = mw.ustring.match( fullName, '^%s*(%a[%a\-]*)%s(%a)\.%s(%a[%a\-]*)%s*$'); if ( f ) then mw.log( 'personNameToAuthorName: «' .. fullName .. '»: have «Im O. Fa» match' ); return f; end local i, o, f = mw.ustring.match( fullName, '^%s*(%a[%a\-]*)%s(%a[%a\-]*)%s(%a[%a\-]*)%s*$'); if ( f ) then mw.log( 'personNameToAuthorName: «' .. fullName .. '»: have «Im Ot Fa» match' ); return f; end local i, o, f = mw.ustring.match( fullName, '^%s*(%a[%a\-]+)%s(%a[%a\-]+)%s+оглы%s+(%a[%a\-]+)%s*$'); if ( f ) then mw.log( 'personNameToAuthorName: «' .. fullName .. '»: have «Im Ot оглы Fa» match' ); return f; 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' ); return f; end mw.log( 'Unmatched any pattern: «' .. fullName .. '»' ); return fullName; end return p;