-
-
Save filbertkm/896ebff99c03f1744375 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"] = "", | |
["approximate"] = '<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 | |
local categoryLinksToEntitiesWithMissingLabel = '[[Категория:Википедия:Статьи со ссылками на элементы Викиданных без русской подписи]]'; | |
local categoryLocalValuePresent = '[[Категория:Википедия:Статьи с переопределением значения из Викиданных]]'; | |
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 | |
}; | |
local preferredSources = { | |
Q5375741 = true, -- Encyclopædia Britannica Online | |
Q17378135 = true, -- Great Soviet Encyclopedia (1969—1978) | |
}; | |
-- Ссылки на используемые модули, которые потребуются в 99% случаев загрузки страниц (чтобы иметь на виду при переименовании) | |
local moduleSources = require('Module:Sources') | |
local p = {} | |
local formatDatavalue, formatEntityId, formatRefs, formatSnak, formatStatement, formatStatementDefault, formatProperty, getSourcingCircumstances, loadCacheSafe, throwError, toBoolean; | |
local function copyTo( obj, target ) | |
for k, v in pairs( obj ) do | |
target[k] = v | |
end | |
return target; | |
end | |
local function loadCacheSafe( entityId ) | |
local status, result = pcall( function() return mw.loadData( 'Module:WikidataCache/' .. entityId ) end ); | |
if ( status == true ) then | |
return result; | |
end | |
return nil; | |
end | |
local function parseISO8601(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 tonumber(os.time({year=Y, month=M, day=D, hour=(h+oh), min=(m+om), sec=s})) | |
end | |
--[[ | |
Преобразует строку в булевое значение | |
Принимает: строковое значение (может отсутствовать) | |
Возвращает: булевое значение true или false, если получается распознать значение, или defaultValue во всех остальных случаях | |
]] | |
local function toBoolean( valueToParse, defaultValue ) | |
if ( valueToParse ) then | |
if valueToParse == '' or valueToParse == 'false' or valueToParse == '0' then | |
return false | |
end | |
return true | |
end | |
return defaultValue; | |
end | |
--[[ | |
Функция для получения сущности (еntity) для текущей страницы | |
Подробнее о сущностях см. d:Wikidata:Glossary/ru | |
Принимает: строковый индентификатор (типа P18, Q42) | |
Возвращает: объект таблицу, элементы которой индексируются с нуля | |
]] | |
local function getEntityFromId( id ) | |
if id then | |
local cached = loadCacheSafe( id ); | |
if ( cached ) then | |
return cached; | |
end | |
return mw.wikibase.getEntityObject( id ) | |
end | |
local entity = mw.wikibase.getEntityObject(); | |
if ( entity ) then | |
local cached = loadCacheSafe( entity.id ); | |
if ( cached ) then | |
return cached; | |
end | |
end | |
return entity; | |
end | |
--[[ | |
Внутрення функция для формирования сообщения об ошибке | |
Принимает: ключ элемента в таблице i18n (например entity-not-found) | |
Возвращает: строку сообщения | |
]] | |
local function throwError( key ) | |
error( i18n.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, propertyId ) | |
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 propertyId ) then error( 'propertyId not specified'); end; | |
local allPropertyClaims = options.entity.claims[ string.upper( propertyId ) ]; | |
if ( not allPropertyClaims ) then | |
return nil; | |
end | |
--Поиск предпочтительного ранга | |
local requiredRank = 'normal' -- ранг по умолчанию (deprecated не используем) | |
-- если есть хотя бы один preferred, используем только их | |
for i, statement in pairs( allPropertyClaims ) do | |
if (statement.rank == 'preferred') then | |
requiredRank = 'preferred'; | |
break | |
end | |
end | |
local result = {}; | |
if ( allPropertyClaims[0] ) then | |
for i = 0, #allPropertyClaims do | |
local statement = allPropertyClaims[i] | |
if (statement.rank == requiredRank) then | |
result[ #result + 1 ] = statement; | |
end | |
end | |
else | |
for i, statement in pairs( allPropertyClaims ) do | |
if (statement.rank == requiredRank) then | |
result[ #result + 1 ] = statement; | |
end | |
end | |
end | |
if ( #result == 0 ) then | |
return nil; | |
end | |
return result; | |
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( i18n, {} ) ); | |
else | |
options.i18n = i18n; | |
end | |
-- create context | |
local context = { formatSnak = formatSnak, formatPropertyDefault = formatPropertyDefault, formatStatementDefault = formatStatementDefault } | |
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 | |
local timeISO6801 = tostring( snak.datavalue.value.time ) | |
return parseISO8601( timeISO6801 ); | |
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 = context.selectClaims( options, options.property ); | |
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) then | |
formattedStatement = '<span class="wikidata-claim" data-wikidata-claim-id="' .. claim.id .. '">' .. formattedStatement .. '</span>' | |
table.insert( formattedClaims, formattedStatement ) | |
end | |
end | |
-- создание текстовой строки со списком оформленых заявлений из таблицы | |
return mw.text.listToText( formattedClaims, options.separator, options.conjunction ) | |
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 | |
local circumstance = 'Q' .. qualifier.datavalue.value["numeric-id"]; | |
if ( 'Q18086598' == circumstance ) then | |
circumstances.approximate = true; | |
end | |
if ( 'Q18122778' == circumstance ) then | |
circumstances.presumably = true; | |
end | |
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 ); | |
if ( options.references ) then | |
return context.formatSnak( options, statement.mainsnak, circumstances ) .. context.formatRefs( options, statement ); | |
else | |
return context.formatSnak( options, statement.mainsnak, circumstances ); | |
end | |
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 | |
return before .. options.i18n['somevalue'] .. after; | |
elseif snak.snaktype == 'novalue' then | |
return before .. options.i18n['novalue'] .. after; | |
elseif snak.snaktype == 'value' then | |
if ( circumstances.presumably ) then | |
before = before .. options.i18n.presumably; | |
end | |
if ( circumstances.approximate ) then | |
before = before .. options.i18n.approximate; | |
end | |
return before .. formatDatavalue( context, options, snak.datavalue ) .. after; | |
else | |
throwError( 'unknown-snak-type' ); | |
end | |
end | |
--[[ | |
Функция для оформления объектов-значений с географическими координатами | |
Принимает: объект-значение и таблицу параметров, | |
Возвращает: строку оформленного текста | |
]] | |
function formatGlobeCoordinate( value, options ) | |
-- проверка на требование в параметрах вызова на возврат сырого значения | |
if options['subvalue'] == 'latitude' then -- широты | |
return value['latitude'] | |
elseif options['subvalue'] == 'longitude' then -- долготы | |
return value['longitude'] | |
else | |
-- в противном случае формируются параметры для вызова шаблона {{coord}} | |
-- нужно дописать в документации шаблона, что он отсюда вызывается, и что | |
-- любое изменние его парамеров должно быть согласовано с кодом тут | |
local eps = 0.0000001 -- < 1/360000 | |
local globe = '' -- TODO | |
local lat = {} | |
lat['abs'] = math.abs(value['latitude']) | |
lat['ns'] = value['latitude'] >= 0 and 'N' or 'S' | |
lat['d'] = math.floor(lat['abs'] + eps) | |
lat['m'] = math.floor((lat['abs'] - lat['d']) * 60 + eps) | |
lat['s'] = math.max(0, ((lat['abs'] - lat['d']) * 60 - lat['m']) * 60) | |
local lon = {} | |
lon['abs'] = math.abs(value['longitude']) | |
lon['ew'] = value['longitude'] >= 0 and 'E' or 'W' | |
lon['d'] = math.floor(lon['abs'] + eps) | |
lon['m'] = math.floor((lon['abs'] - lon['d']) * 60 + eps) | |
lon['s'] = math.max(0, ((lon['abs'] - lon['d']) * 60 - lon['m']) * 60) | |
local coord = '{{coord' | |
if (value['precision'] == nil) or (value['precision'] < 1/60) then -- по умолчанию с точностью до секунды | |
coord = coord .. '|' .. lat['d'] .. '|' .. lat['m'] .. '|' .. lat['s'] .. '|' .. lat['ns'] | |
coord = coord .. '|' .. lon['d'] .. '|' .. lon['m'] .. '|' .. lon['s'] .. '|' .. lon['ew'] | |
elseif value['precision'] < 1 then | |
coord = coord .. '|' .. lat['d'] .. '|' .. lat['m'] .. '|' .. lat['ns'] | |
coord = coord .. '|' .. lon['d'] .. '|' .. lon['m'] .. '|' .. lon['ew'] | |
else | |
coord = coord .. '|' .. lat['d'] .. '|' .. lat['ns'] | |
coord = coord .. '|' .. lon['d'] .. '|' .. lon['ew'] | |
end | |
coord = coord .. '|globe:' .. globe | |
if options['display'] then | |
coord = coord .. '|display=' .. options.display | |
else | |
coord = coord .. '|display=title' | |
end | |
coord = coord .. '}}' | |
return g_frame:preprocess(coord) | |
end | |
end | |
local function getDefaultValueFunction( datavalue ) | |
-- вызов обработчиков по умолчанию для известных типов значений | |
if datavalue.type == 'wikibase-entityid' then | |
-- идентификатор сущности | |
return function( context, options, value ) return formatEntityId( getEntityIdFromValue( value ), options ) end; | |
elseif datavalue.type == 'string' then | |
-- строка | |
return function( context, options, value ) return value end; | |
elseif datavalue.type == 'monolingualtext' then | |
-- моноязычный текст (строка с указанием языка) | |
return function( context, options, value ) return '<span class="lang" lang="' .. value.language .. '">' .. value.text .. '</span>' 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 ) | |
-- диапазон значений | |
local amount = string.gsub(value['amount'], '^%+', '') | |
local lang = mw.language.new( 'ru' ) | |
return lang:formatNum( tonumber( amount ) ) | |
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 ) | |
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 ); | |
local functionToCall = getUserFunction( options, 'value', context.formatValueDefault ); | |
return functionToCall( context, options, datavalue.value ); | |
end | |
-- Небольшой словарь упрощенного отображения (TODO: надо сделать расширенный с учётом даты) | |
local simpleReplaces = { | |
Q30 = '[[Соединённые Штаты Америки|США]]', | |
Q148 = '[[Китайская Народная Республика|КНР]]', | |
Q183 = '[[Германия]]', | |
Q258 = '[[Южно-Африканская Республика|ЮАР]]', | |
Q423 = '[[Корейская Народно-Демократическая Республика|КНДР]]', | |
Q2184 = '[[Российская Советская Федеративная Социалистическая Республика|РСФСР]]', | |
Q2895 = '[[Белорусская Советская Социалистическая Республика|Белорусская ССР]]', | |
Q15180 = '[[Союз Советских Социалистических Республик|СССР]]', | |
Q16957 = '[[Германская Демократическая Республика|ГДР]]', | |
Q130229 = '[[Грузинская Советская Социалистическая Республика|Грузинская ССР]]', | |
Q130280 = '[[Эстонская Советская Социалистическая Республика|Эстонская ССР]]', | |
Q131337 = '[[Азербайджанская Советская Социалистическая Республика|Азербайджанская ССР]]', | |
Q132856 = '[[Армянская Советская Социалистическая Республика|Армянская ССР]]', | |
Q133356 = '[[Украинская Советская Социалистическая Республика|Украинская ССР]]', | |
Q168811 = '[[Казахская Советская Социалистическая Республика|Казахская ССР]]', | |
Q170895 = '[[Молдавская Советская Социалистическая Республика|Молдавская ССР]]', | |
Q173761 = '[[Литовская Советская Социалистическая Республика|Литовская ССР]]', | |
Q192180 = '[[Латвийская Советская Социалистическая Республика|Латвийская ССР]]', | |
Q199707 = '[[Туркменская Советская Социалистическая Республика|Туркменская ССР]]', | |
Q199711 = '[[Таджикская Советская Социалистическая Республика|Таджикская ССР]]', | |
Q484578 = '[[Узбекская Советская Социалистическая Республика|Узбекская ССР]]', | |
Q809806 = '[[Башкирская Автономная Советская Социалистическая Республика|Башкирская АССР]]', | |
Q1157215 = '[[Дагестанская Автономная Советская Социалистическая Республика|Дагестанская АССР]]', | |
} | |
--[[ | |
Функция для оформления идентификатора сущности | |
Принимает: строку индентификатора (типа Q42) и таблицу параметров, | |
Возвращает: строку оформленного текста | |
]] | |
function formatEntityId( entityId, options ) | |
-- получение локализованного названия | |
local label = nil; | |
if ( options.text and options.text ~= '' ) then | |
label = options.text | |
else | |
if ( simpleReplaces[entityId] ) then | |
return simpleReplaces[entityId]; | |
end | |
label = mw.wikibase.label( entityId ); | |
end | |
-- получение ссылки по идентификатору | |
local link = mw.wikibase.sitelink( entityId ) | |
if link then | |
if label then | |
return '[[' .. link .. '|' .. label .. ']]' | |
else | |
return '[[' .. link .. ']]' | |
end | |
end | |
if label then | |
return label | |
end | |
-- сообщение об отсутвии локализованного названия | |
-- not good, but better than nothing | |
return '[[d:' .. entityId .. '|' .. entityId .. ']]<span style="border-bottom: 1px dotted; cursor: help; white-space: nowrap" title="В Викиданных нет русской подписи к элементу. Вы можете помочь, указав русский вариант подписи.">?</span>' .. categoryLinksToEntitiesWithMissingLabel; | |
end | |
--[[ | |
Функция для оформления утверждений (statement) | |
Подробнее о утверждениях см. d:Wikidata:Glossary/ru | |
Принимает: таблицу параметров | |
Возвращает: строку оформленного текста, предназначенного для отображения в статье | |
]] | |
-- устаревшее имя, не использовать | |
function p.formatStatements( frame ) | |
return p.formatProperty( frame ); | |
end | |
function p.formatProperty( frame ) | |
local args = frame.args | |
local plain = toBoolean( frame.args.plain, false ); | |
frame.args.nocat = toBoolean( frame.args.nocat, false ); | |
frame.args.references = toBoolean( frame.args.references, true ); | |
-- проверка на отсутствие обязательного параметра property | |
if not frame.args.property then | |
throwError( 'property-param-not-provided' ) | |
end | |
-- если значение передано в параметрах вызова то выводим только его | |
if frame.args.value and frame.args.value ~= '' then | |
if plain or frame.args.nocat or frame:callParserFunction( '#property', frame.args.property )=='' then | |
-- опция, запрещающая оформление значения, поэтому никак не трогаем | |
return frame.args.value | |
else | |
-- если трогать всё-таки можно, добавляем категорию-маркер | |
return args.value .. categoryLocalValuePresent; | |
end | |
end | |
if ( plain ) then -- вызова стандартного обработчика без оформления, если передана опция plain | |
return frame:callParserFunction( '#property', frame.args.property ); | |
end | |
g_frame = frame | |
-- после проверки всех аргументов -- вызов функции оформления для свойства (набора утверждений) | |
return formatProperty( frame.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 result = ''; | |
if ( statement.references ) then | |
local allReferences = statement.references; | |
local hasPreferred = false; | |
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["numeric-id"] ) then | |
local entityId = "Q" .. reference.snaks.P248[1].datavalue.value["numeric-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["numeric-id"] ) then | |
local entityId = "Q" .. reference.snaks.P248[1].datavalue.value["numeric-id"]; | |
if ( deprecatedSources[entityId] ) then | |
display = false; | |
end | |
end | |
end | |
if ( display ) then | |
result = result .. moduleSources.renderReference( g_frame, options.entity, reference ); | |
end | |
end | |
end | |
return result | |
end | |
return p |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment