Archive:Inline queries 1.0

Semantic MediaWiki includes a simple query language for semantic search, so that users can directly request certain information from the wiki. Readers who do not wish to learn the query syntax can still profit from this feature: inline queries dynamically include query results into pages. So queries created by a few editors can be consumed by many readers.

Inline queries are similar to other semantic search features, and can also be restricted on a site in order to ensure sufficient performance. Since inline queries exploit the existing caching mechanisms of MediaWiki, most requests for a page with such dynamic contents can be served without any performance impact whatsoever.

Introduction
You put a query inline in the wiki-text of a page using the ask parser function implemented by Semantic MediaWiki. Like other parser functions in extensions, the syntax for this is by writing text of the form

where the arguments &hellip; describe the query.

(Note that prior to Semantic MediaWiki version 1.0 there was a different syntax for inline queries using an   tag, it still works and is documented at the end of this page.)

The most important argument to the #ask function is the conditions for display, as described in the help page to semantic search. For example, one can write

to obtain a list of all cities in Germany. Where you insert this query in the wiki-text, a simple list of the matching page titles will be displayed within the page.

Let's do it here: Result:

Note that all the arguments to the #ask: function are ignored by the page parsing, hence the above example does not add a category or a "located in" property annotation to the current page.

To display additional properties of the pages matching the condition, you add additional arguments of the form ?property name, for example Result: A few things to note:
 * We use the pipe '|' symbol to separate the conditions from the property to display.
 * The conditions for display are a single argument to the ask function, so there are no '|' symbols between them.
 * As you can see, you can add white space within the #ask function, the parsing is fairly flexible.
 * The format of the results display changes when you request display of additional properties. SMW picks an appropriate default format for query results, but you have extensive control of the appearance of query results.

Inline query arguments
In general, an inline query is a request to find a number of pages that satisfy certain requirements. The query must answer three questions:


 * 1) Which pages are requested?
 * 2) What information should be displayed about those pages?
 * 3) How should the results be formatted within the page?

The first two points are explained at Help:Semantic search. The third point is largely independent of the first two, but is important to smoothly include query results in pages. In our above example, we might wish to get a bulleted list of countries, which we can achieve using format=ol:

Result:

SMW implements a wide variety of output formats for inline queries, and allows you to futher control results display using a MediaWiki template. In addition to this "format" parameter, inline queries accept other parameters that affect results display, e.g. for sorting the results or for limiting the number of returned items.

Standard parameters
A number of standard parameters are always available for customising the result of inline queries. They are:

Result limits and further results
You can set the parameter limit to restrict the maximum number of results that are returned. For example, the query

displays at most 3 cities in Germany. Even if you do not specify a value for limit, SMW always applies some limit to the results a query returns. Depending on a site's settings, it might be possible to increase the number of displayed results by specifying a higher value for limit. However, there is usually a maximum limit that cannot be exceeded, set by the wiki administratorbased on performance considerations.

Let's run the above query, Result:

As you should see, when a query does not display all matches due to a limit, it will often show a link to "further results". The text of this link can be modified by setting the parameter searchlabel. If the value of searchlabel is "", then no link to further results appears. Some output formats (see below) never display the search link, or display it only if a searchlabel was specified.

An interesting application of limit and searchlabel</tt> is to display only a link to the results of a search, without showing any result inline. You achive this by specifying a limit of "0". For instance, the query

displays Result:.

Nothing returned, nothing appears
If no articles satisfy the conditions of a query, nothing appears. This can be useful, but more often an inline query drifts out of sync with semantic notations on the wiki and nobody notices the query on a page laboring to display nothing. To ensure a query always displays something, you should specify text for the default</tt> parameter, for example Result:

Sorting results
By default, semantic search results are in page title order, ascending. The Special:Ask input form has additional input fields to specify a different sort column and ordering. In inline queries, you specify those sort column with the parameter sort</tt>, and the order with the parameter order</tt>; the value of order</tt> should be "ascending" or "descending" (or the short forms "asc" and "desc", or "reverse").

Sorting on a property implicitly sets a condition that pages must have a value for that property (a condition which you would otherwise set with  Prop name::+</tt>). For example, this query for cities sorted by area only shows cities that have a value for Property:Area:

Result:

Configuring labels/table headers
Queries that return more than just the selected articles (e.g. the population in the above example) will display labels that describe the various output fields.

In the table format, the labels appear as column headers. In other formats, the labels might appear right before the output fields. The display of labels can be controlled with the parameter headers</tt> which currently can take one of two values:
 * "show": display labels (default)
 * "hide": hide all labels or table headers

By default, the label just displays the name of the requested property, or the text "Category" if categories are displayed. The Header label section of the Semantic search page tells you how to specify alternate text for labels. For example, Result:

Labels for properties display as a link to the respective pages in the Property: namespace.

Page title display
When you query to select pages and display using table format, the first column is usually the page titles matching the query with a blank column header.

To set the label for the page titles, use the parameter mainlabel</tt>. If you don't want the page titles to appear, set mainlabel to the magic value '-</tt>'. This might change in future versions to provide an additional simpler method with the |-syntax.

If you want to show the page titles elsewhere than the first column, disable their default display in the first column with mainlabel=-</tt>, then explicitly request their display using | ?</tt> (i.e. display the "unnamed" column). For example, modifying the example above to display the city name after Population, Result:

