Модуль:Wikidata/number
Выгляд
Дакументацыю да гэтага модуля можна стварыць у Модуль:Wikidata/number/Дакументацыя
local p = {}
function p.formatVisualMagnitude ( context, options )
if ( not context ) then error( 'context not specified' ); end;
if ( not options ) then error( 'options not specified' ); end;
min = context.selectClaims( options, 'P1215[P1227:Q76596947][rank:normal,rank:preferred]' )
max = context.selectClaims( options, 'P1215[P1227:Q76596417][rank:normal,rank:preferred]' )
if ( not min or not max) then -- паказваем як звычайна
return context.formatPropertyDefault( context, options )
end
-- паказваем Vmin-Vmax для пераменнай зоркі
local lang = mw.language.getContentLanguage();
for _, claim1 in ipairs(min) do
for _, claim2 in ipairs(max) do
return lang:formatNum(tonumber(claim1.mainsnak.datavalue.value.amount))
.. ' − ' ..
lang:formatNum(tonumber(claim2.mainsnak.datavalue.value.amount))
end
end
end
function p.formatColorIndex( context, options )
if ( not context ) then error( 'context not specified' ); end;
if ( not options ) then error( 'options not specified' ); end;
if ( options.property ) then
-- Атрымоўваем усе claims незалежна ад ранга
claims = context.selectClaims( options, options.property .. '[rank:normal,rank:preferred]' );
end
if ( claims ) then
return context.formatPropertyDefault( context, options )
end
-- Разлічваем B-V або U-B з P1215
b = context.selectClaims( options, 'P1215[P1227:Q6746395][rank:normal,rank:preferred]' )
if ( not b ) then return end
if string.find( options.property, 'Q17773035' ) then
m1 = context.selectClaims( options, 'P1215[P1227:Q15977921][rank:normal,rank:preferred]' )
m2 = b
if ( not m1 ) then return end
else
m1 = b
m2 = context.selectClaims( options, 'P1215[P1227:Q4892529][rank:normal,rank:preferred]' )
if ( not m2 ) then return end
end
for _, claim1 in ipairs(m1) do
for _, claim2 in ipairs(m2) do
newClaim = { mainsnak = { snaktype = 'value', datavalue = { type = 'quantity',
value = { unit = '1', amount = claim1.mainsnak.datavalue.value.amount -
claim2.mainsnak.datavalue.value.amount }}}}
return context.formatStatementDefault( context, options, newClaim )
end
end
end
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 )
--Unit // прыбрана - зараз дублюецца
-- if ( options.unit and options.unit ~= '' ) then
-- snak = snak .. ' ' .. options.unit
-- end
--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
-- сартыроўка па даце: першай вяртаецца апошняя дата, сцвяржэнні без даты ставяцца ў канец
local function orderByDate( teamClaims )
local orderedTeams = {}
while (#teamClaims > 0) do
local j = 1
local firstClaim = teamClaims[1]
for i=2,#teamClaims,1 do
if (teamClaims[i].qualifiers and teamClaims[i].qualifiers.P585 and firstClaim.qualifiers and firstClaim.qualifiers.P585) then
if (teamClaims[i].qualifiers.P585[1].datavalue.value.time > firstClaim.qualifiers.P585[1].datavalue.value.time) then
firstClaim = teamClaims[i]
j = i
end
else
if (teamClaims[i].qualifiers and teamClaims[i].qualifiers.P585 and not(firstClaim.qualifiers and firstClaim.qualifiers.P585)) then
firstClaim = teamClaims[i]
j = i
end
end
end
table.insert(orderedTeams, firstClaim)
table.remove(teamClaims, j)
end
return orderedTeams
end
function p.formatLastNumberWithProgressAndDate( 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;
local claims = context.selectClaims( options, options.property );
if (claims == nil) then
return '' --TODO error?
end
local orderedClaims = orderByDate(claims)
local snak = context.formatSnak( options, orderedClaims[1].mainsnak )
--Unit
if ( options.unit and options.unit ~= '' ) then
snak = snak .. ' ' .. options.unit
end
if (#orderedClaims > 1) then
local currentValue = orderedClaims[1].mainsnak.datavalue.value['amount']
local previousValue = orderedClaims[2].mainsnak.datavalue.value['amount']
if ( currentValue - previousValue < 0) then
snak = snak .. ' {{Рост}}' .. previousValue - currentValue
else
if ( currentValue - previousValue > 0) then
snak = snak .. ' {{Падзенне}}' .. currentValue - previousValue
else
snak = snak .. ' {{Няма змен}}'
end
end
end
--Date
if ( orderedClaims[1].qualifiers and orderedClaims[1].qualifiers.P585 ) then
snak = snak .. ' <small>(' .. context.formatSnak( options, orderedClaims[1].qualifiers.P585[1] ) .. ')</small>'
end
--References
if ( options.references ) then
snak = snak .. context.formatRefs( options, orderedClaims[1]);
end
return options.frame:preprocess(snak)
end
function p.formatLastNumberWithDate( 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;
local claims = context.selectClaims( options, options.property );
if (claims == nil) then
return '' --TODO error?
end
local orderedClaims = orderByDate(claims)
local snak = context.formatSnak( options, orderedClaims[1].mainsnak )
--Unit
if ( options.unit and options.unit ~= '' ) then
snak = snak .. ' ' .. options.unit
end
--Date
if ( orderedClaims[1].qualifiers and orderedClaims[1].qualifiers.P585 ) then
snak = snak .. ' <small>(' .. context.formatSnak( options, orderedClaims[1].qualifiers.P585[1] ) .. ')</small>'
end
--References
if ( options.references ) then
snak = snak .. context.formatRefs( options, orderedClaims[1]);
end
return options.frame:preprocess(snak)
end
function p.formatDMS( context, options, value )
if not value.amount then return value end
if options and options.unit == '-' then return value.amount end
local prefix = "+"
if tonumber( value.amount ) < 0 then
prefix = "−"
end
return p.formatAngle ( math.abs( tonumber( value.amount ) ),
string.len( value.amount ) - string.find( value.amount, '.', 1, true ) - 5,
prefix .. "%s° %02d′ %s″")
end
function p.formatRA( context, options, value )
if not value.amount then return value end
if options and options.unit == '-' then return value.amount end
return p.formatAngle (tonumber( value.amount ) / 15,
string.len( value.amount ) - string.find( value.amount, '.', 1, true ) - 4,
"%s<sup>ч</sup> %02d<sup>м</sup> %s<sup>с</sup>")
end
function p.formatAngle ( angle, sig, format )
local d, angle = math.modf( angle )
local m, angle = math.modf( angle * 60 )
local mult = 10 ^ sig;
local s = math.floor( angle * 60 * mult + 0.5 ) / mult;
local lang = mw.language.getContentLanguage();
return string.format( format, d, m, lang:formatNum( s ) )
end
return p