Semantic MediaWiki: Adding Structured Data to a MediaWiki Wiki

A practical guide to Semantic MediaWiki: installation, property types, inline annotations vs templates, the #ask query language and real configuration settings.

Out of the box, MediaWiki stores free-form text. The Semantic MediaWiki (SMW) extension turns a wiki into a knowledge base: pages carry semantic properties, and those properties become machine-readable data that can be queried, exported and displayed in factboxes and maps. The extension has been in active development since 2005 and is currently at version 7.x, installable through Composer. This guide covers installation, data entry and querying.

Installation

Install SMW with Composer from the MediaWiki root directory:

composer require mediawiki/semantic-media-wiki '^7.2'

The package registers the extension automatically. Then load it and declare your wiki's domain (a unique identifier, typically the site domain) in LocalSettings.php:

wfLoadExtension( 'SemanticMediaWiki' );
// required: declare the wiki domain for semantic parsing
enableSemantics( 'example.org' );

SMW creates its own database tables (about 20 of them), so the database user needs CREATE/ALTER privileges and you must run the updater:

php maintenance/run.php update

Verify the extension on Special:Version. On hosted platforms (Miraheze, wiki.gg) check that SMW is offered before planning around it. Match the SMW version to your MediaWiki release: 7.x targets current and recent releases, older branches need older SMW.

Defining properties

Properties live in the Property: namespace. Create a page per property and declare its type with the special property Has type:

[[Has type::Page]]

Common types are String, Number, Date, Boolean, Page (for links to other pages) and Geographic coordinate. Typed properties enable validation and correct query behavior, so type every property you create.

Adding data to pages

Two ways to store data:

  • Inline annotations — write [[Property::Value]] directly in the page text; useful for one-off values
  • Templates — a template with {{#set: Property=Value }} or by using a form (via Page Forms) keeps data entry consistent across hundreds of pages

Example: a book page annotated inline

A novel by [[written by::F. Scott Fitzgerald]], published in [[publication year::1925]].

Querying with #ask

The #ask parser function is SMW's query language. Find all novels published after 2000, sorted by year:

{{#ask: [[Category:Books]] [[publication year::>2000]]
 |?publication year
 |sort=publication year
 |order=desc
}}

Results render as a table, and can be exported (CSV, JSON), formatted as lists, timelines or maps (with the Semantic Result Formats extension), or reused by other pages. Property values are also shown in the factbox at the bottom of annotated pages.

Configuration that matters

// Show the factbox on content pages
$smwgShowFactbox = true;

// Upper bound on query result rows (default 10000)
$smwgQMaxLimit = 5000;

// Cache query results (default: main cache service)
$smwgQueryResultCacheType = 'hash';
$smwgQueryResultCacheLifetime = 3600;

All options are documented in the SMW configuration reference. The performance-relevant ones are query caching and the limits — unconstrained #ask queries are the main reason SMW wikis slow down.

SMW or Cargo?

For new structured-data projects, compare SMW with Cargo: Cargo stores data in dedicated tables closer to a database with its own #cargo_query language, while SMW keeps data in the page text with richer annotation and inference (including special properties for categories, users and namespaces). Both integrate with Page Forms. SMW's strength is deep semantic modelling; Cargo's is simpler, faster queries. Pick one per use case — running both doubles conceptual overhead.

Subscribe to MediaWiki Tips and Tricks

Don’t miss out on the latest articles. Sign up now to get access to the library of members-only articles.
jamie@example.com
Subscribe