Jump to content
MediaWiki

Module:Tabular data

From mediawiki.org
Module documentation
This module is rated as alpha. It is ready for third party input, and may be used on a few pages to see if problems arise, but should be watched. Suggestions for new features or changes in their input and output mechanisms are welcome.

This module provides basic functions for interacting with tabular data on Wikimedia Commons.

cell

[edit ]

Returns the value of the cell at the given row index and column name.

Usage: {{#invoke:Tabular_data|cell|Page name.tab|output_row=Index of row to output|output_column=Name of column to output}}

A row index of 1 refers to the first row in the table. A row index of -1 refers to the last row in the table. It is an error to specify a row index of 0.

Examples

[edit ]

Latest death toll in c:Data:COVID-19 cases in Santa Clara County, California.tab (regardless of when the table was last updated):

{{#invoke:Tabular data|cell
|output_row=-1
|output_column=deaths
|COVID-19 cases in Santa Clara County, California.tab}}
3226

lookup

[edit ]

Returns the value of the cell(s) in one or more output columns of the row matching the search key and column.

This function is 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:Tabular_data|lookup|Page name.tab|search_value=Value to find in column|search_column=Name of column to search in|output_column=Name of column to output|output_column2=Name of another column to output|output_columnn=...|output_format=String format to format the output}}

If multiple columns are output without an explicit string format, this function formats the output as a human-readable list.

Some may find {{Tabular query }} (which uses this module) an intuitive way to obtain cell data as it resembles a simple SQL query.

Parameters

[edit ]
|1=
Page name on Commons with extension but no namespace
|search_value= or |search_pattern=
Value to find or pattern to match in column
|search_column=
Name of column to search in
|occurrence=
Index of the match to output in case of multiple matching rows. A row index of 1 refers to the first matching row. A row index of -1 refers to the last matching row. It is an error to specify a row index of 0.
|output_column= or |output_column1=, |output_column2=, ...
Names of columns to output
|output_format=
String format to format the output

Examples

[edit ]

Total confirmed case count in c:Data:COVID-19 cases in Santa Clara County, California.tab on the day that the county issued a stay-at-home order:

Lua error at line 77: Output column "totalConfirmedCases" not found..

The last day that a hundred or more patients with COVID-19 were in the hospital in c:Data:COVID-19 cases in Santa Clara County, California.tab:

{{#invoke:Tabular data|lookup
|search_pattern=%d%d%d
|search_column=hospitalized
|output_column=date
|occurrence=-1
|COVID-19 cases in Santa Clara County, California.tab}}
2020年01月27日

Total number of administrators on all Wikimedia wikis using c:Data:Wikipedia statistics/data.tab:

{{#invoke:Tabular data|lookup
|search_column=site
|output_column=admins
|search_value=total.all
|Wikipedia statistics/data.tab}}
5468

Number of administrators and users on all Wikimedia wikis using c:Data:Wikipedia statistics/data.tab:

{{#invoke:Tabular data|lookup
|output_format=%d out of %d users are administrators
|search_column=site
|output_column2=users
|output_column=admins
|search_value=total.all
|Wikipedia statistics/data.tab}}
5468

Note: Wikipedia statistics are shown as an illustration only. In practice, there is a high-performance module {{NUMBEROF}} to access Wikipedia statistics.

wikitable

[edit ]

Returns the entire data table as a (rather plain) table.

