Displaying information
Template:Interlanguagelink: en
Queries in Semantic MediaWiki return lists of pages. The default result simply lists their page titles. Any additional information to fetch from each page, such as a page's property values or categories, is defined by printout statements.
There are different kinds of printout statements, but all of them can be recognized by the question mark ? that they start with in the query. The important difference between printout statements and query descriptions is that the former do not restrict the result set in any way, even if some printout has no values for a given page, an empty field will be printed, but the page is still part of the result.
Note, all the printout statements below are used within queries.
Printing property values
The most common form of printout statements are property printouts that display all the values assigned to a certain property. These are written simply as a question mark followed by the property name, e.g.
?Has population
This prints all the values for the «population» property of the query results.
It is possible to change the label for a printout:
?Has population = Number of inhabitants
This still prints the values for the «population» property, but with the modified label.
As mentioned above, some properties may not be defined on some pages, e.g. if something does not have any population. Property conditions with wildcards can be used to ensure that all elements in a query result have some value for a given property, if this is desired.
Printing categories
There are two ways to print category information: either SMW prints all categories assigned to some page, or SMW checks for one particular category. The first case is achieved by the printout
?Category
where «Category» is the name of the Category namespace in the local language. This printout will show all categories that are directly used on a result page. The other option is to ask for one particular category, such as
?Category:Actor
The result then will contain a column «Actor» that contains X for all pages that directly belong to that category, and is empty otherwise. Again, one can change the label using equality:
?Category:Actor = A
will merely display an «A» as the header of the result column which might be more sensible given that the entries in that column are very short. It is also possible to change the way in which this kind of category queries are formatted, as described below.
The main result column
All queries by default display the main list of result pages in the first column. In some cases, it can be useful to move that to another position. This is not relevant for Special:Ask, but can be quite useful in inline queries. A special printout statement is available for this purpose:
?
This single question mark addresses the «unlabelled result column» that shows the main result list. As before, different labels can be assigned with the equality symbol, e.g.
? = Results
If you like to hide or omit the main result column, you may use the parameter mainlabel=-
(see also here).
Display format
Many printout statements can be further customized by giving a printout format which can be given after a property name, separated by the symbol #. The available formats depend on the type of the printout and involved property.
Plain (unformatted) printouts
A general format that is supported by most types of printouts is the plain format (or empty format), available since SMW 1.4.3. Printouts with this format will avoid all forms of beautification or linking in their presentation, and return a plain value instead. This is particularly useful when results are further processed in templates or parser functions. To select the plain output format, a hyphen ("-") or simply nothing is used as a printout string, as in the following examples:
?Has population # - ?Has capital #
Both printouts select the plain format. For numerical properties like the population number, the plain format is a simple number string without commas to separate digits. For properties of type page, the plain output is simply the name of the page without any link.
It is also possible to get the result of the first result column as a plain (unformatted) printout:
?=#
File:OOjs UI icon lightbulb-yellow.svg <translate> Note:</translate> Prior to Semantic MediaWiki 3.0.0Released on an unknown date unknown versions of MediaWiki it was also possible to use ?#
which is no longer supported.
Note that plain printouts for properties with units (see below) will still include the unit as part of the result (A value that is a (physical) dimension consists of a number multiplied by a quantity (unit), so leaving out the unit would change the value). For this reason, two special printout formats have been added (available since 1.5.1):
Given a value of "12,345.6 km":
?My value # - gives "12345.6 km" (the number and the unit) ?My value # -n gives "12345.6" (just the number) ?My value # -u gives "km" (just the unit)
Formats for specific printout types
Custom units
For properties that support units, queries can thus determine which unit should be used for the output. To print the height in cm, e.g., one would use the following:
?Has height # cm
this assumes that the property height is aware of the unit «cm».
The unit can be omitted all together, returning just a number (#-n
), or queried on its own, returning just the unit (#-u
):
?Has height #-n ?Has height #-u
Display precision
For properties that support units, queries can thus determine which display precision should be used for the output. This is possible since Semantic MediaWiki 2.4.0.1
|?Has number #-p2
Display precision can also be combined with Custom units, e.g.
|?Has number #-n-p2
See the page on <sandbox.semantic-mediawiki.org> for further examples.
Date
For properties of type "Date", the following additional output formats are available:
ISO
- to obtain results in a technical format that conforms to the ISO 8601 standard.
?Has birthday#ISO
ISO-P
2- to obtain results in a format that conforms to the ISO 8601 standard in case of incomplete (partial) dates. Rationale: Sometimes, only a year or a year and month are specified for a date. At a later point in time, the date is completed with a month and day. With
ISO
, a date with only a year specified will be rendered completed with January first which is wrong for the reader. WithISO-P
incomplete dates would be rendered as follows:- 2022 → 2022
- April 2022 → 2022-04
- 1 April 2022 → 2022-04-01
MEDIAWIKI
- is available to use the MediaWiki's i18n methods for time and date formatting for the date range, typically between 1901-12-14 and 2038-01-19.
?Has birthday#MEDIAWIKI
LOCL
- is available to use predefined value formats for time and date formatting for the date range, typically between 1901-12-14 and 2038-01-19.
?Has birthday#LOCL
- Also the localised language string of a certain language may be enforced:
?Has birthday#LOCL@de
F
- is available to use freely definable value formats for time and date formatting for the date range, typically between 1901-12-14 and 2038-01-19.
?Has birthday#-F[F j, Y]
- Example
- Use
d
for day,m
for month,Y
for the year3
- Use
- Syntax
?Has birthday#-F[d-m-Y]
- Result
- 31-11-2019
Boolean
For properties of type "Boolean", one additional output format is available:
LOCL
- is available to use localised language strings.
?Is cool#LOCL
Also the localised language string of a certain language may be enforced:
?Is cool#LOCL@de
URL
For properties of type URL, one additional output format is available:
nowiki
- is available that escapes the URL string will to prevent MediaWiki from making a link of it. Thus it appears as a plain string in HTML and Wikitext alike.
?Has URL#nowiki
Images
In order to set the width-max, height-max and set the thumb on the image, while displaying it -- try the following:
?Has image # 100px;x100px;thumb
Other
Other datatypes may have different printout formats. See the types documentation for details.
For printouts of the form ?Category:Actor, the display format can be used to modify what SMW will display for cases where a page is (or is not) in the category. The following is an example:
?Category:Actor # an actor, not an actor
This will show the text «an actor» for all pages that are actors, and the text «not an actor» otherwise. This can, for example, also be used in combination with small images to display icons for certain categories.
Customizing printout labels
It is also possible to style labels using basic HTML and CSS. Example:
?Has population=<span class="label-highlight">Number of inhabitants</span>
File:OOjs UI icon lightbulb-yellow.svg <translate> Note:</translate> A custom class as shown in the above example needs to be defined in the wiki's CSS, too.
Besides, one can also add tooltips to the labels by using the parser function #info
. Example:
?Has population=Number of inhabitants {{#info: Text to be shown in a bubble. }}
Property chains and paths
The property chains and paths allows selecting pages with help of subqueries and property chain conditions. This is possible since Semantic MediaWiki 2.5.0.4
Inverse properties in printout statements
Starting with Semantic MediaWiki 2.4.0 it is possible to use inverse properties in printout statements.5. The sandbox wiki provides an example demonstrating this.6
Full-text search highlighting
Starting with Semantic MediaWiki 2.5.0 it is possible to highlight the search strings within the search results.7.
Property "Namespace" has a restricted application area and cannot be used as annotation property by a user.Property "Docinfo editor" (as page type) with input value "User:>Kghbln" contains invalid characters or is incomplete and therefore can cause unexpected results during a query or annotation process.