Skip to content
Alvaro Ortiz-Troncoso edited this page Nov 28, 2018 · 1 revision

Introduction

Maintaining a semantic MediaWiki can be a lot of work, as any reasonably complex semantic wiki will require dozens of templates, forms and attribute pages. By separating the model from its implementation, it becomes easier to keep an overview of the semantic components used.

The first step to do this is to model the semantic relationships as an ontology, using a modelling tool such as Protege. Alternatively, simple ontologies can modelled visually using UML and then converted into RDF (see https://gitlab.com/alvarosaurus/UML-ODM-to-OWL-XML).

The second step is to convert the ontology into templates, forms and attributes pages in the wiki. This script takes an ontology file in RDF format as input and creates the necessary wiki pages using the MediaWiki API.

Installation

Follow the steps here: https://gitlab.com/alvarosaurus/RDF-to-MediaWiki/blob/master/README.md

I strongly suggest you add the following line to your MediaWiki LocalSettings.php:

# Open external links in new tab (useful when working with many forms)
$wgExternalLinkTarget = '_blank';

# Allow pages to customize the title 
# (necessary for localization of imported ontologies).
$wgRestrictDisplayTitle = false;

# Allow using icons from wikipedia
$wgAllowExternalImagesFrom = "https://upload.wikimedia.org/";

Create a "bot" user

This script uses the MediaWiki API to create Wiki content. As of MediaWiki 1.27, this requires that the session is initiated by a "bot" user.

Login to the Wiki and open the page Special:BotPasswords. On this page, you can create new bot passwords and manage existing ones. Clients using bot passwords can only access the API, not the normal web interface.

The new bot should be granted the following permissions:

(you can only grant the bot permissions that you already have).

  • Basic rights
  • Edit existing pages
  • Create, edit, and move pages
  • Delete pages, revisions, and log entries

Note down the bot's username (usually Your-User-Name@bot) and the bot password which was generated for you by MediaWiki. You will need these for the script configuration (see next).

Configuration

Copy the file example/example.ini to example/config.ini. Edit config.ini to suit your configuration (table 1).

Table 1: Script configuration options.

Property Description Example
baseMwURL Base URL of the MediaWiki installation http://myurl.org/mw/
username Username of the MediaWiki API bot user user@bot
password Password of the MediaWiki API bot user somepassword
tempDir Local directory where the script will store temporary files /tmp
tplDir Path to xslt template files templates

The example ontology

An simple example ontology is included in the folder "example". This ontology, Calendar.rdf, models an event calendar, such as from thunderbird etc.

The ontology file is provided in two XML formats: Calendar.rdf (the file used for the remaining of this how-to) stores the ontology in RDF/XML format. The file Calendar.owl stores the file in OWL/RDF format. Both formats can be used with the script, but RDF is preferred.

If you wish to edit the example ontology, I recommend you use an ontology editor such as Protege (Figure 1).

Ontology in Protege

Figure 1: The example ontology as shown in the Protege ontology editor (with VOWL Plug-in).

Mapping

The elements of the ontology will be mapped to MediaWiki templates, forms, categories and attribute pages as shown in table 2.

Table 2: Ontology to MediaWiki elements mapping.

Ontology element MediaWiki markup Example
Class Template:Classname, Form:Classname, Category:Classname The ontology class "Entry" is mapped to Template:Entry and Form:Entry
Datatype property Property:propertyname, markup in the corresponding class' template. The property "hasPriority" of the class "Entry" is mapped to the page Property:HasDetails and a snippet of markup is added to the page Template:Entry
Object property The objects in the range are listed in the domain class. The Calendar template has a semantic query that lists all the events.
rdfs:label Labels shown in the templates and forms The datatype property hasEndDate is rendered as a "Enddatum" on a German language site.
rdfs:comment Comments are shown above the input fields and on property pages The comments for each field, if available, are rendered in the language chosen during import.

Importing an ontology into a semantic MediaWiki

Make sure your Wiki is up and running, and that the Semantic MediaWiki and additional extensions are installed. You can list the extensions by opening the Special:Version page of your wiki.

To run the transformation, do:

./rdf2mw.sh -a import -i example/Calendar.rdf

Important: after that, ssh to your MediaWiki server and do:

cd /path/to/your/wiki/www/directory
php maintenance/runJobs.php

After the maintenance script has finished (the first time it takes a while):

  1. Open the page Category:Entry
  2. Create a new Entry using the form (figure 2), call it My first Entry, add some values, save it. Do not worry about "hasLocation" for the moment.
  3. Open the page Category:Location
  4. Create a new Location using the form. Add some values, save it.
  5. Open the page Category:Entry. You should see the created Entry. Open it.
  6. Click on the "Edit with form" button. If the button is not present, reload the page.
  7. Under the "hasLocation" header, select the newly created location and save the Entry.

Entry form

Figure 2: The form for the entry class, as generated from the ontology.

Advanced usage

Localizing the output

To change the language of the wiki, first add labels and comments to the properties you want to customize, e.g. using Protege. Then run the transformation with the language parameter. For example, to create a German language wiki do:

./rdf2mw.sh -a import -i example/Calendar.rdf  -l de 

Customizing layout and setting the order of properties

It may be necessary to customize the layout or to set the display order of the properties on a page. As the order of the properties is not considered relevant in ontologies, the display order has to be set in a custom template file. To import the ontology into a wiki using a custom template file:

  1. Copy the templates in src/smw/templates to a custom template directory
  2. Customize the templates
  3. Set the path to the custom templates directory in your config.ini
  4. Run the rdf2mw as usual

Removing an ontology from the wiki

This will remove the categories, templates, forms and properties of the ontology. It will not remove the content pages (the pages in the article namespace that used the templates, forms etc.)

./rdf2mw.sh -a remove -i example/Calendar.rdf

Tests

To run the tests, I recommend you install "green" and "coverage", which should be installed if you did pip3 install -r requirements.txt.

To run all the tests and get code coverage statistics:

cd test
bash test.sh

To run single test classes (without code coverage statistics), do:

cd test
export PYTHONPATH=${PYTHONPATH}:../src:.
python3 name_of_the_test_file.py

The tests run on a slightly more complex ontology, found here: https://gitlab.com/alvarosaurus/RDF-to-MediaWiki/blob/master/test/testdata/Test.rdf

Developer documentation

The directory docs contains UML diagrams. These can be opened using ArgoUML (http://argouml.tigris.org).

Troubleshooting

Problems while importing an RDF or OWL file
If you are getting 'NoneType' errors during import, make sure all the datatype and object properties have a domain and a range.
Problems with forms, templates queries or categories/
Most of the time, problems with Semantic MediaWiki can be solved by running the '''maintenance script''' as explained above.
Problems with the user interface
If you are missing icons or are seeing links where icons should be, then please check that you have edited your wiki's LocalSettings.php as suggested under ''Installation''.
Problems with tests
If the tests fail with "permission denied" errors, check the ''Create a "bot" user'' section: have you created a bot account, does it have the necessary permissions, are username and password correct in config.ini?

References

API:Login (n.d.). In ''MediaWiki''. Accessed 8 Nov. 2017, https://www.mediawiki.org/wiki/API:Login/en