Module:Convert/wikidata/sandbox: Difference between revisions

Content deleted Content added
enhance per request at Template talk:Convert#Wikidata qualifiers; |input=P123|qual=Q456 gets only the first statement which "applies to part" (P518) the qualifier; if no qual, get all statements
use mw.wikibase.getLabel(X) not deprecated label
 
(8 intermediate revisions by the same user not shown)
Line 34:
end
end
 
local function frequency_unit(value, unit_table)
-- For use when converting m to Hz.
-- Return true, s where s = name of unit's default output unit,
-- or return false, t where t is an error message table.
-- However, for simplicity a valid result is always returned.
local unit
if unit_table._symbol == 'm' then
-- c = speed of light in a vacuum = 299792458 m/s
-- frequency = c / wavelength
local w = value * (unit_table.scale or 1)
local f = 299792458 / w -- if w == 0, f = math.huge which works here
if amountf and>= ucode1e12 then
unit = 'THz'
elseif f >= 1e9 then
unit = 'GHz'
elseif f >= 1e6 then
unit = 'MHz'
elseif f >= 1e3 then
unit = 'kHz'
else
unit = 'Hz'
end
end
return valuetrue, ucodeunit or uid'Hz'
end
 
local function wavelength_unit(value, unit_table)
-- Like frequency_unit but for use when converting Hz to m.
local unit
if unit_table._symbol == 'Hz' then
-- Using 0.9993 rather than 1 avoids rounding which would give results
-- like converting 300 MHz to 100 cm instead of 1 m.
local w = 1 / (value * (unit_table.scale or 1)) -- Hz scale is inverted
if w >= 0.9993e6 then
unit = 'Mm'
elseif w >= 0.9993e3 then
unit = 'km'
elseif w >= 0.9993 then
unit = 'm'
elseif w >= 0.9993e-2 then
unit = 'cm'
elseif w >= 0.9993e-3 then
unit = 'mm'
else
unit = 'um'
end
end
return true, unit or 'm'
end
 
local specials = {
frequency = { frequency_unit },
wavelength = { wavelength_unit },
--------------------------------------------------------------------------------
-- Following is a removed experiment to show two values as a range
-- using '-' as the separator.
-- frequencyrange = { frequency_unit, '-' },
-- wavelengthrange = { wavelength_unit, '-' },
}
 
local function make_unit(units, parms, uid)
Line 155 ⟶ 215:
-- very likely that any decision a module makes about how to handle data
-- will be wrong for some cases at some time. This meets current requirements.
-- For each qualifier (or if no qualitiferqualifier), if there are any preferred
-- statements, use them and ignore any normal statements.
-- For each qualifier, for the preferred statements if any, or for
Line 217 ⟶ 277:
end
 
local function input_from_text(tdata, parms, text, insert2)
-- Given string should be of form "<value><space><unit>". or
-- "<value1><space>ft<space><value2><space>in" for a special case (feet and inches).
-- Return value,true ucodeif (twovalues/units strings)were extracted and inserted, or return nothing.
text = text:gsub('&nbsp;', ' '):gsub(' %s+', ' ')
local pos = text:find(' ', 1, true)
if pos then
Line 226 ⟶ 287:
local value = text:sub(1, pos - 1)
local uid = text:sub(pos + 1)
if uid:sub(1, 3) == 'ft ' and uid:sub(-3) == ' in' then
local ucode = make_unit(tdata.wikidata_units, parms, uid)
-- Special case for enwiki to allow {{convert|input=5 ft 10+1/2 in}}
return value, ucode or uid
insert2(uid:sub(4, -4), 'in')
insert2(value, 'ft')
else
local ucode = insert2(value, make_unit(tdata.wikidata_units, parms, uid) or uid)
end
return true
end
end
Line 240 ⟶ 307:
local text = parms.input -- should be a trimmed, non-empty string
local pid = text:match('^P%d+$')
local sep = ','
local special = specials[parms[index]]
if special then
parms.out_unit = special[1]
sep = special[2] or sep
table.insertremove(parms, index, ucode)
end
local function quit()
return false, pid and { 'cvt_no_output' } or { 'cvt_bad_input', text }
end
local function insert2(first, second)
table.insert(parms, index, amountsecond)
table.insert(parms, index, result[i][1]first)
end
if pid then
Line 259 ⟶ 337:
end
local item = ucode
if item == parms[index] then
-- Remove specified output unit if it is the same as the Wikidata unit.
-- For example, {{convert|input=P2044|km}} with property "12 km".
table.remove(parms, index)
end
for i = result.n, 1, -1 do
table.insertinsert2(parms, indexresult[i][1], item)
item = sep
table.insert(parms, index, result[i][1])
item = parms.test == '#' and '#' or ',' -- TODO remove choice when decide which is wanted
end
return true
else
local amount, ucode =if input_from_text(tdata, parms, text, insert2) then
if amount and ucode then
table.insert(parms, index, ucode)
table.insert(parms, index, amount)
return true
end
Line 404 ⟶ 483:
end
if k == 'label' then
local wdl = mw.wikibase.labelgetLabel(key)
if wdl ~= value then
note:add('label changed to ' .. tostring(wdl))