Модуль:Wikidata/number
Материал из Томская энциклопедии
< Модуль:Wikidata
Версия от 22:29, 22 мая 2017; EAGLEE (обсуждение | вклад)
Для документации этого модуля может быть создана страница Модуль:Wikidata/number/doc
local p = {} function p.formatPropertyWithMostRecentClaimAndIndicator( 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 -- Пролучаем все claims независимо от ранга claims = context.selectClaims( options, options.property .. '[rank:normal,rank:preferred]' ); end if claims == nil then return '' end -- Ищем claim с максимальным значением P585 и форматируем его в out local maxTimestamp = 0; local mostRecentClaim; for i, claim in ipairs(claims) do if (claim.qualifiers and claim.qualifiers.P585) then -- обрабатываем только claims с указанным P585 if (maxTimestamp < context.parseTimeFromSnak( claim.qualifiers.P585[1] )) then maxTimestamp = context.parseTimeFromSnak( claim.qualifiers.P585[1] ) mostRecentClaim = claim end end end if (not mostRecentClaim) then -- нет ни одного claim с указанным P585 return context.formatPropertyDefault( context, options ) end local out = context.formatStatement( options, mostRecentClaim ) if out ~= '' then -- Ищем claim со значением P585 сразу после максимального и запоминаем его в secondMostRecentValue local secondMostRecentTimestamp = 0; local secondMostRecentValue = 0; for i, claim in ipairs(claims) do if (claim.qualifiers and claim.qualifiers.P585) then -- обрабатываем только claims с указанным P585 local timestamp = context.parseTimeFromSnak( claim.qualifiers.P585[1] ) if (secondMostRecentTimestamp < timestamp and maxTimestamp > timestamp ) then secondMostRecentTimestamp = timestamp secondMostRecentValue = tonumber( claim.mainsnak.datavalue.value.amount ) end end end if (secondMostRecentValue ~= 0) then -- если предыдущее значение нашлось if (secondMostRecentValue < tonumber( mostRecentClaim.mainsnak.datavalue.value.amount )) then out = '<span style="color: #0c0; font-size: larger;">▲</span>' .. out else out = '<span style="color: red; font-size: larger;">▼</span>' .. out end end if options.before then out = options.before .. out end if options.after then out = out .. options.after end end return out end function p.formatQuantityWithDateClaim( context, options, statement ) local snak = context.formatSnak( options, statement.mainsnak ) --Date if ( statement.qualifiers and statement.qualifiers.P585 ) then snak = snak .. ' (' .. context.formatSnak( options, statement.qualifiers.P585[1] ) .. ')' end --References if ( options.references ) then snak = snak .. context.formatRefs( options, statement ); end return snak end return p