Introduction and default text
Certain texts should be shown or not shown depending on whether the query has results or not. For example, one may want the query to show an output of the following form:

Upcoming conferences: IJCAI2007, ICCS2007, &hellip;

where the list of conferences is generated by a suitable query. If the query (for whatever reason) would not return any results, the page would look as follows

Upcoming conferences:

which is not desirable. Two parameters exist to prevent this.


 * default</tt>: this parameter can be set to a default text that should be returned when no results are obtained. In the above example, one would probably write something like

Upcoming conferences: ...


 * so that, if no result is obtained, the article will display

Upcoming conferences: none


 * intro</tt>: this parameter specifies a text that should be prepended to the output of a query, but only if one or more results exist. In the above example, one could write

<ask intro="Upcoming conferences:_">...


 * so that, if no result is obtained, nothing will be printed at all. Note that we use "_" to encode the final space. This is needed for initial and final spaces in any parameter, since those are otherwise removed internally (not by SMW).

Both of the above solutions will show the intended output if results are found. It is also possible to combine both parameters if desired. Note that the above parameters only accept simple texts: neither wiki-markup nor HTML-elements are supported at the moment.

Also note that if the set of pages selected in a query is empty, no header row or blank line, not even any blank space, is produced. This can also be useful to "hide" queries that are not applicable. However, it is not recommended to insert great amounts of queries into every page, based on the assumption that this can do no harm since no output is generated. Indeed, answering queries requires much computational resources and should not be done without a purpose.

Output formats
The parameter format</tt> determines how the results of a query are displayed in the article. If it is omitted, all queries are displayed as tables (format table</tt>), unless there would be only one column, in which case the results are displayed as a comma-separated list (format list</tt>). The following formats are available:

Using templates
You can create a wiki template to control the display of an inline query. This works for <tt>format=template</tt> or <tt>format=list</tt>/<tt>ol</tt>/<tt>ul</tt>. In the inline query you specify the wiki template using the following syntax:

Semantic MediaWiki performs the inline query and passes each selected page to your template in turn. In the template you put the text and formatting you want to display around query results; where you want to display part of the query results you use numbered parameters <tt></tt>, <tt></tt>, etc. Each parameter refers to one "field" or column in the results that would be displayed by the inline query for each selected page. Normally the first field a query displays is the page title (see ), so parameter <tt></tt> is the page title, and <tt>,, ...</tt> are the other properties displayed by the query.

The template feature allows greater flexibility in the display of the query, including:
 * Changing the order in which output is displayed, or omitting or duplicating output;
 * Displaying images depending on query results;
 * Creating links for property values;
 * Using CSS styles to vary font-size, alignment, background-color, etc. by column in tables.

Notes:
 * In the template, you cannot apply parser functions to parameters (e.g. for computations and branching), they do not work.
 * Some queries do not display the page title of the matching page, for example if you directly specify the pages in the query. Without the page title, <tt></tt> would refer to the first displayed field about the page.  In order to reuse the same template in this case, you can force the query results to include the page title by adding ? (i.e. the "unnamed" column for the page title) as the first field to display.  See the last example.

These examples all use Template:Query output demo, view its source.

Result:

In the above example you can see how the template ignores any header labels specified in the query such as "Size in km²". But the values the template displays do use the units display format specified in <tt>?Area#km²=Size in km²</tt> (see Display format in Semantic search help).

Below is a similar query sorted by population that uses <tt>format=ol</tt> to produce a numbered list.

Result:

If we directly specify a single page, then normally the query results do not include the page, so to reuse the same template in the query below we must tell the query to display the page title as the first column by adding <tt>|?</tt>

Result:

Using a query to produce annotations
When querying for a property, SMW does not (currently) automatically query for transitive or inverse properties. Pages must have the properties in the query being made. For example, even if there are pages for architects with the property Architect of, you must still annotate pages for buildings with  Has architect::Le Corbusier  to make this inverse property appear on pages and in queries.

If you know there is exactly one annotation, you can put the function form of the query for the inverse inside the annotation. For example, on the building's page you could add Its architect is Has architect::

If there is more than one inverse property, this will not work. For example, there are many pages with the property <tt>Located in::Germany</tt>. However, you can put an inline query on a scratch page that uses its <tt>sep</tt> parameter to generate a list of pages that looks like annotated wiki text. You can then paste the query results into the edit box of the page. In this example, [ [location of::<ask sep="  | ]]location of::  ">[[Located in::Germany | ]]

generates
 * <tt>[ [location of::<ask sep="| ]]location of::">[[located in::Germany | ]]</tt>

You can then copy this generated wikitext to the edit box of the Germany page. (Remove the space between the first two brackets after pasting it in.)

It's possible to do this more directly using templates, see Germany (inverse example).

Old tag format
Prior to SMW version 1.0, you would write inline queries inside an ask tag. <ask params>... where the &hellip; describes the query.

This form still works but is no longer recommended.

The part between the two ask-tags can be any query, as described in the help page to semantic search. For example, located in::Germany

Unlike the function, For example, to display the population of cities in Germany, display their area labelled "Size", sort by population descending, and limit to the biggest 5 cities, you would write <ask sort=Population order=desc limit=5> located in::Germany Population:* Size
 * you specify additional properties to display by mentioning them in annotation format with a '*' in place of any condition, and use '|' syntax to introduce the column label
 * you specify additional parameters in the opening ask tag.

Result: