Help:Inline queries 1.0

From semantic-mediawiki.org
Jump to: navigation, search


This page contains outdated information and is thus OBSOLETE!
This documentation page applies to all SMW versions from 1.0 to 1.1.2.
Other versions: ≥ 1.7.01.5.0 – 1.6.21.4.2 – 1.4.3[…]       Other languages: fr

Help:Inline queries en 1.0 1.1.2


SMW user manual
Introduction
Editing
Properties and types
Special properties
Inverse properties
Custom units

Semantic templates

Service links

Browsing interfaces
Special:Ask
Special:Browse
Semantic search
Selecting pages
Strict comparators
Displaying information
Result formats
Inline queries
Querying for queries
Concepts
Inferencing
Using the API
Semantic Web
RDF export

External tools

Importing vocabulary

SMW admin manual

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

{{#ask: argument 1 | argument 2 | … }}

where the arguments … describe the query.

(Note that prior to Semantic MediaWiki version 1.0 there was a different syntax for inline queries using an <ask> 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

{{#ask: [[Category:City]] [[located in::Germany]] }}

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: Berlin, Cologne, Frankfurt, Munich, Stuttgart, Würzburg

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

{{#ask: [[Category:City]] [[located in::Germany]]
  | ?Population
}}
Result:
 Population
Berlin3,520,061
Cologne
Frankfurt679,664
Munich1,353,186
Stuttgart606,588
Würzburg

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:

{{#ask:
  [[Category:City]] 
  [[located in::Germany]] 
  | ?Population
  | format=ul
}}
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:

Parameter Possible values Description
limit non-negative number maximal number of pages selected (in the case of a table: rows)
offset number where to start
sort property name name of property to use for sorting queries
order ascending/asc, descending/desc/reverse defines how results should be ordered, only applicable if sort is used, ascending is the default
headers show, hide shows or hides the labels/headers used in some output formats such as "table", hide is default
mainlabel plain text title of the first column (the one with the page titles in it), default is no title; set to - to suppress printing the page titles
link none, subject, all defines which article names in the result are hyperlinked, all normally is the default
default plain text if, for any reason, the query returns no results, this will be printed instead
intro plain text initial text that is prepended the output, if at least some results exist
searchlabel plain text text for continuing the search (default is "… further results")
debug true [only SMW≤0.6] gives an SQL statement for debugging instead of the query results
format a format name (see below) selected output format; some formats allow further parameters (see #Output formats)

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

{{#ask:
  [[Category:City]]
  [[located in::Germany]]
  | limit=3
}}

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: Berlin, Frankfurt, Munich... further results

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 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

{{#ask: [[Category:City]] | limit="0" | searchlabel=Click to browse a list of cities }}

displays Result: Amsterdam, Belleville, Berlin, Frankfurt, Karlsruhe, London, Munich, Paris, Porto, Rome, San Diego, Stuttgart, Sydney, Warsaw.

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 parameter, for example

{{#ask: [[Category:City]] [[Located in::Antarctica]]
  | intro=All about cities in the Antarctic |?Population |?Area
  | default=Apparently there are no cities in the Antarctic
}}

Result: Apparently there are no cities in the Antarctic

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, and the order with the parameter order; the value of order 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::+]]). For example, this query for cities sorted by area only shows cities that have a value for Property:Area:

{{#ask:
  mainlabel=cities sorted by area
  | [[Category:City]] [[Located in::Germany]]
  | ?Population
  | sort=Area
}}
Result:
Some subquery has no valid condition.

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 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,
{{#ask:
  [[Category:City]] [[Population::+]] [[Area::+]]
  |?Population=
  |?Area#km²=Size in km²
  |?Category=Category memberships
  |format=table
  |default=nothing found in category City
}}
Result:
  Size in km²Category memberships
Amsterdam783,364219 km²
84.556 sqmi
City
Sample pages
Europe
Belleville43,76560 km²
23.166 sqmi
City
Sample pages
Berlin3,520,061891.85 km²
344.343 sqmi
City
Sample pages
Pages with a map rendered by the Maps extension
Frankfurt679,664248.31 km²
95.872 sqmi
City
Sample pages
Karlsruhe294,761173.46 km²
66.973 sqmi
City
Sample pages
London7,825,2001,706.8 km²
658.995 sqmi
City
Sample pages
Pages with a map rendered by the Maps extension
Munich1,353,186310.43 km²
119.857 sqmi
City
Sample pages
Paris2,234,105105.4 km²
40.695 sqmi
City
Sample pages
Porto237.58441.66 km²
16.085 sqmi
City
Sample pages
Pages with a map rendered by the Maps extension
Rome2,761,4771,285.31 km²
496.258 sqmi
City
Sample pages
San Diego1,307,402963.6 km²
372.046 sqmi
City
Sample pages
Pages with a map rendered by the Maps extension
Stuttgart606,588207.36 km²
80.062 sqmi
City
Sample pages
Sydney4,575,5322,058 km²
794.594 sqmi
City
Sample pages
Warsaw1,720,398516.9 km²
199.575 sqmi
City
Sample pages

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. If you don't want the page titles to appear, set mainlabel to the magic value '-'. 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=-, then explicitly request their display using | ? (i.e. display the "unnamed" column). For example, modifying the example above to display the city name after Population,
{{#ask:
  [[Category:City]] [[Located in::Germany]]
  |?Population=This many people
  |?=Live in
  |format=table
  |mainlabel=-
}}
Result:
This many peopleLive in
3,520,061Berlin
Cologne
679,664Frankfurt
1,353,186Munich
606,588Stuttgart
Würzburg

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, …

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: 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: <ask default="none">...</ask>
so that, if no result is obtained, the article will display
Upcoming conferences: none
  • intro: 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:_">...</ask>
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 determines how the results of a query are displayed in the article. If it is omitted, all queries are displayed as tables (format table), unless there would be only one column, in which case the results are displayed as a comma-separated list (format list). The following formats are available:

Format Description Additional parameters
list Comma-separated list, with additional outputs shown in parentheses sep, template
ol Ordered list, with additional outputs shown in parentheses sep, template
ul Bulleted list, with additional outputs shown in parentheses sep, template
table Tabular output
broadtable Tabular output, where the table is as wide as the article.
timeline Use dates in the output to print a timeline. timelinestart, timelineend, timelinesize (default "300px"), timelinebands, timelineposition (default "middle")
eventline Unstable. Use dates in the output to print a timeline that shows more than two dates per article. timelinestart, timelineend, timelinesize (default "300px"), timelinebands, timelineposition (default "middle")
embedded Unstable. Embed selected articles. embedonly (if set, don't show article link), embedformat (can be ol, ul, h1, h2 ..., h6)
template Unstable. Print results by passing result fields as parameters to a given template. template (mandatory)
count Just the number of results (a count of the number of matching pages), instead of the results themselves
debug Debugging information for analysing problems in query answering.
rss Print links to RSS feeds for query results. rsstitle, rssdescription
googlebar A bar chart comparing numerical results width (optional)
googlepie A pie chart showing shares width (optional), height (optional)


Using templates

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

 {{#ask: ... | format=template/list/ol/ul | template=templatename }}

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 {{{1}}}, {{{2}}}, 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 #Page title display), so parameter {{{1}}} is the page title, and {{{2}}}, {{{3}}}, ... 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, {{{1}}} 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.

{{#ask:
  [[Category:City]] [[Area::+]] [[Population::+]] 
  | ?Population=Inhabitants
  | ?Area#km²=Size in km²
  | format=template
  | template=Query output demo
  | limit=3
}}
Result: 783,364 people squeeze into the 219 km²
84.556 sqmi
of Amsterdam. 43,765 people squeeze into the 60 km²
23.166 sqmi
of Belleville. 3,520,061 people squeeze into the 891.85 km²
344.343 sqmi
of Berlin. ... further results

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 ?Area#km²=Size in km² (see Display format in Semantic search help).

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

{{#ask:
  [[Category:City]] [[Area::+]] [[Population::+]] 
  | ?Population
  | ?Area#km²
  | format=ol
  | template=Query output demo
  | limit=3
  | sort=population
  | order=desc
}}
Result:
  1. 7,825,200 people squeeze into the 1,706.8 km²
    658.995 sqmi
    of London. 
  2. 4,575,532 people squeeze into the 2,058 km²
    794.594 sqmi
    of Sydney. 
  3. 3,520,061 people squeeze into the 891.85 km²
    344.343 sqmi
    of Berlin. 
  4. ... further results


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 |?

{{#ask:
  [[Berlin]] [[Area::+]] [[Population::+]]
  | ?
  | ?Population
  | ?Area#km²
  | format=template
  | template=Query output demo
}}

Result: Berlin people squeeze into the 3,520,061 of Berlin. 

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::{{#ask: [[Architect of::{{PAGENAMEE}}]]|link=none}} ]]

If there is more than one inverse property, this will not work. For example, there are many pages with the property Located in::Germany. However, you can put an inline query on a scratch page that uses its sep 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]]</ask>| ]]

generates

[ [location of::<ask sep="| ]]">[[located in::Germany
""&gt;[[located in::Germany" cannot be used as a page name in this wiki.
</ask>| ]]

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 <ask> tag format

Prior to SMW version 1.0, you would write inline queries inside an ask tag.

<ask params>...</ask>

where the … 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,

<ask>[[Category:City]] [[located in::Germany]]</ask>

Unlike the {{#ask:}} function,

  • 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.

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>
   [[Category:City]] [[located in::Germany]]
   [[Population:*]] [[Area::*|Size]]
</ask>
Result:
 PopulationSize
Berlin3,520,061891.85 km²
344.343 sqmi
Munich1,353,186310.43 km²
119.857 sqmi
Frankfurt679,664248.31 km²
95.872 sqmi
Stuttgart606,588207.36 km²
80.062 sqmi