Creating a Navigable list of Property Values in Semantic MediaWiki

Update: I tweaked the code after posting this. It’s been updated to reflect my working solution!
One of the great things about Semantic MediaWiki (SMW) is the auto-generated fact box at the bottom of any wiki page that has any values for semantic properties.

Screen Shot 2013-05-01 at 10.25.12 AM

It’s also relatively easy to create a list of all defined values for a given property. In fact, this idea uses this template as a basis for what follows.

However, I found that it’s not very intuitive to navigate to a list of all values of a property and see what other articles have that same property value. In order to see a list of all articles that have a particular property value you’d have to navigate to an existing page with that property and then perform a semantic search from there. Or know that there’s a page called Special:SearchByProperty that you could use.

Screen Shot 2013-05-01 at 10.04.58 AM
Example of Special:SearchByProperty results

I wasn’t satisfied with that, so I made a fancy list that’s actually really easy to implement and can be used across an entire SMW for other property values too.

The result is a list of all values for a given property that provide hyperlinks to a list of all wiki articles with that property value. It uses the Special:SearchByProperty page for displaying results.

First, I created a template called Property Values Columns. You can use it like so:

{{Property Values Columns|<em>Name of Property Here</em>}}

Here’s the contents of the “Property Values Columns” template1:

<noinclude>Use this template to list all the unique values for a given property.

== Template code ==
{{#arraydefine: valued
{{#ask: [[{{{1|}}}::+]] | mainlabel =- | headers = hide |? {{{1|}}} | limit = 10000|searchlabel= }}
|,| print=list, sort=asc, unique}}


This is what it does.

1. Gets a comma separated list of property values for whatever property you’ve requested.
2. Parses the name of the property and each unique value to build a URL.
Example: Name/Property Value
3. The secret sauce is another template called ‘Space’ which uses the #replace function to replace all spaces ” ” with the “-20” syntax used in the Special:SearchByProperty query.
3a. The “Space” template contains the following:
{{#replace:{{{1}}}| |-20}}
4. Return the results as a series of list items.

The result is an list of all unique property values that when clicked will query the wiki for all pages containing that property value. You can then use some custom styling to format the results. In the example below I wrapped the template in a div creating two columns.2

<div class="twoColumns">{{Property Values Columns|Accounting Unit}}</div>

Screen Shot 2013-05-01 at 10.13.18 AM

Pretty cool huh?

  1. The template has a weird name, I know. You can obviously change the name to something more fitting for your use.

  2. The div styling in my demo is stored in common.css and looks like this: .twoColumns li {width: 35em;float: left;padding-right: 20px;}

5 thoughts on “Creating a Navigable list of Property Values in Semantic MediaWiki”

  1. I’m trying in vain to replicate this, but the final wiki links are not parsed, meaning I get output like [[Special:SearchByProperty/Manager/….]] Either I’m blind to what I’m doing wrong, or there must be something changed in the parsing of mw 1.22.1 and SMW Any idea?

  2. Hmm. I omitted the need for the ParserFunctions extension. That’s used in the {{Space}} template.

    I also looked at my production code and I’ve made come changes since posting this. Give the new code above a try!

    1. Thanks, Chris. I’ve added a link to – from a user’s perspective this is actually a fairly fundamental feature that SMW still cannot come to terms with (from a dev’s perspective, it is no doubt more complex than it may look). It would be great if inverse queries could also be used for properties of type “text” and were allowed in concepts, but that’s not the case. A few questions / comments:

  3. * Does this approach still work when you have, say, a million values?
    * You’re using Template:Space to encode spaces, but values may contain many other characters, such as hyphens and colons, that may need to be ‘transcoded’ to their HTML equivalent. I don’t know why but I’ve found that short URLs are more troublesome in that respect.
    * There’s currently a bug causing Special:SearchByProperty to require quite sporadically that an extra space is prefixed to a value.

    1. You’re right on the url encoding. I wonder if I’d be better off using something like “urlencode” from the ParserFunctions extension in some way.

      I’m currently using it with a few hundred unique values. I’m sure at a few thousand you’d reach some serious performance issues.

Comments are closed.