Extension:CategoryHook

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.

This extension sets up a new hook called CategoryHook to which you can add auto-categorisation rules. Following is an example which adds articles to Category:Articles containing trees if they have any 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.

Installation
Save the following code as a file in your extensions directory and append it to your LocalSettings.php file as usual. You might want to check OrganicDesign:Extension:CategoryHook to see if a later version is available. <? $wgExtensionCredits['parserhook'][] = array(       'name'   => 'CategoryHook',        'author' => 'User:Nad',        'url'    => 'http://www.mediawiki.org/wiki/Extension:CategoryHook',        ); if ($_REQUEST['action'] == 'submit') { $wgCategoryHookCatList = array; $wgHooks['ParserBeforeStrip'][] = 'wfCategoryHookProcess'; $wgHooks['ParserAfterTidy'][]  = 'wfCategoryHookAdd'; } function wfCategoryHookProcess(&$parser,&$text) { global $wgCategoryHookCatList,$wgTitle; $key = $wgTitle->getDBkey; wfRunHooks('CategoryHook',array(&$parser,&$text,&$wgCategoryHookCatList,$key)); return true; }
 * 1) CategoryHook Extension
 * 2) - Adds a hook allowing articles to be added to additional categories based on wikitext content
 * 3) - Version 0.2 (2007-04-26)
 * 4) - See http://www.mediawiki.org/wiki/Extension:CategoryHook for installation and usage details
 * 5) - Licenced under LGPL (http://www.gnu.org/copyleft/lesser.html)
 * 6) - Author: http://www.organicdesign.co.nz/nad
 * 1) Only process if action is submit, no changes to categories otherwise
 * 1) Determine the additional categories from the article's raw wikitext
 * 2) - each item in the CatList is: catname => [ add(true) | del(false), sortkey ]

function wfCategoryHookAdd(&$parser,&$text) { global $wgCategoryHookCatList; foreach ($wgCategoryHookCatList as $dbkey => $item) { $dbkey = str_replace(' ','_',$dbkey); list($add,$sortkey) = $item; if ($add) $parser->mOutput->addCategory($dbkey,$sortkey); else unset($parser->mOutput->mCategories[$dbkey]); }       return true; }
 * 1) Add the categories

?>