Difference between revisions of "Extension:CategoryHook.php"

From Organic Design wiki
m
Line 6: Line 6:
 
# - 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
#
+
#   {{#security:*|dev}}{{#security:view|*}}{{php}}
  
 
$wgExtensionCredits['parserhook'][] = array(
 
$wgExtensionCredits['parserhook'][] = array(
Line 14: Line 14:
 
         );
 
         );
  
$wgHooks['ParserBeforeStrip'][] = 'wfCategoryHookProcess';
+
# Only process if action is submit, no changes to categories otherwise
$wgHooks['ParserAfterTidy'][]  = 'wfCategoryHookAdd';
+
if ($_REQUEST['action'] == 'submit') {
 
+
        $wgCategoryHookCatList = array();
$wgCategoryHookCatList = array();
+
        $wgHooks['ParserBeforeStrip'][] = 'wfCategoryHookProcess';
 +
        $wgHooks['ParserAfterTidy'][]  = 'wfCategoryHookAdd';
 +
        }
  
 
# Determine the additional categories from the article's raw wikitext
 
# Determine the additional categories from the article's raw wikitext

Revision as of 09:02, 18 April 2007

<?

  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
  7. {{#security:*|dev}}{{#security:view|*}}Template:Php

$wgExtensionCredits['parserhook'][] = array(

       'name'   => 'CategoryHook',
       'author' => 'User:Nad',
       'url'    => 'http://www.mediawiki.org/Extension:CategoryHook',
       );
  1. Only process if action is submit, no changes to categories otherwise

if ($_REQUEST['action'] == 'submit') {

       $wgCategoryHookCatList = array();
       $wgHooks['ParserBeforeStrip'][] = 'wfCategoryHookProcess';
       $wgHooks['ParserAfterTidy'][]   = 'wfCategoryHookAdd';
       }
  1. Determine the additional categories from the article's raw wikitext
  2. - 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;
       }
  1. 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;
       }

?>