Jump to content

Module:Wikipedia 25 birthday event map

From Meta, a Wikimedia project coordination wiki
Module documentation
local mapCounter = 0

local p = {}

local markerColors = {
	-- These colors match the background colors for the event boxes in
	-- [[Template:WP25/Event/styles.css]] (`.wp25-event`). These
	-- are the plain RGB colors, while the colors in the stylesheet are
	-- the same, but with a lower opacity (alpha channel).
	'C0E6FF',
	'FBEEBF',
	'F9DDE9',
}

local function parsePageForTemplates(page)
	local templates = {}

	local title = mw.title.new(page)
	if not title then
		return templates
	end

	local pageContent = title:getContent() or ''

	local curlies = 0
	local squares = 0
	local record = false
	local currentTemplate = ''

	for c in mw.ustring.gmatch(pageContent, '.') do
		if c == '{' then
			curlies = curlies + 1
		end
		if c == '}' then
			curlies = curlies - 1
		end
		if c == '[' then
			squares = squares + 1
		end
		if c == ']' then
			squares = squares - 1
		end

		if curlies > 2 or (squares ~= 0 and c == '|') then
			c = mw.text.nowiki(c)
		end

		if record then
			currentTemplate = currentTemplate .. c
		end

		if curlies >= 2 then
			record = true
		end

		if record and curlies < 2 then
			currentTemplate = mw.ustring.sub(currentTemplate, 1, -2)

			if currentTemplate ~= '' then
				table.insert(templates, currentTemplate)
			end

			currentTemplate = ''
			record = false
		end
	end

	return templates
end

local EARTH = 'http://www.wikidata.org/entity/Q2'

-- Read the coordinates from Wikidata here. One ExternalData object per event
-- makes the browser send one geopoint request per event, which fails at this
-- scale.
local function coordinatesFor(qid)
	local points = {}

	for _, statement in ipairs(mw.wikibase.getBestStatements(qid, 'P625') or {}) do
		local value = statement.mainsnak.datavalue
			and statement.mainsnak.datavalue.value

		if value and value.globe == EARTH then
			table.insert(points, { value.longitude, value.latitude })
		end
	end

	return points
end

local function geojsonify(event)
	local features = {}

	for _, qid in ipairs(event['location item']) do
		for _, point in ipairs(coordinatesFor(qid)) do
			table.insert(features, {
				type = 'Feature',
				geometry = {
					type = 'Point',
					coordinates = point
				},
				properties = {
					title = event.title,
					description = (event.time or '') .. '<br />' .. (event.location or ''),
					['marker-symbol'] = 'star',
					['marker-color'] = event['marker-color'],
					zIndexOffset = event.grant ~= '' and 1000 or 0
				}
			})
		end
	end

	return features
end

local function getParam(template, param)
	local value = mw.ustring.match(
		template,
		'|%s*' .. param .. '%s*=%s*(.-)\n%s*|'
	)

	if not value then
		value = mw.ustring.match(
			template,
			'|%s*' .. param .. '%s*=%s*(.*)$'
		)
	end

	value = value or ''

	return mw.text.decode(mw.text.trim(value))
end

local function jsonifyEventData(page)
	local templates = parsePageForTemplates(page)
	local transformedTemplates = {}

	for num, template in ipairs(templates) do
		local event = {}

		event.title = getParam(template, 'event')
		event.time = mw.getCurrentFrame():preprocess(
				getParam(template, 'time')
					)
		event.location = getParam(template, 'location')
		event.grant = getParam(template, 'grant')

		local qids = {}

		local locationItems =
			mw.ustring.match(
				template,
				'%|%s*location item%s*=%s*([^%|%}]+)'
			) or ''

		for qidMatch in mw.ustring.gmatch(locationItems, 'Q%d+') do
			table.insert(qids, qidMatch)
		end

		event['location item'] = qids
		event['marker-color'] =
			markerColors[((num - 1) % #markerColors) + 1]

		for _, feature in ipairs(geojsonify(event)) do
			table.insert(transformedTemplates, feature)
		end
	end

	return mw.text.jsonEncode(
		transformedTemplates,
		mw.text.JSON_PRETTY
	)
end

function p.getMap(frame)
	frame = frame or mw.getCurrentFrame()

	mapCounter = mapCounter + 1

	local attributes = {
		width = 'full',
		height = '600',
		align = 'center',
		zoom = '2',
		latitude = '0.0',
		longitude = '0.0',
		id = 'wp25-map-' .. mapCounter
	}

	local geojson = jsonifyEventData(
		'Wikipedia_25/Events/Calendar'
	)

	return frame:extensionTag{
		name = 'mapframe',
		content = geojson,
		args = attributes
	}
end

return p