User:GreenC/software/tabular

=Tabular data in Lua templates, an alternative to Wikidata and/or local data pages =

Commons Tabular ("Table") Data is an alternative method to store and access data for Wikipedia templates. An example tabular file can be seen at c:Data:Wikipedia_statistics/data.tab which has been rendered at List of Wikipedias via the templates +, which use Module:NUMBEROF.

Tabular Data is currently an underutilized resource with only a handful of modules deploying it (May 2020).

Advantages:
 * Data hosted on Commons makes it immediately accessible to all 300+ wikis.
 * Templates can be quickly rolled out to other wiki languages because the data is hosted and updated at a single central location. For example, after re-programming to use Tabular data it was rolled out to over 60 wikis in a matter of days without the need for permissions to maintain local data pages via bot.
 * Data can be updated more frequently on Commons then it would make sense to if maintaining separate data files on each wiki.
 * Commons will host data types that may not be appropriate for Wikidata, like daily temperature changes, stock prices or Wikipedia statistics.
 * JSON files are much easier to develop and work with than Wikidata.

Disadvantages:
 * The data files can not be too large or Lua will exceed memory and CPU during page rendering. This can be mitigated by creating multiple data files, but it does not scale well to very large data sets.

Technical
Implementing tabular data at high performance has not been documented anywhere. This shows a best practice example for Template:NUMBEROF

Lua has a hook to access .tab pages, for example  as seen in Module:NUMBEROF/data. The JSON is then loaded into a Lua associative array and used within the template like normal.

One problem that arises is performance. The Lua template might be used multiple times per page and each invocation will result in the JSON being retrieved from Commons which is very sub-optimal. For example in practice Template:NUMBEROF might be used as often as 4,000 times on a single page (List of Wikipedias), which causes timeout red errors due to excessive I/O with Commons. To solve this  is used in a sub-module, such as Module:NUMBEROF/data, and the sub-module is loaded by the main module like so:   - this works because loadData is a special function that loads a sub-module only 1-time per page, meaning   itself is only invoked 1-time.