From 93882a1cc49904d54057540cf788128733faeda3 Mon Sep 17 00:00:00 2001 From: Steve Sanbeg Date: Tue, 13 Mar 2007 22:24:38 +0000 Subject: [PATCH] Add depth argument to allow auto-expanding tree (to small, configurable limit) --- CategoryTree.php | 53 +++++++++++++++++++++++++++++++++++++-- CategoryTreeFunctions.php | 17 +++++++------ README | 9 ++++++- 3 files changed, 68 insertions(+), 11 deletions(-) diff --git a/CategoryTree.php b/CategoryTree.php index 29a7f31c..f6c25fd5 100644 --- a/CategoryTree.php +++ b/CategoryTree.php @@ -33,8 +33,14 @@ define('CT_MODE_ALL', 20); * $wgCategoryTreeDisableCache - disabled the parser cache for pages with a tag. Default is true. * $wgCategoryTreeUseCache - enable HTTP cache for anon users. Default is false. * $wgCategoryTreeUnifiedView - use unified view on category pages, instead of "tree" or "traditional list". Default is true. - * $wgCategoryTreeOmitNamespace - never show namespace prefix. Default is false + * $wgCategoryTreeOmitNamespace - never show namespace prefix. Default is false + + * $wgCategoryMaxDepth - maximum value for depth argument; can be an + * integer, or an array of two integers. The first element is the maximum + * depth for the "pages" and "all" modes; the second is for the categories + * mode. Ignored if $wgCategoryTreeDynamicTag is true. */ + $wgCategoryTreeMaxChildren = 200; $wgCategoryTreeAllowTag = true; $wgCategoryTreeDisableCache = true; @@ -42,6 +48,7 @@ $wgCategoryTreeDynamicTag = false; $wgCategoryTreeHTTPCache = false; $wgCategoryTreeUnifiedView = true; $wgCategoryTreeOmitNamespace = false; +$wgCategoryMaxDepth = array(1,2); /** * Register extension setup hook and credits @@ -111,6 +118,45 @@ function efCategoryTreeAjaxWrapper( $category, $mode = CT_MODE_CATEGORIES ) { return $response; } +/** + * Internal function to cap depth + */ + +function efCategoryTreeCapDepth( $mode, $depth ) +{ + + if (is_numeric($depth)) + $depth = intval($depth); + else + $depth = 1; + + + global $wgCategoryMaxDepth; + if (is_array($wgCategoryMaxDepth)) { + switch($mode) { + case CT_MODE_PAGES: + case CT_MODE_ALL: + $max = isset($wgCategoryMaxDepth[0])?$wgCategoryMaxDepth[0]:1; + break; + case CT_MODE_CATEGORIES: + default: + $max = isset($wgCategoryMaxDepth[1])?$wgCategoryMaxDepth[1]:1; + break; + } + } elseif (is_numeric($wgCategoryMaxDepth)) { + $max = $wgCategoryMaxDepth; + } else { + wfDebug( 'efCategoryTreeCapDepth: $wgCategoryMaxDepth is invalid.' ); + $max = 1; + } + + //echo "mode $mode:max is $max\n"; + if ($depth>$max) + $depth = $max; + + return $depth; +} + /** * Helper function to convert a string to a boolean value. * Perhaps make this a global function in MediaWiki proper @@ -157,12 +203,15 @@ function efCategoryTreeParserHook( $cat, $argv, &$parser ) { $hideroot = isset( $argv[ 'hideroot' ] ) ? efCategoryTreeAsBool( $argv[ 'hideroot' ] ) : null; $onlyroot = isset( $argv[ 'onlyroot' ] ) ? efCategoryTreeAsBool( $argv[ 'onlyroot' ] ) : null; + $depth = efCategoryTreeCapDepth($mode,@$argv[ 'depth' ]); + if ( $onlyroot ) $display = 'onlyroot'; else if ( $hideroot ) $display = 'hideroot'; else $display = 'expandroot'; $ct = new CategoryTree; - return $ct->getTag( $parser, $cat, $mode, $display, $style ); + return $ct->getTag( $parser, $cat, $mode, $hideroot, $style, $depth ); + return $ct->getTag( $parser, $cat, $mode, $display, $style, $depth ); } /** diff --git a/CategoryTreeFunctions.php b/CategoryTreeFunctions.php index 5f91ef13..a00ab391 100644 --- a/CategoryTreeFunctions.php +++ b/CategoryTreeFunctions.php @@ -113,7 +113,7 @@ class CategoryTree { * Custom tag implementation. This is called by efCategoryTreeParserHook, which is used to * load CategoryTreeFunctions.php on demand. */ - function getTag( &$parser, $category, $mode, $display = 'expandroot', $style = '' ) { + function getTag( &$parser, $category, $mode, $display = 'expandroot', $style = '', $depth=1 ) { global $wgCategoryTreeDisableCache, $wgCategoryTreeDynamicTag; static $uniq = 0; @@ -138,8 +138,8 @@ class CategoryTree { $html .= wfCloseElement( 'span' ); } else { - if ( $display != 'hideroot' ) $html .= CategoryTree::renderNode( $title, $mode, $display != 'onlyroot', $wgCategoryTreeDynamicTag ); - else if ( !$wgCategoryTreeDynamicTag ) $html .= $this->renderChildren( $title, $mode ); + if ( $display != 'hideroot' ) $html .= CategoryTree::renderNode( $title, $mode, $depth>0, $wgCategoryTreeDynamicTag, $depth-1 ); + else if ( !$wgCategoryTreeDynamicTag ) $html .= $this->renderChildren( $title, $mode, $depth-1 ); else { $uniq += 1; $load = 'ct-' . $uniq . '-' . mt_rand( 1, 100000 ); @@ -160,7 +160,7 @@ class CategoryTree { * Returns a string with an HTML representation of the children of the given category. * $title must be a Title object */ - function renderChildren( &$title, $mode = CT_MODE_CATEGORIES ) { + function renderChildren( &$title, $mode = CT_MODE_CATEGORIES, $depth=0 ) { global $wgCategoryTreeMaxChildren; $dbr =& wfGetDB( DB_SLAVE ); @@ -198,7 +198,7 @@ class CategoryTree { while ( $row = $dbr->fetchRow( $res ) ) { #TODO: translation support; ideally added to Title object $t = Title::makeTitle( $row['page_namespace'], $row['page_title'] ); - $s .= $this->renderNode( $t, $mode, false ); + $s .= $this->renderNode( $t, $mode, $depth>0, false, $depth-1 ); $s .= "\n\t\t"; } @@ -267,7 +267,7 @@ class CategoryTree { * Returns a string with a HTML represenation of the given page. * $title must be a Title object */ - function renderNode( &$title, $mode = CT_MODE_CATEGORIES, $children = false, $loadchildren = false ) { + function renderNode( &$title, $mode = CT_MODE_CATEGORIES, $children = false, $loadchildren = false, $depth = 1 ) { global $wgCategoryTreeOmitNamespace; static $uniq = 0; @@ -323,7 +323,7 @@ class CategoryTree { } $s = ''; - + #NOTE: things in CategoryTree.js rely on the exact order of tags! # Specifically, the CategoryTreeChildren div must be the first # sibling with nodeName = DIV of the grandparent of the expland link. @@ -343,7 +343,8 @@ class CategoryTree { $s .= wfCloseElement( 'div' ); $s .= "\n\t\t"; $s .= wfOpenElement( 'div', array( 'class' => 'CategoryTreeChildren', 'style' => $children ? "display:block" : "display:none" ) ); - if ( $children ) $s .= $this->renderChildren( $title, $mode ); + //HACK here? + if ( $children ) $s .= $this->renderChildren( $title, $mode, $depth ); $s .= wfCloseElement( 'div' ); $s .= wfCloseElement( 'div' ); diff --git a/README b/README index d3e5ef88..b3be7b79 100644 --- a/README +++ b/README @@ -93,5 +93,12 @@ $wgCategoryTreeOmitNamespace - never show namespace prefix. Default is false. Patch contributed by Manuel Schneider , Bug 8011 +$wgCategoryMaxDepth - maximum value for depth argument; can be an integer, + or an array of two integers. The first element is the + maximum depth for the "pages" and "all" modes; the + second is for the categories mode. Ignored if + $wgCategoryTreeDynamicTag is true. Patch contributed by + Steve Sanbeg. + -------------------------------------------------------------------------- -EOF \ No newline at end of file +EOF