Extension:CategoryHook

From MediaWiki.org
Jump to navigation Jump to search
MediaWiki extensions manual
Crystal Clear action run.svg
CategoryHook
Release status: unmaintained
Implementation Hook, Parser function
Description Adds a hook called "CategoryHook" which allows rules-based categorisation, adds a parser-function for categorisation and adds a parser-function for checking if the current title is a member of a given category.
Author(s) Nadtalk
Latest version 0.2.0 (2007-10-09)
MediaWiki 1.6.0+
License GPL
Download CategoryHook.php
Hooks used
ParserBeforeStrip
ParserAfterTidy
Translate the CategoryHook extension if it is available at translatewiki.net
Check usage and version matrix.

A big part of wiki content maintenance is keeping articles in structured categories, but often there'd be many aspects of an article we'd like to categorise by, such as whether or not they contain various kinds of content, or whether their title fits a certain format etc. Many sites have general caretaking bots for this, but a more dynamic and efficient solution for auto-categorisation is to update the list just before the databases category links are updated when an article is saved.

Category Hook[edit]

This extension sets up a new hook called CategoryHook to which you can add auto-categorisation rules to LocalSettings.php (after including CategoryHook.php--see #Installation). Following is an example which adds articles to Category:Articles containing trees if they have any {{#tree:...}} parser functions in their content.

$wgHooks['CategoryHook'][] = 'wfCategoriseTrees';

function wfCategoriseTrees(&$parser,&$text,&$categories,$sortkey) {
    $categories['Articles containing trees']
        = array(preg_match('/\\{\\{#tree:/i',$text),$sortkey);
    return true;
    }

The hook function has four parameters, the first two are the parser object and the article's wikitext content. The third is an array of the categories which will be added or removed, and the forth is the default sort key to use if adding a category (which is just the title of the current article).

The array which is passed is a list of all the categories which need to be added or removed, and each of these is described by a two element array containing a boolean for add (true) or delete (false), and the sort-key to use if adding.

Categorisation parser-function[edit]

The extension also adds a parser-function which allows article categorisation to be done using curly-brace syntax instead of a square-bracket link. This is useful for articles which do not use the wiki-parser for their output, but still have their templates expanded, such as CSS articles. The following example categorises the article into Category:Foo with sort-key "Bar".

{{Category:Foo|Bar}}

Category conditional parser-function[edit]

A second parser-function called #ifcat was added in version 0.2.0 (2007-10-09) which allows content to be rendered conditionally based on whether or not the current title is a member of a specified category. The following example uses the new parser-function to render a help-related template if the current article is a member of the Help category, or renders an advertising template if not.

{{#ifcat:Help|{{help-article-message}}|{{corporate-propaganda}}}}

Installation[edit]

Save the code from OrganicDesign:Extension:CategoryHook.php as a file in your extensions directory and append it to your LocalSettings.php file as usual. You might want to check to see if a later version is available.

Change log[edit]

  • Version 0.2.0 (2007-10-09): Added #ifcat parser-function