2006-07-26 17:12:30 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
2008-02-04 09:22:12 +00:00
|
|
|
* Setup and Hooks for the CategoryTree extension, an AJAX based gadget
|
2006-07-26 17:12:30 +00:00
|
|
|
* to display the category structure of a wiki
|
|
|
|
*
|
2007-01-20 15:10:35 +00:00
|
|
|
* @addtogroup Extensions
|
2007-03-13 11:39:12 +00:00
|
|
|
* @author Daniel Kinzler, brightbyte.de
|
|
|
|
* @copyright © 2006-2007 Daniel Kinzler
|
2006-07-26 17:12:30 +00:00
|
|
|
* @licence GNU General Public Licence 2.0 or later
|
|
|
|
*/
|
2008-02-04 09:22:12 +00:00
|
|
|
|
2006-07-26 17:12:30 +00:00
|
|
|
if( !defined( 'MEDIAWIKI' ) ) {
|
|
|
|
echo( "This file is an extension to the MediaWiki software and cannot be used standalone.\n" );
|
|
|
|
die( 1 );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Constants for use with efCategoryTreeRenderChildren,
|
|
|
|
* defining what should be shown in the tree
|
|
|
|
*/
|
|
|
|
define('CT_MODE_CATEGORIES', 0);
|
|
|
|
define('CT_MODE_PAGES', 10);
|
|
|
|
define('CT_MODE_ALL', 20);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Options:
|
|
|
|
*
|
|
|
|
* $wgCategoryTreeMaxChildren - maximum number of children shown in a tree node. Default is 200
|
|
|
|
* $wgCategoryTreeAllowTag - enable <categorytree> tag. Default is true.
|
2006-07-28 16:07:55 +00:00
|
|
|
* $wgCategoryTreeDynamicTag - loads the first level of the tree in a <categorytag> dynamically.
|
|
|
|
* This way, the cache does not need to be disabled. Default is false.
|
2006-07-26 17:12:30 +00:00
|
|
|
* $wgCategoryTreeDisableCache - disabled the parser cache for pages with a <categorytree> tag. Default is true.
|
2006-08-23 19:23:17 +00:00
|
|
|
* $wgCategoryTreeUseCache - enable HTTP cache for anon users. Default is false.
|
2006-09-03 11:26:57 +00:00
|
|
|
* $wgCategoryTreeUnifiedView - use unified view on category pages, instead of "tree" or "traditional list". Default is true.
|
2007-03-13 22:24:38 +00:00
|
|
|
* $wgCategoryTreeOmitNamespace - never show namespace prefix. Default is false
|
2007-03-30 13:28:59 +00:00
|
|
|
* $wgCategoryTreeMaxDepth - maximum value for depth argument; An array that maps mode values to
|
2008-02-04 09:22:12 +00:00
|
|
|
* the maximum depth acceptable for the depth option.
|
2007-03-30 13:28:59 +00:00
|
|
|
* Per default, the "categories" mode has a max depth of 2,
|
|
|
|
* all other modes have a max depth of 1.
|
2008-02-04 09:22:12 +00:00
|
|
|
*/
|
2007-03-13 22:24:38 +00:00
|
|
|
|
2006-09-02 21:54:21 +00:00
|
|
|
$wgCategoryTreeMaxChildren = 200;
|
|
|
|
$wgCategoryTreeAllowTag = true;
|
|
|
|
$wgCategoryTreeDisableCache = true;
|
|
|
|
$wgCategoryTreeDynamicTag = false;
|
|
|
|
$wgCategoryTreeHTTPCache = false;
|
2006-09-03 11:26:57 +00:00
|
|
|
$wgCategoryTreeUnifiedView = true;
|
2006-11-22 21:48:54 +00:00
|
|
|
$wgCategoryTreeOmitNamespace = false;
|
2007-03-30 13:28:59 +00:00
|
|
|
$wgCategoryTreeMaxDepth = array(CT_MODE_PAGES => 1, CT_MODE_ALL => 1, CT_MODE_CATEGORIES => 2);
|
2007-04-06 10:30:43 +00:00
|
|
|
$wgCategoryTreeExtPath = '/extensions/CategoryTree';
|
2007-11-14 17:23:26 +00:00
|
|
|
$wgCategoryTreeDefaultMode = CT_MODE_CATEGORIES;
|
|
|
|
$wgCategoryTreeCategoryPageMode = CT_MODE_CATEGORIES;
|
2008-02-27 10:37:52 +00:00
|
|
|
$wgCategoryTreeVersion = '1'; #NOTE: bump this when you change the CSS or JS files!
|
2006-07-26 17:12:30 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Register extension setup hook and credits
|
|
|
|
*/
|
|
|
|
$wgExtensionFunctions[] = 'efCategoryTree';
|
2008-02-04 09:22:12 +00:00
|
|
|
$wgExtensionCredits['specialpage'][] = array(
|
|
|
|
'name' => 'CategoryTree',
|
2008-02-27 10:37:52 +00:00
|
|
|
'version' => preg_replace('/^.* (\d\d\d\d-\d\d-\d\d) .*$/', '\1', '$LastChangedDate$'), #just the date of the last change
|
2008-02-04 09:22:12 +00:00
|
|
|
'author' => 'Daniel Kinzler',
|
2007-02-27 22:39:04 +00:00
|
|
|
'url' => 'http://www.mediawiki.org/wiki/Extension:CategoryTree',
|
2007-01-07 13:57:35 +00:00
|
|
|
'description' => 'AJAX based gadget to display the category structure of a wiki',
|
2008-02-03 17:57:14 +00:00
|
|
|
'descriptionmsg' => 'categorytree-desc',
|
2006-08-24 17:12:13 +00:00
|
|
|
);
|
2008-02-04 09:22:12 +00:00
|
|
|
$wgExtensionCredits['parserhook'][] = array(
|
|
|
|
'name' => 'CategoryTree',
|
2008-02-27 10:37:52 +00:00
|
|
|
'version' => preg_replace('/^.* (\d\d\d\d-\d\d-\d\d) .*$/', '\1', '$LastChangedDate$'), #just the date of the last change
|
2008-02-04 09:22:12 +00:00
|
|
|
'author' => 'Daniel Kinzler',
|
2007-02-27 22:39:04 +00:00
|
|
|
'url' => 'http://www.mediawiki.org/wiki/Extension:CategoryTree',
|
2007-01-07 13:57:35 +00:00
|
|
|
'description' => 'AJAX based gadget to display the category structure of a wiki',
|
2008-02-04 10:25:48 +00:00
|
|
|
'descriptionmsg' => 'categorytree-desc',
|
2006-08-24 17:12:13 +00:00
|
|
|
);
|
2006-07-26 17:12:30 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Register the special page
|
|
|
|
*/
|
2008-02-04 10:25:48 +00:00
|
|
|
$dir = dirname(__FILE__) . '/';
|
|
|
|
$wgExtensionMessagesFiles['CategoryTree'] = $dir . 'CategoryTree.i18n.php';
|
|
|
|
$wgAutoloadClasses['CategoryTreePage'] = $dir . 'CategoryTreePage.php';
|
|
|
|
$wgAutoloadClasses['CategoryTree'] = $dir . 'CategoryTreeFunctions.php';
|
|
|
|
$wgAutoloadClasses['CategoryTreeCategoryPage'] = $dir . 'CategoryPageSubclass.php';
|
2006-08-24 17:12:13 +00:00
|
|
|
$wgSpecialPages['CategoryTree'] = 'CategoryTreePage';
|
|
|
|
#$wgHooks['SkinTemplateTabs'][] = 'efCategoryTreeInstallTabs';
|
|
|
|
$wgHooks['OutputPageParserOutput'][] = 'efCategoryTreeParserOutput';
|
|
|
|
$wgHooks['ArticleFromTitle'][] = 'efCategoryTreeArticleFromTitle';
|
2007-09-13 10:23:45 +00:00
|
|
|
$wgHooks['LanguageGetMagic'][] = 'efCategoryTreeGetMagic';
|
2006-07-26 17:12:30 +00:00
|
|
|
/**
|
|
|
|
* register Ajax function
|
|
|
|
*/
|
|
|
|
$wgAjaxExportList[] = 'efCategoryTreeAjaxWrapper';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Hook it up
|
|
|
|
*/
|
|
|
|
function efCategoryTree() {
|
2008-03-02 13:37:26 +00:00
|
|
|
global $wgUseAjax, $wgHooks;
|
2006-09-01 12:53:07 +00:00
|
|
|
|
|
|
|
# Abort if AJAX is not enabled
|
|
|
|
if ( !$wgUseAjax ) {
|
|
|
|
wfDebug( 'efCategoryTree: $wgUseAjax is not enabled, aborting extension setup.' );
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2008-03-02 13:37:26 +00:00
|
|
|
if ( defined( 'MW_SUPPORTS_PARSERFIRSTCALLINIT' ) ) {
|
|
|
|
$wgHooks['ParserFirstCallInit'][] = 'efCategoryTreeSetHooks';
|
|
|
|
} else {
|
|
|
|
efCategoryTreeSetHooks();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
function efCategoryTreeSetHooks() {
|
|
|
|
global $wgParser, $wgCategoryTreeAllowTag;
|
2007-09-13 10:23:45 +00:00
|
|
|
if ( $wgCategoryTreeAllowTag ) {
|
|
|
|
$wgParser->setHook( 'categorytree' , 'efCategoryTreeParserHook' );
|
|
|
|
$wgParser->setFunctionHook( 'categorytree' , 'efCategoryTreeParserFunction' );
|
|
|
|
}
|
2008-03-02 13:37:26 +00:00
|
|
|
return true;
|
2007-09-13 10:23:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Hook magic word
|
|
|
|
*/
|
|
|
|
function efCategoryTreeGetMagic( &$magicWords, $langCode ) {
|
|
|
|
global $wgUseAjax, $wgCategoryTreeAllowTag;
|
|
|
|
|
|
|
|
if ( $wgUseAjax && $wgCategoryTreeAllowTag ) {
|
|
|
|
//XXX: should we allow a local alias?
|
|
|
|
$magicWords['categorytree'] = array( 0, 'categorytree' );
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
2006-07-26 17:12:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Entry point for Ajax, registered in $wgAjaxExportList.
|
2006-08-24 17:12:13 +00:00
|
|
|
* This loads CategoryTreeFunctions.php and calls CategoryTree::ajax()
|
2006-07-26 17:12:30 +00:00
|
|
|
*/
|
2007-04-30 21:18:56 +00:00
|
|
|
function efCategoryTreeAjaxWrapper( $category, $mode ) {
|
2006-08-29 15:49:23 +00:00
|
|
|
global $wgCategoryTreeHTTPCache, $wgSquidMaxAge, $wgUseSquid;
|
2008-02-04 09:22:12 +00:00
|
|
|
|
2006-08-29 15:49:23 +00:00
|
|
|
$ct = new CategoryTree;
|
2007-03-30 13:43:37 +00:00
|
|
|
$response = $ct->ajax( $category, $mode ); //FIXME: would need to pass on depth parameter here.
|
2008-02-04 09:22:12 +00:00
|
|
|
|
2006-08-23 19:23:17 +00:00
|
|
|
if ( $wgCategoryTreeHTTPCache && $wgSquidMaxAge && $wgUseSquid ) {
|
2006-08-29 15:49:23 +00:00
|
|
|
$response->setCacheDuration( $wgSquidMaxAge );
|
|
|
|
$response->setVary( 'Accept-Encoding, Cookie' ); #cache for anons only
|
2006-08-23 19:23:17 +00:00
|
|
|
#TODO: purge the squid cache when a category page is invalidated
|
|
|
|
}
|
2006-08-24 17:12:13 +00:00
|
|
|
|
2006-08-29 15:49:23 +00:00
|
|
|
return $response;
|
2006-07-26 17:12:30 +00:00
|
|
|
}
|
|
|
|
|
2007-03-13 22:24:38 +00:00
|
|
|
/**
|
|
|
|
* Internal function to cap depth
|
|
|
|
*/
|
|
|
|
|
2007-03-30 13:28:59 +00:00
|
|
|
function efCategoryTreeCapDepth( $mode, $depth ) {
|
|
|
|
global $wgCategoryTreeMaxDepth;
|
|
|
|
|
|
|
|
if (is_numeric($depth))
|
|
|
|
$depth = intval($depth);
|
|
|
|
else return 1;
|
|
|
|
|
|
|
|
if (is_array($wgCategoryTreeMaxDepth)) {
|
|
|
|
$max = isset($wgCategoryTreeMaxDepth[$mode]) ? $wgCategoryTreeMaxDepth[$mode] : 1;
|
|
|
|
} elseif (is_numeric($wgCategoryTreeMaxDepth)) {
|
|
|
|
$max = $wgCategoryTreeMaxDepth;
|
|
|
|
} else {
|
|
|
|
wfDebug( 'efCategoryTreeCapDepth: $wgCategoryTreeMaxDepth is invalid.' );
|
|
|
|
$max = 1;
|
|
|
|
}
|
2008-02-04 09:22:12 +00:00
|
|
|
|
2007-03-30 13:28:59 +00:00
|
|
|
return min($depth, $max);
|
2007-03-13 22:24:38 +00:00
|
|
|
}
|
|
|
|
|
2007-03-13 11:39:12 +00:00
|
|
|
/**
|
|
|
|
* Helper function to convert a string to a boolean value.
|
|
|
|
* Perhaps make this a global function in MediaWiki proper
|
|
|
|
*/
|
|
|
|
function efCategoryTreeAsBool( $s ) {
|
|
|
|
if ( is_null( $s ) || is_bool( $s ) ) return $s;
|
|
|
|
$s = trim( strtolower( $s ) );
|
|
|
|
|
|
|
|
if ( $s === '1' || $s === 'yes' || $s === 'on' || $s === 'true' ) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else if ( $s === '0' || $s === 'no' || $s === 'off' || $s === 'false' ) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-09-13 10:23:45 +00:00
|
|
|
/**
|
|
|
|
* Entry point for the {{#categorytree}} tag parser function.
|
|
|
|
* This is a wrapper around efCategoryTreeParserHook
|
|
|
|
*/
|
|
|
|
function efCategoryTreeParserFunction( &$parser ) {
|
|
|
|
$params = func_get_args();
|
|
|
|
array_shift( $params ); //first is &$parser, strip it
|
|
|
|
|
|
|
|
//first user-supplied parameter must be category name
|
|
|
|
if ( !$params ) return ''; //no category specified, return nothing
|
|
|
|
$cat = array_shift( $params );
|
|
|
|
|
|
|
|
//build associative arguments from flat parameter list
|
|
|
|
$argv = array();
|
|
|
|
foreach ( $params as $p ) {
|
|
|
|
if ( preg_match('/^\s*(\S.*?)\s*=\s*(.*?)\s*$/', $p, $m) ) {
|
|
|
|
$k = $m[1];
|
|
|
|
$v = $m[2];
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$k = trim($p);
|
|
|
|
$v = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
$argv[$k] = $v;
|
|
|
|
}
|
|
|
|
|
|
|
|
//now handle just like a <categorytree> tag
|
|
|
|
$html = efCategoryTreeParserHook( $cat, $argv, $parser );
|
2008-01-25 10:44:05 +00:00
|
|
|
return array( $html, 'isHTML' => true );
|
2007-09-13 10:23:45 +00:00
|
|
|
}
|
|
|
|
|
2006-07-26 17:12:30 +00:00
|
|
|
/**
|
|
|
|
* Entry point for the <categorytree> tag parser hook.
|
2006-08-24 17:12:13 +00:00
|
|
|
* This loads CategoryTreeFunctions.php and calls CategoryTree::getTag()
|
2006-07-26 17:12:30 +00:00
|
|
|
*/
|
2006-08-23 19:23:17 +00:00
|
|
|
function efCategoryTreeParserHook( $cat, $argv, &$parser ) {
|
2007-04-30 21:18:56 +00:00
|
|
|
global $wgCategoryTreeDefaultMode;
|
|
|
|
|
2006-08-24 17:12:13 +00:00
|
|
|
$parser->mOutput->mCategoryTreeTag = true; # flag for use by efCategoryTreeParserOutput
|
2008-02-04 09:22:12 +00:00
|
|
|
|
2006-08-23 19:23:17 +00:00
|
|
|
static $initialized = false;
|
2008-02-04 09:22:12 +00:00
|
|
|
|
2006-09-02 21:54:21 +00:00
|
|
|
$divAttribs = Sanitizer::validateTagAttributes( $argv, 'div' );
|
2006-11-29 13:06:21 +00:00
|
|
|
$style = isset( $divAttribs['style'] ) ? $divAttribs['style'] : null;
|
2008-02-04 09:22:12 +00:00
|
|
|
|
2006-11-29 13:06:21 +00:00
|
|
|
$mode = isset( $argv[ 'mode' ] ) ? $argv[ 'mode' ] : null;
|
2006-07-26 17:12:30 +00:00
|
|
|
if ( $mode !== NULL ) {
|
|
|
|
$mode= trim( strtolower( $mode ) );
|
2008-02-04 09:22:12 +00:00
|
|
|
|
2006-07-26 17:12:30 +00:00
|
|
|
if ( $mode == 'all' ) $mode = CT_MODE_ALL;
|
|
|
|
else if ( $mode == 'pages' ) $mode = CT_MODE_PAGES;
|
|
|
|
else if ( $mode == 'categories' ) $mode = CT_MODE_CATEGORIES;
|
|
|
|
}
|
|
|
|
else {
|
2007-04-30 21:18:56 +00:00
|
|
|
$mode = $wgCategoryTreeDefaultMode;
|
2006-07-26 17:12:30 +00:00
|
|
|
}
|
2008-02-04 09:22:12 +00:00
|
|
|
|
2007-03-13 11:39:12 +00:00
|
|
|
$hideroot = isset( $argv[ 'hideroot' ] ) ? efCategoryTreeAsBool( $argv[ 'hideroot' ] ) : null;
|
|
|
|
$onlyroot = isset( $argv[ 'onlyroot' ] ) ? efCategoryTreeAsBool( $argv[ 'onlyroot' ] ) : null;
|
2007-05-02 01:56:43 +00:00
|
|
|
$depthArg = isset( $argv[ 'depth' ] ) ? $argv[ 'depth' ] : null;
|
2007-03-13 11:39:12 +00:00
|
|
|
|
2007-05-02 01:56:43 +00:00
|
|
|
$depth = efCategoryTreeCapDepth($mode, $depthArg);
|
2008-02-04 09:22:12 +00:00
|
|
|
|
2007-03-30 13:28:59 +00:00
|
|
|
if ( $onlyroot ) $depth = 0;
|
2006-08-23 19:23:17 +00:00
|
|
|
|
2006-08-24 17:12:13 +00:00
|
|
|
$ct = new CategoryTree;
|
2007-03-13 22:24:38 +00:00
|
|
|
return $ct->getTag( $parser, $cat, $mode, $hideroot, $style, $depth );
|
2006-07-26 17:12:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Hook callback that installs a tab for CategoryTree on Category pages
|
2006-08-24 17:12:13 +00:00
|
|
|
*/
|
|
|
|
/*
|
2006-07-26 17:12:30 +00:00
|
|
|
function efCategoryTreeInstallTabs( &$skin, &$content_actions ) {
|
|
|
|
global $wgTitle;
|
2008-02-04 09:22:12 +00:00
|
|
|
|
2006-07-26 17:12:30 +00:00
|
|
|
if ( $wgTitle->getNamespace() != NS_CATEGORY ) return true;
|
2008-02-04 09:22:12 +00:00
|
|
|
|
2006-07-26 17:12:30 +00:00
|
|
|
$special = Title::makeTitle( NS_SPECIAL, 'CategoryTree' );
|
2008-02-04 09:22:12 +00:00
|
|
|
|
2006-07-26 17:12:30 +00:00
|
|
|
$content_actions['categorytree'] = array(
|
|
|
|
'class' => false,
|
2006-08-24 17:12:13 +00:00
|
|
|
'text' => htmlspecialchars( CategoryTree::msg( 'tab' ) ),
|
2008-02-04 09:22:12 +00:00
|
|
|
'href' => $special->getLocalUrl() . '/' . $wgTitle->getPartialURL() );
|
2006-07-26 17:12:30 +00:00
|
|
|
return true;
|
2006-08-24 17:12:13 +00:00
|
|
|
}*/
|
2006-07-26 17:12:30 +00:00
|
|
|
|
2006-07-29 09:18:34 +00:00
|
|
|
/**
|
|
|
|
* Hook callback that injects messages and things into the <head> tag
|
|
|
|
* Does nothing if $parserOutput->mCategoryTreeTag is not set
|
|
|
|
*/
|
2006-08-24 17:12:13 +00:00
|
|
|
function efCategoryTreeParserOutput( &$outputPage, &$parserOutput ) {
|
|
|
|
if ( !empty( $parserOutput->mCategoryTreeTag ) ) {
|
|
|
|
CategoryTree::setHeaders( $outputPage );
|
|
|
|
}
|
2006-08-28 17:13:16 +00:00
|
|
|
return true;
|
2006-08-24 17:12:13 +00:00
|
|
|
}
|
2006-07-29 09:18:34 +00:00
|
|
|
|
2006-07-26 17:12:30 +00:00
|
|
|
/**
|
2006-08-24 17:12:13 +00:00
|
|
|
* ArticleFromTitle hook, override category page handling
|
|
|
|
*/
|
|
|
|
function efCategoryTreeArticleFromTitle( &$title, &$article ) {
|
|
|
|
if ( $title->getNamespace() == NS_CATEGORY ) {
|
|
|
|
$article = new CategoryTreeCategoryPage( $title );
|
2006-08-11 23:36:53 +00:00
|
|
|
}
|
2006-08-24 17:12:13 +00:00
|
|
|
return true;
|
2006-07-26 17:12:30 +00:00
|
|
|
}
|