--- Example usage:--- --- =p._externalData
local p = local lang = mw.getContentLanguagelocal english = mw.getLanguage("en")
local function round(x) return (math.modf(x + (x < 0 and -0.5 or 0.5)))end
local function formatChange(previous, current) if not previous or previous
current then return "=" end local change = current / previous * 100 - 100 local sign = change < 0 and "−" or "+" if (math.abs(change)) >= 10 then return mw.ustring.format("%s%.0f%%", sign, (math.abs(change))) end if (math.abs(change)) < 1 then return mw.ustring.format("%s%.3f%%", sign, (math.abs(change))) end return mw.ustring.format("%s%.2f%%", sign, (math.abs(change)))end
function p._externalData(args) local data = mw.ext.data.get(args.datapage) local dateIndex local deathsIndex local recoveriesIndex local casesIndex local class4Index local class5Index for i, field in ipairs(data.schema.fields) do if field.name
args.datadate then dateIndex = i elseif field.name
args.datadeaths then deathsIndex = i elseif field.name
args.datarecoveries then recoveriesIndex = i elseif field.name
args.datacases then casesIndex = i elseif field.name
args.dataclass4 then class4Index = i elseif field.name
args.dataclass5 then class5Index = i end end assert(dateIndex, "Date field not found.") assert(deathsIndex or not args.datadeaths, "Deaths field not found.") assert(recoveriesIndex or not args.datarecoveries, "Recoveries field not found.") assert(casesIndex or not args.datacases, "Cases field not found.") assert(class4Index or not args.dataclass4, "Class 4 field not found.") assert(class5Index or not args.dataclass5, "Class 5 field not found.") -- Restructure the data as tables with keys. local records = for i, row in ipairs(data.data) do local record = local prevRecord = records[#records] or if casesIndex and not prevRecord.cases and record.cases and record.cases > 0 then record.options.firstright1 = "y" end if deathsIndex and prevRecord.deaths
prevRecord.deaths and record.recoveries
prevRecord.cases and record.class4
prevRecord.class5 then record.streak = prevRecord.streak + 1 end table.insert(records, record) end -- Collapse streaks of identical data. for i = #records, 1, -1 do local record = records[i] if i < #records and record.streak > 3 then for j = i, i - record.streak + 3, -1 do table.remove(records, j) end i = i - record.streak + 2 record = records[i] record.options.collapse = "y" record.options.id = english:formatDate("M", record.date):lower record.date = nil end end -- Stringify the data. local rows = for i, record in ipairs(records) do local prevRecord = records[i - 1] or local row = for k, v in pairs(record.options) do table.insert(row, string.format("%s=%s", k, v)) end table.insert(rows, table.concat(row, ";")) end return table.concat(rows, "\n")end
function p.externalData(frame) local args = for k,v in pairs(frame.args) do if (v or ) ~= then args['data'..k] = v end end return p._externalData(args)endreturn p