Extension:CategoryHook.php
From Organic Design wiki
<?
- CategoryHook Extension
- - Adds a hook allowing articles to be added to additional categories based on wikitext content
- - Version 0.1 (2007-04-18)
- - See http://www.mediawiki.org/wiki/Extension:CategoryHook for installation and usage details
- - Licenced under LGPL (http://www.gnu.org/copyleft/lesser.html)
- - Author: http://www.organicdesign.co.nz/nad
$wgExtensionCredits['parserhook'][] = array(
'name' => 'CategoryHook',
'author' => 'User:Nad',
'url' => 'http://www.mediawiki.org/Extension:CategoryHook',
);
$wgHooks['ParserBeforeStrip'][] = 'wfCategoryHookProcess'; $wgHooks['ParserAfterTidy'][] = 'wfCategoryHookAdd';
$wgCategoryHookCatList = array();
- Determine the additional categories from the article's raw wikitext
- - each item in the CatList is: catname => [ add(true) | del(false) , sortkey ]
function wfCategoryHookProcess(&$parser,&$text) {
global $wgCategoryHookCatList,$wgTitle;
$defaultSortKey = $wgTitle->getDBkey();
# Articles containing trees
$wgCategoryHookCatList['Articles_containing_trees']
= array(preg_match('/\\{\\{#tree:/i',$text),$defaultSortKey);
return true;
}
- Add the categories
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;
}
?>



