Customizing MediaWiki skin actions with SMW

From semantic-mediawiki.org
< SMWCon Fall 2012
SMWCon Fall 2012Customizing MediaWiki skin actions with SMW
SMWCon Fall 2012
Customizing MediaWiki skin actions with SMW
Talk details
Description: Micro-tutorial on improving the wiki UI using SMW data
Speaker(s): Markus Krötzsch
Type: Lightning talk
Audience: Everyone
Event start: 24 Oct 2012 12:00
Event finish: 24 Oct 2012 12:05
Length: 5 minutes
Video: click here
Keywords:
Give feedback

This short talk shows how to customise the MediaWiki page actions based on semantic data stored by SMW.

Example: Download URL[edit]

As an example, we want to create a "Download" tab that links to the value of the property Download URL, if such a value exists.

Warning: the example takes user input from wiki pages to create HTML interface elements. It will thus allow any user who can edit a page to add such elements to the UI. In our case, users are enabled to link to an arbitrary page, which could be unsafe in public wikis (used to trick other users into clicking something). In general, user input must be escaped or sanitised before being used in HTML, or otherwise this could be used to compromise the website. URLs are sanitized, so the risk there is only to link to undesired pages.

The following steps are enough to realise this little example:

  • Create the property Download URL and set its type to "URL"
  • Insert the following at the end of LocalSettings.php:
$wgHooks['SkinTemplateNavigation'][] = function ( SkinTemplate &$sktemplate, array &$links ) { 
	global $wgTitle; // this should be the title of our current page

	if ( is_null( $wgTitle ) ) {
		return true; // but let's be prepared for strange things
	}

	// Create an SMW object for the subject page ...
	$subjectDi = SMWDIWikiPage::newFromTitle( $wgTitle );

	// ... and for the property that we care about (note the _)
	$propertyDi = new SMWDIProperty( 'Download_URL' );

	// Find all values that this page has for this property:
	$propValues = smwfGetStore()->getPropertyValues( $subjectDi, $propertyDi );

	// if there are results at all
	if ( !empty( $propValues ) ) {
		// take the first one
		$valueDi = reset( $propValues );

		// check that it is really a URL
		if ( $valueDi instanceof SMWDIURI ) {
			// get the URI
			$url = $valueDi->getURI();

			// Modify the MediaWiki actions to show this URL
			// Use 'views' instead of 'namespaces' to get the tab on the right (in Vector)
			$links['namespaces']['mytest'] =
				array(
					'class' => '', 
					'text' => 'Download',
					'href' => $url
				);
		}
	}

	return true; // always return true in a hook
}

This code works at least under MediaWiki 1.17 up to 1.21 using SMW 1.7 or 1.8.

While enabled on this wiki, this feature can be demoed on the page Semantic MediaWiki: An Introduction.

Performance considerations[edit]

The above code will be executed on many page views, even if the page is not edited. This is no concern for small and medium sites, but one should still keep this in mind when doing similar extensions. If you are using an HTTP cache (Squid, Varnish) then the page HTML will be cached and no code will be executed if it is available from the cache. But the normal MediaWiki parser cache does probably not cache the page actions, so it will not prevent the code to be run on page views.

Recording[edit]