Extension:CategoryHook.php

From Organic Design wiki
Revision as of 11:07, 29 September 2007 by Nad (talk | contribs) (Undo revision 77344 by Nad (Talk))

<?php

  1. CategoryHook ExtensionTemplate:Php
Info.svg These are the MediaWiki extensions we're using and/or developing. Please refer to the information on the mediawiki.org wiki for installation and usage details. Extensions here which have no corresponding mediawiki article are either not ready for use or have been superseded. You can also browse our extension code in our local Subversion repository or our GitHub mirror.
  1. - Adds a hook allowing articles to be added to additional categories based on wikitext content
  2. - Started: 2007-04-18
  3. - See http://www.mediawiki.org/wiki/Extension:CategoryHook for installation and usage details
  4. - Licenced under LGPL (http://www.gnu.org/copyleft/lesser.html)
  5. - Author: http://www.organicdesign.co.nz/nad

if (!defined('MEDIAWIKI')) die('Not an entry point.');

define('CATEGORYHOOK_VERSION', '1.1.2, 2007-06-10');

$wgCatHookMagic = "category"; $wgExtensionFunctions[] = 'wfSetupCatHook'; $wgHooks['LanguageGetMagic'][] = 'wfCatHookLanguageGetMagic';

$wgExtensionCredits['parserhook'][] = array( 'name' => 'CategoryHook', 'author' => 'User:Nad', 'description' => 'Hook allows categorisation based on wikitext content', 'url' => 'http://www.mediawiki.org/wiki/Extension:CategoryHook', 'version' => CATEGORYHOOK_VERSION );

class CatHook {

var $catList = array();

# Constructor function CatHook() { global $wgParser,$wgCatHookMagic,$wgHooks; $wgParser->setFunctionHook($wgCatHookMagic,array($this,'magicCategory'),SFH_NO_HASH);

# 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,false,false); return ; }

# Run the hook to determine the categories to add/remove # - each item in the CatList is: catname => [ add(true) | del(false) , sortkey ] function onParserBeforeStrip(&$parser,&$text) {

               $title = $parser->mTitle;
               if (is_object($title) && is_object($parser->mOptions))
                       wfRunHooks('CategoryHook',array(&$parser,&$text,&$this->catList,$title->getDBkey()));
               return true;

}

# Add the categories function onParserAfterTidy(&$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; }

# Needed in some versions to prevent Special:Version from breaking function __toString() { return 'CategoryHook'; }

	}

  1. Called from $wgExtensionFunctions array when initialising extensions

function wfSetupCatHook() { global $wgCatHook; $wgCatHook = new CatHook(); }

  1. 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; } ?>