Модуль:Вікізьвесткі/элемэнт
Дакумэнтацыю да гэтага модуля можна стварыць у Модуль:Вікізьвесткі/элемэнт/Дакумэнтацыя
local WDS = require( 'Модуль:Сьцьверджаньні Вікізьвестак' );
local p = {};
local categoryLinksToEntitiesWithMissingLabel = '[[Катэгорыя:Вікіпэдыя:Артыкулы з элемэнтамі зь Вікізьвестак, якія патрабуюць перакладу]]';
local function getGenderLabelForEntityId( entityId, gender )
local genderPropertyId
if gender == 1 then
genderPropertyId = 'P3321'
elseif gender == 2 then
genderPropertyId = 'P2521'
else
return mw.wikibase.label( entityId )
end
local genderLabels = mw.wikibase.getAllStatements( entityId, genderPropertyId )
if genderLabels then
genderLabels = WDS.filterByLanguage( WDS.filterByRank( genderLabels, 'deprecated', true ), 'be-tarask' );
end
if genderLabels and
genderLabels[ 1 ] and
genderLabels[ 1 ].mainsnak and
genderLabels[ 1 ].mainsnak.datatype == 'monolingualtext'
then
return genderLabels[ 1 ].mainsnak.datavalue.value.text;
end
return mw.wikibase.label( entityId )
end
local function formatOccupationLinkForEntityId( entityId, gender )
local label = getGenderLabelForEntityId( entityId, gender );
-- першая спроба спаслацца на артыкул з прафэсіяй
local link = mw.wikibase.sitelink( entityId )
if link and not label then
label = link
end
-- калі артыкул пра прафэсію адсутнічае, спрабуем спаслацца на занятак
if not link then
local fooEntity = mw.wikibase.getEntity( entityId );
if fooEntity and fooEntity.claims and fooEntity.claims.P425 then
fooEntity = WDS.filterByRank( fooEntity.claims.P425, 'deprecated', true );
if fooEntity ~= nil and
fooEntity[1] and
fooEntity[1].mainsnak and
fooEntity[1].mainsnak.datavalue and
fooEntity[1].mainsnak.datavalue.value and
fooEntity[1].mainsnak.datavalue.value.id then
link = mw.wikibase.sitelink( fooEntity[1].mainsnak.datavalue.value.id );
end
end
end
-- артыкулы пра прафэсію і занятак адсутнічаюць, спасылаемся на вікізьвесткі
if not link then
link = ':d:' .. entityId
end
if label then
return '[[' .. link .. '|' .. label .. ']]'
end
-- паведамленьне пра адсутнасьць перакладзенай назвы
-- лепш чым нішто, хоць і не ідэальна
return '[[' .. link .. '|' .. entityId .. ']]<span style="border-bottom: 1px dotted; cursor: help; white-space: nowrap" title="У Вікізьвестках адсутнічае беларускі подпіс элемэнту. Вы можаце дапамагчы.">?</span>' .. categoryLinksToEntitiesWithMissingLabel;
end
function p.formatEntityWithGenderClaim( context, options, statement )
if ( not context ) then error( 'не зададзены кантэкст'); end;
if ( not options ) then error( 'не зададзеныя опцыі'); end;
if ( not options.entity ) then error( 'адсутнічае options.entity'); end;
if ( not statement ) then error( 'не зададзенае сьцьверджаньне'); end;
local gender = 0 -- паводле [[ISO/IEC 5218]]
if options.entity.claims.P21 and
options.entity.claims.P21[1] and
options.entity.claims.P21[1].mainsnak and
options.entity.claims.P21[1].mainsnak.datavalue and
options.entity.claims.P21[1].mainsnak.datavalue.value
then
local genderId = options.entity.claims.P21[1].mainsnak.datavalue.value.id
if genderId == 'Q6581097' or genderId == 'Q2449503' or genderId == 'Q44148' then
gender = 1
elseif genderId == 'Q6581072' or genderId == 'Q1052281' or genderId == 'Q43445' then
gender = 2
end
end
local snak = statement.mainsnak;
if snak and snak.datavalue and snak.datavalue.value and snak.datavalue.value.id then
local entityId = snak.datavalue.value.id;
local value = formatOccupationLinkForEntityId( entityId, gender )
return context.wrapSnak( value, snak.hash )
end
return context.formatSnak( options, snak )
end
function p.applyDefaultTemplate( context, options, statement )
local snak = context.formatSnak( options, statement.mainsnak )
if ( statement.qualifiers and statement.qualifiers.P249 and statement.qualifiers.P249[1].snaktype == 'value') then
snak = statement.qualifiers.P249[1].datavalue.value
entityId = statement.mainsnak.datavalue.value.id
wbStatus, entity = pcall( mw.wikibase.getEntity, entityId );
if ( entity.claims and entity.claims.P1424 ) then
wbStatus, template = pcall( mw.wikibase.getEntity,
entity.claims.P1424[1].mainsnak.datavalue.value.id )
if template:getSitelink( ) then
snak = options.frame:preprocess( string.gsub( template:getSitelink( ),
'Шаблён:', '{{' ) .. '|' ..
statement.qualifiers.P249[1].datavalue.value .. '}}' )
end
end
end
--Крыніцы
if ( options.references and snak ) then
snak = snak .. context.formatRefs( options, statement );
end
return snak
end
function p.formatCategoryOnly( context, options, value )
return context.extractCategory( options, value )
end
return p