Adding authorship information to MediaWiki pages

The perhaps most visible example of search engines starting to look for semantic data is the author information in Google search results, such as shown in the image above. Here is how we made pages like that one (a ginger bread recipe) on Sä appear with author information in Google’s search results.

First of all: The whole point with using a wiki is to have many people, or personas, editing pages collaboratively. And Google won’t be able to handle multiple authors, as far as I know. Still, there might be cases where showing a single author is relevant. In our case we have a namespace for recipes, where the creator of an article is by default the only one allowed to make changes, and other people can only fork the recipe to start creating their own version.

So we have pages that make good candidates for author markup, now what? Sä is heavily based on Semantic MediaWiki, so that’s what we will be using to retrieve and print the author(s). It would, however, not be difficult to write an extension for the same purpose without using Semantic MediaWiki.

Get the authors

To automatically get the list of authors of an article we use an extension called Semantic Extra Special Properties (first version written by us, heavily improved by others), that automatically adds semantic data about things like authors, number of revisions, etc.

If you plan to add authorship information to just an handful of articles, you could skip this step and create the links by hand.

Print out the authors

The way to tell Google that a certain name is refering to the author of an article is to add the attribute rel=author to the link’s html code. This is not something that MediaWiki will allow us to do by default, so we needed to write yet another extension: LinkAttributes. This extension extends MediaWiki’s link syntax so that we can write things like [[User:Leo|Leo|rel=author]]. Changing the wiki syntax is not always a good idea, so you might want to adjust the extension for your own needs (e.g. by creating a parser function instead). This works for us, however.

Next we need to create a template to be included in pages where we want authorship information visible. If you are using the page creator property of SemanticExtraSpecialProperties, you just need to query SMW for it, and put the result in a link. If you want all page contributors, you will need to use the Arrays extensions, like we do in our template:

{{#arraydefine:authors|{{#show:{{FULLPAGENAME}}|?Page contributor|link=none}}}}{{#if:{{#arrayprint:authors}}|

And in the template author-row:


Which will produce something like this:

Which will in its turn produce a html output like this:
<a href="ändare:Leo" rel="author">Leo</a>

Update: Claus points out in the comments that the #replace parser function is part of the ParserFunctions extension, that needs to be installed. Furthermore $wgPFEnableStringFunctions must be set to true in LocalSettings.php.

Note that it’s also possible to have MediaWiki print out the N last contributors to a page in the footer by setting $wgMaxCredits to N. You could use that output to add this data, by writing your own extension hooking into.

Connect user pages with Google+

To make authors appear in Google search results they need to have a Google+ page. And Google will need to be able to connect the author to that Google+ page. We do that by adding a field to the template that makes up user pages, making room for an optional Google+ profile url, and by, once again, adding an attribute to that url when printing it: rel=me. And while we are on it we add the same attribute to Facebook, blog and Twitter links as well.

Social media links in the user page form
As we are using SemanticForms to edit pages in the wiki, and have set the whole user page namespace to use the same form by default, it’s easy to add and remove templates from each user’s page.

Finally, the user needs to add our site to the list of contributing sites in Google+ (profile/edit)

It can take some time for results to appear in Google search results.


  • You need MediaWiki with the SemanticMediaWiki, ParserFunctions, SemanticExtraSpecialProperties and LinkAttributes extensions (disclaimer: I wrote the last two, and they most certainly contain some examples of bad coding in them). Make sure $wgPFEnableStringFunctions = true; in LocalSettings.php.
  • You probably want the Array and SemanticForms extensions as well
  • Add a template to query Semantic MediaWiki for author(s), and put the result in a link to the author user page with the rel=author attribute
  • Make sure users use a user page template with a field for Google+ user page url. If you are using SemanticForms, this will be a piece of cake.
  • Tell users that they need to add your site to the list of sites they are contributing to in Google+, in order to be credited in the Google search results.

Creating a URL shortener with MediaWiki

Today I tried out the new MediaWiki extension ShortUrl, that is intended to be used (it’s still in beta) on some Wikipedia editions, and therefore probably quite well coded. Using it together with some server rewrite rules, and Semantic MediaWiki, we now have a short URL for each article, printed at the bottom of relevant pages, like here (next to the QR code) in sä (11 characters) will now redirect to sä (71 characters), etc.

This is how it was done:

First, we need an short domain (in our case, with the DNS set up to point to our site. We also need access to .htaccess or even better httpd.conf on our server, we need of course to be able to install extensions to our MediaWiki installation, and we need to be able to run MySQL commands on the wiki’s database.

  1. Install the MediaWiki extension ShortUrl as usual, by dropping it in the extensions folder and including it in LocalSettings.php: require_once( "$IP/extensions/ShortUrl/ShortUrl.php" );
  2. In LocalSettings.php, below the require_once-line for ShortUrl, add $wgShortUrlPrefix = '//'; (in our case it would be $wgShortUrlPrefix = '//';).
  3. Run the maintenance script php maintainence/update.php
  4. Now you need to edit your httpd.conf file or your .htaccess file, whichever you have access to. The ShortUrl extension works by creating a special page (Special:ShortUrl), from which pages can be reached by their short url ID like this:sä
    What we want to do is to redirect all traffic from the short domain to this page. We also need an extra rewrite condition to make sure that we are not sending the vistitors into an infinite loop. It might look something like this (there is probably a better way, please let me know if you are good with these things!):

    RewriteCond %{HTTP_HOST} ^
    RewriteCond %{REQUEST_URI} !^\/w\/index\.php.*$
    RewriteRule ^(.*)$ /w/index.php?title=Special:ShortUrl/$1

  5. The short urls that you get from the toolbox should now be working. Now if you want to print the short urls in the page, you have a couple of options:
    • If you know a bit of php you could edit your skin to do that for you, using the method ShortUrlUtils::encodeTitle( $title ), and e.g. print a link at the bottom of each page.
    • If you want to use the links inside the actual page, and want to try developing a small MediaWiki extension, you could write a parser function doing that (i.e. creating a new parser function to be used like this: {{#shorturl:Pagename}}). I’m sure more people would be interested in using it.
    • If you are running Semantic MediaWiki, the extension SemanticExtraSpecialProperties (version >= 0.2.3) now support the short url extensions, adding an extra special property for all pages with a short url. This is how we do it. The special property can be displayed just like any other property: {{#show:{{FULLPAGENAME}}|?Short URL}} (the property name will vary depending on your language settings).

Edit: The short urls are now shown by the skin instead, among the share buttons in the bottom of each article.

Edit 2: No need to run the database commands yourself, Jon Robson (Jdlrobson) points out that you just need to run the MediaWiki update script: php maintainance/php.php. I have updated this post to reflect that.