Difference between revisions of "Extension:CategoryHook.php"

From Organic Design wiki
m (const version)
m
 
(15 intermediate revisions by 2 users not shown)
Line 1: Line 1:
 
<?php
 
<?php
# CategoryHook Extension{{#security:*|dev}}{{#security:view|*}}{{php}}{{Category:Extensions}}
+
# CategoryHook Extension{{php}}{{Category:Extensions|CategoryHook}}
 
# - 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
 
# - Started: 2007-04-18
Line 7: Line 7:
 
# - Author: http://www.organicdesign.co.nz/nad
 
# - Author: http://www.organicdesign.co.nz/nad
 
   
 
   
define('CATEGORYHOOK_VERSION', '1.1.1, 2007-05-08');
+
if (!defined('MEDIAWIKI')) die('Not an entry point.');
 +
 +
define('CATEGORYHOOK_VERSION', '0.2.0, 2007-10-09');
 +
 
 
$wgCatHookMagic                = "category";
 
$wgCatHookMagic                = "category";
 +
$wgCatHookIfMagic              = "ifcat";
 
$wgExtensionFunctions[]        = 'wfSetupCatHook';
 
$wgExtensionFunctions[]        = 'wfSetupCatHook';
 
$wgHooks['LanguageGetMagic'][] = 'wfCatHookLanguageGetMagic';
 
$wgHooks['LanguageGetMagic'][] = 'wfCatHookLanguageGetMagic';
 
   
 
   
$wgExtensionCredits['other'][] = array(
+
$wgExtensionCredits['parserhook'][] = array(
 
'name'        => 'CategoryHook',
 
'name'        => 'CategoryHook',
'author'      => '[http://www.organicdesign.co.nz/nad User:Nad',
+
'author'      => '[http://www.organicdesign.co.nz/nad User:Nad]',
'description' => 'Hook allows categorisation based on wikitext content.',
+
'description' => 'Adds a hook called "CategoryHook" which allows rules-based categorisation, adds a parser-function for categorisation and adds a parser-function for checking if the current title is a member of a given category.',
'url'        => 'http://www.organicdesign.co.nz/Template:Extension',
+
'url'        => 'http://www.mediawiki.org/wiki/Extension:CategoryHook',
 
'version'    => CATEGORYHOOK_VERSION
 
'version'    => CATEGORYHOOK_VERSION
 
);
 
);
Line 26: Line 30:
 
# Constructor
 
# Constructor
 
function CatHook() {
 
function CatHook() {
global $wgParser,$wgCatHookMagic,$wgHooks;
+
global $wgParser,$wgCatHookMagic,$wgCatHookIfMagic,$wgHooks;
 
$wgParser->setFunctionHook($wgCatHookMagic,array($this,'magicCategory'),SFH_NO_HASH);
 
$wgParser->setFunctionHook($wgCatHookMagic,array($this,'magicCategory'),SFH_NO_HASH);
 +
$wgParser->setFunctionHook($wgCatHookIfMagic,array($this,'magicIf'));
  
 
# Only process if action is submit, no changes to categories otherwise
 
# Only process if action is submit, no changes to categories otherwise
Line 42: Line 47:
 
$parser->parse("[[Category:$cat$sortkey]]",$parser->mTitle,$parser->mOptions,false,false);
 
$parser->parse("[[Category:$cat$sortkey]]",$parser->mTitle,$parser->mOptions,false,false);
 
return '';
 
return '';
 +
}
 +
 +
# Expand the #ifcat condition
 +
function magicIf(&$parser,$cat,$then = '',$else = '') {
 +
global $wgTitle;
 +
if (!is_object($wgTitle)) return $else;
 +
$id      = $wgTitle->getArticleID();
 +
$db      = &wfGetDB(DB_SLAVE);
 +
$cl      = $db->tableName('categorylinks');
 +
$result  = $db->query("SELECT 0 FROM $cl WHERE cl_from = $id AND cl_to = '$cat'");
 +
if ($result instanceof ResultWrapper) $result = $result->result;
 +
return is_array($db->fetchRow($result)) ? $then : $else;
 
}
 
}
 
   
 
   
Line 75: Line 92:
 
# Needed in MediaWiki >1.8.0 for magic word hooks to work properly
 
# Needed in MediaWiki >1.8.0 for magic word hooks to work properly
 
function wfCatHookLanguageGetMagic(&$magicWords,$langCode = 0) {
 
function wfCatHookLanguageGetMagic(&$magicWords,$langCode = 0) {
global $wgCatHookMagic;
+
global $wgCatHookMagic,$wgCatHookIfMagic;
$magicWords[$wgCatHookMagic] = array(0,$wgCatHookMagic);
+
$magicWords[$wgCatHookMagic]   = array(0,$wgCatHookMagic);
 +
$magicWords[$wgCatHookIfMagic] = array(0,$wgCatHookIfMagic);
 
return true;
 
return true;
 
}
 
}
?>
 

Latest revision as of 21:27, 6 November 2007

<?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', '0.2.0, 2007-10-09');

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

$wgExtensionCredits['parserhook'][] = array( 'name' => 'CategoryHook', 'author' => 'User:Nad', 'description' => 'Adds a hook called "CategoryHook" which allows rules-based categorisation, adds a parser-function for categorisation and adds a parser-function for checking if the current title is a member of a given category.', 'url' => 'http://www.mediawiki.org/wiki/Extension:CategoryHook', 'version' => CATEGORYHOOK_VERSION );

class CatHook {

var $catList = array();

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

# 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 ; }

# Expand the #ifcat condition function magicIf(&$parser,$cat,$then = ,$else = ) { global $wgTitle; if (!is_object($wgTitle)) return $else; $id = $wgTitle->getArticleID(); $db = &wfGetDB(DB_SLAVE); $cl = $db->tableName('categorylinks'); $result = $db->query("SELECT 0 FROM $cl WHERE cl_from = $id AND cl_to = '$cat'"); if ($result instanceof ResultWrapper) $result = $result->result; return is_array($db->fetchRow($result)) ? $then : $else; }

# 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,$wgCatHookIfMagic; $magicWords[$wgCatHookMagic] = array(0,$wgCatHookMagic); $magicWords[$wgCatHookIfMagic] = array(0,$wgCatHookIfMagic); return true; }