Модуль:Wikidata: различия между версиями

Материал из Буинский уезд (Буинск, Байбулатово, Кайрево, Бурундуки) - генеалогические исследовании
Перейти к навигации Перейти к поиску
м (1 версия импортирована)
 
м (1 версия импортирована)
 
(не показана 1 промежуточная версия 1 участника)
Строка 1: Строка 1:
local i18n = {
 
    ["errors"] = {
 
        ["property-param-not-provided"] = "Не дан параметр свойства",
 
        ["entity-not-found"] = "Сущность не найдена.",
 
        ["unknown-claim-type"] = "Неизвестный тип заявления.",
 
        ["unknown-snak-type"] = "Неизвестный тип снэка.",
 
        ["unknown-datavalue-type"] = "Неизвестный тип значения данных.",
 
        ["unknown-entity-type"] = "Неизвестный тип сущности.",
 
        ["unknown-property-module"] = "Вы должны установить и property-module, и property-function.",
 
        ["unknown-claim-module"] = "Вы должны установить и claim-module, и claim-function.",
 
        ["unknown-value-module"] = "Вы должны установить и value-module, и value-function.",
 
        ["property-module-not-found"] = "Модуль для отображения свойства не найден",
 
        ["property-function-not-found"] = "Функция для отображения свойства не найдена",
 
        ["claim-module-not-found"] = "Модуль для отображения утверждения не найден.",
 
        ["claim-function-not-found"] = "Функция для отображения утверждения не найдена.",
 
        ["value-module-not-found"] = "Модуль для отображения значения не найден.",
 
        ["value-function-not-found"] = "Функция для отображения значения не найдена."
 
    },
 
    ["somevalue"] = "''неизвестно''",
 
    ["novalue"] = "",
 
    ["circa"] = '<span style="border-bottom: 1px dotted; cursor: help;" title="около, приблизительно">прибл. </span>',
 
    ["presumably"] = '<span style="border-bottom: 1px dotted; cursor: help;" title="предположительно">предп. </span>',
 
}
 
 
 
-- settings, may differ from project to project
 
-- settings, may differ from project to project
local categoryLinksToEntitiesWithMissingLabel = '[[Категория:Википедия:Статьи со ссылками на элементы Викиданных без русской подписи]]';
+
local fileDefaultSize = '267x400px';
local categoryLocalValuePresent = '[[Категория:Википедия:Статьи с переопределением значения из Викиданных]]';
 
 
local outputReferences = true;
 
local outputReferences = true;
  
Строка 33: Строка 8:
 
Q63056 = true, -- Find a Grave
 
Q63056 = true, -- Find a Grave
 
Q15222191 = true, -- BNF
 
Q15222191 = true, -- BNF
 +
Q15241312 = true, -- Freebase
 
};
 
};
 
