Template:GetCategory
From Pestinfo-Wiki
Version of February 2012 - old version see Template:GetCategoryOLD
The template takes one parameter, the name of a page (e.g. the page name of a pest, disease or weed). It determines the first higher category of this page name and returns it, if any exist. For crops a different procedure is used, see Template:GetCategory2.
The template uses the following procedure:
- It checks for higher categories of the page name using "{{#ask:[[<page name>]]|?Category=|link=none|format=list}}". This will only return a category if <page name> is a normal page and not a category itself. However, <page name> may be redirected to a normal page. The result may be several categories, separated by commas.
- If several categories are found, only the first category is used. In this case it is checked if this is a valid pagename.
- The value may be blank, if no (valid) category is found.
Note:
The result of the above category query has changed repeatedly since 2010. For SMW Version 1.6.2 the query
{{#ask:[[Helicoverpa zea]]|?Category=|link=none|format=list}}
gave the result "Category:Helicoverpa (genus)". Since Version 1.7 it gives "Helicoverpa zea (Category:Helicoverpa (genus))".
The template has the following structure:
1. Define the variable "CategoryTemp" as the result of the query for categories of the pagename and the variable "Category" as an empty string: {{#vardefine:CategoryTemp|{{#ask:[[{{{1}}}]]|?Category=|link=none|format=list}} }}{{#vardefine:Category|}} 2. If the query result is not empty.... {{#if:{{#var:CategoryTemp}}| 3. define the variable "Position1" which is used to determine if a category was found {{#vardefine:Position1| {{#pos:{{#var:CategoryTemp}}|(Category:}} }} 4. A category is found... {{#if:{{#var:Position1}}| 5. define the variable "Position2" which the position of the next comma {{#vardefine:Position2| {{#pos:{{#var:CategoryTemp}}|,|{{#var:Position1}}}} }} 5a. if a comma was found define variable "Category" as the text between "(Category:" and the comma {{#if:{{#var:Position2}}| {{#vardefine:Category| {{#sub:{{#var:CategoryTemp}}|{{#expr:{{#var:Position1}}+10}}| {{#expr:{{#var:Position2}}-{{#var:Position1}}-10}}}} }} 5b. if a comma was not found define variable "Category" as the remaining text minus the last ")" | {{#vardefine:Category| {{#sub:{{#var:CategoryTemp}}|{{#expr:{{#var:Position1}}+10}}| {{#expr:{{#len:{{#var:CategoryTemp}}}}-{{#var:Position1}}-11}}}} }} }} (end of "{{#if:{{#var:Position2}} ...") }} (end of "{{#if:{{#var:Position1}} ...") }} (end of "{{#if:{{#var:CategoryTemp}} ...") 6. return the variable "Category" {{#var:Category}}