Search code examples
mediawikimediawiki-templates

Referencing data from one article on another


(For the downvote: the reason I am asking on Stack Overflow is because this is a problem involving "programming" with MediaWiki's template system)

I am looking for a way of including data on a MediaWiki article page, such that the data values can be referenced from other pages as well, without needing to duplicate the data on the other pages. Preferably without installing extra extensions.

What I am after is the ability to create an article page that looks like this:

<!-- This page is 'Example' in the main namespace -->
{{Infobox
 | CreationDate = 2015-01-01
 | CreatedBy = John Smith
}}
This article is about the item created by {{d|CreatedBy}}.

When this page is viewed in the browser, it should appear like this:

+------------------------+
|        Example         |
| Created on: 2015-01-01 |
| Created by: John Smith |
+------------------------+
This article is about the item created by John Smith.

And then on another page, I can reference the data in the above 'Example' page, like this:

* Example created by {{d|Example|CreatedBy}} on {{d|Example|CreationDate}}

Which will appear like this:

* Example created by John Smith on 2015-01-01

The typical use for this is to place the data on the article page, then be able to provide lists which are richer than you can achieve by using categories. Currently all the data on the lists is duplicated, so if it is ever changed it needs to be updated in two places - both within the article and in the "rich list".


Solution

  • I think I have worked out how to do this. Firstly, the correct solution seems to be to use Semantic MediaWiki, an extension designed for using MediaWiki for data storage. However in normal MediaWiki, this can be achieved as follows.

    Page Template:D:

    {{#if:{{{2|}}}|{{:{{{1}}}|{{{2}}}}}
    | {{:{{FULLPAGENAME}}|{{{1}}}}}
    }}
    

    This allows {{d|Page|Field}} and {{d|Field}} to work as in the original question.

    The infobox code also needs to be changed so that it is surrounded by this:

    {{#if:{{{1|}}}|{{#ifeq:{{{1}}}|_valid|1|{{{{{{1}}}|}}}}}|
        <!-- Existing infobox goes here -->
    }}
    

    This makes the infobox data-enabled. It means you can treat pages with one of these infoboxes on it as a template, and include it in another page. This is what Template:D does - it includes the whole article as a template, and this bit of code in the infobox ensures that instead of the entire page content being included at the Template:D point, only the field of interest gets included.

    It also adds a special field called _valid with a value of 1 which can be used to detect pages that contain a valid infobox or not. If you make sure {{d|Pagename|_valid}} is equal to 1 (e.g. with {{#ifeq:...}}) before using any fields on that page, then you will get correct data. This is important because if the page you are using does not have a data-enabled infobox, then each Template:D occurrence will embed the entire contents of the page!

    Last of all, each infobox has to be changed to be entered like this:

    <onlyinclude>{{My Infobox|{{{1|}}}
     | Field1 = Value1
     | Field2 = Value2
     | etc.
    }}</onlyinclude>
    

    This is required because MediaWiki can't embed <onlyinclude> tags within templates. The |{{{1|}}} at the end of the infobox name is used to pass the field parameter from Template:D through the article page and onto the infobox template itself.

    Here is a working example of this: