Help:SPARQL endpoint 1.5

Semantic MediaWiki supports external Semantic Web applications via its RDF export.

Furthermore, a Semantic MediaWiki installation can be configured in order to provide an external SPARQL endpoint. This page describes how to configure a Semantic MediaWiki installation in order to provide such a service. A test service is running on semanticweb.org.

Note that configuring the SPARQL endpoint is not trivial, and currently a number of steps is needed. We hope that setting a SPARQL endpoint on a Semantic MediaWiki becomes much more easier in the future (see SPARQL and RDF stores for SMW), but we need developing resources to make this happen.

Note that the procedure described here currently works only on the SVN development trunk. It will be part of the upcoming release.

Configuration
Setting up a SPARQL endpoint consits of three (not too easy) steps.
 * 1) Setting up RAP with a MySQL backend
 * 2) Connecting SMW to RAP
 * 3) Setting up Netapi for the SPARQL endpoint

Setting up RAP
The SPARQL endpoint is provided by RAP -- RDF API for PHP. The full documentation about RAP can be found at RAP website. All tests have been done against Version 0.9.6. It seems also to run with the SVN version of RAP. Therefore you need to first get and install RAP. For a description of how to install RAP refer to the RAP documentation. We suggest to install RAP into the subdirectory libs of the Semantic MediaWiki path.

RAP will save the triples in a MySQL database (other bindings of RAP are currently not used). It is recommended to use a fresh database for that, but it is not strictly required. If you want to use the same database that you use for the rest of MediaWiki, you can simply set the RAP database variables to the same values as your MediaWiki database variables. Note that RAP does not allow to add prefixes on the table names (that's why we advice you to have a second database).

Create the database, set up a user with it, give the user all rights. You can use the RAP rdfdb-utils in order to take a look at the RAP database and to see that it really works. Note that you need to do that manually, i.e. enter MySQL and use the following commands: CREATE DATABASE smw_rap; CREATE USER 'smw_rapuser'; SET PASSWORD FOR smw_rapuser = PASSWORD('smw_rappassword'); GRANT ALL ON smw_rap.* TO 'smw_rapuser'; Choose some sensible values for smw_rap, smw_rapuser, and smw_rappasword, and remember them.

Connecting SMW with RAP
The second step is to save triples into the RAP triplestore. For this, set the variable for RAP in LocalSettings.php. Add the following line: $smwgRAPPath = $smwgIP. '/libs/rdfapi-php/api/'; The path should point to the directory where you have installed RAP in the previous step. Note to point it to RAP's api directory.

Now set the global variable for the store to use the RAP store. Add the following line to LocalSettings.php: $smwgDefaultStore = SMW_STORE_RAP; Add the following global variables with the values that you used during installing RAP previously: $smwgRapDBserver      = 'localhost'; $smwgRapDBname        = 'smw_rap'; $smwgRapDBuser        = 'smw_rapuser'; $smwgRapDBpassword    = 'smw_rappassword'; If you decided not to use a different database, or you don't have enough rights to do so, add the following lines instead: $smwgRapDBserver	= $wgDBserver; $smwgRapDBname		= $wgDBname; $smwgRapDBuser		= $wgDBuser; $smwgRapDBpassword	= $wgDBpassword; This makes RAP use the same database and credentials as MediaWiki itself.

Now you need to create the database tables for RAP. For that go to the Special:SMWAdmin page in your wiki, and click on the Initialise or upgrade tables button. The log will show you that the tables for RAP have been set up, and a model has been created. The model is where RAP will save all the triples.

Test if you can write pages, especially such pages that have semantic annotations of any kind. They should be saved to the triplestore as well (as you can check with the rdfdb-utils of RAP. After that, you should run the maintenances script refreshData. This will save the triples for all existing pages into the triple store.

Setting up the SPARQL endpoint
The last step is to set up Netapi. Netapi is part of RAP and includes its own documentation. See a full description on how to set up RAP Netapi. This involves fishing around with your htaccess file, so be warned (but it's well described).

Note the following caveats: as of now, RAP does not limit incoming queries, and it allows to export the whole triplestore very easily, and does no caching. Please bother the RAP developers to fix that.

If you set up a SPARQL endpoint, please inform us. We would be very interested in gathering experience.

Implementation notes
As of now, the implementation is done via SMW RAPStore. The RAPStore class is a subclass of the MySQL-Store, and extends all relevant functions in order to write triples into the triple store besides writing them into MySQL. This means that the triple store mirrors completely the SQL store. Queries are performed as before, so the triple store is actually not used for internal queries. Changes to the triple store are in no way reflected in the wiki but will be only seen by the SPARQL endpoint.

The same system could be used in order to connect to other triple store. A tighter integration could also use the triple store for querying the data, thus making the SQL store superfluous. Before this is done, speed evaluations should be done to see if this makes any sense.