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

Population for Los Angeles (Q65): Year01,000,0002,000,0003,000,0004,000,00018301860189019201950198020102040population

Population for London (Q84): Year02,000,0004,000,0006,000,0008,000,00010,000,000180018501900195020002050population

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
--
-- 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
	-- * lang - language to load labels in, eg 'de'
	local label_entity, lang_entity
	local label_prop, lang_prop
	local label_qualifier, lang_qualifier
	if args.lang then
		label_entity = mw.wikibase.getLabelByLang(args.entity, args.lang)
		label_prop = mw.wikibase.getLabelByLang(args.property, args.lang)
		label_qualifier = mw.wikibase.getLabelByLang(args.qualifier, args.lang)
		lang_prop, lang_entity, lang_qualifier = args.lang, args.lang, args.lang
	else
		label_entity, lang_entity = mw.wikibase.getLabelWithLang(args.entity)
		label_prop, lang_prop = mw.wikibase.getLabelWithLang(args.property)
		label_qualifier, lang_qualifier = mw.wikibase.getLabelWithLang(args.qualifier)
	end
	tab.description = { [lang_entity] = label_entity .. " - " .. label_prop }
	table.insert(tab.schema.fields, {
		["name"] = args.qualifier,
		["type"] = "number",
		["title"] = { [lang_qualifier] = label_qualifier },
	})
	table.insert(tab.schema.fields, {
		["name"] = args.entity,
		["type"] = "number", -- @todo: support other types
		["title"] = { [lang_prop] = label_prop },
	})

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

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

	local TabUtils = require( "Module:TabUtils" )
	return TabUtils.select(tab, args)
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:Chart transform modules Category:Modules in pre-alpha development Category:Pages using the Chart extension