Module:Report

From OpenStreetMap Wiki
Jump to navigation Jump to search
[Create] Documentation
-- A module to generate tables from the data in Module:Report/data.
local p = {}

local data = mw.loadData('Module:Report/data')

function p.listRelations (frame)
	local args = frame:getParent().args
	return p.listRelationsTable(frame, mw.text.split(mw.text.trim(args[1]), '%s'), args.length)	
end

function addLink(el, url, label)
	el:wikitext(string.format('[%s %s]', url, label))
end

function p.listRelationsTable(frame, ids, with_length)
	-- exists as a separate function to allow debugging with e.g. =p.listRelationsTable(mw.getCurrentFrame(), {'1234'})
	local table = mw.html.create('table'):addClass('wikitable')
	if with_length then
		table:addClass('sortable')
	end
	
	local header = table:tag('tr')
	header:tag('th'):wikitext('Name')
	if with_length then
		header:tag('th'):wikitext('Mapped length')
		header:tag('th'):wikitext('[[Template:List relations#length|Official length]]')
	end
	header:tag('th'):wikitext('[[Relation]]')
	header:tag('th'):wikitext('Links'):attr('colspan', 3)
	
	for _, id in pairs( ids ) do
		local row = table:tag('tr')
		local info = data.relations[tonumber(id)] or {tags = {}, length = '???'}
		if info.tags.name then
			row:tag('td'):wikitext(mw.text.nowiki(info.tags.name))
		else
			row:tag('td'):wikitext("''not yet queried ([[Template:List relations#Not yet queried?|?]])''"):attr('align', 'center')
		end
		if with_length then
			local lang = mw.language.new("en")
			row:tag('td'):wikitext(lang:formatNum(info.mapped_length) .. ' km'):attr('align', 'right')

			if info.wikidata_length then
				row:tag('td'):wikitext(lang:formatNum(info.wikidata_length) .. ' km'):attr('align', 'right')
			else
				row:tag('td'):wikitext('?'):attr('align', 'center')
			end

		end
		row:tag('td'):wikitext(frame:expandTemplate{ title = 'relation', args = { id, tools = 'mini' } })
		local websiteCell = row:tag('td')
		local wikipediaCell = row:tag('td')
		local wikidataCell = row:tag('td')
		if info.tags.website then
			addLink(websiteCell, info.tags.website, 'website')
		end
		if info.tags.wikipedia then
			addLink(wikipediaCell, 'https://en.wikipedia.org/wiki/' .. mw.uri.encode(info.tags.wikipedia, "WIKI"), 'wikipedia')
		end
		if info.tags.wikidata then
			addLink(wikidataCell, 'https://www.wikidata.org/wiki/' .. mw.uri.encode(info.tags.wikidata, "WIKI"), 'wikidata')
		end
	end
	return tostring( table )
end

return p