Module:Charts/Wikibase property/test

Lua

CodeDiscussionEditHistoryLinksLink count Subpages:DocumentationTestsResultsSandboxLive code All modules


UsageUsage

Use in a Chart (beware this feature is not yet live!) as transform:

"source": "<any empty template page>.tab"
"transform": {
    "module": "Charts/Wikibase_property/test",
    "function": "transform",
    "args": {
        "entity": "Q65", // Los Angeles
        "property": "P1082", // population
        "qualifier": "P585" // point in time
    }
}


DemoDemo

(These demos are pre-transformed during testing. The feature has not yet deployed.)

Population for Los Angeles Q65: PopulationYear01,000,0002,000,0003,000,0004,000,000185018701890191020162020population

Population for London Q84: PopulationYear02,000,0004,000,0006,000,0008,000,00010,000,0001801184118811921196120012021population


Code

--
-- Experimental module for Chart transform testing.
-- Fetches claims for a single Wikidata item on a single property,
-- grouping by a single qualifier.
--
-- Can eg show a list of population figures for a city over time.
--
-- Returns data in tabular JSON format:
-- see https://www.mediawiki.org/wiki/Help:Tabular_data
--
-- Meant to be used through reference in a Chart format definition:
-- see https://www.mediawiki.org/wiki/Extension:Chart
-- but this feature hasn't landed. This test module is living here
-- on Commons in order to facilitate production of test data for
-- now.
--
-- Currently hardcodes values as numbers and qualifiers as datetimes.
--
-- To use in a .chart:
-- "source": "<any empty template page>.tab"
-- "transform": {
--     "module": "Charts/Wikibase_property/test",
--     "function": "transform",
--     "args": {
--         "entity": "Q65", // Los Angeles
--         "property": "P1082", // population
--         "qualifier": "P585" // point in time
--     }
-- }
--
-- To dump raw JSON in a wiki template:
-- {{#invoke:Charts/Wikibase_property/test
-- |invoke
-- |entity=Q65
-- |property=P1082
-- |qualifier=P585}}
--
-- maintained by [[User:Brooke Vibber (WMF)]] during testing
--
-- @todo clarify localization behavior
-- @todo validate data format (numeric)
-- @todo validate qualifier format (timestamp, or support grouping others)
--
local p = {}

function p.transform(tab, args)
	--
	-- Transform function for tabular data loaded into charts
	-- see https://www.mediawiki.org/wiki/Extension:JsonConfig/Transforms
	--
	-- tab is a template tabular data setfenv
	-- see https://www.mediawiki.org/wiki/Help:Tabular_data
	-- return value is modified dataset, with our lookup data inserted
	--
	-- args:
	-- * entity - entity to look up on, eg 'Q65' - Los Angeles
	-- * property - property to look up claims for , eg 'P1082' - population
	-- * qualifier - qualifier to show lookups for, eg 'P585' - point in time
	local label_entity = mw.wikibase.getLabel(args.entity)
	local label_prop = mw.wikibase.getLabel(args.property)
	local label_qualifier = mw.wikibase.getLabel(args.qualifier)
	tab.description = { ["en"] = label_entity .. " - " .. label_prop }
	table.insert(tab.schema.fields, {
		["name"] = args.qualifier,
		["type"] = "string",
		["title"] = { ["en"] = label_qualifier },
	})
	table.insert(tab.schema.fields, {
		["name"] = args.entity,
		["type"] = "number", -- @todo: support other types
		["title"] = { ["en"] = label_prop },
	})

	local statements = mw.wikibase.getAllStatements(args.entity, args.property)
	for _, statement in ipairs(statements) do
		local row = {}
		local label_qual = ""
		local qualifiers = statement.qualifiers[args.qualifier]
		if qualifiers then
			for _, snak in pairs(qualifiers) do
				-- hack assumes gregorian date
				-- @fixme use the month/day if available too
				local date = string.match(snak.datavalue.value.time, "^+(%d%d%d%d)%-.*$")
				if date then
					label_qual = date
				else
					label_qual = snak.datavalue.value.time
				end
			end
		end
		table.insert(row, label_qual)
		
		-- hack assumes numeric
		local value = statement.mainsnak.datavalue.value.amount
		value = tonumber(value)
		table.insert(row, value)

		table.insert(tab.data, row)
	end
	
	table.sort(tab.data, function(a, b)
		return a[1] < b[1]
	end)

	return tab
end

--
-- #invoke-compatible demo
-- dumps raw JSON to output
--
function p.invoke(frame)
	local tab = {
    	["license"] = "CC0-1.0",
    	["description"] = {},
    	["sources"] = "from wikibase", -- @todo fill out sources
    	["schema"] = {
        	["fields"] = {}
        },
        ["data"] = {}
    }
    tab = p.transform(tab, frame.args)
	return mw.text.jsonEncode(tab)
end

function p.demo(entity)
	mw.log(p.invoke({
		["args"] = {
			["entity"] = entity or "Q65", -- Los Angeles
			["property"] = "P1082", -- Population
			["qualifier"] = "P585" -- point in time
		}
	}))
end

return p
Category:Pages using the Chart extension