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äsongsmat.nu 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äsongsmat.nu 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}}|
==Authors==
{{#arrayprint:authors||@@@@|{{Author-row|@@@@}}}}
}}}}

And in the template author-row:

[[{{{1|User:Example}}}|{{#replace:{{{1|User:Example}}}|{{ns:User}}:|}}|rel=author]]

Which will produce something like this:

[[Användare:Leo|Leo|rel=author]]
Which will in its turn produce a html output like this:
<a href="http://xn--ssongsmat-v2a.nu/ssm/Anvä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.

Summary

  • 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.

Framtidens webb är semantisk

Wikimedia Deutschland (den tyska lokalavdelningen av stiftelsen Wikimedia, som driver bland annat Wikipedia) har just anställt Lydia Pintscher (en övermänskligt produktiv open source-aktivist) för att göra sajter som Wikipedia till en del av den semantiska webben. Det är goda nyheter för Säsongsmat.nu, eftersom sajten bygger på samma mjukvara, MediaWiki, som alla Wikimedias sajter, och eftersom Säsongsmat.nu är helt och hållet byggd kring principen om semantisk data.

I en mening betyder ”den semantiska webben” att sajter byggs så att både människor och maskiner kan förstå dem. Det är ett nytt, stort steg i webbens utveckling, som mycket väl kan leda till en ny explosion av tjänster vi ännu inte kan föreställa, de kommande åren.

När webben skapades i början av 1990-talet byggde den på att den som skapade en sida talade om vilken typ av innehåll olika delar av den utgjorde. Det gjordes med html-taggar av typen h1 ”Det här är en rubrik”, table ”det här är en tabell”, och så vidare. Sedan fick webbläsaren presentera det innehållet som den fann för gott.

Nästa steg blev att låta sidorna tala om inte bara vilken typ av information den innehåll, utan också hur innehållet skulle se ut. Men en några nya tekniker (särskilda tillägg till taggarna och stilmallar, CSS) kunde den som skapade en sida nu tala om att rubriken skulle vara grön och blinka.

Den semantiska webben är ett paraplybegrepp för en serie tekniker som talar om också vad innehållet på en webbsida betyder. Med hjälp av semantisk uppmärkning kan även datorer som läser webbsidan med den blinkande gröna rubriken ”Norge” förstå att den här rubriken handlar om landet Norge (och inte om en blomkruka, ett tankefel, två halmstrån i kors eller byn Norge i Oklahoma. Till exempel.)

Många tekniker för semantisk uppmärkning av material är gamla, men det är först nu webbvärlden börjar enas om några standarder för dels hur uppmärkningen rent tekniskt ska se ut, och dels hur man ska definiera t.ex. ”är” och ”ett land” i meningen ”Norge är ett land”. (Betyder ”är” att Östtyskland måste märkas upp semantiskt på ett helt annat sätt? Är Nagorno-Karabach ett land? Ontologi kallas det inom datavetenskapen.)

Med semantiskt uppmärkta sidor blir varje sajt på webben en egen databas, som andra sajter kan hämta information ifrån. Om både Nationalencyklopedin och Wikipedia definierat bananen som en ört, så skulle kanske också Säsongsmat.nu kunna skriva ut den informationen automatiskt i sin artikel om banan, för att ta ett ganska banalt exempel.

Utvecklingen av den semantiska webben hänger också intimt samman med den allt större ”sociala webben” — sociala nätverk, och allt det som delas i dem och kopplas till dem. En av de mest utvecklade tillämpningarna av den semantiska webben är nämligen sådan semantisk märkning som anger hur olika människor känner varandra.

På Säsongsmat är all data du ser i faktarutorna till höger, och en del annat, semantiskt uppmärkt, så att till exempel andra sajter, bloggplugins och mobilappar kan hämta data härifrån. Så kan andra sajter t.ex. enkelt (nåja, relativt) hitta alla fruktträd på fruktkartan som står inom 10 mils radie från Stockholm: Här. Eller hitta alla råvaror som är i säsong i oktober. Eller hitta de råvaror som är i säsong i oktober, och som innehåller mycket vitamin C.

I april ska Lydia Pintscher sätta igång med sitt projekt, och tanken är att det ska göra MediaWiki-plattformen, som alltså även Säsongsmat.nu bygger på, mer anpassad för den semantiska webben.

Länkar:

  1. WikiData-projktet
  2. Säsongsmats API
  3. Semantic MediaWiki, motorn bakom Säsongsmat

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äsongsmat.nu/ssm/Potatis:

sasm.at/244 (11 characters) will now redirect to säsongsmat.nu/ssm/Recept:Sl%C3%A5nb%C3%A4rssaft_%C3%A0_la_Gusem%C3%A5la (71 characters), etc.

This is how it was done:

First, we need an short domain (in our case sasm.at), 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 = '//shortdomain.com'; (in our case it would be $wgShortUrlPrefix = '//sasm.at';).
  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äsongsmat.nu/ssm/Special:ShortUrl/52
    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} ^shortdomain.com
    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.