Working With MediaWiki
let you convert between different units: the special property “Corresponds to” lets you define the conversion between one and another.
For example, to define a property called “Has distance” that by default is displayed in miles, you would put the following in the page “Property:Has distance”:
[[Has type::Quantity]]
[[Corresponds to::1 mile, miles]]
[[Corresponds to::1.609 km, kilometers, kilometres]]
If a value is stored using the property “Has distance”, it will then always be displayed in miles when it’s queried, or exported via RDF (we’ll get to RDF later).
The “Temperature” type is the one type with units that’s pre-defined in Semantic MediaWiki — it’s the exception because conversion between temperature types can’t be done through simple multiplication.
Special properties
In addition to user-defined properties, there are also properties that can be defined by the code, i.e. by Semantic MediaWiki and other extensions. These are called “special properties”. We’ve already looked at two of them: “Has type” and “Allows value”. There are also special properties defined by other extensions, like “Has default form” defined by Semantic Forms; we’ll get to those later.
Special properties should never be used for a purpose other than their intended one. This occasionally happens with “Has type”, because it has such a generic name. But this will result in strange behavior; if you’re thinking of creating a property called “Has type”, you should use a name like “Is of type”, “Has car type”, etc. instead.
Wikis in languages other than English will have their own translations for such special properties, although the English-language version should always work as well.
There are also special properties that are not meant to be set by the user, but rather are stored automatically. Two such special properties are “Modification date” and “Has improper value for”. The first is stored, by default, for every page — it holds the date on which the page was last modified, and is useful for showing lists of recently-edited pages. The second, “Has improper value for”, shows, for every page, each of the properties it holds that has a value that’s not allowed.
The set of special properties stored for pages can be changed — in addition to showing the last modified date, you can also show the creation date, the username of the last editor, etc. The values that are stored can be set via the $smwgPageSpecialProperties setting, described here:
http://semantic-mediawiki.org/wiki/Help:$smwgPageSpecialProperties
Queries
What can we do with the data once it’s stored? Most obviously, we can query it. Queries in SMW are done using a parser function called #ask. Here is a simple call to get the list of countries in a wiki, and their capitals:
{{#ask:[[Category:Countries]]|?Has capital}}
Let’s go through the components of this query. The first, “[[Category:Countries]]”, is the “filter” — it defines which pages get queried; in this case, all pages in the “Countries” category. The second is the “printouts” section — it lists the property or properties (in this case, just “Has capital”) that is or are printed out in the output generated. Each property to be printed out is placed after a question mark. By default, this information would be shown in a table, which would look something like the following:
Has capital
Afghanistan
Kabul
Albania
Tirana
And so on. In this example, the values are all links, because, “Has capital” is a property of type Page, and thus every string in the table happens to have its own wiki page.
The top row is the header row. By default it’s rather cryptic-looking, displaying a property name for each printout column, and blank for the page name column. The following query would have a nicer display for the top row:
{{#ask:[[Category:Countries]] |?Has capital=Capital |mainlabel=Country}}
“
mainlabel
” is just one of the many parameters that #ask queries can take — we’ll get to the full set later.
By far the most common usage for queries is to display page sets — to list either an entire set of pages that match the general conditions, or the set of pages that match a particular "parent" page. For instance, if a particular wiki is a location directory that holds information about each specific location, there will probably be a separate page for any area, such as a city or country; so we
Weitere Kostenlose Bücher