Module:Sandbox/RedWolf
Appearance
local p = {}
--local wd = require('Module:Wd')
local wd_properties = {
elevation = "P2044",
prominence = "P2660",
mtn_range = "P4552",
sig_event = "P793",
pt_in_time = "P585"
}
local QID_FIRST_ASCENT = "Q1194369"
local errors
function p.start(frame)
local unit = frame.args[1]
return header(unit)
end
local function split(inputstr, sep)
-- if sep is null, set it as comma
if sep == nil then
sep = ','
end
local t = {}
for str in string.gmatch(inputstr, '([^'..sep..']+)')
do
table.insert(t, str)
end
return t
end
local function extractRaw(value)
local i, i1, i2, n
if value == nil then return -1 end
i = string.find(value, " metre")
if i then
n = string.sub(value, 1, i-1):gsub(',', '')
else
n = -1
end
return n
end
local function getElevationCells(frame, elev, unit) --23
if frame.expandTemplate then
return frame:expandTemplate{title='elevation_cells', args= { elev, unit}}
end
return "{{elevation_cells|" .. elev .. "|" .. unit .. "}}"
end
local function getPage(name)
local parts = split(name,"|")
local page = parts[1]
local n = string.find(page, "%[%[")
if n then
page = string.sub(page,3)
end
n = string.find(page, "%]%]")
if n then
page = string.sub(page,1,n-1)
end
return page
end
local function getWDProperty(frame, eid, name) -- 45
--[[local eid = mw.wikibase.getEntityIdForTitle(page)
mw.log("page="..page .. ",eid=" .. eid)
local stmts = mw.wikibase.getBestStatements(eid, name)
if stmts ~= nil then
mw.logObject(stmts)
for i=1,#stmts do
mw.log("stmts["..i.."]="..stmts[i]);
end
end ]]
if frame.preprocess then
--return frame:preprocess("{{#invoke:Wd|property|" .. name .. "|page=" .. page .. "}}")
return frame:preprocess("{{#invoke:Wd|property|" .. name .. "|eid=" .. eid .. "}}")
-- if frame.expandTemplate then
-- local args = { ['1'] = name, ['page'] = page }
-- return wd._property({eid, args})
-- local args = { ['1'] = 'property', ['2'] = name, ['page'] = page }
-- return frame:expandTemplate{title='Wikidata', args= args}
end
return "{{Wikidata|property|" .. name .. "|eid=" .. eid .. "}}"
end
local function getElevation(frame, eid)
if frame.preprocess == nil then
return "????"
end
value = getWDProperty(frame, eid, wd_properties.elevation)
return extractRaw(value)
end
local function getFirstAscent(frame, eid)
if frame.preprocess == nil then
return "????"
end
local names = wd_properties.sig_event .. "|" .. QID_FIRST_ASCENT .. "|" .. wd_properties.pt_in_time
local value = frame:preprocess("{{#invoke:Wd|property|qualifier|" .. names .. "|eid=" .. eid .. "}}")
mw.log("FA value = " .. value)
if value == "" then
mw.log("No FA found for eid=" .. eid)
return " "
end
-- local i1, i2 = string.find(value, "%(%d%d%d%d%-%d%d%-%d%d%)")
-- Find date such as: 10 July 1913
local i1, i2 = string.find(value, "%(%d+%s%a*%s%d%d%d%d%)")
if i1 then
--mw.log("i1="..i1)
local date = string.sub(value, i1+1, i2-1)
mw.log("FA date = " .. date)
local len = string.len(date)
local year = string.sub(date, len-4, len)
return year
end
-- Find date with just the year
i1, i2 = string.find(value, "%(%d+%)")
if i1 then
local year = string.sub(value, i1+1, i2-1)
return year
end
return "FA date format unknown"
end
-- get prominence from wikidata
local function getProminence(frame, eid)
local prom, value, n
value = getWDProperty(frame, eid, wd_properties.prominence)
return extractRaw(value)
end
local function getRange(frame, eid)
return getWDProperty(frame, eid, wd_properties.mtn_range)
end
local function header(unit) -- 8
local unit_1, unit_2
if unit == nil or unit == "" then
unit = 'm'
end
if unit == 'm' then unit_1 = 'm'; unit_2 = 'ft'
else unit_1 = 'ft'; unit_2 = 'm'
end
local s = '{| class="wikitable sortable"\n' ..
'! align="left" rowspan=2|Rank||rowspan=2|Mountain/Peak ||colspan=2|Elevation ||colspan=2| Prominence ||rowspan=2| Subrange' ..
'||rowspan=2| FA ||rowspan=2| Notes\n' ..
'|-\n'
s = s .. '!' .. unit_1 .. '||' .. unit_2 .. '||' .. unit_1 .. '||' .. unit_2 .. "\n"
return s
end
local function finish() -- 30
return "|}"
end
local function processLine(frame, line)
local elev_cells, prom_cells, parts, n
local name, elev, elev_wd, page, prom, notes, fa, range
local s = ""
-- argument contains elevation, page link and notes
parts = split(line, ",")
if #parts < 3 then
local m = "<br/>Bad format on argument (" .. line .. ") -- skipped"
errors = errors .. m
mw.log(m)
return nil
end
elev = parts[1]
name = parts[2]
if name then
page = getPage(name)
mw.log("name = " .. name .. ";page = " .. page)
else
mw.log("name is null")
return nil
end
notes = parts[3]
n = string.find(notes, "\n")
if n then
notes = string.sub(notes, 1, n-1)
end
local eid = mw.wikibase.getEntityIdForTitle(page)
mw.log("page="..page .. ",eid=" .. eid)
elev_wd = getElevation(frame, eid)
prom = getProminence(frame, eid)
range = getRange(frame, eid)
fa = getFirstAscent(frame, eid)
mw.log("elev="..elev..";elev_wd="..elev_wd)
elev_cells = getElevationCells(frame, elev, unit)
if prom ~= nil and prom ~= "-1" and prom ~= "" then
prom_cells = getElevationCells(frame, prom, unit)
else
prom_cells = " || "
end
if elev ~= elev_wd then
notes = notes .. "<br/><font color=green>Local/WD elevations mismatch: " .. "\"" .. elev .. "\"".. "/\"" .. elev_wd.. "\"</font>"
end
s = '||' .. name .. '\n|' .. elev_cells .. '\n|' .. prom_cells .. '\n|' .. range .. '\n|' .. fa .. '\n|' .. notes .. '\n'
return elev, s
end
function p.list(frame) -- 34
local debug = false
errors = ""
local unit = frame.args[1]
local rank_number = 0
local last_elev = ""
local output = header(unit)
for i=3,999,1 do
line = frame.args[i]
if line == nil then break end
mw.log("line=" .. line)
local elev, o = processLine(frame, line)
if o then
if last_elev ~= elev then
rank_number = rank_number + 1
last_elev = elev
end
output = output .. '|-\n|align=center|' .. rank_number
output = output .. o
end
-- argument contains elevation, page link and notes
end -- for
output = output .. finish()
if debug then
output = "prom="..prom.."</br><nowiki>"..output.."</nowiki>".."\n"..output
end
if string.len(errors) > 0 then
output = output .. "<font color=red>" .. errors .. "</font>"
end
return output
end
--[[
Test via Preview Window Debug console
print(p.test())
]]
function p.test()
local frame = mw.getCurrentFrame()
-- if frame then mw.logObject(frame) end
-- local frame = {}
frame.args = {}
frame.args[1] = 'm'
frame.args[2] = "options"
frame.args[3] = "3954,[[Mount Robson]],Highest point in the Canadian Rockies\n"
frame.args[4] = "3747,[[Mount Columbia (Canada)|Mount Columbia]],Highest point in [[Alberta]]"
frame.args[5] = "3246,[[Mount Smythe (Alberta)|Mount Smythe]],Named for an English mountaineer"
frame.args[6] = ""
frame.args[7] = " "
return p.list(frame)
end
return p
--[[
{| class="wikitable sortable"
|- bgcolor="#ffffcc"
! align="left" rowspan=2|Rank||rowspan=2|Mountain/Peak ||colspan=2|Elevation ||colspan=2| Prominence ||rowspan=2| Subrange
!rowspan=2| FA ||rowspan=2| Notes ||rowspan=2| References
|-
!m || ft || m || ft
|-
|align=center|1||Mount Robson
|{{elevation_cells|3,959|m}}|{{elevation_cells|2829|m}}||Rainbow Range
|1913||Highest point in the Canadian Rockies|| <ref name=robson/>
]]