List the set of unique values for a property

From Semantic MediaWiki Community Wiki
Jump to: navigation, search

Extension: Semantic MediaWiki


Warning: Using the following may lead to a problem with duplicate SMW properties.

One feature that is still missing from SMW is the ability to easily list the set of unique values that have been assigned to a given property. To achieve this goal, we need to use the Extension:Arrays.

Note in advance: While values of type "Page" may be retrieved using inverse querying, this approach does not work for other property types such as "Text".

Step one[edit]

First, we need to query all the values for a given property in a simple, unformatted list. This step can be quite challenging if you are not familiar with the [1] syntax! Lets assume the property in question is called 'MyProp', the query would look something like this:

{{#ask: [[MyProp::+]] | mainlabel =- | headers = hide |? MyProp }}

Lets try an example on this wiki, using the property 'Is in language', which gives the following result:

Spanish, English, English, German, English, English, German, English, English, Dutch, English, English, English, English, English, English, English, French, Catalan, Ancient Greek, English, English, English, English, English, Dutch, English, English, Russian, English, English, English, Portuguese, English, English, French, English, English, English, French, English, English, English, English, German, French, English, English, English, English, Japanese, Spanish, German, English, English … further results

See the note below if there too many results to be presented on a single page.

Step two[edit]

Next, we need to use the arraydefine function of the Arrays extension. This step has been greatly simplified in newer versions of the extension. For example:

{{#arraydefine: my.array
 | red, red, green, blue, yellow, red, red, blue, green
 | , | print=list, sort=asc, unique

Which gives the following output:

blue, green, red, yellow

Step three[edit]

Putting it together, we'd do something like this:

{{#arraydefine: my.array
 | {{#ask: [[MyProp::+]] | mainlabel =- | headers = hide |? MyProp | limit = 10000 }}
 | , | print=list, sort=asc, unique


So much code! Lets wrap it up in a nice convenient template! :-)

Calling the template like this:

{{Property values|Is in language}}

Gives the following result:

Afrikaans, Amharic, Ancient Greek, Ancient Hebrew, Arabic, Bulgarian, Catalan, Chinese, Croatian, Czech, Dutch, Eng, English, Finnish, Français, French, German, Hebrew, Indonesian, Italian, Japanese, Latin, Lithuanian, Malayalam, Muisca, Norwegian, Polish, Portuguese, Primarily English, Romanian, Russian, Slovenian, Spanis, Spanish, Swedish, Türkçe, Ukrainian, Vietnamese, multiple languages

Limitations and issues[edit]

Duplicate properties[edit]

As noted above, this approach may lead to a problem with duplicate SMW properties.

Limitation with numerous values[edit]

This approach may present a problem when a query returns numerous values: the query needs to produce all values of the property in order for the individual instances of the array to be made unique. That's why the above query (and the query in the template) has a very large value for the 'limit' option (limit = 10000). If the limit is set too low, you will see a string "further results" appearing in the list of values. Solutions:

  • If you can, increase the limit of your values query.
  • If this is not an option for you (because the total number of values is too large for your website), there may be no workaround.

Possible alternatives[edit]

Retrieving values from Special:SearchByProperty[edit]

  • Chris Koerner has described an alternative based on Special:SearchByProperty on his blog here. Note that because it relies on this special page, more work may be necessary in order that values are correctly transliterated to their URL equivalent in encoding.

One value per subobject[edit]

If it is possible and desirable in your case, a more radical alternative to consider is to use dedicated subobjects for every single value.

Extension:SemanticPropertyTools (?)[edit]

  • Extension:SemanticPropertyTools, a continuation of the unmaintained SemanticQueryFormTool, "retrieves sets (unique lists) of ... property values (and single field values of record type properties)". Its current status is unknown: it is listed as experimental, no download location is given and the most recent version dates from 2012.