r20410 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r20409‎ | r20410 | r20411 >
Date:22:24, 13 March 2007
Author:sanbeg
Status:old
Tags:
Comment:
Add depth argument to allow auto-expanding tree (to small, configurable limit)
Modified paths:
  • /trunk/extensions/CategoryTree/CategoryTree.php (modified) (history)
  • /trunk/extensions/CategoryTree/CategoryTreeFunctions.php (modified) (history)
  • /trunk/extensions/CategoryTree/README (modified) (history)

Diff [purge]

Index: trunk/extensions/CategoryTree/CategoryTreeFunctions.php
@@ -113,7 +113,7 @@
114114 * Custom tag implementation. This is called by efCategoryTreeParserHook, which is used to
115115 * load CategoryTreeFunctions.php on demand.
116116 */
117 - function getTag( &$parser, $category, $mode, $display = 'expandroot', $style = '' ) {
 117+ function getTag( &$parser, $category, $mode, $display = 'expandroot', $style = '', $depth=1 ) {
118118 global $wgCategoryTreeDisableCache, $wgCategoryTreeDynamicTag;
119119 static $uniq = 0;
120120
@@ -138,8 +138,8 @@
139139 $html .= wfCloseElement( 'span' );
140140 }
141141 else {
142 - if ( $display != 'hideroot' ) $html .= CategoryTree::renderNode( $title, $mode, $display != 'onlyroot', $wgCategoryTreeDynamicTag );
143 - else if ( !$wgCategoryTreeDynamicTag ) $html .= $this->renderChildren( $title, $mode );
 142+ if ( $display != 'hideroot' ) $html .= CategoryTree::renderNode( $title, $mode, $depth>0, $wgCategoryTreeDynamicTag, $depth-1 );
 143+ else if ( !$wgCategoryTreeDynamicTag ) $html .= $this->renderChildren( $title, $mode, $depth-1 );
144144 else {
145145 $uniq += 1;
146146 $load = 'ct-' . $uniq . '-' . mt_rand( 1, 100000 );
@@ -160,7 +160,7 @@
161161 * Returns a string with an HTML representation of the children of the given category.
162162 * $title must be a Title object
163163 */
164 - function renderChildren( &$title, $mode = CT_MODE_CATEGORIES ) {
 164+ function renderChildren( &$title, $mode = CT_MODE_CATEGORIES, $depth=0 ) {
165165 global $wgCategoryTreeMaxChildren;
166166
167167 $dbr =& wfGetDB( DB_SLAVE );
@@ -198,7 +198,7 @@
199199 while ( $row = $dbr->fetchRow( $res ) ) {
200200 #TODO: translation support; ideally added to Title object
201201 $t = Title::makeTitle( $row['page_namespace'], $row['page_title'] );
202 - $s .= $this->renderNode( $t, $mode, false );
 202+ $s .= $this->renderNode( $t, $mode, $depth>0, false, $depth-1 );
203203 $s .= "\n\t\t";
204204 }
205205
@@ -267,7 +267,7 @@
268268 * Returns a string with a HTML represenation of the given page.
269269 * $title must be a Title object
270270 */
271 - function renderNode( &$title, $mode = CT_MODE_CATEGORIES, $children = false, $loadchildren = false ) {
 271+ function renderNode( &$title, $mode = CT_MODE_CATEGORIES, $children = false, $loadchildren = false, $depth = 1 ) {
272272 global $wgCategoryTreeOmitNamespace;
273273 static $uniq = 0;
274274
@@ -323,7 +323,7 @@
324324 }
325325
326326 $s = '';
327 -
 327+
328328 #NOTE: things in CategoryTree.js rely on the exact order of tags!
329329 # Specifically, the CategoryTreeChildren div must be the first
330330 # sibling with nodeName = DIV of the grandparent of the expland link.
@@ -343,7 +343,8 @@
344344 $s .= wfCloseElement( 'div' );
345345 $s .= "\n\t\t";
346346 $s .= wfOpenElement( 'div', array( 'class' => 'CategoryTreeChildren', 'style' => $children ? "display:block" : "display:none" ) );
347 - if ( $children ) $s .= $this->renderChildren( $title, $mode );
 347+ //HACK here?
 348+ if ( $children ) $s .= $this->renderChildren( $title, $mode, $depth );
348349 $s .= wfCloseElement( 'div' );
349350 $s .= wfCloseElement( 'div' );
350351
Index: trunk/extensions/CategoryTree/CategoryTree.php
@@ -33,8 +33,14 @@
3434 * $wgCategoryTreeDisableCache - disabled the parser cache for pages with a <categorytree> tag. Default is true.
3535 * $wgCategoryTreeUseCache - enable HTTP cache for anon users. Default is false.
3636 * $wgCategoryTreeUnifiedView - use unified view on category pages, instead of "tree" or "traditional list". Default is true.
37 - * $wgCategoryTreeOmitNamespace - never show namespace prefix. Default is false
 37+ * $wgCategoryTreeOmitNamespace - never show namespace prefix. Default is false
 38+
 39+ * $wgCategoryMaxDepth - maximum value for depth argument; can be an
 40+ * integer, or an array of two integers. The first element is the maximum
 41+ * depth for the "pages" and "all" modes; the second is for the categories
 42+ * mode. Ignored if $wgCategoryTreeDynamicTag is true.
3843 */
 44+
3945 $wgCategoryTreeMaxChildren = 200;
4046 $wgCategoryTreeAllowTag = true;
4147 $wgCategoryTreeDisableCache = true;
@@ -42,6 +48,7 @@
4349 $wgCategoryTreeHTTPCache = false;
4450 $wgCategoryTreeUnifiedView = true;
4551 $wgCategoryTreeOmitNamespace = false;
 52+$wgCategoryMaxDepth = array(1,2);
4653
4754 /**
4855 * Register extension setup hook and credits
@@ -112,6 +119,45 @@
113120 }
114121
115122 /**
 123+ * Internal function to cap depth
 124+ */
 125+
 126+function efCategoryTreeCapDepth( $mode, $depth )
 127+{
 128+
 129+ if (is_numeric($depth))
 130+ $depth = intval($depth);
 131+ else
 132+ $depth = 1;
 133+
 134+
 135+ global $wgCategoryMaxDepth;
 136+ if (is_array($wgCategoryMaxDepth)) {
 137+ switch($mode) {
 138+ case CT_MODE_PAGES:
 139+ case CT_MODE_ALL:
 140+ $max = isset($wgCategoryMaxDepth[0])?$wgCategoryMaxDepth[0]:1;
 141+ break;
 142+ case CT_MODE_CATEGORIES:
 143+ default:
 144+ $max = isset($wgCategoryMaxDepth[1])?$wgCategoryMaxDepth[1]:1;
 145+ break;
 146+ }
 147+ } elseif (is_numeric($wgCategoryMaxDepth)) {
 148+ $max = $wgCategoryMaxDepth;
 149+ } else {
 150+ wfDebug( 'efCategoryTreeCapDepth: $wgCategoryMaxDepth is invalid.' );
 151+ $max = 1;
 152+ }
 153+
 154+ //echo "mode $mode:max is $max\n";
 155+ if ($depth>$max)
 156+ $depth = $max;
 157+
 158+ return $depth;
 159+}
 160+
 161+/**
116162 * Helper function to convert a string to a boolean value.
117163 * Perhaps make this a global function in MediaWiki proper
118164 */
@@ -157,12 +203,15 @@
158204 $hideroot = isset( $argv[ 'hideroot' ] ) ? efCategoryTreeAsBool( $argv[ 'hideroot' ] ) : null;
159205 $onlyroot = isset( $argv[ 'onlyroot' ] ) ? efCategoryTreeAsBool( $argv[ 'onlyroot' ] ) : null;
160206
 207+ $depth = efCategoryTreeCapDepth($mode,@$argv[ 'depth' ]);
 208+
161209 if ( $onlyroot ) $display = 'onlyroot';
162210 else if ( $hideroot ) $display = 'hideroot';
163211 else $display = 'expandroot';
164212
165213 $ct = new CategoryTree;
166 - return $ct->getTag( $parser, $cat, $mode, $display, $style );
 214+ return $ct->getTag( $parser, $cat, $mode, $hideroot, $style, $depth );
 215+ return $ct->getTag( $parser, $cat, $mode, $display, $style, $depth );
167216 }
168217
169218 /**
Index: trunk/extensions/CategoryTree/README
@@ -93,5 +93,12 @@
9494 false. Patch contributed by Manuel Schneider
9595 <manuel.schneider@wikimedia.ch>, Bug 8011
9696
 97+$wgCategoryMaxDepth - maximum value for depth argument; can be an integer,
 98+ or an array of two integers. The first element is the
 99+ maximum depth for the "pages" and "all" modes; the
 100+ second is for the categories mode. Ignored if
 101+ $wgCategoryTreeDynamicTag is true. Patch contributed by
 102+ Steve Sanbeg.
 103+
97104 --------------------------------------------------------------------------
98 -EOF
\ No newline at end of file
 105+EOF