Extension:DataTable
From MediaWiki.org
| WARNING: the code or configuration described here poses a major security risk.
Problem: Possible injection of raw SQL |
|
Release status: beta |
|||
|---|---|---|---|
| Implementation | Tag, Parser function, Database | ||
| Description | store data in a table and retrieve it on other pages | ||
| Author(s) | RV1971Talk | ||
| Last Version | 0.5 (2007-10-10) | ||
| MediaWiki | >=1.13.3 | ||
| License | No license specified | ||
| Download | Extension:DataTable/Source Extension:DataTable/ChangeLog |
||
|
|||
|
check usage (experimental) |
|||
Contents |
[edit] Motivation
I work as an administrator when installing MediaWiki which contains, among others, a history of days where new software versions were / will be released to production. For each release, there is a page describing the steps which were done / will be done; furthermore there is an overview page listing all releases in tabular form. The single pages and the overview page share some data like release date, status, affected component, summary. Obviously I want to update these data in one place only, as they may change frequently (for instance the date of a planned release).
There are some extensions which could be used for such a task to a limited extent. For instance, with recent versions of Extension:DynamicPageList, you could enter the data in each detail page and display it in a master page.
However, this (and other extension which might be used for such a purpose) is not that straightforward, and from a performance point of view it would be inadequate to manage large amounts of data. Therefore I wrote a new extension, borrowing the basic idea of the Tasks Extension: a tag is provided which displays data and at the same time saves it to a database table, so that it can be retrieved on other pages.
[edit] How to Install the DataTable Extension
- Apply the SQL script to create a new table in your database.
- Save the extension source code as a file extensions/DataTable/DataTable.php.
- Add the following line to your LocalSettings.php:
require_once( "extensions/DataTable/DataTable.php" );
[edit] Usage
[edit] Storing Data
To store data, you use the datatable tag. Each line within the tag corresponds to a record with fields separated by pipe characters. The parser is sufficiently intelligent so that you can use links or parameterized templates and similar staff within a field, even if this contains pipe characters.
In the simplest case, data is displayed just in a normal table. For instance, you can set up a list of important pages with some attributes like this:
<datatable table=demo>
<head>
! Page
! Rating
! Type
</head>
{{PAGENAME}}|interesting page|extension description
[[Manual:Contents|Manual]]|very interesting page|manual page
</datatable>
will display
| Page | Rating | Type |
|---|---|---|
| this page | interesting page | extension description |
| Manual | very interesting page | manual page |
and save the data in your database table.
Use of the <head> tag causes DataTable to automatically generate open-table and close-table wikitext around your data. If you do not need a headline, you can insert an empty <head> tag. If you only want to save the data without any display, you can provide the parameter display=no in the <datatable> tag (which is not a good idea in most cases since it makes it difficult to understand the way your page works). You can specify class='wikitable sortable' or similar things in the <datatable> tag to specify a css class for the table.
For fancier formatting, you can provide a parameter template=some_template. Then, the template will be invoked for each record, with the fields as template parameters. The template does not need to create tabular output, it can just do anything with the data; if you don't want to create a table, do not include a <head> tag. You can also use the <datatable> tag as a comfortable way to display a series of template invocations (tabluar or not) without saving the data to the database. In that case, just omit the table parameter.
Instead of specifying a template name, you can provide template code just after the <datatable> tag between <template> … </template> tags; this is a handy solution if you need the template just for one table. However, the template is not exactly parsed like a real template so that parameters like {{{1}}} as well as template invocations and parser functions work, but parameters with defaults like {{{1|foo}}} don't.
You can have several datatable tags on a page storing data in the same table or in different tables. You can also have datatable tags on different pages storing data to the same table. The way the extension works is fairly simple: each time a page is saved or imported, all data from this page which was previously stored in the database table is erased, and then all data defined on the new version of this page is saved. Furthermore, if a page is deleted from MediaWiki, the corresponding data is erased as well.
[edit] Retrieving Data
To retrieve data, there is a new parser function data whose syntax is as follows:
{{#data: table|columns|template|condition|sort|p1|p2|p3|p4}}
You will not always need to supply all these parameters. Evaluation of this function works as follows:
- The extension performs a select distinct of the given columns from the given table which satisfy the given condition (if any) and sorts the result as indicated (if any). Columns in the condition and the sort specification are simply called c1, c2, c3, .... If no columns are specified, all columns are taken.
- If no template is indicated, all rows found are displayed separated by semicolon, with fields separated by comma. Normally this is useful only when retrieving a single column from a single row.
- If a template is specified, it is invoked for each row with the selected columns as parameters. Furthermore, the contents of p1, p2, ... are passed to the template as named parameters p1, p2, ....
You might consider using the StackFunctions Extension to use the query results in more sophisticated ways.
[edit] Notes
- If you retrieve data on the same page where it is stored, changes will not immediately be visible after saving. This is because the data in the database is updated after the page has been rendered for output. To see the changes, you need to purge the page after saving.