r20410 - Code Review

From MediaWiki.org

Jump to: navigation, search
Repository:MediaWiki
Revision:r20409 | r20410 (on ViewVC) | r20411 >
Date:22:24, 13 March 2007
Author:sanbeg
Status:new
Tags:
Comment:Add depth argument to allow auto-expanding tree (to small, configurable limit)
Modified paths:

Diff [purge]

Index: trunk/extensions/CategoryTree/CategoryTree.php
===================================================================
--- trunk/extensions/CategoryTree/CategoryTree.php	(revision 20409)
+++ trunk/extensions/CategoryTree/CategoryTree.php	(revision 20410)
@@ -33,8 +33,14 @@
  * $wgCategoryTreeDisableCache - disabled the parser cache for pages with a <categorytree> 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 @@
 $wgCategoryTreeHTTPCache = false;
 $wgCategoryTreeUnifiedView = true;
 $wgCategoryTreeOmitNamespace = false;
+$wgCategoryMaxDepth = array(1,2);
 
 /**
  * Register extension setup hook and credits
@@ -112,6 +119,45 @@
 }
 
 /**
+ * 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 @@
 	$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 );
 }
 
 /**
Index: trunk/extensions/CategoryTree/README
===================================================================
--- trunk/extensions/CategoryTree/README	(revision 20409)
+++ trunk/extensions/CategoryTree/README	(revision 20410)
@@ -93,5 +93,12 @@
                                false. Patch contributed by Manuel Schneider
                                <manuel.schneider@wikimedia.ch>, 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
Index: trunk/extensions/CategoryTree/CategoryTreeFunctions.php
===================================================================
--- trunk/extensions/CategoryTree/CategoryTreeFunctions.php	(revision 20409)
+++ trunk/extensions/CategoryTree/CategoryTreeFunctions.php	(revision 20410)
@@ -113,7 +113,7 @@
 	* 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 @@
 			$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 @@
 	* 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 @@
 		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 @@
 	* 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 @@
 		}
 		
 		$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 @@
 		$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' );
 		
Views
Toolbox