local preferredSources = {
 
local preferredSources = {
Строка 40: Строка 16:
  
 
-- Ссылки на используемые модули, которые потребуются в 99% случаев загрузки страниц (чтобы иметь на виду при переименовании)
 
-- Ссылки на используемые модули, которые потребуются в 99% случаев загрузки страниц (чтобы иметь на виду при переименовании)
local moduleSources = require('Module:Sources')
+
local moduleSources = require( 'Module:Sources' )
 +
local WDS = require( 'Module:WikidataSelectors' );
  
local p = {}
+
-- Константы
 +
local contentLanguageCode = mw.getContentLanguage():getCode();
 +
 
 +
local p = {};
 +
local config = nil;
  
 
local formatDatavalue, formatEntityId, formatRefs, formatSnak, formatStatement,
 
local formatDatavalue, formatEntityId, formatRefs, formatSnak, formatStatement,
 
formatStatementDefault, formatProperty, getSourcingCircumstances,
 
formatStatementDefault, formatProperty, getSourcingCircumstances,
getPropertyDatatype, loadCacheSafe, throwError, toBoolean;
+
getPropertyDatatype, getPropertyParams, throwError, toBoolean;
  
local function copyTo( obj, target )
+
local function copyTo( obj, target, skipEmpty )
 
for k, v in pairs( obj ) do
 
for k, v in pairs( obj ) do
target[k] = v
+
if skipEmpty ~= true or ( v ~= nil and v ~= '' ) then
 +
target[k] = v;
 +
end
 
end
 
end
 
return target;
 
return target;
 
end
 
end
  
local function loadCacheSafe( entityId )
+
local function min( prev, next )
local status, result = pcall( function() return mw.loadData( 'Module:WikidataCache/' .. entityId ) end );
+
if ( prev == nil ) then return next;
if ( status == true ) then
+
elseif ( prev > next ) then return next;
return result;
+
else return prev; end
 +
end
 +
 
 +
local function max( prev, next )
 +
if ( prev == nil ) then return next;
 +
elseif ( prev < next ) then return next;
 +
else return prev; end
 +
end
 +
 
 +
local function getConfig( section, code )
 +
if config == nil then
 +
config = require( 'Module:Wikidata/config' );
 +
end;
 +
if not config then
 +
config = {};
 +
end
 +
 
 +
if not section then
 +
return config;
 
end
 
end
return nil;
+
if not code then
 +
return config[ section ] or {};
 +
end
 +
 
 +
if not config[ section ] then
 +
return nil;
 +
end
 +
return config[ section ][ code ];
 +
end
 +
 
 +
local function getCategoryByCode( code )
 +
local value = getConfig( 'categories', code );
 +
if not value or value == '' then
 +
return '';
 +
end
 +
return '[[Category:' .. value .. ']]';
 
end
 
end
  
Строка 71: Строка 87:
 
end
 
end
 
end
 
end
local Y, M, D = (function(str)  
+
local Y, M, D = (function(str)
 
local pattern = "(%-?%d+)%-(%d+)%-(%d+)T"
 
local pattern = "(%-?%d+)%-(%d+)%-(%d+)T"
 
local Y, M, D = mw.ustring.match( str, pattern )
 
local Y, M, D = mw.ustring.match( str, pattern )
 
return tonumber(Y), tonumber(M), tonumber(D)
 
return tonumber(Y), tonumber(M), tonumber(D)
 
end) (str);
 
end) (str);
local h, m, s = (function(str)  
+
local h, m, s = (function(str)
 
local pattern = "T(%d+):(%d+):(%d+)%Z";
 
local pattern = "T(%d+):(%d+):(%d+)%Z";
 
local H, M, S = mw.ustring.match( str, pattern);
 
local H, M, S = mw.ustring.match( str, pattern);
Строка 139: Строка 155:
 
end
 
end
  
--[[  
+
--[[
 
  Преобразует строку в булевое значение
 
  Преобразует строку в булевое значение
  
Строка 146: Строка 162:
 
]]
 
]]
 
local function toBoolean( valueToParse, defaultValue )
 
local function toBoolean( valueToParse, defaultValue )
    if ( valueToParse ) then
+
if ( valueToParse ~= nil ) then
        if valueToParse == '' or valueToParse == 'false' or valueToParse == '0' then
+
if valueToParse == false or valueToParse == '' or valueToParse == 'false' or valueToParse == '0' then
            return false
+
return false
        end
+
end
        return true
+
return true
    end
+
end
    return defaultValue;
+
return defaultValue;
 +
end
 +
 
 +
--[[
 +
Обрачивает отформатированное значение в тег
 +
 +
Принимает: строковое значение, строку с атрибутами (может отсутствовать)
 +
Возвращает: строковое значение, значения с блочными тегами остаются блоком, текст встраиваем в строку
 +
]]
 +
local function wrapFormatProperty( value, attributes )
 +
local tagName = 'span';
 +
local spacer = '';
 +
if ( string.match( value, '\n' )
 +
or string.match( value, '<t[dhr][ >]' )
 +
or string.match( value, '<div[ >]' )
 +
or string.find( value, 'UNIQ%-%-imagemap' ) ) then
 +
tagName = 'div';
 +
spacer = '\n'
 +
end
 +
return '<' .. tagName .. ' ' .. ( attributes or '' ) .. '>' .. spacer .. value .. '</' .. tagName .. '>';
 
end
 
end
  
--[[  
+
--[[
  Функция для получения сущности (еntity) для текущей страницы
+
Функция для получения сущности (еntity) для текущей страницы
  Подробнее о сущностях см. d:Wikidata:Glossary/ru
+
Подробнее о сущностях см. d:Wikidata:Glossary/ru
  
  Принимает: строковый индентификатор (типа P18, Q42)
+
Принимает: строковый индентификатор (типа P18, Q42)
  Возвращает: объект таблицу, элементы которой индексируются с нуля
+
Возвращает: объект таблицу, элементы которой индексируются с нуля
 
]]
 
]]
 
local function getEntityFromId( id )
 
local function getEntityFromId( id )
    if id then
+
local entity;
    local cached = loadCacheSafe( id );
+
local wbStatus;
    if ( cached ) then
+
 
    return cached;
+
if id then
    end
+
wbStatus, entity = pcall( mw.wikibase.getEntityObject, id )
        return mw.wikibase.getEntityObject( id )
+
else
    end
+
wbStatus, entity = pcall( mw.wikibase.getEntityObject );
    local entity = mw.wikibase.getEntityObject();
 
    if ( entity ) then
 
    local cached = loadCacheSafe( entity.id );
 
    if ( cached ) then
 
    return cached;
 
    end
 
 
end
 
end
    return entity;
+
 
 +
return entity;
 
end
 
end
  
--[[  
+
--[[
  Внутрення функция для формирования сообщения об ошибке
+
Внутрення функция для формирования сообщения об ошибке
+
 
  Принимает: ключ элемента в таблице i18n (например entity-not-found)
+
Принимает: ключ элемента в таблице config.errors (например entity-not-found)
  Возвращает: строку сообщения
+
Возвращает: строку сообщения
 
]]
 
]]
 
local function throwError( key )
 
local function throwError( key )
    error( i18n.errors[key] );
+
error( getConfig( 'errors', key ) );
 
end
 
end
  
--[[  
+
--[[
  Функция для получения идентификатора сущностей  
+
Функция для получения идентификатора сущностей
  
  Принимает: объект таблицу сущности
+
Принимает: объект таблицу сущности
  Возвращает: строковый индентификатор (типа P18, Q42)
+
Возвращает: строковый индентификатор (типа P18, Q42)
 
]]
 
]]
 
local function getEntityIdFromValue( value )
 
local function getEntityIdFromValue( value )
    local prefix = ''
+
local prefix = ''
    if value['entity-type'] == 'item' then
+
if value['entity-type'] == 'item' then
        prefix = 'Q'
+
prefix = 'Q'
    elseif value['entity-type'] == 'property' then
+
elseif value['entity-type'] == 'property' then
        prefix = 'P'
+
prefix = 'P'
    else
+
else
        throwError( 'unknown-entity-type' )
+
throwError( 'unknown-entity-type' )
    end
+
end
    return prefix .. value['numeric-id']
+
return prefix .. value['numeric-id']
 
end
 
end
  
 
-- проверка на наличие специилизированной функции в опциях
 
-- проверка на наличие специилизированной функции в опциях
 
local function getUserFunction( options, prefix, defaultFunction )
 
local function getUserFunction( options, prefix, defaultFunction )
    -- проверка на указание специализированных обработчиков в параметрах,
+
-- проверка на указание специализированных обработчиков в параметрах,
    -- переданных при вызове
+
-- переданных при вызове
    if options[ prefix .. '-module' ] or options[ prefix .. '-function' ] then
+
if options[ prefix .. '-module' ] or options[ prefix .. '-function' ] then
    -- проверка на пустые строки в параметрах или их отсутствие  
+
-- проверка на пустые строки в параметрах или их отсутствие
        if not options[ prefix .. '-module' ] or not options[ prefix .. '-function' ] then
+
if not options[ prefix .. '-module' ] or not options[ prefix .. '-function' ] then
            throwError( 'unknown-' .. prefix .. '-module' );
+
throwError( 'unknown-' .. prefix .. '-module' );
        end
+
end
        -- динамическая загруза модуля с обработчиком указанным в параметре
+
-- динамическая загруза модуля с обработчиком указанным в параметре
        local formatter = require ('Module:' .. options[ prefix .. '-module' ]);
+
local formatter = require( 'Module:' .. options[ prefix .. '-module' ] );
        if formatter == nil then
+
if formatter == nil then
            throwError( prefix .. '-module-not-found' )
+
throwError( prefix .. '-module-not-found' )
        end
+
end
        local fun = formatter[ options[ prefix .. '-function' ] ]
+
local fun = formatter[ options[ prefix .. '-function' ] ]
        if fun == nil then
+
if fun == nil then
            throwError( prefix .. '-function-not-found' )
+
throwError( prefix .. '-function-not-found' )
        end
+
end
        return fun;
+
return fun;
    end
+
end
  
  return defaultFunction;
+
return defaultFunction;
 
end
 
end
  
 
-- Выбирает свойства по property id, дополнительно фильтруя их по рангу
 
-- Выбирает свойства по property id, дополнительно фильтруя их по рангу
 
local function selectClaims( context, options, propertySelector )
 
local function selectClaims( context, options, propertySelector )
if ( not context ) then error( 'context not specified'); end;
+
if ( not context ) then error( 'context not specified' ); end;
if ( not options ) then error( 'options not specified'); end;
+
if ( not options ) then error( 'options not specified' ); end;
if ( not options.entity ) then error( 'options.entity is missing'); end;
+
if ( not options.entity ) then error( 'options.entity is missing' ); end;
if ( not propertySelector ) then error( 'propertySelector not specified'); end;
+
if ( not propertySelector ) then error( 'propertySelector not specified' ); end;
 +
 
 +
result = WDS.filter( options.entity.claims, propertySelector );
 +
 
 +
if ( not result or #result == 0 ) then
 +
return nil;
 +
end
 +
 
 +
if options.limit and options.limit ~= '' and options.limit ~= '-'  then
 +
local limit = tonumber( options.limit, 10 );
 +
while #result > limit do
 +
table.remove( result );
 +
end
 +
end
 +
 
 +
return result;
 +
end
 +
 
 +
--[[
 +
Функция для получения значения свойства элемента в заданный момент времени.
 +
 
 +
Принимает: контекст, элемент, временные границы, таблица ID свойства
 +
Возвращает: таблицу соответствующих значений свойства
 +
]]
 +
local function getPropertyInBoundaries( context, entityId, boundaries, propertyIds, selectors )
 +
if (type(entityId) ~= 'string') then error('type of entityId argument expected string, but was ' .. type(entityId)); end
 +
 
 +
local results = {};
 +
 
 +
if not propertyIds or #propertyIds == 0 then
 +
return results;
 +
end
 +
 
 +
for _, propertyId in ipairs( propertyIds ) do
 +
local selector = selectors[_];
 +
local propertyClaims = mw.wikibase.getAllStatements( entityId, propertyId );
 +
local fakeAllClaims = {};
 +
fakeAllClaims[propertyId] = propertyClaims;
 +
 +
local filteredClaims = WDS.filter( fakeAllClaims, selector .. '[rank:preferred, rank:normal]' );
 +
if filteredClaims then
 +
for _, claim in pairs( filteredClaims ) do
 +
if not boundaries then
 +
table.insert( results, claim.mainsnak );
 +
else
 +
local startBoundaries = p.getTimeBoundariesFromQualifier( context.frame, context, claim, 'P580' );
 +
local endBoundaries = p.getTimeBoundariesFromQualifier( context.frame, context, claim, 'P582' );
 +
 
 +
if ( (startBoundaries == nil or ( startBoundaries[2] <= boundaries[1]))
 +
and (endBoundaries == nil or ( endBoundaries[1] >= boundaries[2]))) then
 +
table.insert( results, claim.mainsnak );
 +
end
 +
end
 +
end
 +
end
 +
 
 +
if #results > 0 then
 +
break;
 +
end
 +
end
 +
 
 +
return results;
 +
end
 +
 
 +
--[[
 +
TODO
 +
]]
 +
function p.getTimeBoundariesFromQualifier( frame, context, statement, qualifierId )
 +
-- only support exact date so far, but need improvment
 +
local left = nil;
 +
local right = nil;
 +
if ( statement.qualifiers and statement.qualifiers[qualifierId] ) then
 +
for _, qualifier in pairs( statement.qualifiers[qualifierId] ) do
 +
local boundaries = context.parseTimeBoundariesFromSnak( qualifier );
 +
if ( not boundaries ) then return nil; end
 +
left = min( left, boundaries[1] );
 +
right = max( right, boundaries[2] );
 +
end
 +
end
 +
 
 +
if ( not left or not right ) then
 +
return nil;
 +
end
 +
 
 +
return { left, right };
 +
end
 +
 
 +
--[[
 +
TODO
 +
]]
 +
function p.getTimeBoundariesFromQualifiers( frame, context, statement, qualifierIds )
 +
if not qualifierIds then
 +
qualifierIds = { 'P582', 'P580', 'P585' };
 +
end
 +
 
 +
for _, qualifierId in ipairs( qualifierIds ) do
 +
local result = p.getTimeBoundariesFromQualifier( frame, context, statement, qualifierId );
 +
if result then
 +
return result;
 +
end
 +
end
 +
 
 +
return nil;
 +
end
  
local WDS = require('Module:WikidataSelectors')
+
local CONTENT_LANGUAGE_CODE = mw.language.getContentLanguage():getCode();
result = WDS.filter(options.entity.claims, propertySelector)
+
local getLabelWithLang_DEFAULT_PROPERTIES = { "P1813", "P1448", "P1705" };
 +
local getLabelWithLang_DEFAULT_SELECTORS = {
 +
'P1813[language:' .. CONTENT_LANGUAGE_CODE .. ']',
 +
'P1448[language:' .. CONTENT_LANGUAGE_CODE .. ']',
 +
'P1705[language:' .. CONTENT_LANGUAGE_CODE .. ']'
 +
};
  
    if ( not result or #result == 0 ) then
+
--[[
    return nil;
+
Функция для получения метки элемента в заданный момент времени.
    end
+
 
 +
Принимает: контекст, элемент, временные границы
 +
Возвращает: текстовую метку элемента, язык метки
 +
]]
 +
function getLabelWithLang( context, options, entityId, boundaries, propertyIds, selectors )
 +
if (type(entityId) ~= 'string') then error('type of entityId argument expected string, but was ' .. type(entityId)); end
 +
if not entityId then
 +
return nil;
 +
end
 +
 
 +
local langCode = CONTENT_LANGUAGE_CODE;
 +
 
 +
-- name from label
 +
local label = nil;
 +
if ( options.text and options.text ~= '' ) then
 +
label = options.text;
 +
else
 +
if not propertyIds then
 +
propertyIds = getLabelWithLang_DEFAULT_PROPERTIES;
 +
selectors = getLabelWithLang_DEFAULT_SELECTORS;
 +
end
 +
 
 +
-- name from properties
 +
local results = getPropertyInBoundaries( context, entityId, boundaries, propertyIds, selectors );
 +
 
 +
for _, result in pairs( results ) do
 +
if result.datavalue and result.datavalue.value then
 +
if result.datavalue.type == 'monolingualtext' and result.datavalue.value.text then
 +
label = result.datavalue.value.text;
 +
langCode = result.datavalue.value.language;
 +
break;
 +
elseif result.datavalue.type == 'string' then
 +
label = result.datavalue.value;
 +
break;
 +
end
 +
end
 +
end
 +
 +
if (not label) then
 +
label, langCode = mw.wikibase.getLabelWithLang( entityId );
 +
if not langCode then
 +
return nil;
 +
end
 +
end
 +
end
  
    return result;
+
return label, langCode;
 
end
 
end
  
--[[  
+
--[[
  Функция для оформления утверждений (statement)
+
Функция для оформления утверждений (statement)
  Подробнее о утверждениях см. d:Wikidata:Glossary/ru
+
Подробнее о утверждениях см. d:Wikidata:Glossary/ru
  
  Принимает: таблицу параметров
+
Принимает: таблицу параметров
  Возвращает: строку оформленного текста, предназначенного для отображения в статье
+
Возвращает: строку оформленного текста, предназначенного для отображения в статье
 
]]
 
]]
 
local function formatProperty( options )
 
local function formatProperty( options )
    -- Получение сущности по идентификатору
+
-- Получение сущности по идентификатору
    local entity = getEntityFromId( options.entityId )
+
local entity = getEntityFromId( options.entityId )
    if not entity then
+
if not entity then
        return -- throwError( 'entity-not-found' )
+
return -- throwError( 'entity-not-found' )
    end
+
end
 
-- проверка на присутсвие у сущности заявлений (claim)
 
-- проверка на присутсвие у сущности заявлений (claim)
 
-- подробнее о заявлениях см. d:Викиданные:Глоссарий
 
-- подробнее о заявлениях см. d:Викиданные:Глоссарий
    if (entity.claims == nil) then
+
if (entity.claims == nil) then
        return '' --TODO error?
+
return '' --TODO error?
    end
+
end
  
 
-- improve options
 
-- improve options
Строка 276: Строка 458:
  
 
if ( options.i18n ) then
 
if ( options.i18n ) then
options.i18n = copyTo( options.i18n, copyTo( i18n, {} ) );
+
options.i18n = copyTo( options.i18n, copyTo( getConfig( 'i18n' ), {} ) );
 
else
 
else
options.i18n = i18n;
+
options.i18n = getConfig( 'i18n' );
 
end
 
end
  
Строка 287: Строка 469:
 
formatPropertyDefault = formatPropertyDefault,
 
formatPropertyDefault = formatPropertyDefault,
 
formatStatementDefault = formatStatementDefault }
 
formatStatementDefault = formatStatementDefault }
context.formatProperty = function( options )  
+
context.cloneOptions = function( options )
 +
local entity = options.entity;
 +
options.entity = nil;
 +
 
 +
newOptions = mw.clone( options );
 +
options.entity = entity;
 +
newOptions.entity = entity;
 +
newOptions.frame = options.frame; -- На склонированном фрейме frame:expandTemplate()
 +
 
 +
return newOptions;
 +
end;
 +
context.formatProperty = function( options )
 
local func = getUserFunction( options, 'property', context.formatPropertyDefault );
 
local func = getUserFunction( options, 'property', context.formatPropertyDefault );
 
return func( context, options )
 
return func( context, options )
Строка 294: Строка 487:
 
context.formatSnak = function( options, snak, circumstances ) return formatSnak( context, options, snak, circumstances ) end;
 
context.formatSnak = function( options, snak, circumstances ) return formatSnak( context, options, snak, circumstances ) end;
 
context.formatRefs = function( options, statement ) return formatRefs( context, options, statement ) end;
 
context.formatRefs = function( options, statement ) return formatRefs( context, options, statement ) end;
+
 
 
context.parseTimeFromSnak = function( snak )
 
context.parseTimeFromSnak = function( snak )
 
if ( snak and snak.datavalue and snak.datavalue.value and snak.datavalue.value.time ) then
 
if ( snak and snak.datavalue and snak.datavalue.value and snak.datavalue.value.time ) then
Строка 318: Строка 511:
 
if ( not options.entity ) then error( 'options.entity missing' ); end;
 
if ( not options.entity ) then error( 'options.entity missing' ); end;
  
    local claims = context.selectClaims( options, options.property );
+
local claims;
    if (claims == nil) then
+
if options.property then -- TODO: Почему тут может не быть property?
        return '' --TODO error?
+
claims = context.selectClaims( options, options.property );
    end
+
end
 +
if claims == nil then
 +
return '' --TODO error?
 +
end
  
    -- Обход всех заявлений утверждения и с накоплением оформленых предпочтительных  
+
-- Обход всех заявлений утверждения и с накоплением оформленых предпочтительных
    -- заявлений в таблице
+
-- заявлений в таблице
    local formattedClaims = {}
+
local formattedClaims = {}
  
    for i, claim in ipairs(claims) do
+
for i, claim in ipairs(claims) do
        local formattedStatement = context.formatStatement( options, claim )
+
local formattedStatement = context.formatStatement( options, claim )
        -- здесь может вернуться либо оформленный текст заявления
+
-- здесь может вернуться либо оформленный текст заявления, либо строка ошибки, либо nil
        -- либо строка ошибки nil похоже никогда не возвращается
+
if ( formattedStatement and formattedStatement ~= '' ) then
        if (formattedStatement) then
+
formattedStatement = '<span class="wikidata-claim" data-wikidata-property-id="' .. string.upper( options.property ) .. '" data-wikidata-claim-id="' .. claim.id .. '">' .. formattedStatement .. '</span>'
            formattedStatement = '<span class="wikidata-claim" data-wikidata-property-id="' .. string.upper( options.property ) .. '" data-wikidata-claim-id="' .. claim.id .. '">' .. formattedStatement .. '</span>'
+
table.insert( formattedClaims, formattedStatement )
            table.insert( formattedClaims, formattedStatement )
+
end
        end
+
end
    end
 
  
-- создание текстовой строки со списком оформленых заявлений из таблицы
+
-- создание текстовой строки со списком оформленых заявлений из таблицы
    local out = mw.text.listToText( formattedClaims, options.separator, options.conjunction )
+
local out = mw.text.listToText( formattedClaims, options.separator, options.conjunction )
    if out ~= '' then
+
if out ~= '' then
    if options.before then
+
if options.before then
    out = options.before .. out
+
out = options.before .. out
 
end
 
end
    if options.after then
+
if options.after then
    out = out .. options.after
+
out = out .. options.after
 
end
 
end
 
end
 
end
  
    return out
+
return out
 
end
 
end
  
--[[  
+
--[[
  Функция для оформления одного утверждения (statement)
+
Функция для оформления одного утверждения (statement)
  
  Принимает: объект-таблицу утверждение и таблицу параметров
+
Принимает: объект-таблицу утверждение и таблицу параметров
  Возвращает: строку оформленного текста с заявлением (claim)
+
Возвращает: строку оформленного текста с заявлением (claim)
 
]]
 
]]
 
function formatStatement( context, options, statement )
 
function formatStatement( context, options, statement )
Строка 361: Строка 556:
 
error( 'statement is not specified or nil' );
 
error( 'statement is not specified or nil' );
 
end
 
end
    if not statement.type or statement.type ~= 'statement' then
+
if not statement.type or statement.type ~= 'statement' then
        throwError( 'unknown-claim-type' )
+
throwError( 'unknown-claim-type' )
    end
+
end
  
    local functionToCall = getUserFunction( options, 'claim', context.formatStatementDefault );
+
local functionToCall = getUserFunction( options, 'claim', context.formatStatementDefault );
    return functionToCall( context, options, statement );
+
return functionToCall( context, options, statement );
 
end
 
end
  
Строка 380: Строка 575:
 
and qualifier.datavalue.type == 'wikibase-entityid'
 
and qualifier.datavalue.type == 'wikibase-entityid'
 
and qualifier.datavalue.value
 
and qualifier.datavalue.value
and qualifier.datavalue.value["entity-type"] == 'item' ) then
+
and qualifier.datavalue.value['entity-type'] == 'item' ) then
local circumstance = 'Q' .. qualifier.datavalue.value["numeric-id"];
+
table.insert(circumstances, qualifier.datavalue.value.id)
if ( 'Q5727902' == circumstance ) then
 
circumstances.circa = true;
 
end
 
if ( 'Q18122778' == circumstance ) then
 
circumstances.presumably = true;
 
end
 
 
end
 
end
 
end
 
end
Строка 394: Строка 583:
 
end
 
end
  
--[[  
+
--[[
  Функция для оформления одного утверждения (statement)
+
Функция для оформления одного утверждения (statement)
  
  Принимает: объект-таблицу утверждение, таблицу параметров,
+
Принимает: объект-таблицу утверждение, таблицу параметров,
  объект-функцию оформления внутренних структур утверждения (snak) и
+
объект-функцию оформления внутренних структур утверждения (snak) и
  объект-функцию оформления ссылки на источники (reference)
+
объект-функцию оформления ссылки на источники (reference)
  Возвращает: строку оформленного текста с заявлением (claim)
+
Возвращает: строку оформленного текста с заявлением (claim)
 
]]
 
]]
 
function formatStatementDefault( context, options, statement )
 
function formatStatementDefault( context, options, statement )
Строка 409: Строка 598:
 
local circumstances = context.getSourcingCircumstances( statement );
 
local circumstances = context.getSourcingCircumstances( statement );
  
if ( options.references ) then
+
options.qualifiers = statement.qualifiers;
    return context.formatSnak( options, statement.mainsnak, circumstances ) .. context.formatRefs( options, statement );
+
 
    else
+
local result = context.formatSnak( options, statement.mainsnak, circumstances );
    return context.formatSnak( options, statement.mainsnak, circumstances );
+
 +
    if ( options.qualifier and statement.qualifiers and statement.qualifiers[ options.qualifier ] ) then
 +
    qualConfig = getPropertyParams( options.qualifier, nil, {})
 +
    if options.i18n then qualConfig.i18n = options.i18n end
 +
    local qualifierValues = {};
 +
for _, qualifierSnak in pairs( statement.qualifiers[ options.qualifier ] ) do
 +
local snakValue = context.formatSnak( qualConfig, qualifierSnak );
 +
if snakValue and snakValue ~= '' then
 +
table.insert( qualifierValues, snakValue );
 +
end
 +
end
 +
if ( #qualifierValues ) then
 +
if qualConfig.invisible then
 +
        result = result .. table.concat( qualifierValues, ', ' );
 +
else
 +
        result = result .. ' (' .. table.concat( qualifierValues, ', ' ) .. ')';
 +
        end
 +
        end
 
     end
 
     end
 +
 +
if ( result and result ~= '' and options.references ) then
 +
result = result .. context.formatRefs( options, statement );
 +
end
 +
 +
return result;
 
end
 
end
  
--[[  
+
--[[
  Функция для оформления части утверждения (snak)
+
Функция для оформления части утверждения (snak)
  Подробнее о snak см. d:Викиданные:Глоссарий
+
Подробнее о snak см. d:Викиданные:Глоссарий
  
  Принимает: таблицу snak объекта (main snak или же snak от квалификатора) и таблицу опций
+
Принимает: таблицу snak объекта (main snak или же snak от квалификатора) и таблицу опций
  Возвращает: строку оформленного викитекста
+
Возвращает: строку оформленного викитекста
 
]]
 
]]
 
function formatSnak( context, options, snak, circumstances )
 
function formatSnak( context, options, snak, circumstances )
Строка 436: Строка 648:
 
local after = '</span>'
 
local after = '</span>'
  
    if snak.snaktype == 'somevalue' then
+
if snak.snaktype == 'somevalue' then
        if ( options['somevalue'] and options['somevalue'] ~= '' ) then
+
if ( options['somevalue'] and options['somevalue'] ~= '' ) then
            return before .. options['somevalue'] .. after;
+
result = options['somevalue'];
        end
+
else
        return before .. options.i18n['somevalue'] .. after;
+
result = options.i18n['somevalue'];
    elseif snak.snaktype == 'novalue' then
+
end
        if ( options['novalue'] and options['novalue'] ~= '' ) then
+
elseif snak.snaktype == 'novalue' then
            return before .. options['novalue'] .. after;
+
if ( options['novalue'] and options['novalue'] ~= '' ) then
        end
+
result = options['novalue'];
        return before .. options.i18n['novalue'] .. after;
+
else
    elseif snak.snaktype == 'value' then
+
result = options.i18n['novalue'];
if ( circumstances.presumably ) then
 
before = before .. options.i18n.presumably;
 
 
end
 
end
if ( circumstances.circa ) then
+
elseif snak.snaktype == 'value' then
before = before .. options.i18n.circa;
+
result = formatDatavalue( context, options, snak.datavalue, snak.datatype );
 +
for _, item in pairs(circumstances) do
 +
if options.i18n[item] then
 +
result = options.i18n[item] .. result;
 +
end
 
end
 
end
 +
else
 +
throwError( 'unknown-snak-type' );
 +
end
 +
 +
if ( not result or result == '' ) then
 +
return nil;
 +
end
  
        return before .. formatDatavalue( context, options, snak.datavalue, snak.datatype ) .. after;
+
return before .. result .. after;
    else
 
        throwError( 'unknown-snak-type' );
 
    end
 
 
end
 
end
  
--[[  
+
--[[
  Функция для оформления объектов-значений с географическими координатами
+
Функция для оформления объектов-значений с географическими координатами
  
  Принимает: объект-значение и таблицу параметров,
+
Принимает: объект-значение и таблицу параметров,
  Возвращает: строку оформленного текста
+
Возвращает: строку оформленного текста
 
]]
 
]]
 
function formatGlobeCoordinate( value, options )
 
function formatGlobeCoordinate( value, options )
-- проверка на требование в параметрах вызова на возврат сырого значения  
+
-- проверка на требование в параметрах вызова на возврат сырого значения
    if options['subvalue'] == 'latitude' then -- широты
+
if options['subvalue'] == 'latitude' then -- широты
        return value['latitude']
+
return value['latitude']
    elseif options['subvalue'] == 'longitude' then -- долготы
+
elseif options['subvalue'] == 'longitude' then -- долготы
        return value['longitude']
+
return value['longitude']
    else
+
elseif options['nocoord'] and options['nocoord'] ~= '' then
    -- в противном случае формируются параметры для вызова шаблона {{coord}}
+
-- если передан параметр nocoord, то не выводить координаты
    -- нужно дописать в документации шаблона, что он отсюда вызывается, и что
+
-- обычно это делается при использовании нескольких карточек на странице
    -- любое изменние его парамеров  должно быть согласовано с кодом тут
+
return ''
        local eps = 0.0000001 -- < 1/360000
+
else
        local globe = '' -- TODO
+
-- в противном случае формируются параметры для вызова шаблона {{coord}}
        local lat = {}
+
-- нужно дописать в документации шаблона, что он отсюда вызывается, и что
        lat['abs'] = math.abs(value['latitude'])
+
-- любое изменние его парамеров  должно быть согласовано с кодом тут
        lat['ns'] = value['latitude'] >= 0 and 'N' or 'S'
+
        lat['d'] = math.floor(lat['abs'] + eps)
+
coord_mod = require( "Module:Coordinates" );
        lat['m'] = math.floor((lat['abs'] - lat['d']) * 60 + eps)
+
        lat['s'] = math.max(0, ((lat['abs'] - lat['d']) * 60 - lat['m']) * 60 + eps)
+
local globe = options.globe or ''
        local lon = {}
+
if globe == '' and value['globe'] then
        lon['abs'] = math.abs(value['longitude'])
+
globes = require( 'Module:Wikidata/Globes' )
        lon['ew'] = value['longitude'] >= 0 and 'E' or 'W'
+
globe = globes[value['globe']] or ''
        lon['d'] = math.floor(lon['abs'] + eps)
+
end
        lon['m'] = math.floor((lon['abs'] - lon['d']) * 60 + eps)
+
        lon['s'] = math.max(0, ((lon['abs'] - lon['d']) * 60 - lon['m']) * 60 + eps)
+
local display = 'inline'
        -- TODO: round seconds with precision
+
if options.display and options.display ~= '' then
        local coord = '{{coord'
+
display = options.display
        if (value['precision'] == nil) or (value['precision'] < 1/60) then -- по умолчанию с точностью до секунды
+
elseif ( options.property:upper() == 'P625' ) then
            coord = coord .. '|' .. lat['d'] .. '|' .. lat['m'] .. '|' .. lat['s'] .. '|' .. lat['ns']
+
display = 'title'
            coord = coord .. '|' .. lon['d'] .. '|' .. lon['m'] .. '|' .. lon['s'] .. '|' .. lon['ew']
+
end
        elseif value['precision'] < 1 then
+
            coord = coord .. '|' .. lat['d'] .. '|' .. lat['m'] .. '|' .. lat['ns']
+
g_frame.args = {tostring(value['latitude']), tostring(value['longitude']), globe = globe, type = options.type and options.type or '', display = display }
            coord = coord .. '|' .. lon['d'] .. '|' .. lon['m'] .. '|' .. lon['ew']
+
        else
+
return coord_mod.coord(g_frame)
            coord = coord .. '|' .. lat['d'] .. '|' .. lat['ns']
+
end
            coord = coord .. '|' .. lon['d'] .. '|' .. lon['ew']
 
        end
 
        coord = coord .. '|globe:' .. globe
 
        if options['display'] and options['display'] ~= '' then
 
            coord = coord .. '|display=' .. options.display
 
        else
 
            coord = coord .. '|display=title'
 
        end
 
        coord = coord .. '}}'
 
 
 
        return g_frame:preprocess(coord)
 
    end
 
 
end
 
end
  
--[[  
+
--[[
  Функция для оформления объектов-значений с файлами с Викисклада
+
Функция для оформления объектов-значений с файлами с Викисклада
  
  Принимает: объект-значение и таблицу параметров,
+
Принимает: объект-значение и таблицу параметров,
  Возвращает: строку оформленного текста
+
Возвращает: строку оформленного текста
 
]]
 
]]
 
function formatCommonsMedia( value, options )
 
function formatCommonsMedia( value, options )
local image = '[[File:' .. value
+
local image = value;
if options['border'] and options['border'] ~= '' then
+
 
    image = image .. '|border'
+
local caption = '';
    end
+
if options[ 'caption' ] and options[ 'caption' ] ~= '' then
 +
caption = options[ 'caption' ];
 +
elseif options[ 'description' ] and options[ 'description' ] ~= '' then
 +
caption = options[ 'description' ];
 +
end
 +
if caption ~= '' then
 +
caption = wrapFormatProperty( caption, 'class="media-caption" data-wikidata-qualifier-id="P2096" style="display:block;"' );
 +
end
 +
 
 +
if not string.find( value, '[%[%]%{%}]' ) and not string.find( value, 'UNIQ%-%-imagemap' ) then
 +
-- если в value не содержится викикод или imagemap, то викифицируем имя файла
 +
-- ищем слово imagemap в строке, потому что вставляется плейсхолдер: [[PHAB:T28213]]
 +
image = '[[File:' .. value .. '|frameless';
 +
if options[ 'border' ] and options[ 'border' ] ~= '' then
 +
image = image .. '|border';
 +
end
 +
 
 +
local size = options[ 'size' ];
 +
if size and size ~= '' then
 +
if not string.match( size, 'px$' )
 +
and not string.match( size, 'пкс$' ) -- TODO: использовать перевод для языка вики
 +
then
 +
size = size .. 'px'
 +
end
 +
else
 +
size = fileDefaultSize;
 +
end
 +
image = image .. '|' .. size;
 +
 
 +
if options[ 'alt' ] and options[ 'alt' ] ~= '' then
 +
image = image .. '|' .. options[ 'alt' ];
 +
end
 +
image = image .. ']]';
 +
 
 +
if caption ~= '' then
 +
image = image .. '<br>' .. caption;
 +
end
  
  local size = options['size']
+
if options[ 'local_caption' ] and options[ 'local_caption' ] ~= '' then
    if size and size ~= '' then
+
image = image .. getCategoryByCode( 'media-contains-local-caption' )
    if not string.match( size, 'px$' )
+
end
and not string.match( size, 'пкс$' ) -- TODO: использовать перевод для языка вики
 
then
 
    size = size .. 'px'
 
    end
 
 
else
 
else
size = '250x350px' -- TODO: вынести в настройки
+
image = image .. caption .. getCategoryByCode( 'media-contains-markup' );
    end
+
end
image = image .. '|' .. size
+
 +
if options.entity and options.fixdouble then
 +
local page = mw.title.getCurrentTitle()
 +
local txt = page:getContent()
 +
if txt and txt:match(':' .. value) and mw.title.getCurrentTitle():inNamespace(0) then image = image .. getCategoryByCode( 'media-contains-local-double' ) end
 +
end
 +
 +
return image
 +
end
 +
 
 +
--[[
 +
Fonction for render math formulas
 +
 
 +
@param string Value.
 +
@param table Parameters.
 +
@return string Formatted string.
 +
]]
 +
function formatMath( value, options )
 +
return options.frame:extensionTag{ name = 'math', content = value };
 +
end
 +
 
 +
--[[
 +
Функция для оформления внешних идентификаторов
 +
 
 +
Принимает: объект-значение и таблицу параметров,
 +
Возвращает: строку оформленного текста
 +
]]
 +
local function formatExternalId( value, options )
 +
local formatter = options.formatter;
 +
 
 +
if not formatter or formatter == '' then
 +
local wbStatus, propertyEntity = pcall( mw.wikibase.getEntity, options.property:upper() )
 +
if wbStatus == true and propertyEntity then
 +
local isGoodFormat = false;
 +
local statements = propertyEntity:getBestStatements( 'P1793' );
 +
for _, statement in pairs( statements ) do
 +
if statement.mainsnak.snaktype == 'value' then
 +
local pattern = mw.ustring.gsub( statement.mainsnak.datavalue.value, '\\', '%' );
 +
pattern = mw.ustring.gsub( pattern, '{%d+,?%d*}', '+' );
 +
if ( string.find( pattern, '|' ) or string.find( pattern, '%)%?' )
 +
or mw.ustring.match( value, '^' .. pattern .. '$' ) ~= nil ) then
 +
isGoodFormat = true;
 +
break;
 +
end
 +
end
 +
end
 +
 
 +
if ( isGoodFormat == true ) then
 +
statements = propertyEntity:getBestStatements( 'P1630' );
 +
for _, statement in pairs( statements ) do
 +
if statement.mainsnak.snaktype == 'value' then
 +
formatter = statement.mainsnak.datavalue.value;
 +
break
 +
end
 +
end
 +
end
 +
end
 +
end
 +
 
 +
if formatter and formatter ~= '' then
 +
local link = mw.ustring.gsub(
 +
mw.ustring.gsub( formatter, '$1', value ), '.',
 +
{ [' '] = '%20', ['+'] = '%2b' } )
 +
 
 +
local title = options.title
 +
if not title or title == '' then
 +
title = '$1'
 +
end
 +
title = mw.ustring.gsub( title, '$1', value )
 +
 
 +
return '[' .. link .. ' ' .. title .. ']'
 +
end
 +
 
 +
return value
 +
end
 +
 
 +
--[[
 +
Функция для оформления числовых значений
 +
 
 +
Принимает: объект-значение и таблицу параметров,
 +
Возвращает: строку оформленного текста
 +
]]
 +
local function formatQuantity( value, options )
 +
-- диапазон значений
 +
local amount = string.gsub( value['amount'], '^%+', '' );
 +
local lang = mw.language.getContentLanguage();
 +
local langCode = lang:getCode();
 +
 
 +
local function formatNum( number, sigfig )
 +
sigfig = sigfig or 12 -- округление до 12 знаков после запятой, на 13-м возникает ошибка в точности
 +
local mult = 10^sigfig;
 +
number = math.floor( number * mult + 0.5 ) / mult;
 +
return string.gsub( lang:formatNum( number ), '^-', '−' );
 +
end
 +
 
 +
local out = formatNum( tonumber( amount ) );
 +
if value.upperBound then
 +
local diff = tonumber( value.upperBound ) - tonumber( amount )
 +
if diff > 0 then -- временная провека, пока у большинства значений не будет убрано ±0
 +
-- Пробуем понять до какого знака округлять
 +
local integer, dot, decimals, expstr = value.upperBound:match( '^+?-?(%d*)(%.?)(%d*)(.*)' )
 +
local prec
 +
if dot == '' then
 +
prec = -integer:match('0*$'):len()
 +
else
 +
prec = #decimals
 +
end
 +
bound = formatNum( diff, prec )
 +
if string.match( bound, 'E%-(%d+)' ) then -- если в экспоненциальном формате
 +
digits = tonumber( string.match( bound, 'E%-(%d+)' ) ) - 2
 +
bound = formatNum( diff * 10 ^ digits, prec )
 +
bound = string.sub( bound, 0, 2 ) .. string.rep( '0', digits ) .. string.sub( bound, -string.len( bound ) + 2 )
 +
end
 +
out = out .. ' ± ' .. bound
 +
end
 +
end
  
    if options['alt'] and options['alt'] ~= '' then
+
if options.unit and options.unit ~= '' then
    image = image .. '|' .. options['alt']
+
if options.unit ~= '-' then
    end
+
out = out .. ' ' .. options.unit
image = image .. ']]'
+
end
 +
elseif value.unit and string.match( value.unit, 'http://www.wikidata.org/entity/' ) then
 +
local unitEntityId = string.gsub( value.unit, 'http://www.wikidata.org/entity/', '' );
 +
if unitEntityId ~= 'undefined' then
 +
local wbStatus, unitEntity = pcall( mw.wikibase.getEntity, unitEntityId );
 +
if wbStatus == true and unitEntity then
 +
if unitEntity.claims.P2370 and
 +
unitEntity.claims.P2370[1].mainsnak.snaktype == 'value' and
 +
not value.upperBound and
 +
options.siConversion == true
 +
then
 +
conversionToSIunit = string.gsub( unitEntity.claims.P2370[1].mainsnak.datavalue.value.amount, '^%+', '' );
 +
if math.floor( math.log10( conversionToSIunit )) ~= math.log10( conversionToSIunit ) then
 +
-- Если не степени десятки (переводить сантиметры в метры не надо!)
 +
outValue = tonumber( amount ) * conversionToSIunit
 +
 +
if ( outValue > 0 ) then
 +
-- Пробуем понять до какого знака округлять
 +
local integer, dot, decimals, expstr = amount:match( '^(%d*)(%.?)(%d*)(.*)' )
 +
local prec
 +
if dot == '' then
 +
prec = -integer:match('0*$'):len()
 +
else
 +
prec = #decimals
 +
end
 +
local adjust = math.log10( math.abs( conversionToSIunit )) + math.log10( 2 )
 +
local minprec = 1 - math.floor( math.log10( outValue ) + 2e-14 );
 +
out = formatNum( outValue, math.max( math.floor( prec + adjust ), minprec ));
 +
else
 +
out = formatNum( outValue, 0 )
 +
end
 +
unitEntityId = string.gsub( unitEntity.claims.P2370[1].mainsnak.datavalue.value.unit, 'http://www.wikidata.org/entity/', '' );
 +
wbStatus, unitEntity = pcall( mw.wikibase.getEntity, unitEntityId );
 +
end
 +
end
 +
 +
local writingSystemElementId = 'Q8209';
 +
local langElementId = 'Q7737';
 +
local label = getLabelWithLang( context, options, unitEntity.id, nil, { "P5061", "P558", "P558" }, {
 +
'P5061[language:' .. langCode .. ']',
 +
'P558[P282:' .. writingSystemElementId .. ', P407:' .. langElementId .. ']',
 +
'P558[!P282][!P407]'
 +
} );
 
 
    return image
+
out = out .. ' ' .. label;
 +
end
 +
end
 +
end
 +
 
 +
return out;
 
end
 
end
 +
 +
local DATATYPE_CACHE = {}
  
 
--[[
 
--[[
 
Get property datatype by ID.
 
Get property datatype by ID.
+
 
 
@param string Property ID, e.g. 'P123'.
 
@param string Property ID, e.g. 'P123'.
 
@return string Property datatype, e.g. 'commonsMedia', 'time' or 'url'.
 
@return string Property datatype, e.g. 'commonsMedia', 'time' or 'url'.
Строка 557: Строка 959:
 
end
 
end
 
 
local propertyEntity = mw.wikibase.getEntity( propertyId );
+
local cached = DATATYPE_CACHE[propertyId];
if not propertyEntity then
+
if (cached ~= nil) then return cached; end
 +
 
 +
local wbStatus, propertyEntity = pcall( mw.wikibase.getEntity, propertyId );
 +
if wbStatus ~= true or not propertyEntity then
 
return nil;
 
return nil;
 
end
 
end
 +
mw.log("Loaded datatype " .. propertyEntity.datatype .. " of " .. propertyId .. ' from wikidata, consider passing datatype argument to formatProperty call or to Wikidata/config' )
  
 +
DATATYPE_CACHE[propertyId] = propertyEntity.datatype;
 
return propertyEntity.datatype;
 
return propertyEntity.datatype;
 +
end
 +
 +
local function formatLangRefs( options )
 +
local langRefs = ''
 +
if ( options.qualifiers and options.qualifiers.P407 ) then
 +
for i, qualifier in pairs( options.qualifiers.P407 ) do
 +
if ( qualifier
 +
and qualifier.datavalue
 +
and qualifier.datavalue.type == 'wikibase-entityid' ) then
 +
local langRefEntity = getEntityFromId( qualifier.datavalue.value.id )
 +
if ( langRefEntity and langRefEntity.claims ) then
 +
local langRefCodeClaims = WDS.filter( langRefEntity.claims, 'P218' )
 +
if langRefCodeClaims then
 +
for _, claim in pairs( langRefCodeClaims ) do
 +
if ( claim.mainsnak
 +
and claim.mainsnak
 +
and claim.mainsnak.datavalue
 +
and claim.mainsnak.datavalue.type == 'string' ) then
 +
local langRefCode = claim.mainsnak.datavalue.value
 +
langRefs = langRefs .. '&#8203;' .. options.frame:expandTemplate{ title = 'ref-' ..langRefCode }
 +
end
 +
end
 +
end
 +
end
 +
end
 +
end
 +
end
 +
 +
return langRefs
 
end
 
end
  
 
local function getDefaultValueFunction( datavalue, datatype )
 
local function getDefaultValueFunction( datavalue, datatype )
    -- вызов обработчиков по умолчанию для известных типов значений
+
-- вызов обработчиков по умолчанию для известных типов значений
    if datavalue.type == 'wikibase-entityid' then
+
if datavalue.type == 'wikibase-entityid' then
    -- идентификатор сущности
+
-- Entity ID
        return function( context, options, value ) return formatEntityId( getEntityIdFromValue( value ), options ) end;
+
return function( context, options, value ) return formatEntityId( context, options, getEntityIdFromValue( value ) ) end;
    elseif datavalue.type == 'string' then
+
elseif datavalue.type == 'string' then
    -- строка
+
-- String
    if datatype and datatype == 'commonsMedia' then
+
if datatype and datatype == 'commonsMedia' then
    -- медиафайл
+
-- Media
        return function( context, options, value ) return formatCommonsMedia( value, options ) end;
+
return function( context, options, value )
    elseif datatype and datatype == 'url' then
+
if options.caption and options.caption ~= '' then
    -- URL
+
options.local_caption = options.caption;
        return function( context, options, value )
+
elseif options.description and options.description ~= '' then
 +
options.local_caption = options.description;
 +
end
 +
options.caption = ''
 +
options.description = ''
 +
if options.qualifiers and options.qualifiers.P2096 then
 +
for i, qualifier in pairs( options.qualifiers.P2096 ) do
 +
if ( qualifier
 +
and qualifier.datavalue
 +
and qualifier.datavalue.type == 'monolingualtext'
 +
and qualifier.datavalue.value
 +
and qualifier.datavalue.value.language == contentLanguageCode ) then
 +
options.caption = qualifier.datavalue.value.text
 +
options.description = qualifier.datavalue.value.text
 +
break
 +
end
 +
end
 +
end
 +
if options['appendTimestamp'] and options.qualifiers and options.qualifiers.P585 and options.qualifiers.P585[1] then
 +
local moment = formatDatavalue (context, options, options.qualifiers.P585[1].datavalue, 'time')
 +
if not options.caption or options.caption == ''  then
 +
options.caption = moment
 +
options.description = moment
 +
else
 +
options.caption = options.caption .. ', ' .. moment
 +
options.description = options.description .. ', ' .. moment
 +
end
 +
end
 +
return formatCommonsMedia( value, options )
 +
end;
 +
elseif datatype and datatype == 'external-id' then
 +
-- External ID
 +
return function( context, options, value )
 +
return formatExternalId( value, options )
 +
end
 +
elseif datatype and datatype == 'math' then
 +
-- Math formula
 +
return function( context, options, value )
 +
return formatMath( value, options )
 +
end
 +
elseif datatype and datatype == 'url' then
 +
-- URL
 +
return function( context, options, value )
 
local moduleUrl = require( 'Module:URL' )
 
local moduleUrl = require( 'Module:URL' )
    return moduleUrl.formatUrlSingle( context, options, value );
+
local langRefs = formatLangRefs( options )
        end
+
if not options.length or options.length == '' then
        end
+
options.length = math.max( 18, 25 - #langRefs )
        return function( context, options, value ) return value end;
+
end
    elseif datavalue.type == 'monolingualtext' then
+
return moduleUrl.formatUrlSingle( context, options, value ) .. langRefs
    -- моноязычный текст (строка с указанием языка)
+
end
        return function( context, options, value )
+
end
        if ( options.monolingualLangTemplate == 'lang' ) then
+
return function( context, options, value ) return value end;
        return options.frame:expandTemplate{ title = 'lang-' .. value.language, args = { value.text } };
+
elseif datavalue.type == 'monolingualtext' then
        elseif ( options.monolingualLangTemplate == 'ref' ) then
+
-- моноязычный текст (строка с указанием языка)
        return '<span class="lang" lang="' .. value.language .. '">' .. value.text .. '</span>' .. options.frame:expandTemplate{ title = 'ref-' .. value.language };
+
return function( context, options, value )
        else
+
if ( options.monolingualLangTemplate == 'lang' ) then
        return '<span class="lang" lang="' .. value.language .. '">' .. value.text .. '</span>';
+
if ( value.language == contentLanguageCode ) then
        end
+
return value.text;
        end;
+
end
    elseif datavalue.type == 'globecoordinate' then
+
return options.frame:expandTemplate{ title = 'lang-' .. value.language, args = { value.text } };
    -- географические координаты
+
elseif ( options.monolingualLangTemplate == 'ref' ) then
        return function( context, options, value ) return formatGlobeCoordinate( value, options )  end;
+
return '<span class="lang" lang="' .. value.language .. '">' .. value.text .. '</span>' .. options.frame:expandTemplate{ title = 'ref-' .. value.language };
    elseif datavalue.type == 'quantity' then
+
else
        return function( context, options, value )
+
return '<span class="lang" lang="' .. value.language .. '">' .. value.text .. '</span>';
    -- диапазон значений
+
end
        local amount = string.gsub( value['amount'], '^%+', '' )
+
end;
        local lang = mw.language.getContentLanguage()
+
elseif datavalue.type == 'globecoordinate' then
        return lang:formatNum( tonumber( amount ) )
+
-- географические координаты
        end;
+
return function( context, options, value ) return formatGlobeCoordinate( value, options )  end;
    elseif datavalue.type == 'time' then
+
elseif datavalue.type == 'quantity' then
        return function( context, options, value )
+
return function( context, options, value ) return formatQuantity( value, options ) end;
 +
elseif datavalue.type == 'time' then
 +
return function( context, options, value )
 
local moduleDate = require( 'Module:Wikidata/date' )
 
local moduleDate = require( 'Module:Wikidata/date' )
    return moduleDate.formatDate( context, options, value );
+
return moduleDate.formatDate( context, options, value );
        end;
+
end;
    else
+
else
    -- во всех стальных случаях возвращаем ошибку
+
-- во всех стальных случаях возвращаем ошибку
        throwError( 'unknown-datavalue-type' )
+
throwError( 'unknown-datavalue-type' )
    end
+
end
 
end
 
end
  
--[[  
+
--[[
  Функция для оформления значений (value)
+
Функция для оформления значений (value)
  Подробнее о значениях  см. d:Wikidata:Glossary/ru
+
Подробнее о значениях  см. d:Wikidata:Glossary/ru
  
  Принимает: объект-значение и таблицу параметров,
+
Принимает: объект-значение и таблицу параметров,
  Возвращает: строку оформленного текста
+
Возвращает: строку оформленного текста
 
]]
 
]]
 
function formatDatavalue( context, options, datavalue, datatype )
 
function formatDatavalue( context, options, datavalue, datatype )
Строка 628: Строка 1108:
 
if ( not datavalue.value ) then error( 'datavalue.value is missng' ); end;
 
if ( not datavalue.value ) then error( 'datavalue.value is missng' ); end;
  
    -- проверка на указание специализированных обработчиков в параметрах,
+
-- проверка на указание специализированных обработчиков в параметрах,
    -- переданных при вызове
+
-- переданных при вызове
    context.formatValueDefault = getDefaultValueFunction( datavalue, datatype );
+
context.formatValueDefault = getDefaultValueFunction( datavalue, datatype );
    local functionToCall = getUserFunction( options, 'value', context.formatValueDefault );
+
local functionToCall = getUserFunction( options, 'value', context.formatValueDefault );
    return functionToCall( context, options, datavalue.value );
+
return functionToCall( context, options, datavalue.value );
 
end
 
end
  
-- Небольшой словарь упрощенного отображения (TODO: надо сделать расширенный с учётом даты)
+
local DEFAULT_BOUNDARIES = { os.time() * 1000, os.time() * 1000};
local simpleReplaces = {}
 
  
--[[  
+
--[[
  Функция для оформления идентификатора сущности
+
Функция для оформления идентификатора сущности
  
  Принимает: строку индентификатора (типа Q42) и таблицу параметров,
+
Принимает: строку индентификатора (типа Q42) и таблицу параметров,
  Возвращает: строку оформленного текста
+
Возвращает: строку оформленного текста
 
]]
 
]]
function formatEntityId( entityId, options )
+
function formatEntityId( context, options, entityId )
-- получение локализованного названия  
+
-- получение локализованного названия
    local label = nil;
+
local boundaries = nil
    if ( options.text and options.text ~= '' ) then
+
if options.qualifiers then
        label = options.text
+
boundaries = p.getTimeBoundariesFromQualifiers( frame, context, { qualifiers = options.qualifiers } )
    else
+
end
    if ( simpleReplaces[entityId] ) then
+
if not boundaries then
return simpleReplaces[entityId];
+
boundaries = DEFAULT_BOUNDARIES;
end
+
end
label = mw.wikibase.label( entityId );
+
local label, labelLanguageCode = getLabelWithLang( context, options, entityId, boundaries )
    end
+
 
 +
-- определение соответствующей показываемому элементу категории
 +
local category = p.extractCategory( context, options, { id = entityId } )
  
 
-- получение ссылки по идентификатору
 
-- получение ссылки по идентификатору
    local link = mw.wikibase.sitelink( entityId )
+
local link = mw.wikibase.sitelink( entityId )
    if link then
+
if link then
        if label then
+
-- ссылка на категорию, а не добавление страницы в неё
            return '[[' .. link .. '|' .. label .. ']]'
+
if mw.ustring.match( link, '^' .. mw.site.namespaces[ 14 ].name .. ':' ) then
        else
+
link = ':' .. link
            return '[[' .. link .. ']]'
+
end
        end
+
if label and not options.rawArticle then
    end
+
local a = link == label and ('[[' .. link .. ']]') or '[[' .. link .. '|' .. label .. ']]';
 +
if ( contentLanguageCode ~= labelLanguageCode ) then
 +
return a .. getCategoryByCode( 'links-to-entities-with-missing-local-language-label' ) .. category;
 +
else
 +
return a .. category;
 +
end
 +
else
 +
return '[[' .. link .. ']]' .. category;
 +
end
 +
end
  
    if label then
+
if label then
        -- красная ссылка
+
-- красная ссылка
        -- TODO: разобраться, почему не всегда есть options.frame
+
-- TODO: разобраться, почему не всегда есть options.frame
        if not mw.title.new( label ).exists and options.frame then
+
local title = mw.title.new( label );
            return options.frame:expandTemplate{
+
if title and not title.exists and options.frame then
                title = 'не переведено 5',
+
local redLink = options.frame:expandTemplate{ title='Ш:Красная ссылка с рыбой', args = { entityId, label } };
                args = { label, '', 'd', entityId }
+
return redLink .. '<sup>[[:d:' .. entityId .. '|[d]]]</sup>' .. category;
            }
+
end
        end
 
  
 
-- TODO: перенести до проверки на существование статьи
 
-- TODO: перенести до проверки на существование статьи
Строка 681: Строка 1170:
 
and entityId ~= 'Q6581072' and entityId ~= 'Q6581097' -- TODO: переписать на format=text
 
and entityId ~= 'Q6581072' and entityId ~= 'Q6581097' -- TODO: переписать на format=text
 
then
 
then
local lang = mw.language.getContentLanguage()
+
sup = '<sup class="plainlinks noprint">[//www.wikidata.org/wiki/' .. entityId .. '?uselang=' .. contentLanguageCode .. ' [d&#x5d;]</sup>'
sup = '<sup class="plainlinks noprint">[//www.wikidata.org/wiki/' .. entityId .. '?uselang=' .. lang:getCode() .. ' [d]]</sup>'
 
 
end
 
end
  
        -- одноимённая статья уже существует - выводится текст и ссылка на ВД
+
-- одноимённая статья уже существует - выводится текст и ссылка на ВД
        return '<span class="iw" data-title="' .. label .. '">' .. label
+
return '<span class="iw" data-title="' .. label .. '">' .. label
        .. sup
+
.. sup
        .. '</span>'
+
.. '</span>' .. category
    end
+
end
    -- сообщение об отсутвии локализованного названия
+
-- сообщение об отсутвии локализованного названия
    -- not good, but better than nothing
+
-- not good, but better than nothing
    return '[[:d:' .. entityId .. '|' .. entityId .. ']]<span style="border-bottom: 1px dotted; cursor: help; white-space: nowrap" title="В Викиданных нет русской подписи к элементу. Вы можете помочь, указав русский вариант подписи.">?</span>' .. categoryLinksToEntitiesWithMissingLabel;
+
return '[[:d:' .. entityId .. '|' .. entityId .. ']]<span style="border-bottom: 1px dotted; cursor: help; white-space: nowrap" title="В Викиданных нет русской подписи к элементу. Вы можете помочь, указав русский вариант подписи.">?</span>' .. getCategoryByCode( 'links-to-entities-with-missing-label' ) .. category;
 
end
 
end
  
--[[  
+
--[[
  Функция для оформления утверждений (statement)
+
Функция для формирования категории на основе wikidata/config
  Подробнее о утверждениях см. d:Wikidata:Glossary/ru
+
]]
 +
function p.extractCategory( context, options, value )
 +
if ( not options.category ) then
 +
return '';
 +
end
 +
local propertyId = string.gsub( options.category, '([^Pp0-9].*)$', '');
 +
local wbStatus, claims = pcall( mw.wikibase.getAllStatements, value.id, propertyId );
 +
if ( wbStatus ~= true or not claims ) then return ''; end
 +
allClaims = {}
 +
allClaims[ propertyId ] = claims
 +
claims = WDS.filter( allClaims, options.category )
 +
if not claims then return ''; end
 +
 +
for _, claim in pairs( claims ) do
 +
if ( claim
 +
and claim.mainsnak
 +
and claim.mainsnak.datavalue
 +
and claim.mainsnak.datavalue.type == 'wikibase-entityid' ) then
 +
 +
local catEntityId = claim.mainsnak.datavalue.value.id;
 +
local wbStatus, catSiteLink = pcall( mw.wikibase.getSitelink, catEntityId );
 +
 
 +
if ( wbStatus == true and catSiteLink ) then
 +
return '[[' .. catSiteLink .. ']]';
 +
end
 +
end
 +
end
 +
 
 +
return '';
 +
end
 +
--[[
 +
Функция для оформления утверждений (statement)
 +
Подробнее о утверждениях см. d:Wikidata:Glossary/ru
  
  Принимает: таблицу параметров
+
Принимает: таблицу параметров
  Возвращает: строку оформленного текста, предназначенного для отображения в статье
+
Возвращает: строку оформленного текста, предназначенного для отображения в статье
 
]]
 
]]
 
-- устаревшее имя, не использовать
 
-- устаревшее имя, не использовать
 
function p.formatStatements( frame )
 
function p.formatStatements( frame )
 
return p.formatProperty( frame );
 
return p.formatProperty( frame );
 +
end
 +
 +
--[[
 +
Получение параметров, которые обычно используются для вывода свойства.
 +
]]
 +
function getPropertyParams( propertyId, datatype, params )
 +
local config = getConfig();
 +
 +
-- Различные уровни настройки параметров, по убыванию приоритета
 +
local propertyParams = {};
 +
 +
-- 1. Параметры, указанные явно при вызове
 +
if params then
 +
for key, value in pairs( params ) do
 +
if value ~= '' then
 +
propertyParams[ key ] = value;
 +
end
 +
end
 +
end
 +
 +
-- 2. Настройки конкретного параметра
 +
if config[ 'properties' ] and config[ 'properties' ][ propertyId ] then
 +
for key, value in pairs( config[ 'properties' ][ propertyId ] ) do
 +
if propertyParams[ key ] == nil then
 +
propertyParams[ key ] = value;
 +
end
 +
end
 +
end
 +
 +
-- 3. Указанный пресет настроек
 +
if propertyParams[ 'preset' ] and config[ 'presets' ] and
 +
config[ 'presets' ][ propertyParams[ 'preset' ] ]
 +
then
 +
for key, value in pairs( config[ 'presets' ][ propertyParams[ 'preset' ] ] ) do
 +
if propertyParams[ key ] == nil then
 +
propertyParams[ key ] = value;
 +
end
 +
end
 +
end
 +
 +
local datatype = datatype or params.datatype or propertyParams.datatype or getPropertyDatatype( propertyId );
 +
if propertyParams.datatype == nil then
 +
propertyParams.datatype = datatype;
 +
end
 +
 +
-- 4. Настройки для типа данных
 +
if datatype and config[ 'datatypes' ] and config[ 'datatypes' ][ datatype ] then
 +
for key, value in pairs( config[ 'datatypes' ][ datatype ] ) do
 +
if propertyParams[ key ] == nil then
 +
propertyParams[ key ] = value;
 +
end
 +
end
 +
end
 +
 +
-- 5. Общие настройки для всех свойств
 +
if config[ 'global' ] then
 +
for key, value in pairs( config[ 'global' ] ) do
 +
if propertyParams[ key ] == nil then
 +
propertyParams[ key ] = value;
 +
end
 +
end
 +
end
 +
 +
return propertyParams;
 
end
 
end
  
 
function p.formatProperty( frame )
 
function p.formatProperty( frame )
    local plain = toBoolean( frame.args.plain, false );
+
local args = frame.args
    frame.args.nocat = toBoolean( frame.args.nocat, false );
+
 
    frame.args.references = toBoolean( frame.args.references, true );
+
-- проверка на отсутствие обязательного параметра property
    local args = frame.args
+
if not args.property then
 +
throwError( 'property-param-not-provided' )
 +
end
 +
local override;
 +
local propertyId = mw.language.getContentLanguage():ucfirst( string.gsub( args.property, '([^Pp0-9].*)$', function(w)
 +
if string.sub( w, 1, 1 ) == '~' then override = w; end
 +
return '';
 +
end ) )
 +
args = getPropertyParams( propertyId, nil, args );
 +
if (override) then
 +
args[override:match('[,~]([^=]*)=')] = override:match('=(.*)')
 +
args['property'] = propertyId
 +
end
 +
 
 +
local datatype = args.datatype;
 +
 
 +
-- проброс всех параметров из шаблона {wikidata} и параметра from откуда угодно
 +
p_frame = frame
 +
while p_frame do
 +
if p_frame:getTitle() == mw.site.namespaces[10].name .. ':Wikidata' then
 +
copyTo( p_frame.args, args, true );
 +
end
 +
if p_frame.args and p_frame.args.from and p_frame.args.from ~= '' then
 +
args.entityId = p_frame.args.from;
 +
end
 +
p_frame = p_frame:getParent();
 +
end
  
    -- проверка на отсутствие обязательного параметра property
+
args.plain = toBoolean( args.plain, false );
    if not args.property then
+
args.nocat = toBoolean( args.nocat, false );
        throwError( 'property-param-not-provided' )
+
args.references = toBoolean( args.references, true );
    end
 
local propertyId = mw.language.getContentLanguage():ucfirst( string.gsub( args.property, '%[.*$', '' ) )
 
  
 
-- если значение передано в параметрах вызова то выводим только его
 
-- если значение передано в параметрах вызова то выводим только его
    if args.value and args.value ~= '' then
+
if args.value and args.value ~= '' then
        -- специальное значение для скрытия Викиданных
+
-- специальное значение для скрытия Викиданных
        if args.value == '-' then
+
if args.value == '-' then
            return ''
+
return ''
        end
+
end
 
local value = args.value
 
local value = args.value
  
        -- опция, запрещающая оформление значения, поэтому никак не трогаем
+
-- опция, запрещающая оформление значения, поэтому никак не трогаем
        if plain then
+
if args.plain then
            return value
+
return value
        end
+
end
  
 
-- обработчики по типу значения
 
-- обработчики по типу значения
local datatype = getPropertyDatatype( propertyId );
+
local wrapperExtraArgs = ''
if datatype == 'commonsMedia' and not string.find( value, '[%[%]%{%}]' ) then
+
if args['value-module'] and args['value-function'] and not string.find( value, '[%[%]%{%}]' ) then
 +
local func = getUserFunction( args, 'value' );
 +
value = func( {}, args, value );
 +
elseif datatype == 'commonsMedia' then
 
value = formatCommonsMedia( value, args );
 
value = formatCommonsMedia( value, args );
 +
elseif datatype == 'external-id' and not string.find( value, '[%[%]%{%}]' ) then
 +
wrapperExtraArgs = wrapperExtraArgs .. ' data-wikidata-external-id="' .. mw.text.encode( value ).. '"';
 +
value = formatExternalId( value, args );
 +
elseif datatype == 'math' then
 +
value = formatMath( value, args );
 
elseif datatype == 'url' then
 
elseif datatype == 'url' then
 
local moduleUrl = require( 'Module:URL' );
 
local moduleUrl = require( 'Module:URL' );
    value = moduleUrl.formatUrlSingle( nil, args, value );
+
if not args.length or args.length == '' then
 +
args.length = 25
 +
end
 +
value = moduleUrl.formatUrlSingle( nil, args, value );
 +
end
 +
 
 +
-- оборачиваем в тег для JS-функций
 +
if string.match( propertyId, '^P%d+$' ) then
 +
value = mw.text.trim( value )
 +
 
 +
-- временная штрафная категория для исправления табличных вставок
 +
if ( propertyId ~= 'P166'
 +
and string.match( value, '<t[dr][ >]' )
 +
and not string.match( value, '<table >]' )
 +
and not string.match( value, '^%{%|' ) ) then
 +
value = value .. getCategoryByCode( 'value-contains-table' )
 +
else
 +
value = wrapFormatProperty( value, 'class="no-wikidata"'
 +
.. wrapperExtraArgs .. ' data-wikidata-property-id="'
 +
.. propertyId .. '"' );
 +
end
 
end
 
end
  
        -- если трогать всё-таки можно, добавляем категорию-маркер
+
-- добавляем категорию-маркер
    if not args.nocat then
+
if not args.nocat then
value = value .. categoryLocalValuePresent;
+
local pageTitle = mw.title.getCurrentTitle();
 +
if pageTitle.namespace == 0 then
 +
value = value .. getCategoryByCode( 'local-value-present' );
 +
end
 
end
 
end
  
        return value
+
return value
    end
+
end
  
    if ( plain ) then -- вызова стандартного обработчика без оформления, если передана опция plain
+
if ( args.plain ) then -- вызова стандартного обработчика без оформления, если передана опция plain
    return frame:callParserFunction( '#property', propertyId );
+
local callArgs = { propertyId };
    end
+
if args.entityId then
 +
callArgs.from = args.entityId;
 +
end
 +
return frame:callParserFunction( '#property', callArgs );
 +
end
  
 
g_frame = frame
 
g_frame = frame
 
-- после проверки всех аргументов -- вызов функции оформления для свойства (набора утверждений)
 
-- после проверки всех аргументов -- вызов функции оформления для свойства (набора утверждений)
    return formatProperty( args )
+
return formatProperty( args )
 
end
 
end
  
 
--[[
 
--[[
  Функция оформления ссылок на источники (reference)  
+
Функция оформления ссылок на источники (reference)
  Подробнее о ссылках на источники см. d:Wikidata:Glossary/ru
+
Подробнее о ссылках на источники см. d:Wikidata:Glossary/ru
  
  Экспортируется в качестве зарезервированной точки для вызова из функций-расширения вида claim-module/claim-function через context
+
Экспортируется в качестве зарезервированной точки для вызова из функций-расширения вида claim-module/claim-function через context
  Вызов из других модулей напрямую осуществляться не должен (используйте frame:expandTemplate вместе с одним из специлизированных шаблонов вывода значения свойства).
+
Вызов из других модулей напрямую осуществляться не должен (используйте frame:expandTemplate вместе с одним из специлизированных шаблонов вывода значения свойства).
  
  Принимает: объект-таблицу утверждение
+
Принимает: объект-таблицу утверждение
  Возвращает: строку оформленных ссылок для отображения в статье
+
Возвращает: строку оформленных ссылок для отображения в статье
 
]]
 
]]
 
function formatRefs( context, options, statement )
 
function formatRefs( context, options, statement )
Строка 778: Строка 1421:
 
end
 
end
  
local result = '';
+
local references = {};
 
if ( statement.references ) then
 
if ( statement.references ) then
  
 
local allReferences = statement.references;
 
local allReferences = statement.references;
 
local hasPreferred = false;
 
local hasPreferred = false;
 +
local displayCount = 0;
 
for _, reference in pairs( statement.references ) do
 
for _, reference in pairs( statement.references ) do
 
if ( reference.snaks
 
if ( reference.snaks
Строка 788: Строка 1432:
 
and reference.snaks.P248[1]
 
and reference.snaks.P248[1]
 
and reference.snaks.P248[1].datavalue
 
and reference.snaks.P248[1].datavalue
and reference.snaks.P248[1].datavalue.value["numeric-id"] ) then
+
and reference.snaks.P248[1].datavalue.value.id ) then
local entityId = "Q" .. reference.snaks.P248[1].datavalue.value["numeric-id"];
+
local entityId = reference.snaks.P248[1].datavalue.value.id;
 
if ( preferredSources[entityId] ) then
 
if ( preferredSources[entityId] ) then
 
hasPreferred = true;
 
hasPreferred = true;
Строка 803: Строка 1447:
 
and reference.snaks.P248[1]
 
and reference.snaks.P248[1]
 
and reference.snaks.P248[1].datavalue
 
and reference.snaks.P248[1].datavalue
and reference.snaks.P248[1].datavalue.value["numeric-id"] ) then
+
and reference.snaks.P248[1].datavalue.value.id ) then
local entityId = "Q" .. reference.snaks.P248[1].datavalue.value["numeric-id"];
+
local entityId = reference.snaks.P248[1].datavalue.value.id;
 
if ( deprecatedSources[entityId] ) then
 
if ( deprecatedSources[entityId] ) then
 
display = false;
 
display = false;
Строка 810: Строка 1454:
 
end
 
end
 
end
 
end
if ( display ) then
+
if ( display == true ) then
result = result .. moduleSources.renderReference( g_frame, options.entity, reference );
+
if ( displayCount > 2 ) then
 +
if ( options.entity and options.property ) then
 +
table.remove( references );
 +
local moreReferences = '<sup>[[d:' .. options.entity.id .. '#' .. string.upper( options.property ) .. '|[…]]]</sup>';
 +
table.insert( references, moreReferences );
 +
end
 +
break;
 +
end;
 +
local refText = moduleSources.renderReference( g_frame, options.entity, reference );
 +
if ( refText ~= '' ) then
 +
table.insert( references, refText );
 +
displayCount = displayCount + 1;
 +
end
 
end
 
end
 
end
 
end
 
end
 
end
return result
+
return table.concat( references );
 
end
 
end
  
 
return p
 
return p

Текущая версия на 16:33, 22 февраля 2020

Используется в {{Wikidata}} (см. описания параметров там же). Настраивается при помощи Модуль:Wikidata/config.

Функции данного модуля не предназначены для прямого вызова из шаблонов карточек или других модулей, не являющихся функциями расширения данного. Для вызова из шаблонов карточек используйте шаблон {{wikidata}} или один из специализированных шаблонов для свойств. Для вызова функций Викиданных предназначенных для отображения чаще всего достаточно вызова frame:expandTemplate{} с вызовом шаблона, ответственного за отрисовку свойства. С другой стороны, вызов определённых функций модуля (в основном это касается getEntityObject()) может в будущем стать предпочтительным. Данный Lua-функционал в любом случае стоит рассматривать как unstable с точки зрения сохранения совместимости на уровне кода (вместе с соответствующими функциями API для Wikibase Client).

Далее описывается внутренняя документация. Названия функций и параметров могут изменяться. При их изменении автор изменений обязан обновить шаблон {{wikidata}} и специализированные шаблоны свойств. Изменения в других местах, если кто-то всё-таки вызывает функции модуля напрямую, остаются на совести автора «костыля». Итак, при вызове шаблона {{wikidata}} или специализированного шаблона свойства управление отдаётся на функцию formatStatements, которая принимает frame. Из frame достаются следующие опции, которые так или иначе передаются в остальные функции:

  • plain — булевый переключатель (по умолчанию false). Если true, результат совпадает с обычным вызовом {{#property:pNNN}} (по факту им и будет являться)
  • references — булевый переключатель (по умолчанию true). Если true, после вывода значения параметра дополнительно выводит ссылки на источники, указанные в Викиданных. Для вывода используется Модуль:Sources. Обычно отключается для тех свойств, которые являются «самоописываемыми», например, внешними идентификаторами или ссылками (когда такая ссылка является доказательством своей актуальности), например, идентификаторы IMDb.
  • value — значение, которое надо выводить вместо значений из Викиданных (используется, если что-то задано уже в карточке в виде т. н. локального свойства)

По умолчанию модуль поддерживает вывод следующих значений без дополнительных настроек:

  • географические координаты (coordinates)
  • количественные значения (quantity)
  • моноязычный текст (monolingualtext)
  • строки (string)
  • даты (time)

Остальные типы данных требуют указания функции форматирования значения.

Поддерживаются три типа параметров-функций, которые дополнительно указывают, как надо форматировать значения:

  • property-module, property-function — название модуля и функции модуля, которые отвечают за форматирование вывода массива значений свойства (statements, claims) с учётом квалификаторов, ссылок и прочего. Например, оформляет множество выводов в таблицу или график. Характерные примеры:
    Спецификация функции: function p.…( context, options ), поведение по умолчанию: Модуль:Wikidata#formatPropertyDefault.
  • claim-module, claim-function — название модуля и функции модуля, которые отвечают за форматирование вывода значения свойства (statement, claim) с учётом квалификаторов, ссылок и прочего. Может, например, дополнительно к основному значению (main snak) вывести значения квалификаторов. Характерные примеры:
    Спецификация функции: function p.…( context, statement )
  • value-module, value-function — название модуля и функции модуля, которые отвечают за форматирование значения (snak, snak data value), в зависимости от контекста, как значений свойства, так и значений квалификатора (если вызывается из claim-module/claim-function). Необходимо для изменения отображения свойства, например, генерации викиссылки вместо простой строки или даже вставки изображения вместо отображения имени файла изображения (так как ссылки на изображения хранятся как строки). Характерные примеры:
    Спецификация функции: function p.…( value, options )

См. также


-- settings, may differ from project to project
local fileDefaultSize = '267x400px';
local outputReferences = true;

-- sources that shall be omitted if any preffered sources exists
local deprecatedSources = {
	Q36578 = true, -- Gemeinsame Normdatei
	Q63056 = true, -- Find a Grave
	Q15222191 = true, -- BNF
	Q15241312 = true, -- Freebase
};
local preferredSources = {
	Q5375741  = true, -- Encyclopædia Britannica Online
	Q17378135  = true, -- Great Soviet Encyclopedia (1969—1978)
};

-- Ссылки на используемые модули, которые потребуются в 99% случаев загрузки страниц (чтобы иметь на виду при переименовании)
local moduleSources = require( 'Module:Sources' )
local WDS = require( 'Module:WikidataSelectors' );

-- Константы
local contentLanguageCode = mw.getContentLanguage():getCode();

local p = {};
local config = nil;

local formatDatavalue, formatEntityId, formatRefs, formatSnak, formatStatement,
	formatStatementDefault, formatProperty, getSourcingCircumstances,
	getPropertyDatatype, getPropertyParams, throwError, toBoolean;

local function copyTo( obj, target, skipEmpty )
	for k, v in pairs( obj ) do
		if skipEmpty ~= true or ( v ~= nil and v ~= '' ) then
			target[k] = v;
		end
	end
	return target;
end

local function min( prev, next )
	if ( prev == nil ) then return next;
	elseif ( prev > next ) then return next;
	else return prev; end
end

local function max( prev, next )
	if ( prev == nil ) then return next;
	elseif ( prev < next ) then return next;
	else return prev; end
end

local function getConfig( section, code )
	if config == nil then
		config = require( 'Module:Wikidata/config' );
	end;
	if not config then
		config = {};
	end

	if not section then
		return config;
	end
	if not code then
		return config[ section ] or {};
	end

	if not config[ section ] then
		return nil;
	end
	return config[ section ][ code ];
end

local function getCategoryByCode( code )
	local value = getConfig( 'categories', code );
	if not value or value == '' then
		return '';
	end
	return '[[Category:' .. value .. ']]';
end

local function splitISO8601(str)
	if 'table' == type(str) then
		if str.args and str.args[1] then
			str = '' .. str.args[1]
		else
			return 'unknown argument type: ' .. type( str ) .. ': ' .. table.tostring( str )
		end
	end
	local Y, M, D = (function(str)
		local pattern = "(%-?%d+)%-(%d+)%-(%d+)T"
		local Y, M, D = mw.ustring.match( str, pattern )
		return tonumber(Y), tonumber(M), tonumber(D)
	end) (str);
	local h, m, s = (function(str)
		local pattern = "T(%d+):(%d+):(%d+)%Z";
		local H, M, S = mw.ustring.match( str, pattern);
		return tonumber(H), tonumber(M), tonumber(S);
	end) (str);
	local oh,om = ( function(str)
		if str:sub(-1)=="Z" then return 0,0 end; -- ends with Z, Zulu time
		-- matches ±hh:mm, ±hhmm or ±hh; else returns nils
		local pattern = "([-+])(%d%d):?(%d?%d?)$";
		local sign, oh, om = mw.ustring.match( str, pattern);
		sign, oh, om = sign or "+", oh or "00", om or "00";
		return tonumber(sign .. oh), tonumber(sign .. om);
	end )(str)
	return {year=Y, month=M, day=D, hour=(h+oh), min=(m+om), sec=s};
end

local function parseTimeBoundaries( time, precision )
	local s = splitISO8601( time );
	if (not s) then return nil; end

	if ( precision >= 0 and precision <= 8 ) then
		local powers = { 1000000000 , 100000000, 10000000, 1000000, 100000, 10000, 1000, 100, 10 }
		local power = powers[ precision + 1 ];
		local left = s.year - ( s.year % power );
		return { tonumber(os.time( {year=left, month=1, day=1, hour=0, min=0, sec=0} )) * 1000,
			tonumber(os.time( {year=left + power - 1, month=12, day=31, hour=29, min=59, sec=58} )) * 1000 + 1999 };
	end

	if ( precision == 9 ) then
		return { tonumber(os.time( {year=s.year, month=1, day=1, hour=0, min=0, sec=0} )) * 1000,
			tonumber(os.time( {year=s.year, month=12, day=31, hour=23, min=59, sec=58} )) * 1000 + 1999 };
	end

	if ( precision == 10 ) then
		local lastDays = {31, 28.25, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
		local lastDay = lastDays[s.month];
		return { tonumber(os.time( {year=s.year, month=s.month, day=1, hour=0, min=0, sec=0} )) * 1000,
			tonumber(os.time( {year=s.year, month=s.month, day=lastDay, hour=23, min=59, sec=58} )) * 1000 + 1999 };
	end

	if ( precision == 11 ) then
		return { tonumber(os.time( {year=s.year, month=s.month, day=s.day, hour=0, min=0, sec=0} )) * 1000,
			tonumber(os.time( {year=s.year, month=s.month, day=s.day, hour=23, min=59, sec=58} )) * 1000 + 1999 };
	end

	if ( precision == 12 ) then
		return { tonumber(os.time( {year=s.year, month=s.month, day=s.day, hour=s.hour, min=0, sec=0} )) * 1000,
			tonumber(os.time( {year=s.year, month=s.month, day=s.day, hour=s.hour, min=59, sec=58} )) * 1000 + 19991999 };
	end

	if ( precision == 13 ) then
		return { tonumber(os.time( {year=s.year, month=s.month, day=s.day, hour=s.hour, min=s.min, sec=0} )) * 1000,
			tonumber(os.time( {year=s.year, month=s.month, day=s.day, hour=s.hour, min=s.min, sec=58} )) * 1000 + 1999 };
	end

	if ( precision == 14 ) then
		local t = tonumber(os.time( {year=s.year, month=s.month, day=s.day, hour=s.hour, min=s.min, sec=0} ) );
		return { t * 1000, t * 1000 + 999 };
	end

	error('Unsupported precision: ' .. precision );
end

--[[
 Преобразует строку в булевое значение

 Принимает: строковое значение (может отсутствовать)
 Возвращает: булевое значение true или false, если получается распознать значение, или defaultValue во всех остальных  случаях
]]
local function toBoolean( valueToParse, defaultValue )
	if ( valueToParse ~= nil ) then
		if valueToParse == false or valueToParse == '' or valueToParse == 'false' or valueToParse == '0' then
			return false
		end
		return true
	end
	return defaultValue;
end

--[[
 Обрачивает отформатированное значение в тег
 
 Принимает: строковое значение, строку с атрибутами (может отсутствовать)
 Возвращает: строковое значение, значения с блочными тегами остаются блоком, текст встраиваем в строку
]]
local function wrapFormatProperty( value, attributes )
	local tagName = 'span';
	local spacer = '';
	if ( string.match( value, '\n' )
			or string.match( value, '<t[dhr][ >]' )
			or string.match( value, '<div[ >]' )
			or string.find( value, 'UNIQ%-%-imagemap' ) ) then
		tagName = 'div';
		spacer = '\n'
	end
	return '<' .. tagName .. ' ' .. ( attributes or '' ) .. '>' .. spacer .. value .. '</' .. tagName .. '>';
end

--[[
	Функция для получения сущности (еntity) для текущей страницы
	Подробнее о сущностях см. d:Wikidata:Glossary/ru

	Принимает: строковый индентификатор (типа P18, Q42)
	Возвращает: объект таблицу, элементы которой индексируются с нуля
]]
local function getEntityFromId( id )
	local entity;
	local wbStatus;

	if id then
		wbStatus, entity = pcall( mw.wikibase.getEntityObject, id )
	else
		wbStatus, entity = pcall( mw.wikibase.getEntityObject );
	end

	return entity;
end

--[[
	Внутрення функция для формирования сообщения об ошибке

	Принимает: ключ элемента в таблице config.errors (например entity-not-found)
	Возвращает: строку сообщения
]]
local function throwError( key )
	error( getConfig( 'errors', key ) );
end

--[[
	Функция для получения идентификатора сущностей

	Принимает: объект таблицу сущности
	Возвращает: строковый индентификатор (типа P18, Q42)
]]
local function getEntityIdFromValue( value )
	local prefix = ''
	if value['entity-type'] == 'item' then
		prefix = 'Q'
	elseif value['entity-type'] == 'property' then
		prefix = 'P'
	else
		throwError( 'unknown-entity-type' )
	end
	return prefix .. value['numeric-id']
end

-- проверка на наличие специилизированной функции в опциях
local function getUserFunction( options, prefix, defaultFunction )
	-- проверка на указание специализированных обработчиков в параметрах,
	-- переданных при вызове
	if options[ prefix .. '-module' ] or options[ prefix .. '-function' ] then
		-- проверка на пустые строки в параметрах или их отсутствие
		if not options[ prefix .. '-module' ] or not options[ prefix .. '-function' ] then
			throwError( 'unknown-' .. prefix .. '-module' );
		end
		-- динамическая загруза модуля с обработчиком указанным в параметре
		local formatter = require( 'Module:' .. options[ prefix .. '-module' ] );
		if formatter == nil then
			throwError( prefix .. '-module-not-found' )
		end
		local fun = formatter[ options[ prefix .. '-function' ] ]
		if fun == nil then
			throwError( prefix .. '-function-not-found' )
		end
		return fun;
	end

	return defaultFunction;
end

-- Выбирает свойства по property id, дополнительно фильтруя их по рангу
local function selectClaims( context, options, propertySelector )
	if ( not context ) then error( 'context not specified' ); end;
	if ( not options ) then error( 'options not specified' ); end;
	if ( not options.entity ) then error( 'options.entity is missing' ); end;
	if ( not propertySelector ) then error( 'propertySelector not specified' ); end;

	result = WDS.filter( options.entity.claims, propertySelector );

	if ( not result or #result == 0 ) then
		return nil;
	end

	if options.limit and options.limit ~= '' and options.limit ~= '-'  then
		local limit = tonumber( options.limit, 10 );
		while #result > limit do
			table.remove( result );
		end
	end

	return result;
end

--[[
	Функция для получения значения свойства элемента в заданный момент времени.

	Принимает: контекст, элемент, временные границы, таблица ID свойства
	Возвращает: таблицу соответствующих значений свойства
]]
local function getPropertyInBoundaries( context, entityId, boundaries, propertyIds, selectors )
	if (type(entityId) ~= 'string') then error('type of entityId argument expected string, but was ' .. type(entityId)); end

	local results = {};

	if not propertyIds or #propertyIds == 0 then
		return results;
	end

	for _, propertyId in ipairs( propertyIds ) do
		local selector = selectors[_];
		local propertyClaims = mw.wikibase.getAllStatements( entityId, propertyId );
		local fakeAllClaims = {};
		fakeAllClaims[propertyId] = propertyClaims;
		
		local filteredClaims = WDS.filter( fakeAllClaims, selector .. '[rank:preferred, rank:normal]' );
		if filteredClaims then
			for _, claim in pairs( filteredClaims ) do
				if not boundaries then
					table.insert( results, claim.mainsnak );
				else
					local startBoundaries = p.getTimeBoundariesFromQualifier( context.frame, context, claim, 'P580' );
					local endBoundaries = p.getTimeBoundariesFromQualifier( context.frame, context, claim, 'P582' );

					if ( (startBoundaries == nil or ( startBoundaries[2] <= boundaries[1]))
							and (endBoundaries == nil or ( endBoundaries[1] >= boundaries[2]))) then
						table.insert( results, claim.mainsnak );
					end
				end
			end
		end

		if #results > 0 then
			break;
		end
	end

	return results;
end

--[[
	TODO
]]
function p.getTimeBoundariesFromQualifier( frame, context, statement, qualifierId )
	-- only support exact date so far, but need improvment
	local left = nil;
	local right = nil;
	if ( statement.qualifiers and statement.qualifiers[qualifierId] ) then
		for _, qualifier in pairs( statement.qualifiers[qualifierId] ) do
			local boundaries = context.parseTimeBoundariesFromSnak( qualifier );
			if ( not boundaries ) then return nil; end
			left = min( left, boundaries[1] );
			right = max( right, boundaries[2] );
		end
	end

	if ( not left or not right ) then
		return nil;
	end

	return { left, right };
end

--[[
	TODO
]]
function p.getTimeBoundariesFromQualifiers( frame, context, statement, qualifierIds )
	if not qualifierIds then
		qualifierIds = { 'P582', 'P580', 'P585' };
	end

	for _, qualifierId in ipairs( qualifierIds ) do
		local result = p.getTimeBoundariesFromQualifier( frame, context, statement, qualifierId );
		if result then
			return result;
		end
	end

	return nil;
end

local CONTENT_LANGUAGE_CODE = mw.language.getContentLanguage():getCode();
local getLabelWithLang_DEFAULT_PROPERTIES = { "P1813", "P1448", "P1705" };
local getLabelWithLang_DEFAULT_SELECTORS = {
	'P1813[language:' .. CONTENT_LANGUAGE_CODE .. ']',
	'P1448[language:' .. CONTENT_LANGUAGE_CODE .. ']',
	'P1705[language:' .. CONTENT_LANGUAGE_CODE .. ']'
};

--[[
	Функция для получения метки элемента в заданный момент времени.

	Принимает: контекст, элемент, временные границы
	Возвращает: текстовую метку элемента, язык метки
]]
function getLabelWithLang( context, options, entityId, boundaries, propertyIds, selectors )
	if (type(entityId) ~= 'string') then error('type of entityId argument expected string, but was ' .. type(entityId)); end
	if not entityId then
		return nil;
	end

	local langCode = CONTENT_LANGUAGE_CODE;

	-- name from label
	local label = nil;
	if ( options.text and options.text ~= '' ) then
		label = options.text;
	else
		if not propertyIds then
			propertyIds = getLabelWithLang_DEFAULT_PROPERTIES;
			selectors = getLabelWithLang_DEFAULT_SELECTORS;
		end

		-- name from properties
		local results = getPropertyInBoundaries( context, entityId, boundaries, propertyIds, selectors );

		for _, result in pairs( results ) do
			if result.datavalue and result.datavalue.value then
				if result.datavalue.type == 'monolingualtext' and result.datavalue.value.text then
					label = result.datavalue.value.text;
					langCode = result.datavalue.value.language;
					break;
				elseif result.datavalue.type == 'string' then
					label = result.datavalue.value;
					break;
				end
			end
		end
		
		if (not label) then
			label, langCode = mw.wikibase.getLabelWithLang( entityId );
			if not langCode then
				return nil;
			end
		end
	end

	return label, langCode;
end

--[[
	Функция для оформления утверждений (statement)
	Подробнее о утверждениях см. d:Wikidata:Glossary/ru

	Принимает: таблицу параметров
	Возвращает: строку оформленного текста, предназначенного для отображения в статье
]]
local function formatProperty( options )
	-- Получение сущности по идентификатору
	local entity = getEntityFromId( options.entityId )
	if not entity then
		return -- throwError( 'entity-not-found' )
	end
	-- проверка на присутсвие у сущности заявлений (claim)
	-- подробнее о заявлениях см. d:Викиданные:Глоссарий
	if (entity.claims == nil) then
		return '' --TODO error?
	end

	-- improve options
	options.frame = g_frame;
	options.entity = entity;
	options.extends = function( self, newOptions )
		return copyTo( newOptions, copyTo( self, {} ) )
	end

	if ( options.i18n ) then
		options.i18n = copyTo( options.i18n, copyTo( getConfig( 'i18n' ), {} ) );
	else
		options.i18n = getConfig( 'i18n' );
	end

	-- create context
	local context = {
		entity = options.entity,
		formatSnak = formatSnak,
		formatPropertyDefault = formatPropertyDefault,
		formatStatementDefault = formatStatementDefault }
	context.cloneOptions = function( options )
		local entity = options.entity;
		options.entity = nil;

		newOptions = mw.clone( options );
		options.entity = entity;
		newOptions.entity = entity;
		newOptions.frame = options.frame; -- На склонированном фрейме frame:expandTemplate()

		return newOptions;
	end;
	context.formatProperty = function( options )
		local func = getUserFunction( options, 'property', context.formatPropertyDefault );
		return func( context, options )
	end;
	context.formatStatement = function( options, statement ) return formatStatement( context, options, statement ) end;
	context.formatSnak = function( options, snak, circumstances ) return formatSnak( context, options, snak, circumstances ) end;
	context.formatRefs = function( options, statement ) return formatRefs( context, options, statement ) end;

	context.parseTimeFromSnak = function( snak )
			if ( snak and snak.datavalue and snak.datavalue.value and snak.datavalue.value.time ) then
				return tonumber(os.time( splitISO8601( tostring( snak.datavalue.value.time ) ) ) ) * 1000;
			end
			return nil;
		end
	context.parseTimeBoundariesFromSnak = function( snak )
			if ( snak and snak.datavalue and snak.datavalue.value and snak.datavalue.value.time and snak.datavalue.value.precision ) then
				return parseTimeBoundaries( snak.datavalue.value.time, snak.datavalue.value.precision );
			end
			return nil;
		end
	context.getSourcingCircumstances = function( statement ) return getSourcingCircumstances( statement ) end;
	context.selectClaims = function( options, propertyId ) return selectClaims( context, options, propertyId ) end;

	return context.formatProperty( options );
end

function formatPropertyDefault( context, options )
	if ( not context ) then error( 'context not specified' ); end;
	if ( not options ) then error( 'options not specified' ); end;
	if ( not options.entity ) then error( 'options.entity missing' ); end;

	local claims;
	if options.property then -- TODO: Почему тут может не быть property?
		claims = context.selectClaims( options, options.property );
	end
	if claims == nil then
		return '' --TODO error?
	end

	-- Обход всех заявлений утверждения и с накоплением оформленых предпочтительных
	-- заявлений в таблице
	local formattedClaims = {}

	for i, claim in ipairs(claims) do
		local formattedStatement = context.formatStatement( options, claim )
		-- здесь может вернуться либо оформленный текст заявления, либо строка ошибки, либо nil
		if ( formattedStatement and formattedStatement ~= '' ) then
			formattedStatement = '<span class="wikidata-claim" data-wikidata-property-id="' .. string.upper( options.property ) .. '" data-wikidata-claim-id="' .. claim.id .. '">' .. formattedStatement .. '</span>'
			table.insert( formattedClaims, formattedStatement )
		end
	end

	-- создание текстовой строки со списком оформленых заявлений из таблицы
	local out = mw.text.listToText( formattedClaims, options.separator, options.conjunction )
	if out ~= '' then
		if options.before then
			out = options.before .. out
		end
		if options.after then
			out = out .. options.after
		end
	end

	return out
end

--[[
	Функция для оформления одного утверждения (statement)

	Принимает: объект-таблицу утверждение и таблицу параметров
	Возвращает: строку оформленного текста с заявлением (claim)
]]
function formatStatement( context, options, statement )
	if ( not statement ) then
		error( 'statement is not specified or nil' );
	end
	if not statement.type or statement.type ~= 'statement' then
		throwError( 'unknown-claim-type' )
	end

	local functionToCall = getUserFunction( options, 'claim', context.formatStatementDefault );
	return functionToCall( context, options, statement );
end

function getSourcingCircumstances( statement )
	if (not statement) then error('statement is not specified') end;

	local circumstances = {};
	if ( statement.qualifiers
			and statement.qualifiers.P1480 ) then
		for i, qualifier in pairs( statement.qualifiers.P1480 ) do
			if ( qualifier
					and qualifier.datavalue
					and qualifier.datavalue.type == 'wikibase-entityid'
					and qualifier.datavalue.value
					and qualifier.datavalue.value['entity-type'] == 'item' ) then
				table.insert(circumstances, qualifier.datavalue.value.id)
			end
		end
	end
	return circumstances;
end

--[[
	Функция для оформления одного утверждения (statement)

	Принимает: объект-таблицу утверждение, таблицу параметров,
	объект-функцию оформления внутренних структур утверждения (snak) и
	объект-функцию оформления ссылки на источники (reference)
	Возвращает: строку оформленного текста с заявлением (claim)
]]
function formatStatementDefault( context, options, statement )
	if (not context) then error('context is not specified') end;
	if (not options) then error('options is not specified') end;
	if (not statement) then error('statement is not specified') end;

	local circumstances = context.getSourcingCircumstances( statement );

	options.qualifiers = statement.qualifiers;

	local result = context.formatSnak( options, statement.mainsnak, circumstances );
	
    if ( options.qualifier and statement.qualifiers and statement.qualifiers[ options.qualifier ] ) then
    	qualConfig = getPropertyParams( options.qualifier, nil, {})
    	if options.i18n then qualConfig.i18n = options.i18n end
    	local qualifierValues = {};
		for _, qualifierSnak in pairs( statement.qualifiers[ options.qualifier ] ) do
			local snakValue = context.formatSnak( qualConfig, qualifierSnak );
			if snakValue and snakValue ~= '' then
				table.insert( qualifierValues, snakValue );
			end
		end
		if ( #qualifierValues ) then
			if qualConfig.invisible then 
	        	result = result .. table.concat( qualifierValues, ', ' );
			else
	        	result = result .. ' (' .. table.concat( qualifierValues, ', ' ) .. ')';
	        end
        end
    end

	if ( result and result ~= '' and options.references ) then
		result = result .. context.formatRefs( options, statement );
	end

	return result;
end

--[[
	Функция для оформления части утверждения (snak)
	Подробнее о snak см. d:Викиданные:Глоссарий

	Принимает: таблицу snak объекта (main snak или же snak от квалификатора) и таблицу опций
	Возвращает: строку оформленного викитекста
]]
function formatSnak( context, options, snak, circumstances )
	circumstances = circumstances or {};
	local hash = '';
	local mainSnakClass = '';
	if ( snak.hash ) then
		hash = ' data-wikidata-hash="' .. snak.hash .. '"';
	else
		mainSnakClass = ' wikidata-main-snak';
	end

	local before = '<span class="wikidata-snak ' .. mainSnakClass .. '"' .. hash .. '>'
	local after = '</span>'

	if snak.snaktype == 'somevalue' then
		if ( options['somevalue'] and options['somevalue'] ~= '' ) then
			result = options['somevalue'];
		else
			result = options.i18n['somevalue'];
		end
	elseif snak.snaktype == 'novalue' then
		if ( options['novalue'] and options['novalue'] ~= '' ) then
			result = options['novalue'];
		else
			result = options.i18n['novalue'];
		end
	elseif snak.snaktype == 'value' then
		result = formatDatavalue( context, options, snak.datavalue, snak.datatype );
		for _, item in pairs(circumstances) do
			if options.i18n[item] then
				result = options.i18n[item] .. result;
			end
		end
	else
		throwError( 'unknown-snak-type' );
	end
	
	if ( not result or result == '' ) then
		return nil;
	end

	return before .. result .. after;
end

--[[
	Функция для оформления объектов-значений с географическими координатами

	Принимает: объект-значение и таблицу параметров,
	Возвращает: строку оформленного текста
]]
function formatGlobeCoordinate( value, options )
	-- проверка на требование в параметрах вызова на возврат сырого значения
	if options['subvalue'] == 'latitude' then -- широты
		return value['latitude']
	elseif options['subvalue'] == 'longitude' then -- долготы
		return value['longitude']
	elseif options['nocoord'] and options['nocoord'] ~= '' then
		-- если передан параметр nocoord, то не выводить координаты
		-- обычно это делается при использовании нескольких карточек на странице
		return ''
	else
		-- в противном случае формируются параметры для вызова шаблона {{coord}}
		-- нужно дописать в документации шаблона, что он отсюда вызывается, и что
		-- любое изменние его парамеров  должно быть согласовано с кодом тут
		
		coord_mod = require( "Module:Coordinates" );
		
		local globe = options.globe or ''
		if globe == '' and value['globe'] then
			globes = require( 'Module:Wikidata/Globes' )
			globe = globes[value['globe']] or ''
		end
		
		local display = 'inline'
		if options.display and options.display ~= '' then
			display = options.display
		elseif ( options.property:upper() == 'P625' ) then
			display = 'title'
		end
		
		g_frame.args = {tostring(value['latitude']), tostring(value['longitude']), globe = globe, type = options.type and options.type or '', display = display  }
		
		return coord_mod.coord(g_frame)
	end
end

--[[
	Функция для оформления объектов-значений с файлами с Викисклада

	Принимает: объект-значение и таблицу параметров,
	Возвращает: строку оформленного текста
]]
function formatCommonsMedia( value, options )
	local image = value;

	local caption = '';
	if options[ 'caption' ] and options[ 'caption' ] ~= '' then
		caption = options[ 'caption' ];
	elseif options[ 'description' ] and options[ 'description' ] ~= '' then
		caption = options[ 'description' ];
	end
	if caption ~= '' then
		caption = wrapFormatProperty( caption, 'class="media-caption" data-wikidata-qualifier-id="P2096" style="display:block;"' );
	end

	if not string.find( value, '[%[%]%{%}]' ) and not string.find( value, 'UNIQ%-%-imagemap' ) then
		-- если в value не содержится викикод или imagemap, то викифицируем имя файла
		-- ищем слово imagemap в строке, потому что вставляется плейсхолдер: [[PHAB:T28213]]
		image = '[[File:' .. value .. '|frameless';
		if options[ 'border' ] and options[ 'border' ] ~= '' then
			image = image .. '|border';
		end

		local size = options[ 'size' ];
		if size and size ~= '' then
			if not string.match( size, 'px$' )
				and not string.match( size, 'пкс$' ) -- TODO: использовать перевод для языка вики
			then
				size = size .. 'px'
			end
		else
			size = fileDefaultSize;
		end
		image = image .. '|' .. size;

		if options[ 'alt' ] and options[ 'alt' ] ~= '' then
			image = image .. '|' .. options[ 'alt' ];
		end
		image = image .. ']]';

		if caption ~= '' then
			image = image .. '<br>' .. caption;
		end

		if options[ 'local_caption' ] and options[ 'local_caption' ] ~= '' then
			image = image .. getCategoryByCode( 'media-contains-local-caption' )
		end
	else
		image = image .. caption .. getCategoryByCode( 'media-contains-markup' );
	end
	
	if options.entity and options.fixdouble then
		local page = mw.title.getCurrentTitle()
		local txt = page:getContent()
		if txt and txt:match(':' .. value) and mw.title.getCurrentTitle():inNamespace(0) then image = image .. getCategoryByCode( 'media-contains-local-double' ) end
	end
	
	return image
end

--[[
	Fonction for render math formulas

	@param string Value.
	@param table Parameters.
	@return string Formatted string.
]]
function formatMath( value, options )
	return options.frame:extensionTag{ name = 'math', content = value };
end

--[[
	Функция для оформления внешних идентификаторов

	Принимает: объект-значение и таблицу параметров,
	Возвращает: строку оформленного текста
]]
local function formatExternalId( value, options )
	local formatter = options.formatter;

	if not formatter or formatter == '' then
		local wbStatus, propertyEntity = pcall( mw.wikibase.getEntity, options.property:upper() )
		if wbStatus == true and propertyEntity then
			local isGoodFormat = false;
			local statements = propertyEntity:getBestStatements( 'P1793' );
			for _, statement in pairs( statements ) do
				if statement.mainsnak.snaktype == 'value' then
					local pattern = mw.ustring.gsub( statement.mainsnak.datavalue.value, '\\', '%' );
					pattern = mw.ustring.gsub( pattern, '{%d+,?%d*}', '+' );
					if ( string.find( pattern, '|' ) or string.find( pattern, '%)%?' )
							or mw.ustring.match( value, '^' .. pattern .. '$' ) ~= nil ) then
						isGoodFormat = true;
						break;
					end
				end
			end

			if ( isGoodFormat == true ) then
				statements = propertyEntity:getBestStatements( 'P1630' );
				for _, statement in pairs( statements ) do
					if statement.mainsnak.snaktype == 'value' then
						formatter = statement.mainsnak.datavalue.value;
						break
					end
				end
			end
		end
	end

	if formatter and formatter ~= '' then
		local link = mw.ustring.gsub( 
						mw.ustring.gsub( formatter, '$1', value ), '.',
							{ [' '] = '%20', ['+'] = '%2b' } )

		local title = options.title
		if not title or title == '' then
			title = '$1'
		end
		title = mw.ustring.gsub( title, '$1', value )

		return '[' .. link .. ' ' .. title .. ']'
	end

	return value
end

--[[
	Функция для оформления числовых значений

	Принимает: объект-значение и таблицу параметров,
	Возвращает: строку оформленного текста
]]
local function formatQuantity( value, options )
	-- диапазон значений
	local amount = string.gsub( value['amount'], '^%+', '' );
	local lang = mw.language.getContentLanguage();
	local langCode = lang:getCode();

	local function formatNum( number, sigfig )
		sigfig = sigfig or 12 -- округление до 12 знаков после запятой, на 13-м возникает ошибка в точности
		local mult = 10^sigfig;
		number = math.floor( number * mult + 0.5 ) / mult;
		return string.gsub( lang:formatNum( number ), '^-', '−' );
	end

	local out = formatNum( tonumber( amount ) );
	if value.upperBound then
		local diff = tonumber( value.upperBound ) - tonumber( amount )
		if diff > 0 then -- временная провека, пока у большинства значений не будет убрано ±0
			-- Пробуем понять до какого знака округлять
			local integer, dot, decimals, expstr = value.upperBound:match( '^+?-?(%d*)(%.?)(%d*)(.*)' )
			local prec 
			if dot == '' then
				prec = -integer:match('0*$'):len()
			else
				prec = #decimals
			end			
			bound = formatNum( diff, prec )
			if string.match( bound, 'E%-(%d+)' ) then -- если в экспоненциальном формате
				digits = tonumber( string.match( bound, 'E%-(%d+)' ) ) - 2
				bound = formatNum( diff * 10 ^ digits, prec )
				bound = string.sub( bound, 0, 2 ) .. string.rep( '0', digits ) .. string.sub( bound, -string.len( bound ) + 2 )
			end
			out = out .. ' ± ' .. bound
		end
	end

	if options.unit and options.unit ~= '' then
		if options.unit ~= '-' then
			out = out .. ' ' .. options.unit
		end
	elseif value.unit and string.match( value.unit, 'http://www.wikidata.org/entity/' ) then
		local unitEntityId = string.gsub( value.unit, 'http://www.wikidata.org/entity/', '' );
		if unitEntityId ~= 'undefined' then 
			local wbStatus, unitEntity = pcall( mw.wikibase.getEntity, unitEntityId );
			if wbStatus == true and unitEntity then
				if unitEntity.claims.P2370 and
					unitEntity.claims.P2370[1].mainsnak.snaktype == 'value' and
					not value.upperBound and
					options.siConversion == true
				then
					conversionToSIunit = string.gsub( unitEntity.claims.P2370[1].mainsnak.datavalue.value.amount, '^%+', '' );
					if math.floor( math.log10( conversionToSIunit )) ~= math.log10( conversionToSIunit ) then
						-- Если не степени десятки (переводить сантиметры в метры не надо!)
						outValue = tonumber( amount ) * conversionToSIunit
	
						if ( outValue > 0 ) then
							-- Пробуем понять до какого знака округлять
							local integer, dot, decimals, expstr = amount:match( '^(%d*)(%.?)(%d*)(.*)' )
							local prec 
							if dot == '' then
								prec = -integer:match('0*$'):len()
							else
								prec = #decimals
							end
							local adjust = math.log10( math.abs( conversionToSIunit )) + math.log10( 2 )
							local minprec = 1 - math.floor( math.log10( outValue ) + 2e-14 );
							out = formatNum( outValue, math.max( math.floor( prec + adjust ), minprec ));
						else
							out = formatNum( outValue, 0 )
						end
						unitEntityId = string.gsub( unitEntity.claims.P2370[1].mainsnak.datavalue.value.unit, 'http://www.wikidata.org/entity/', '' );
						wbStatus, unitEntity = pcall( mw.wikibase.getEntity, unitEntityId );
					end
				end
	
				local writingSystemElementId = 'Q8209';
				local langElementId = 'Q7737';
				local label = getLabelWithLang( context, options, unitEntity.id, nil, { "P5061", "P558", "P558" }, {
					'P5061[language:' .. langCode .. ']',
					'P558[P282:' .. writingSystemElementId .. ', P407:' .. langElementId .. ']',
					'P558[!P282][!P407]'
				} );
	
				out = out .. ' ' .. label;
			end
		end
	end

	return out;
end

local DATATYPE_CACHE = {}

--[[
	Get property datatype by ID.

	@param string Property ID, e.g. 'P123'.
	@return string Property datatype, e.g. 'commonsMedia', 'time' or 'url'.
]]
local function getPropertyDatatype( propertyId )
	if not propertyId or not string.match( propertyId, '^P%d+$' ) then
		return nil;
	end
	
	local cached = DATATYPE_CACHE[propertyId];
	if (cached ~= nil) then return cached; end

	local wbStatus, propertyEntity = pcall( mw.wikibase.getEntity, propertyId );
	if wbStatus ~= true or not propertyEntity then
		return nil;
	end
	mw.log("Loaded datatype " .. propertyEntity.datatype .. " of " .. propertyId .. ' from wikidata, consider passing datatype argument to formatProperty call or to Wikidata/config' )

	DATATYPE_CACHE[propertyId] = propertyEntity.datatype;
	return propertyEntity.datatype;
end

local function formatLangRefs( options )
	local langRefs = ''
	if ( options.qualifiers and options.qualifiers.P407 ) then
		for i, qualifier in pairs( options.qualifiers.P407 ) do
			if ( qualifier
					and qualifier.datavalue
					and qualifier.datavalue.type == 'wikibase-entityid' ) then
				local langRefEntity = getEntityFromId( qualifier.datavalue.value.id )
				if ( langRefEntity and langRefEntity.claims ) then
					local langRefCodeClaims = WDS.filter( langRefEntity.claims, 'P218' )
					if langRefCodeClaims then
						for _, claim in pairs( langRefCodeClaims ) do
							if ( claim.mainsnak
									and claim.mainsnak
									and claim.mainsnak.datavalue
									and claim.mainsnak.datavalue.type == 'string' ) then
								local langRefCode = claim.mainsnak.datavalue.value
								langRefs = langRefs .. '&#8203;' .. options.frame:expandTemplate{ title = 'ref-' ..langRefCode }
							end
						end
					end
				end
			end
		end
	end

	return langRefs
end

local function getDefaultValueFunction( datavalue, datatype )
	-- вызов обработчиков по умолчанию для известных типов значений
	if datavalue.type == 'wikibase-entityid' then
		-- Entity ID
		return function( context, options, value ) return formatEntityId( context, options, getEntityIdFromValue( value ) ) end;
	elseif datavalue.type == 'string' then
		-- String
		if datatype and datatype == 'commonsMedia' then
			-- Media
			return function( context, options, value )
				if options.caption and options.caption ~= '' then
					options.local_caption = options.caption;
				elseif options.description and options.description ~= '' then
					options.local_caption = options.description;
				end
				options.caption = ''
				options.description = ''
				if options.qualifiers and options.qualifiers.P2096 then
					for i, qualifier in pairs( options.qualifiers.P2096 ) do
						if ( qualifier
								and qualifier.datavalue
								and qualifier.datavalue.type == 'monolingualtext'
								and qualifier.datavalue.value
								and qualifier.datavalue.value.language == contentLanguageCode ) then
							options.caption = qualifier.datavalue.value.text
							options.description = qualifier.datavalue.value.text
							break
						end
					end
				end
				if options['appendTimestamp'] and options.qualifiers and options.qualifiers.P585 and options.qualifiers.P585[1] then
					local moment = formatDatavalue (context, options, options.qualifiers.P585[1].datavalue, 'time')
					if not options.caption or options.caption == ''  then 
						options.caption = moment
						options.description = moment
					else
						options.caption = options.caption .. ', ' .. moment
						options.description = options.description .. ', ' .. moment
					end
				end
				return formatCommonsMedia( value, options )
			end;
		elseif datatype and datatype == 'external-id' then
			-- External ID
			return function( context, options, value )
				return formatExternalId( value, options )
			end
		elseif datatype and datatype == 'math' then
			-- Math formula
			return function( context, options, value )
				return formatMath( value, options )
			end
		elseif datatype and datatype == 'url' then
			-- URL
			return function( context, options, value )
				local moduleUrl = require( 'Module:URL' )
				local langRefs = formatLangRefs( options )
				if not options.length or options.length == '' then
					options.length = math.max( 18, 25 - #langRefs )
				end
				return moduleUrl.formatUrlSingle( context, options, value ) .. langRefs
			end
		end
		return function( context, options, value ) return value end;
	elseif datavalue.type == 'monolingualtext' then
		-- моноязычный текст (строка с указанием языка)
		return function( context, options, value )
			if ( options.monolingualLangTemplate == 'lang' ) then
				if ( value.language == contentLanguageCode ) then
					return value.text;
				end
				return options.frame:expandTemplate{ title = 'lang-' .. value.language, args = { value.text } };
			elseif ( options.monolingualLangTemplate == 'ref' ) then
				return '<span class="lang" lang="' .. value.language .. '">' .. value.text .. '</span>' .. options.frame:expandTemplate{ title = 'ref-' .. value.language };
			else
				return '<span class="lang" lang="' .. value.language .. '">' .. value.text .. '</span>';
			end
		end;
	elseif datavalue.type == 'globecoordinate' then
		-- географические координаты
		return function( context, options, value ) return formatGlobeCoordinate( value, options )  end;
	elseif datavalue.type == 'quantity' then
		return function( context, options, value ) return formatQuantity( value, options )  end;
	elseif datavalue.type == 'time' then
		return function( context, options, value )
			local moduleDate = require( 'Module:Wikidata/date' )
			return moduleDate.formatDate( context, options, value );
		end;
	else
		-- во всех стальных случаях возвращаем ошибку
		throwError( 'unknown-datavalue-type' )
	end
end

--[[
	Функция для оформления значений (value)
	Подробнее о значениях  см. d:Wikidata:Glossary/ru

	Принимает: объект-значение и таблицу параметров,
	Возвращает: строку оформленного текста
]]
function formatDatavalue( context, options, datavalue, datatype )
	if ( not context ) then error( 'context not specified' ); end;
	if ( not options ) then error( 'options not specified' ); end;
	if ( not datavalue ) then error( 'datavalue not specified' ); end;
	if ( not datavalue.value ) then error( 'datavalue.value is missng' ); end;

	-- проверка на указание специализированных обработчиков в параметрах,
	-- переданных при вызове
	context.formatValueDefault = getDefaultValueFunction( datavalue, datatype );
	local functionToCall = getUserFunction( options, 'value', context.formatValueDefault );
	return functionToCall( context, options, datavalue.value );
end

local DEFAULT_BOUNDARIES = { os.time() * 1000, os.time() * 1000};

--[[
	Функция для оформления идентификатора сущности

	Принимает: строку индентификатора (типа Q42) и таблицу параметров,
	Возвращает: строку оформленного текста
]]
function formatEntityId( context, options, entityId )
	-- получение локализованного названия
	local boundaries = nil
	if options.qualifiers then
		boundaries = p.getTimeBoundariesFromQualifiers( frame, context, { qualifiers = options.qualifiers } )
	end
	if not boundaries then
		boundaries = DEFAULT_BOUNDARIES;
	end
	local label, labelLanguageCode = getLabelWithLang( context, options, entityId, boundaries )

	-- определение соответствующей показываемому элементу категории
	local category = p.extractCategory( context, options, { id = entityId } )

	-- получение ссылки по идентификатору
	local link = mw.wikibase.sitelink( entityId )
	if link then
		-- ссылка на категорию, а не добавление страницы в неё
		if mw.ustring.match( link, '^' .. mw.site.namespaces[ 14 ].name .. ':' ) then
			link = ':' .. link
		end
		if label and not options.rawArticle then
			local a = link == label and ('[[' .. link .. ']]') or '[[' .. link .. '|' .. label .. ']]';
			if ( contentLanguageCode ~= labelLanguageCode ) then
				return a .. getCategoryByCode( 'links-to-entities-with-missing-local-language-label' ) .. category;
			else
				return a .. category;
			end
		else
			return '[[' .. link .. ']]' .. category;
		end
	end

	if label then
		-- красная ссылка
		-- TODO: разобраться, почему не всегда есть options.frame
		local title = mw.title.new( label );
		if title and not title.exists and options.frame then
			local redLink = options.frame:expandTemplate{ title='Ш:Красная ссылка с рыбой', args = { entityId, label } };
			return redLink .. '<sup>[[:d:' .. entityId .. '|[d]]]</sup>' .. category;
		end

		-- TODO: перенести до проверки на существование статьи
		local sup = '';
		if ( not options.format or options.format ~= 'text' )
				and entityId ~= 'Q6581072' and entityId ~= 'Q6581097' -- TODO: переписать на format=text
				then
			sup = '<sup class="plainlinks noprint">[//www.wikidata.org/wiki/' .. entityId .. '?uselang=' .. contentLanguageCode .. ' [d&#x5d;]</sup>'
		end

		-- одноимённая статья уже существует - выводится текст и ссылка на ВД
		return '<span class="iw" data-title="' .. label .. '">' .. label
			.. sup
			.. '</span>' .. category
	end
	-- сообщение об отсутвии локализованного названия
	-- not good, but better than nothing
	return '[[:d:' .. entityId .. '|' .. entityId .. ']]<span style="border-bottom: 1px dotted; cursor: help; white-space: nowrap" title="В Викиданных нет русской подписи к элементу. Вы можете помочь, указав русский вариант подписи.">?</span>' .. getCategoryByCode( 'links-to-entities-with-missing-label' ) .. category;
end

--[[
	Функция для формирования категории на основе wikidata/config
]]
function p.extractCategory( context, options, value )
	if ( not options.category ) then
		return '';
	end
	local propertyId = string.gsub( options.category, '([^Pp0-9].*)$', '');
	local wbStatus, claims = pcall( mw.wikibase.getAllStatements, value.id, propertyId );
	if ( wbStatus ~= true or not claims ) then return ''; end
	allClaims = {}
	allClaims[ propertyId ] = claims
	claims = WDS.filter( allClaims, options.category )
	if not claims then return ''; end
	
	for _, claim in pairs( claims ) do
		if ( claim
			and claim.mainsnak
			and claim.mainsnak.datavalue
			and claim.mainsnak.datavalue.type == 'wikibase-entityid' ) then
			
			local catEntityId = claim.mainsnak.datavalue.value.id;
			local wbStatus, catSiteLink = pcall( mw.wikibase.getSitelink, catEntityId );

			if ( wbStatus == true and catSiteLink ) then
				return '[[' .. catSiteLink .. ']]';
			end
		end
	end

	return '';
end
--[[
	Функция для оформления утверждений (statement)
	Подробнее о утверждениях см. d:Wikidata:Glossary/ru

	Принимает: таблицу параметров
	Возвращает: строку оформленного текста, предназначенного для отображения в статье
]]
-- устаревшее имя, не использовать
function p.formatStatements( frame )
	return p.formatProperty( frame );
end

--[[
	Получение параметров, которые обычно используются для вывода свойства.
]]
function getPropertyParams( propertyId, datatype, params )
	local config = getConfig();

	-- Различные уровни настройки параметров, по убыванию приоритета
	local propertyParams = {};

	-- 1. Параметры, указанные явно при вызове
	if params then
		for key, value in pairs( params ) do
			if value ~= '' then
				propertyParams[ key ] = value;
			end
		end
	end

	-- 2. Настройки конкретного параметра
	if config[ 'properties' ] and config[ 'properties' ][ propertyId ] then
		for key, value in pairs( config[ 'properties' ][ propertyId ] ) do
			if propertyParams[ key ] == nil then
				propertyParams[ key ] = value;
			end
		end
	end

	-- 3. Указанный пресет настроек
	if propertyParams[ 'preset' ] and config[ 'presets' ] and
		config[ 'presets' ][ propertyParams[ 'preset' ] ]
	then
		for key, value in pairs( config[ 'presets' ][ propertyParams[ 'preset' ] ] ) do
			if propertyParams[ key ] == nil then
				propertyParams[ key ] = value;
			end
		end
	end

	local datatype = datatype or params.datatype or propertyParams.datatype or getPropertyDatatype( propertyId );
	if propertyParams.datatype == nil then
		propertyParams.datatype = datatype;
	end

	-- 4. Настройки для типа данных
	if datatype and config[ 'datatypes' ] and config[ 'datatypes' ][ datatype ] then
		for key, value in pairs( config[ 'datatypes' ][ datatype ] ) do
			if propertyParams[ key ] == nil then
				propertyParams[ key ] = value;
			end
		end
	end

	-- 5. Общие настройки для всех свойств
	if config[ 'global' ] then
		for key, value in pairs( config[ 'global' ] ) do
			if propertyParams[ key ] == nil then
				propertyParams[ key ] = value;
			end
		end
	end

	return propertyParams;
end

function p.formatProperty( frame )
	local args = frame.args

	-- проверка на отсутствие обязательного параметра property
	if not args.property then
		throwError( 'property-param-not-provided' )
	end
	local override;
	local propertyId = mw.language.getContentLanguage():ucfirst( string.gsub( args.property, '([^Pp0-9].*)$', function(w) 
		if string.sub( w, 1, 1 ) == '~' then override = w; end
		return ''; 
	end ) ) 
	args = getPropertyParams( propertyId, nil, args );
	if (override) then 
		args[override:match('[,~]([^=]*)=')] = override:match('=(.*)')
		args['property'] = propertyId
	end

	local datatype = args.datatype;

	-- проброс всех параметров из шаблона {wikidata} и параметра from откуда угодно
	p_frame = frame
	while p_frame do
		if p_frame:getTitle() == mw.site.namespaces[10].name .. ':Wikidata' then
			copyTo( p_frame.args, args, true );
		end
		if p_frame.args and p_frame.args.from and p_frame.args.from ~= '' then
			args.entityId = p_frame.args.from;
		end
		p_frame = p_frame:getParent();
	end

	args.plain = toBoolean( args.plain, false );
	args.nocat = toBoolean( args.nocat, false );
	args.references = toBoolean( args.references, true );

	-- если значение передано в параметрах вызова то выводим только его
	if args.value and args.value ~= '' then
		-- специальное значение для скрытия Викиданных
		if args.value == '-' then
			return ''
		end
		local value = args.value

		-- опция, запрещающая оформление значения, поэтому никак не трогаем
		if args.plain then
			return value
		end

		-- обработчики по типу значения
		local wrapperExtraArgs = ''
		if args['value-module'] and args['value-function'] and not string.find( value, '[%[%]%{%}]' ) then
			local func = getUserFunction( args, 'value' );
			value = func( {}, args, value );
		elseif datatype == 'commonsMedia' then
			value = formatCommonsMedia( value, args );
		elseif datatype == 'external-id' and not string.find( value, '[%[%]%{%}]' ) then
			wrapperExtraArgs = wrapperExtraArgs .. ' data-wikidata-external-id="' .. mw.text.encode( value ).. '"';
			value = formatExternalId( value, args );
		elseif datatype == 'math' then
			value = formatMath( value, args );
		elseif datatype == 'url' then
			local moduleUrl = require( 'Module:URL' );
			if not args.length or args.length == '' then
				args.length = 25
			end
			value = moduleUrl.formatUrlSingle( nil, args, value );
		end

		-- оборачиваем в тег для JS-функций
		if string.match( propertyId, '^P%d+$' ) then
			value = mw.text.trim( value )

			-- временная штрафная категория для исправления табличных вставок
			if ( propertyId ~= 'P166'
					and string.match( value, '<t[dr][ >]' )
					and not string.match( value, '<table >]' )
					and not string.match( value, '^%{%|' ) ) then
				value = value .. getCategoryByCode( 'value-contains-table' )
			else
				value = wrapFormatProperty( value, 'class="no-wikidata"'
					.. wrapperExtraArgs .. ' data-wikidata-property-id="'
					.. propertyId .. '"' );
			end
		end

		-- добавляем категорию-маркер
		if not args.nocat then
			local pageTitle = mw.title.getCurrentTitle();
			if pageTitle.namespace == 0 then
				value = value .. getCategoryByCode( 'local-value-present' );
			end
		end

		return value
	end

	if ( args.plain ) then -- вызова стандартного обработчика без оформления, если передана опция plain
		local callArgs = { propertyId };
		if args.entityId then
			callArgs.from = args.entityId;
		end
		return frame:callParserFunction( '#property', callArgs );
	end

	g_frame = frame
	-- после проверки всех аргументов -- вызов функции оформления для свойства (набора утверждений)
	return formatProperty( args )
end

--[[
	Функция оформления ссылок на источники (reference)
	Подробнее о ссылках на источники см. d:Wikidata:Glossary/ru

	Экспортируется в качестве зарезервированной точки для вызова из функций-расширения вида claim-module/claim-function через context
	Вызов из других модулей напрямую осуществляться не должен (используйте frame:expandTemplate вместе с одним из специлизированных шаблонов вывода значения свойства).

	Принимает: объект-таблицу утверждение
	Возвращает: строку оформленных ссылок для отображения в статье
]]
function formatRefs( context, options, statement )
	if ( not context ) then error( 'context not specified' ); end;
	if ( not options ) then error( 'options not specified' ); end;
	if ( not options.entity ) then error( 'options.entity missing' ); end;
	if ( not statement ) then error( 'statement not specified' ); end;

	if ( not outputReferences ) then
		return '';
	end

	local references = {};
	if ( statement.references ) then

		local allReferences = statement.references;
		local hasPreferred = false;
		local displayCount = 0;
		for _, reference in pairs( statement.references ) do
			if ( reference.snaks
					and reference.snaks.P248
					and reference.snaks.P248[1]
					and reference.snaks.P248[1].datavalue
					and reference.snaks.P248[1].datavalue.value.id ) then
				local entityId = reference.snaks.P248[1].datavalue.value.id;
				if ( preferredSources[entityId] ) then
					hasPreferred = true;
				end
			end
		end

		for _, reference in pairs( statement.references ) do
			local display = true;
			if ( hasPreferred ) then
				if ( reference.snaks
						and reference.snaks.P248
						and reference.snaks.P248[1]
						and reference.snaks.P248[1].datavalue
						and reference.snaks.P248[1].datavalue.value.id ) then
					local entityId = reference.snaks.P248[1].datavalue.value.id;
					if ( deprecatedSources[entityId] ) then
						display = false;
					end
				end
			end
			if ( display == true ) then
				if ( displayCount > 2 ) then
					if ( options.entity and options.property ) then
						table.remove( references );
						local moreReferences = '<sup>[[d:' .. options.entity.id .. '#' .. string.upper( options.property ) .. '|[…]]]</sup>';
						table.insert( references, moreReferences );
					end
					break;
				end;
				local refText = moduleSources.renderReference( g_frame, options.entity, reference );
				if ( refText ~= '' ) then
					table.insert( references, refText );
					displayCount = displayCount + 1;
				end
			end
		end
	end
	return table.concat( references );
end

return p