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 function (the one at the end called wfCategoryHookProcess) into which you can add such auto-categorisation rules. One example has been added which adds articles to Category:Articles containing trees if they have any parser functions in their content.

Adding new rules to the wfCategoryHookProcess function
The function should add an entry to the $wgCategoryHookCatList global variable using the category name it wants to change as the key. The value is a two element array, the first item being a boolean value which is set to true to add the category, or false to remove it. The second element is the sort-key which defaults to the name of the article being categorised.

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/Extension:CategoryHook',        ); if ($_REQUEST['action'] == 'submit') { $wgCategoryHookCatList = array; $wgHooks['ParserBeforeStrip'][] = 'wfCategoryHookProcess'; $wgHooks['ParserAfterTidy'][]  = 'wfCategoryHookAdd'; } function wfCategoryHookAdd(&$parser,&$text) { global $wgCategoryHookCatList; foreach ($wgCategoryHookCatList as $dbkey => $item) { list($add,$sortkey) = $item; if ($add) $parser->mOutput->addCategory($dbkey,$sortkey); else unset($parser->mOutput->mCategories[$dbkey]); }       return true; }
 * 1) CategoryHook Extension
 * 2) - Adds a hook allowing articles to be added to additional categories based on wikitext content
 * 3) - Version 0.1 (2007-04-18)
 * 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) Add the categories

function wfCategoryHookProcess(&$parser,&$text) { global $wgCategoryHookCatList,$wgTitle; $defaultSortKey = $wgTitle->getDBkey; # Example: Catgegorise articles containing "{{#tree:" into Category:Articles containing trees $wgCategoryHookCatList['Articles_containing_trees'] = array(preg_match('/\\{\\{#tree:/i',$text),$defaultSortKey); return true; } ?>
 * 1) Determine the additional categories from the article's raw wikitext
 * 2) - each item in the CatList is: catname => [ add(true) | del(false), sortkey ]