Help:Inline queries 1.0

From Edge of Darkness Wiki

Inline queries 1.0
Jump to: navigation, search

SMW user manual

Introduction

Browsing interfaces

Semantic search

Selecting pages

Displaying information

Concepts

Inline queries

Inferencing

Editing

Properties and types

Custom units

Semantic templates

Service links

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.

Contents

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:

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: A few things to note:

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:Vampire]] 
  [[Clan::Daeva]] 
  [[Character Status::Active]]
  | ?Covenant
  | 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:

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

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:

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:

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#km²=Size in km²
  |?Category=Category memberships
  |format=table
  |default=nothing found in category City
}}

Result: nothing found in category City

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:

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.

Upcoming conferences: <ask default="none">...</ask>
so that, if no result is obtained, the article will display
Upcoming conferences: none
<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:

Notes:

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:

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:

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:

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.

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,

Personal tools
Namespaces
Variants
Actions
Navigation
games
Toolbox