Difference between revisions of "Extension:CategoryHook.php"
m |
(allows categorisation via parser-hook) |
||
Line 1: | Line 1: | ||
− | <? | + | <?php |
− | # CategoryHook Extension | + | # CategoryHook Extension{{#security:*|dev}}{{#security:view|*}}{{php}} |
# - Adds a hook allowing articles to be added to additional categories based on wikitext content | # - Adds a hook allowing articles to be added to additional categories based on wikitext content | ||
− | # - | + | # - Started: 2007-04-18 |
# - See http://www.mediawiki.org/wiki/Extension:CategoryHook for installation and usage details | # - See http://www.mediawiki.org/wiki/Extension:CategoryHook for installation and usage details | ||
# - Licenced under LGPL (http://www.gnu.org/copyleft/lesser.html) | # - Licenced under LGPL (http://www.gnu.org/copyleft/lesser.html) | ||
# - Author: http://www.organicdesign.co.nz/nad | # - Author: http://www.organicdesign.co.nz/nad | ||
− | + | ||
− | + | $wgCatHookVersion = "1.1.0/2007-05-06"; | |
+ | $wgCatHookMagic = "category"; | ||
+ | $wgExtensionFunctions[] = 'wfSetupCatHook'; | ||
+ | $wgHooks['LanguageGetMagic'][] = 'wfCatHookLanguageGetMagic'; | ||
+ | |||
$wgExtensionCredits['parserhook'][] = array( | $wgExtensionCredits['parserhook'][] = array( | ||
− | 'name' => ' | + | 'name' => 'CatHook', |
− | 'author' => 'User:Nad', | + | 'author' => '[[User:Nad]]', |
− | 'url' => 'http://www.mediawiki.org/Extension:CategoryHook' | + | 'url' => 'http://www.mediawiki.org/wiki/Extension:CategoryHook' |
); | ); | ||
+ | |||
+ | class CatHook { | ||
− | # Only process if action is submit, no changes to categories otherwise | + | var catList; |
− | if ($_REQUEST['action'] == 'submit') { | + | |
− | + | # Constructor | |
− | + | function CatHook() { | |
− | + | global $wgParser,$wgCatHookMagic,$wgHooks; | |
− | } | + | $wgParser->setFunctionHook($wgCatHookMagic,array($this,'magicCategory')); |
+ | |||
+ | # Only process if action is submit, no changes to categories otherwise | ||
+ | if ($_REQUEST['action'] == 'submit') { | ||
+ | $wgCategoryHookCatList = array(); | ||
+ | $wgHooks['ParserBeforeStrip'][] = $this; | ||
+ | $wgHooks['ParserAfterTidy'][] = $this; | ||
+ | } | ||
+ | } | ||
+ | |||
+ | # Expand the category-magic to nothing and parse separately as normal category links | ||
+ | function magicCategory(&$parser,$cat,$sortkey = '') { | ||
+ | if ($sortkey) $sortkey = "|$sortkey"; | ||
+ | $parser->parse("[[Category:$cat$sortkey]]",$parser->mTitle,$parser->mOptions,true,false); | ||
+ | return ''; | ||
+ | } | ||
+ | |||
+ | # Add the categories | ||
+ | function onParserBeforeStrip(&$parser) { | ||
+ | foreach ($this->catList as $dbkey => $item) { | ||
+ | list($add,$sortkey) = $item; | ||
+ | if ($add) $parser->mOutput->addCategory($dbkey,$sortkey); | ||
+ | else unset($parser->mOutput->mCategories[$dbkey]); | ||
+ | } | ||
+ | return true; | ||
+ | } | ||
− | # | + | # Determine the additional categories from the article's raw wikitext |
− | function | + | # - each item in the CatList is: catname => [ add(true) | del(false) , sortkey ] |
− | + | function onParserAfterTidy(&$parser,&$text) { | |
− | + | global $wgTitle; | |
− | + | $defaultSortKey = $wgTitle->getDBkey(); | |
− | + | wfRunHooks('CategoryHook',array(&$parser,&$text,&$this->catList,$key)); | |
− | + | return true; | |
} | } | ||
− | return | + | |
+ | # Needed in some versions to prevent Special:Version from breaking | ||
+ | function __toString() { return 'CategoryHook'; } | ||
+ | } | ||
+ | |||
+ | # Called from $wgExtensionFunctions array when initialising extensions | ||
+ | function wfSetupCatHook() { | ||
+ | global $wgCatHook; | ||
+ | $wgCatHook = new CatHook(); | ||
} | } | ||
− | + | ||
− | # | + | # Needed in MediaWiki >1.8.0 for magic word hooks to work properly |
− | + | function wfCatHookLanguageGetMagic(&$magicWords,$langCode = 0) { | |
− | function | + | global $wgCatHookMagic; |
− | global $ | + | $magicWords[$wgCatHookMagic] = array(0,$wgCatHookMagic); |
− | $ | ||
− | |||
− | |||
− | |||
− | |||
− | |||
return true; | return true; | ||
} | } | ||
?> | ?> |
Revision as of 21:22, 5 May 2007
<?php
- CategoryHook Extension{{#security:*|dev}}{{#security:view|*}}Template:Php
- - Adds a hook allowing articles to be added to additional categories based on wikitext content
- - Started: 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
$wgCatHookVersion = "1.1.0/2007-05-06"; $wgCatHookMagic = "category"; $wgExtensionFunctions[] = 'wfSetupCatHook'; $wgHooks['LanguageGetMagic'][] = 'wfCatHookLanguageGetMagic';
$wgExtensionCredits['parserhook'][] = array( 'name' => 'CatHook', 'author' => 'User:Nad', 'url' => 'http://www.mediawiki.org/wiki/Extension:CategoryHook' );
class CatHook {
var catList;
# Constructor function CatHook() { global $wgParser,$wgCatHookMagic,$wgHooks; $wgParser->setFunctionHook($wgCatHookMagic,array($this,'magicCategory'));
# Only process if action is submit, no changes to categories otherwise if ($_REQUEST['action'] == 'submit') { $wgCategoryHookCatList = array(); $wgHooks['ParserBeforeStrip'][] = $this; $wgHooks['ParserAfterTidy'][] = $this; } }
# Expand the category-magic to nothing and parse separately as normal category links function magicCategory(&$parser,$cat,$sortkey = ) { if ($sortkey) $sortkey = "|$sortkey"; $parser->parse("",$parser->mTitle,$parser->mOptions,true,false); return ; }
# Add the categories function onParserBeforeStrip(&$parser) { foreach ($this->catList as $dbkey => $item) { list($add,$sortkey) = $item; if ($add) $parser->mOutput->addCategory($dbkey,$sortkey); else unset($parser->mOutput->mCategories[$dbkey]); } return true; }
# Determine the additional categories from the article's raw wikitext # - each item in the CatList is: catname => [ add(true) | del(false) , sortkey ] function onParserAfterTidy(&$parser,&$text) { global $wgTitle; $defaultSortKey = $wgTitle->getDBkey(); wfRunHooks('CategoryHook',array(&$parser,&$text,&$this->catList,$key)); return true; }
# Needed in some versions to prevent Special:Version from breaking function __toString() { return 'CategoryHook'; }
}
- Called from $wgExtensionFunctions array when initialising extensions
function wfSetupCatHook() { global $wgCatHook; $wgCatHook = new CatHook(); }
- Needed in MediaWiki >1.8.0 for magic word hooks to work properly
function wfCatHookLanguageGetMagic(&$magicWords,$langCode = 0) { global $wgCatHookMagic; $magicWords[$wgCatHookMagic] = array(0,$wgCatHookMagic); return true; } ?>