How to Use MediaWiki's Semantic MediaWiki Extension to Add Structured Data to Your Wiki
The Semantic MediaWiki (SMW) extension turns a wiki into a knowledge base by allowing you to attach semantic properties to pages
MediaWiki is the engine behind Wikipedia, but out of the box it stores only free‑form text. The Semantic MediaWiki (SMW) extension turns a wiki into a knowledge base by allowing you to attach semantic properties to pages. Those properties become machine‑readable data that can be queried, visualised and exported – essentially a lightweight database embedded in your wiki.
1. Prerequisites
- MediaWiki ≥ 1.43 (the current stable release).
- PHP ≥ 8.1 – the same version required by MediaWiki.
- Composer – the recommended way to install SMW and its dependencies.
- Database user with CREATE/ALTER privileges (SMW creates its own tables).
If you run a hosted wiki (Fandom, Miraheze, etc.) check that the host already offers SMW; otherwise you can install it yourself.
2. Installing the Extension
From your MediaWiki root directory run:
composer require mediawiki/semantic-media-wiki "~6.0"This pulls the latest stable SMW (6.0.x at the time of writing) and registers it via extension.json. Next, enable the extension in LocalSettings.php:
wfLoadExtension( 'SemanticMediaWiki' );
// Turn on semantic parsing for the whole wiki
enableSemantics( 'mywiki' ); // replace with your wiki's internal name
Run the MediaWiki maintenance script to create the required tables:
php maintenance/update.phpVisit Special:Version to verify that “Semantic MediaWiki” appears in the list of installed extensions.
3. Basic Configuration
SMW works out of the box, but a few settings improve performance and usability. Add them to LocalSettings.php after the wfLoadExtension line:
// Cache query results (highly recommended for larger wikis)
$smwgEnableCache = true;
// Upper bound for query result sets – prevents runaway queries
$smwgQMaxResults = 5000;
// Show a factbox on every page (quick view of its properties)
$smwgShowFactbox = true;
All configuration variables are documented on the SMW manual page; you can search the MediaWiki site for “Semantic MediaWiki configuration parameters”.
4. Defining Semantic Properties
Properties live in the Property: namespace. Create a page for each property you need and give it a type via the special property Has type. Example – a property to store an author name:
[[Has type::String]]
[[Display hint::text]]
Other common types are Number, Date, Boolean, Geographic coordinate and Page. Defining the type improves query accuracy and lets SMW enforce validation.
5. Adding Structured Data to Pages
There are two primary ways to store data:
- Inline annotations – simple
[[Property::Value]]brackets placed directly in the page text. - Semantic templates – a
#setblock inside a template that isolates the data from the visible content.
Inline example (a book page):
[[Has author::F. Scott Fitzgerald]]
[[Publication year::1925]]
[[Genre::Novel]]
Using a template keeps the markup tidy and guarantees a consistent data model:
{{#set:
|Has author={{{author|}}}
|Publication year={{{year|}}}
|Genre={{{genre|}}}
}}Then the page simply transcludes the template:
{{BookInfo|author=F. Scott Fitzgerald|year=1925|genre=Novel}}All pages that use BookInfo will automatically receive the three semantic properties.
6. Querying the Data
SMW provides the #ask parser function for inline queries and #show for a single‑entity view.
Simple table of all books
{{#ask: [[Category:Books]]
|?Has author
|?Publication year
|?Genre
|format=table
|sort=Publication year
|order=desc
|limit=50
}}The result is a sortable table that updates automatically whenever a book page adds or changes a property.
Finding recent publications
{{#ask: [[Category:Books]]
|[[Publication year::>2020]]
|?Has author
|format=ul
}}Result format options include table, ul (unordered list), ol, csv, chart, map (requires the Maps extension) and many more via the Semantic Result Formats extension.
7. User‑Friendly Data Entry with Page Forms
Editing raw [[Property::Value]] markup can be intimidating. The Page Forms extension (formerly Semantic Forms) lets you build HTML‑like forms that write the semantic data for you.
Example form definition (saved as Form:Book):
{{{for template|BookInfo}}}
{{{field|author|label=Author}}}
{{{field|year|label=Publication year|type=number}}}
{{{field|genre|label=Genre}}}
{{{end template}}}
Now a user clicks “Create a book” → the generated form appears → after submission the page is populated with the BookInfo template and the corresponding semantic properties.
8. Visualising Data
SMW’s core can output data as tables, but for richer visualisations install one of the result‑format extensions:
- Maps – renders geographic coordinates on an interactive map.
- Semantic Result Formats – adds calendars, timelines, charts, and filterable lists.
Example map of museums:
{{#ask: [[Category:Museum]]
|?Location
|format=map
|link=title
}}Each Location property must be a Geographic coordinate value (e.g. [[Location::52.5163,13.3777]]).
9. Performance & Scaling Tips
- Enable caching –
$smwgEnableCache = true;reduces repeated query work. - Limit result size – keep
$smwgQMaxResultsreasonable; use|limit=in queries. - Use subobjects for repeatable groups – the
#subobjectparser stores repeatable data structures (e.g. multiple authors per book) without cluttering the main page. - Monitor slow queries via
Special:Profileror the MediaWiki debug log; rewrite them with indexes or narrower filters. - Consider external stores – for very large wikis SMW can be configured to use an RDF or SPARQL store (see the “Using SPARQL and RDF stores” documentation).
10. Permissions & Rights
SMW adds a handful of custom rights that you can grant via $wgGroupPermissions. The most relevant are:
$wgGroupPermissions['sysop']['smw-admin'] = true; // full SMW admin rights
$wgGroupPermissions['editor']['smw-pageedit'] = true; // edit semantic data
$wgGroupPermissions['editor']['smw-patternedit'] = true; // edit property patterns
Fine‑grained control prevents accidental property creation or mass data changes.
11. Hooks for Custom Behaviour
If you need to react to semantic updates, SMW provides several hooks. A common use‑case is to regenerate a cached summary page whenever a property changes:
$wgHooks['SMW::Store::AfterDataUpdateComplete'][] = function ( $subject, $store ) {
// $subject is the page that just changed
if ( $subject->getTitle()->inNamespace( NS_CATEGORY ) ) {
// Rebuild a derived category overview
// (implementation left to the developer)
}
return true;
};
Full hook documentation is on the SMW page; search for the hook name to see the expected parameters.
12. Summary
Semantic MediaWiki adds a powerful, query‑driven layer to any MediaWiki installation. By defining properties, annotating pages (inline or via templates), and using the #ask function, you turn free‑form wiki text into a structured knowledge base. With optional extensions such as Page Forms, Maps, and Semantic Result Formats you can provide friendly data‑entry forms and visualisations that make the structured data useful for both humans and machines.
Follow the steps above, experiment with a small test namespace, and then expand the model to cover the whole wiki. Once the data model stabilises, the same SMW queries can drive dashboards, export feeds, or even feed external applications via SPARQL.