Help:Querying for the absence of a property

From semantic-mediawiki.org

While querying for the absence of a property is not currently possible, if there are templates on each page in question that can be modified then a workaround is possible.

This workaround has the following requirements:

  • Only some pages have the property of interest
  • All pages can have a new property defined on them (e.g. through modification of a template that is already in use on all pages.)

In this example the property we are interested in is of datatype "Boolean"Holds boolean (true/false) values called "Is capital city". This property has only been defined on some pages:

Page name Property definition
Berlin [[Is capital city::true]]
London [[Is capital city::true]]
Sydney -
Kyoto -

Here, if we were to perform a query for non-capital cities, we would get no results.

{{#ask:
 [[Category:Cities]]
 [[Is capital city::false]]
}}

There are no results, because there are no pages where "Is capital city" is false. (There are two pages where the property is true, and two pages with the property unset.)

In order for this to work, we need a template that appears on all pages. Here we assume there is a "Template:City" that we can modify, and add the following code:

{{#set:
 |Is capital city (all)={{#ifeq: {{#show: {{PAGENAME}} |?Is capital city }} |true |true |false }}
}}

This creates a new property called "Is capital city (all)" which will be set on all pages using the template. When the value for this property is set, it looks for the original "Is capital city" property on the same page, and if that is true then it sets the new property to true as well, otherwise it sets the new property to false instead.

Now, queries can be done on the new property instead:

{{#ask:
 [[Category:Cities]]
 [[Is capital city (all)::false]]
}}
  • Sydney
  • Kyoto

Be aware that because this workaround requires a page to look up information on itself, there can be some delay after editing a page, as the value retrieved will be done so before any modifications to the page have been saved. In this case editing the page again and saving it with no modifications is sufficient to update MediaWiki's cache. (This no-op edit does not appear in the page's revision history as nothing was changed.)