Usage: {{#invoke:Tabular_data|wikitable|Page name.tab}}


Implementation notes

[edit ]

The implementation of this function incorporates {{N/a }} (to represent null values), {{Yes }} (true), and {{No }} (false). The templates themselves cannot be reused because they are incompatible with the mw.html library, which builds the table using an HTML DOM instead of pure wikitext.

Internationalization

[edit ]

You can most likely port this template to a wiki in another language without making major modifications. The wikitable function automatically localizes the table's description, column titles, and license name into the wiki's content language. It also formats numbers according to the content language. However, you should localize the cells representing true, false, and null by changing the values in the messages, bgColors, and colors variables to match the wiki's own {{Yes }}, {{No }}, and {{N/a }} templates, respectively.

The above documentation is transcluded from Module:Tabular data/doc. (edit | history)
Editors can experiment in this module’s sandbox (create | mirror) and testcases (create) pages.
Subpages of this module.

 localp={}
 locallang=mw.getContentLanguage()
 localnavbar=require("Module:Navbar")

 localmessages={
 ["true"]="Yes",
 ["false"]="No",
 null="N/A",
 }

 localbgColors={
 ["true"]="#9f9",
 ["false"]="#f99",
 null="#ececec",
 }

 localcolors={
 null="#2c2c2c",
 }

 --- Returns the value of the cell at the given row index and column name.
 --- A row index of 1 refers to the first row in the table. A row index of -1
 --- refers to the last row in the table. It is an error to specify a row index
 --- of 0.
 --- Usage: {{#invoke:Tabular data | cell | Table name | output_row = Index of row to output | output_column = Name of column to output }}
 functionp.cell(frame)
 localdata=mw.ext.data.get(frame.args[1])
 localrowIdx=tonumber(frame.args.output_row)
 localoutputColumnName=frame.args.output_column

 localoutputColumnIdx
 fori,fieldinipairs(data.schema.fields)do
 iffield.name==outputColumnNamethen
 outputColumnIdx=i
 break
 end
 end
 assert(outputColumnIdx,mw.ustring.format("Output column "%s" not found.",outputColumnName))

 ifrowIdx>0then
 rowIdx=(rowIdx-1)%#data.data+1
 elseifrowIdx<0then
 rowIdx=rowIdx%#data.data+1
 else
 error("0 is not a valid row index.")
 end

 returndata.data[rowIdx][outputColumnIdx]
 end

 --- 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: Tabular data | lookup | Table name | search_value = Value to find in column | search_column = Name of column to search in | output_column = Name of column to output }}
 functionp.lookup(frame)
 localdata=mw.ext.data.get(frame.args[1])
 localsearchValue=frame.args.search_value
 localsearchColumnName=frame.args.search_column
 localoutputColumnName=frame.args.output_column

 localsearchColumnIdx
 localoutputColumnIdx
 fori,fieldinipairs(data.schema.fields)do
 iffield.name==searchColumnNamethen
 searchColumnIdx=i
 end
 iffield.name==outputColumnNamethen
 outputColumnIdx=i
 end
 ifsearchColumnIdxandoutputColumnIdxthen
 break
 end
 end
 assert(searchColumnIdx,mw.ustring.format("Search column "%s" not found.",searchColumnName))
 assert(outputColumnIdx,mw.ustring.format("Output column "%s" not found.",outputColumnName))

 fori,recordinipairs(data.data)do
 ifrecord[searchColumnIdx]==searchValuethen
 returnrecord[outputColumnIdx]
 end
 end
 end

 --- Returns a tabular data page as a wikitext table.
 --- Usage: {{#invoke: Module:Tabular data | wikitable | Table name }}
 functionp.wikitable(frame)
 localpageName=frame.args[1]
 localdata=mw.ext.data.get(pageName)

 localdatatypes={}

 localhtmlTable=mw.html.create("table")
 :addClass("wikitable sortable")
 htmlTable
 :tag("caption")
 :wikitext(navbar.navbar({
 template=":c:Data:"..pageName,
 mini="y",
 style="float: right;",
 "view","edit",
 }))
 :wikitext(data.description)

 localheaderRow=htmlTable
 :tag("tr")
 fori,fieldinipairs(data.schema.fields)do
 headerRow
 :tag("th")
 :attr("scope","col")
 :attr("data-sort-type",datatypes[j]=="text"and"string"ordatatypes[j])
 :wikitext(field.title)
 datatypes[i]=field.type
 end

 fori,recordinipairs(data.data)do
 localrow=htmlTable:tag("tr")
 forj=1,#data.schema.fieldsdo
 localcell=row:tag("td")
 ifrecord[j]then
 localformattedData=record[j]
 ifdatatypes[j]=="number"then
 formattedData=lang:formatNum(formattedData)
 cell:attr("align","right")
 elseifdatatypes[j]=="boolean"then
 cell
 :addClass(record[j]and"table-yes"or"table-no")
 :css({
 background=record[j]andbgColors["true"]orbgColors["false"],
 color=record[j]andcolors["true"]orcolors["false"],
 ["vertical-align"]="middle",
 ["text-align"]="center",
 })
 :wikitext(record[j]andmessages["true"]ormessages["false"])
 end
 cell:wikitext(formattedData)
 else
 cell
 :addClass("mw-tabular-value-null")
 :addClass("table-na")
 :css({
 background=bgColors.null,
 color=colors.null,
 ["vertical-align"]="middle",
 ["text-align"]="center",
 })
 :wikitext(messages.null)
 end
 end
 end

 localfooter=htmlTable
 :tag("tr")
 :tag("td")
 :addClass("sortbottom")
 :attr("colspan",#data.schema.fields)
 footer:wikitext(data.sources)
 footer:tag("br")

 locallicenseText=mw.message.new("Jsonconfig-license",
 mw.ustring.format("[%s %s]",data.license.url,data.license.text))
 footer
 :tag("i")
 :wikitext(tostring(licenseText))

 returnhtmlTable
 end

 returnp

AltStyle によって変換されたページ (->オリジナル) /