List the set of unique values for a property

From Semantic MediaWiki Community Wiki
Jump to: navigation, search

Extension: Semantic MediaWiki

Description:

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

One feature 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 ArrayExtension.

Step one 
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, English, English, English, French, English, English, English, English, French, English, English, English, English, Japanese, Spanish, German, English, English, German, English … further results


Step two 
Next, we need to use the arraydefine function of the ArrayExtension 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 
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 convenience template! :-)


Calling the template like this:

{{Property values|Is in language}}


Gives the following result:

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


[edit] Notes

The above query (and the query in the template) has a very large value for the 'limit' option (limit = 10000). This is required to ensure that *all* the values of the property are added to the array before making it unique. If you see the string 'further results' appearing in the list of values, you know that you need to increase the limit of your values query.

Personal tools
Namespaces
Variants
Actions
Navigation
Add data
Help
Data export
Tools