Difference between revisions of "Extension:CategoryHook.php"

From Organic Design wiki
(redo autocategorisation extension)
 
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(
'name'  => 'CategoryHook',
+
        'name'  => 'CategoryHook',
'author' => 'User:Nad',
+
        'author' => 'User:Nad',
'url'    => 'http://www.mediawiki.org/Extension:CategoryHook',
+
        'url'    => 'http://www.mediawiki.org/Extension:CategoryHook',
);
+
        );
  
$wgHooks['ArticleAfterFetchContent'][] = 'wfCategoryHookProcess';
+
$wgHooks['ParserBeforeStrip'][] = 'wfCategoryHookProcess';
$wgHooks['ParserAfterTidy'][]         = 'wfCategoryHookAdd';
+
$wgHooks['ParserAfterTidy'][]   = 'wfCategoryHookAdd';
  
 
$wgCategoryHookCatList = array();
 
$wgCategoryHookCatList = array();
  
 
# Determine the additional categories from the article's raw wikitext
 
# Determine the additional categories from the article's raw wikitext
function wfCategoryHookProcess(&$article,&$text) {
+
# - each item in the CatList is: catname => [ add(true) | del(false) , sortkey ]
global $wgCategoryHookCatList;
+
function wfCategoryHookProcess(&$parser,&$text) {
$defaultSortKey = $article->mTitle->getDBkey();
+
        global $wgCategoryHookCatList,$wgTitle;
 +
        $defaultSortKey = $wgTitle->getDBkey();
  
# Articles containing trees
+
        # Articles containing trees
if (preg_match('/\\{\\{#tree:/i',$text)) $wgCategoryHookCatList['Articles_containing_trees'] = $defaultSortKey;
+
        $wgCategoryHookCatList['Articles_containing_trees']
 +
                = array(preg_match('/\\{\\{#tree:/i',$text),$defaultSortKey);
  
return true;
+
        return true;
}
+
        }
  
 
# Add the categories
 
# Add the categories
function wfCategoryHookAdd(&$parser,&$text,&$strip_state) {
+
function wfCategoryHookAdd(&$parser,&$text) {
global $wgCategoryHookCatList,$wgProfiler;
+
        global $wgCategoryHookCatList;
$updating = $wgProfiler->getCurrentSection === 'Article::editUpdates';
+
        foreach ($wgCategoryHookCatList as $dbkey => $item) {
foreach ($wgCategoryHookCatList as $dbkey => $sortkey) $parser->mOutput->addCategory($dbkey,$sortkey);
+
                list($add,$sortkey) = $item;
return true;
+
                if ($add) $parser->mOutput->addCategory($dbkey,$sortkey);
}
+
                else unset($parser->mOutput->mCategories[$dbkey]);
 +
                }
 +
        return true;
 +
        }
 
?>
 
?>

Revision as of 08:56, 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

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

       'name'   => 'CategoryHook',
       'author' => 'User:Nad',
       'url'    => 'http://www.mediawiki.org/Extension:CategoryHook',
       );

$wgHooks['ParserBeforeStrip'][] = 'wfCategoryHookProcess'; $wgHooks['ParserAfterTidy'][] = 'wfCategoryHookAdd';

$wgCategoryHookCatList = array();

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

?>