Модуль:Universal infocard
Копия модуля ru:Модуль:Universal infocard из русской Википедии. Используется в шаблоне {{Универсальной карточка}}.
Не правьте текст самого модуля, все изменения будут затёрты при следующем обновлении. Настройки модуля можно изменить на подстранице Модуль:Universal infocard/config.
--[[
Lua code for universal infocard.
]]
local isConfig, config = pcall( require, 'Module:Universal infocard/config' );
if isConfig == false then
config = {
skipPropertyIds = {
P31 = true,
P279 = true,
}
};
end
local p = {};
local lang = mw.getContentLanguage();
local entityId = nil;
-- Styles.
local styles = {
infobox = '',
error = '',
title = 'font-size:125%; font-weight:bold; background:#eaecf0',
original = 'font-style:oblique',
label = 'width:9em; background:#eaecf0',
text = '',
media = '',
split = 'vertical-align:middle; text-align:center',
footer = 'background:#eaecf0',
};
if config and config.styles then
for key, value in pairs( config.styles ) do
styles[ key ] = value;
end
end
function getTemplate( propertyId )
if config and config.templates then
if config.templates[ propertyId ] then
return config.templates[ propertyId ];
end
if config.templates.default then
return config.templates.default;
end
end
if propertyId == 'title' then
return '{{PAGENAME}}';
end
if string.match( propertyId, '^P%d+$' ) then
return '#statements:' .. propertyId;
end
return nil;
end
function expandTemplate( frame, title, args )
if not title then
return '';
elseif string.match( title, '^#' ) then
return frame:callParserFunction{ name = title, args = args };
elseif string.match( title, '^{' ) then
return frame:preprocess( title );
else
return frame:expandTemplate{ title = title, args = args };
end
end
function splitLine( value1, value2 )
local result = '';
if ( value1 and string.len( value1 ) ~= 0 ) or ( value2 and string.len( value2 ) ~= 0 ) then
result = '<tr>';
if ( value1 and string.len( value1 ) ~= 0 ) then
local colspan = '';
if ( not value2 or string.len( value2 ) == 0 ) then
colspan = 'colspan="2"';
end
result = result .. '<td ' .. colspan .. ' style="' .. styles.split .. '">';
result = result .. value1;
result = result .. '</td>';
end
if ( value2 and string.len( value2 ) ~= 0 ) then
local colspan = '';
if ( not value1 or string.len( value1 ) == 0 ) then
colspan = 'colspan="2"';
end
result = result .. '<td ' .. colspan .. ' style="' .. styles.split .. '">';
result = result .. value2;
result = result .. '</td>';
end
result = result .. '</tr>\n';
end
return result;
end
function getLine( value, style )
local result = '';
if ( value and string.len( value ) ~= 0 ) then
result = result .. '<tr><td colspan="2" style="text-align:center;' .. ( style or '' ) .. '">';
result = result .. value;
result = result .. '</td></tr>\n';
return result;
end
return result;
end
function getValue( label, value )
local result = '';
if ( value ~= nil and string.len( value ) ~= 0 ) then
if label then
result = result .. '<tr><th style="' .. styles.label .. '">' .. label .. '</th>';
result = result .. '<td style="' .. styles.text .. '">\n';
else
result = result .. '<tr><td colspan="2" style="' .. styles.text .. '">';
end
result = result .. value;
result = result .. '</td></tr>\n';
return result;
end
return result;
end
function renderValue( frame, propertyId, args )
local tplArgs = { propertyId, from = entityId };
if args then
local k = nil;
repeat
k = next( args, k );
if k then
tplArgs[ k ] = args[ k ];
end
until not k
end
return expandTemplate( frame, getTemplate( propertyId ), tplArgs );
end
-- Filter deprecated claims and returning only preferred ones if present.
function filterClaims( entity, propertyId )
if ( entity.claims == nil or entity.claims[ propertyId ] == nil ) then
return {};
end
local all = entity.claims[ propertyId ];
local normal = {};
local preferred = {};
for _, claim in pairs( all ) do
local rank = claim.rank or 'normal';
if ( rank == 'normal' ) then
table.insert( normal, claim );
end
if ( rank == 'preferred' ) then
table.insert( preferred, claim );
end
end
if ( #preferred > 0 ) then
return preferred;
end
return normal;
end
-- Filter deprecated claims and returning only preferred ones if present.
function propertyHasEntity( claims, itemId )
if not claims then
return false;
end
for _, claim in pairs( claims ) do
if claim.mainsnak
and claim.mainsnak.datavalue
and claim.mainsnak.datavalue.value
and claim.mainsnak.datavalue.value.id
and claim.mainsnak.datavalue.value.id == itemId then
return true;
end
end
return false;
end
function propertyLabel( propertyId )
local label, labelLang = mw.wikibase.getLabelWithLang( propertyId );
label = lang:ucfirst( label );
if labelLang ~= lang:getCode() then
label = '[[d:Property:' .. propertyId .. '|' .. label .. ']]';
end
return label;
end
function simpleLabel( entityId )
local label = mw.wikibase.label( entityId );
label = lang:ucfirst( label );
return label;
end
function getErrorMessage( message )
local result = '<table class="infobox" cellspacing="2"';
if config and config.styles and config.styles.inferrorobox then
result = result .. ' style="' .. config.styles.error .. '"';
end
result = result .. '>\n';
result = result .. '<tr><td colspan="2" style="text-align:center">' .. message .. '</td></tr>\n';
result = result .. '</table>';
return result;
end
function p.render( frame )
local i18n_error_emptyWikidataEntity = '';
local i18n_error_noWikidataEntity = '';
if config and config.i18n and config.i18n.error then
if config.i18n.error.emptyWikidataEntity then
i18n_error_emptyWikidataEntity = config.i18n.error.emptyWikidataEntity;
end
if config.i18n.error.noWikidataEntity then
i18n_error_noWikidataEntity = config.i18n.error.noWikidataEntity;
end
end
local result = '<table class="infobox" cellspacing="2"';
if config and config.styles and config.styles.infobox then
result = result .. ' style="' .. config.styles.infobox .. '"';
end
result = result .. '>\n';
local localImage = nil;
if ( frame ~= nil and frame:getParent() ~= nil ) then
local p_frame = frame:getParent();
if p_frame.args ~= nil then
-- image under FU only in local
localImage = p_frame.args.image;
if p_frame.args.from ~= nil and p_frame.args.from ~= '' then
entityId = p_frame.args.from;
elseif p_frame.args[ 1 ] ~= nil and string.gmatch( p_frame.args[ 1 ], '^Q\d+$' ) then
entityId = p_frame.args[ 1 ];
end
end
end
local wdStatus, entity = pcall( mw.wikibase.getEntity, entityId );
if wdStatus ~= true or entity == nil then
return getErrorMessage( i18n_error_noWikidataEntity );
elseif entity.claims == nil then
return getErrorMessage( i18n_error_emptyWikidataEntity );
end
-- TODO: Need to consider how to display class properties (P31, P279, P361, ...).
local skipPropertyIds = {};
if config.skipPropertyIds then
skipPropertyIds = mw.clone( config.skipPropertyIds );
end
local claims = entity.claims;
local order = mw.wikibase.getPropertyOrder() or {};
-- Header.
local entityLabel, entityLabelLang = entity:getLabelWithLang( lang:getCode() );
local label;
---- Name.
local titleTemplate = getTemplate( 'title' );
-- TODO: Make it possible to specify a template for any value, not just Q5.
if propertyHasEntity( claims.P31, 'Q5' ) then
local titleTemplateQ5 = getTemplate( 'title_Q5' );
if titleTemplateQ5 then
titleTemplate = titleTemplateQ5;
end
end
if entityLabelLang == lang:getCode() then
label = expandTemplate( frame, titleTemplate, { wdLabel, from = entityId } );
else
label = expandTemplate( frame, titleTemplate, { from = entityId } );
end
result = result .. getLine( label, styles.title );
---- Original name.
if claims.P1559 ~= nil then
result = result .. getLine( expandTemplate( frame, getTemplate( 'P1559' ), { from = entityId } ), styles.original );
elseif claims.P1705 ~= nil then
result = result .. getLine( expandTemplate( frame, getTemplate( 'P1705' ), { from = entityId } ), styles.original );
end
---- Flag and COA.
if claims.P41 or claims.P94 then
local flag = nil;
local flagLabel = nil;
local coa = nil;
local coaLabel = nil;
if claims.P41 then
flag = renderValue( frame, 'P41' );
if claims.P163 then
flagLabel = renderValue( frame, 'P163', { text = simpleLabel( 'Q14660' ) } );
else
flagLabel = simpleLabel( 'Q14660' );
end
end
if claims.P94 then
coa = renderValue( frame, 'P94' );
if claims.P163 then
coaLabel = renderValue( frame, 'P237', { text = simpleLabel( 'Q14659' ) } );
else
coaLabel = simpleLabel( 'Q14659' );
end
end
result = result .. splitLine( flagLabel, coaLabel );
result = result .. splitLine( flag, coa );
end
-- Body.
local propertyIds = {};
for propertyId, claim in pairs( entity.claims ) do
table.insert( propertyIds, propertyId );
end
local orderedProperties = mw.wikibase.orderProperties( propertyIds )
for i, propertyId in ipairs( orderedProperties ) do
local propertyClaims = claims[ propertyId ];
if not skipPropertyIds[ propertyId ]
and propertyClaims
and propertyClaims[ 1 ]
and propertyClaims[ 1 ].mainsnak
and propertyClaims[ 1 ].mainsnak.datatype
and propertyClaims[ 1 ].mainsnak.datatype ~= 'external-id'
then
local label = propertyLabel( propertyId );
if propertyClaims[ 1 ].mainsnak.datatype == 'commonsMedia' then
result = result .. getLine( renderValue( frame, propertyId, { alt = label } ), styles.media );
else
result = result .. getValue( label, renderValue( frame, propertyId ) );
end
skipPropertyIds[ propertyId ] = true
end
end
-- Footer.
if claims.P625 ~= nil then
if claims.P17 ~= nil then
-- TODO: Don't use templates specific to Russian Wikipedia by default,
-- but give an option to specify it through the settings.
result = result .. getLine( expandTemplate(
frame,
'wikidata-coords',
{
'ЭТ/ПозКарта',
['эрзянь лем'] = expandTemplate( frame, getTemplate( 'title' ), { from = entityId } ),
['Мастор'] = renderValue( frame, 'P17', { plain = 'true', from = entityId } ),
from = entityId,
}
), styles.footer );
end
end
---- Commons.
if claims.P373 ~= nil then
result = result .. getLine( expandTemplate( frame, getTemplate( 'P373' ), { from = entityId } ), styles.footer );
end
result = result .. '</table>';
---- Coords.
if claims.P625 ~= nil then
result = result .. renderValue( frame, 'P625', { display = 'title', from = entityId } );
end
return result;
end
return p;