Jump to content

Module:Tabular data

Permanently protected module
From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Mxn (talk | contribs) at 23:20, 9 May 2020 (LOOKUP()). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

local p = {}
local lang = mw.getContentLanguage()

--- Returns the value of the cell in the given output column of the row matching
--- the search key and column.
--- Reminiscent of LOOKUP() macros in popular spreadsheet applications, except
--- that the search key must match exactly. (On the other hand, this means the
--- table does not need to be sorted.)
--- Usage: {{#invoke: Module:Sandbox/Mxn/Tabular data | lookup | Table name | Search key | Search column name | Output column name }}
function p.lookup(frame)
	local data = mw.ext.data.get(frame.args[1])
	local searchValue = frame.args[2]
	local searchColumnName = frame.args[3]
	local outputColumnName = frame.args[4]
	
	local searchColumnIdx
	local outputColumnIdx
	for i, field in ipairs(data.schema.fields) do
		if field.name == searchColumnName then
			searchColumnIdx = i
		end
		if field.name == outputColumnName then
			outputColumnIdx = i
		end
		if searchColumnIdx and outputColumnIdx then
			break
		end
	end
	assert(searchColumnIdx, "Search column “%s” not found.", searchColumnName)
	assert(outputColumnIdx, "Output column “%s” not found.", outputColumnName)
	
	for i, record in ipairs(data.data) do
		if record[searchColumnName] == searchValue then
			return record[outputColumnIdx]
		end
	end
end

--- Returns a tabular data page as a wikitext table.
--- Usage: {{#invoke: Module:Sandbox/Mxn/Tabular data | wikitable | Table name }}
function p.wikitable(frame)
	local data = mw.ext.data.get(frame.args[1])
	
	local datatypes = {}
	
	local htmlTable = mw.html.create("table")
		:addClass("wikitable sortable")
	htmlTable
		:tag("caption")
		:wikitext(data.description)
	
	local headerRow = htmlTable
		:tag("tr")
	for i, field in ipairs(data.schema.fields) do
		headerRow
			:tag("th")
			:attr("scope", "col")
			:attr("data-sort-type", datatypes[j] == "text" and "string" or datatypes[j])
			:wikitext(field.title)
		datatypes[i] = field.type
	end
	
	for i, record in ipairs(data.data) do
		local row = htmlTable:tag("tr")
		for j = 1, #data.schema.fields do
			local cell = row:tag("td")
			if record[j] then
				local formattedData = record[j]
				if datatypes[j] == "number" then
					formattedData = lang:formatNum(formattedData)
					cell:attr("align", "right")
				end
				cell:wikitext(formattedData)
			else
				cell
					:addClass("mw-tabular-value-null")
					:addClass("table-na")
					:css({
						background = "#ececec",
						color = "#2c2c2c",
						["vertical-align"] = "middle",
						["text-align"] = "center",
					})
					:wikitext("N/A")
			end
		end
	end
	
	local footer = htmlTable
		:tag("tr")
		:tag("td")
		:addClass("sortbottom")
		:attr("colspan", #data.schema.fields)
	footer:wikitext(data.sources)
	footer:tag("br")
	
	local licenseText = mw.message.new("Jsonconfig-license",
		mw.ustring.format("[%s %s]", data.license.url, data.license.text))
	footer
		:tag("i")
		:wikitext(tostring(licenseText))
	
	return htmlTable
end

return p