Extension:Semantic Compound Queries

From MediaWiki.org
Jump to: navigation, search
MediaWiki extensions manualManual:Extensions
Crystal Clear action run.png
Semantic Compound Queries

Release status:Extension status stable

ImplementationTemplate:Extension#type Parser function
DescriptionTemplate:Extension#description Defines a parser function, #compound_query, that can make multiple Semantic MediaWiki queries at the same time
Author(s)Template:Extension#username Yaron Koren
Latest versionTemplate:Extension#version 1.0 (September 2016)
MediaWikiTemplate:Extension#mediawiki 1.23.* or greater
PHPTemplate:Extension#php 5.3+
Database changesTemplate:Extension#needs-updatephp No
ComposerComposer mediawiki/semantic-compound-queries
LicenseTemplate:Extension#license GNU General Public License 2.0 or later
Download See here or

ExampleTemplate:Extension#example A Google Map showing two different kinds of points, with a different marker for each

Translate the Semantic Compound Queries extension if it is available at translatewiki.net

Check usage and version matrix.

The Semantic Compound Queries extension is meant to work with Semantic MediaWiki and allows for the display of more than one SMW inline query in one results display set. It uses its own parser function, #compound_query, which takes inputs similar to that of #ask but can take in an unlimited number of queries, instead of just one.

Download[edit source]

You can download the Semantic Compound Queries code, in .zip format, here.

You can also download the code directly via Git from the MediaWiki source code repository. From a command line, you can call the following:

git clone https://github.com/SemanticMediaWiki/SemanticCompoundQueries.git

To view the code online, including version history for each file, go here.

Installation[edit source]

From tarballs[edit source]

To install this extension, create a 'SemanticCompoundQueries' directory (either by extracting a compressed file or downloading via Git), and place this directory within the main MediaWiki 'extensions' directory. Then, in the file 'LocalSettings.php', somewhere below the inclusion of Semantic MediaWiki (both the main 'include_once' line and the 'enableSemantics' line), add the following line:

require_once "$IP/extensions/SemanticCompoundQueries/SemanticCompoundQueries.php";

With Composer[edit source]

The recommended way to install Semantic Compound Queries is by using Composer with an entry in MediaWiki's "composer.local.json" file.

    "require": {
        "mediawiki/semantic-compound-queries": "~1.0"

From your MediaWiki installation directory, execute

composer require mediawiki/semantic-compound-queries:~1.0

Usage[edit source]

A normal inline query, using the #ask parser function, takes in arguments of three types: (1) a filter that determines the results, (2) the names of properties to be displayed, and (3) arguments that determine the format and other aspects of the display. Here is an example of a normal inline query:

{{#ask:[[Category:Stores]][[Has type::Clothing]]|?Has country=Country|format=table}}

In this query there are three arguments separated by pipes and they belong to argument types 1, 2 and 3, respectively.

For compound queries the first two types of argument are unique to each sub-query and are separated from each other by semicolons instead of pipes; pipes are used to separate one sub-query from the rest. The third type of argument, setting the display of the results, is common across all sub-queries. Here is an example of a call to #compound_query that shows both clothing stores and fast-food restaurants in a table:

{{#compound_query:[[Category:Stores]][[Has type::Clothing]];?Has country=Country
    |[[Category:Restaurants]][[Has cuisine::Fast food]];?Has country=Country

One important exception to this rule is the limit= parameter, which sets the maximum number of results displayed: it must be used per sub-query, not for the overall compound query.

Maps[edit source]

To make things more interesting, we can place all these results on a map, using the Semantic Maps extension. Here is the same set of results, using SM's 'googlemaps' format, and a different property displayed for both sets of points to set their geographical coordinates:

{{#compound_query: [[Category:Stores]] [[Has type::Clothing]]; ?Has coordinates
    | [[Category:Restaurants]] [[Has cuisine::Fast food]]; ?Has coordinates
    | format=googlemaps}}

We can set a different icon to appear for each location type in the map, by using the "icon" parameter, which Semantic Maps understands (note that this feature only works with MediaWiki 1.13 or higher). Assuming your wiki contains uploaded images by the name of "Shirt.png" and "Hamburger.png", you could call the following:

{{#compound_query: [[Category:Stores]] [[Has type::Clothing]]; ?Has coordinates; icon=Shirt.png
    | [[Category:Restaurants]] [[Has cuisine::Fast food]]; ?Has coordinates; icon=Hamburger.png
    | format=googlemaps|height=400|width=600}}

This query also sets the height and width of the map - additional arguments of the third type.

#compound_query avoids displaying the same page more than once, so if a page gets returned by more than sub-query, it's displayed only for the first one. In this way, a compound query can have specific sub-queries, and then a general catch-all sub-query at the end:

{{#compound_query:[[Category:Restaurants]][[Has cuisine::Fast food]];?Has coordinates;icon=Hamburger.png
    |[[Category:Restaurants]][[Has cuisine::Italian]];?Has coordinates;icon=Spaghetti.png
    |[[Category:Restaurants]];?Has coordinates;icon=Fork.png

Semantic Maps displaying the result of a compound query with Google Maps: Sm-qp-gmaps-referata.jpg

Calendars[edit source]

There's also special handling for the 'calendar' format, defined in Semantic Result Formats - if you display a calendar, you can set a different color for each set of events displayed. Here is an example:

{{#compound_query:[[Category:Events]][[Has priority::High]];?Has date;color=red
    |[[Category:Events]][[Has priority::Low]];?Has date;color=#99cc99
    |[[Category:Events]];?Has date

Limitations[edit source]

  • Unlike regular #ask queries, #compound_query does not display a "further results" link if the number of results exceeds the limit. For this reason, the searchlabel parameter available for regular #ask queries cannot be used.

Authors[edit source]

Semantic Compound Queries was mostly written by Yaron Koren, reachable at yaron57 -at- gmail.com. Important contributions were made by Jeroen De Dauw.

Semantic Compound Queries was funded initially by the KDZ - Centre for Public Administration Research in Vienna, Austria.

Version history[edit source]

Semantic Compound Queries is currently at version 1.0. See the entire version history.

Bugs and feature requests[edit source]

You should use the Semantic MediaWiki mailing list, semediawiki-user, for any questions, suggestions or bug reports about Semantic Compound Queries. If possible, please add "[SCQ]" at the beginning of the subject line, to clarify the subject matter.