Working With MediaWiki
for each query result. It’s of course available for the “template” format, but it’s also available for many other formats, including “list”, “ol”, “ul”, “category” and various formats defined in the extensions Semantic Result Formats and Semantic Maps, like “calendar” and “maps”. (We’ll get to these extensions and formats in Chapter 18 .) To apply a template, you need to create a template on the wiki that takes in values and applies some formatting to them, then add “
|template= template name
” to the #ask query.
The template needs to have numbered parameters for each value, starting with 1, where the first parameter is passed in the page name. Here’s an example of the contents a template that could be used to display information about music albums, if the additional query printouts are for the artist, year and genre:
''{{{1|}}}'', {{{2|}}} ({{{3||}}}) - genre: {{{4|}}}
Here’s what a query that called that template could look like, if that template were named “Album display”:
{{#ask:[[Category:Albums]]|format=ul|template=Album display|?Has artist|?Released in year|?Has genre}}
Notice that the format here is “ul”, not “template” — that’s so each row will appear as a nice bulleted item. The output would be a series of lines that looked like this:
Computer World , Kraftwerk (1981) - genre: Electronic
Crescent , John Coltrane (1964) - genre: Jazz
Here, you can see that even simple formatting can serve to make the display of data much more legible and reader-friendly.
Concepts
Categories ( see here ) are the basic building block, within both MediaWiki and Semantic MediaWiki, for defining a collection of pages, but they’re not the only way to do it. Semantic MediaWiki also provides for "concepts", which are essentially the set of pages that correspond to a particular query: you can think of a concept as a query that can be referred to.
Let’s take a simple case: you have a category called “Cars”, and each page for a car has, among other fields, one indicating the car’s country of origin: Germany, England, etc. You could indicate the country using a category, e.g. “German cars” etc., but by now you know that using SMW’s semantic properties is the much better way to go. So you instead go with a property like "Has nationality". But what if you have a lot of queries on your wiki that refer to, say, Japanese cars, and you’re tired of typing “
[[Category:Cars]][[Has nationality::Japan]]
” every time, and you long for the simplicity of “
[[Category:Japanese cars]]
”? In that case, concepts are the answer.
Concepts are defined within their own namespace, which in English is "Concept:", and they use the parser function #concept. So you could create a page called "Concept:Japanese cars", which just contains the following text:
{{#concept:[[Category:Cars]][[Has nationality::Japan]]}}
A #concept function call looks just like a call to #ask, but with no display-related parameters: the call contains only the filtering for the set of pages. After it’s defined, you could add "[[Concept:Japanese cars]]" into any query, and it would work just like a category tag. For example, you could have the following query:
{{#ask:[[Concept:Japanese cars]][[Has layout::Front-wheel drive]]|?Has size |?Has manufacturer}}
The concept page itself will list all the pages it "contains", i.e. those that match its query, just like a category page does; and the display of a concept page mimics that of a category page (see here ).
Concepts are also useful with at least two other extensions: Semantic Forms and Semantic Watchlist. With Semantic Forms, you can have either autocompletion or a dropdown in a form based on the pages in a concept, just as you could with the pages in a category. And with Semantic Watchlist, you can watch for changes in the set of pages contained within a concept. Both extensions are covered later in the book.
Storing compound data
Not all data can be stored using simple properties. Specifically, “two-dimensional” data — data that is usually displayed in a table — cannot be stored using regular Semantic MediaWiki properties.
Let’s take as an example the set of ingredients for a recipe — which you can think of as a table of data, with each row for an ingredient corresponding to a row in a table. One of the recipe’s rows calls for 3 tomatoes. How would you store that information? You could add the tag “
[[Has
Weitere Kostenlose Bücher