r36815 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r36814‎ | r36815 | r36816 >
Date:14:09, 30 June 2008
Author:daniel
Status:old
Tags:
Comment:
Show member counts in category tree. Requires r36814 of Category.php and CategoryPage.php.
Modified paths:
  • /trunk/extensions/CategoryTree/CategoryPageSubclass.php (modified) (history)
  • /trunk/extensions/CategoryTree/CategoryTree.i18n.php (modified) (history)
  • /trunk/extensions/CategoryTree/CategoryTree.php (modified) (history)
  • /trunk/extensions/CategoryTree/CategoryTreeFunctions.php (modified) (history)
  • /trunk/extensions/CategoryTree/CategoryTreePage.php (modified) (history)

Diff [purge]

Index: trunk/extensions/CategoryTree/CategoryPageSubclass.php
@@ -12,7 +12,7 @@
1313 }
1414
1515 class CategoryTreeCategoryViewer extends CategoryViewer {
16 - var $child_titles;
 16+ var $child_cats;
1717
1818 function getCategoryTree() {
1919 global $wgOut, $wgCategoryTreeCategoryPageOptions;
@@ -29,25 +29,29 @@
3030 /**
3131 * Add a subcategory to the internal lists
3232 */
33 - function addSubcategory( $title, $sortkey, $pageLength ) {
 33+ function addSubcategoryObject( $cat, $sortkey, $pageLength ) {
3434 global $wgContLang, $wgOut, $wgRequest;
3535
 36+ $title = $cat->getTitle();
 37+
3638 if ( $wgRequest->getCheck( 'notree' ) ) {
37 - return parent::addSubcategory( $title, $sortkey, $pageLength );
 39+ return parent::addSubcategoryObject( $cat, $sortkey, $pageLength );
3840 }
3941
40 - if ( ! $GLOBALS['wgCategoryTreeUnifiedView'] ) {
41 - $this->child_titles[] = $title;
42 - return parent::addSubcategory( $title, $sortkey, $pageLength );
43 - }
 42+ /*if ( ! $GLOBALS['wgCategoryTreeUnifiedView'] ) {
 43+ $this->child_cats[] = $cat;
 44+ return parent::addSubcategory( $cat, $sortkey, $pageLength );
 45+ }*/
4446
4547 $tree = $this->getCategoryTree();
4648
47 - $this->children[] = $tree->renderNode( $title );
 49+ $this->children[] = $tree->renderNodeInfo( $title, $cat );
4850
4951 $this->children_start_char[] = $this->getSubcategorySortChar( $title, $sortkey );
5052 }
5153
 54+ /*
 55+ # this is a pain to keep this consistent, and no one should be using wgCategoryTreeUnifiedView = false anyway.
5256 function getSubcategorySection() {
5357 global $wgOut, $wgRequest, $wgCookiePrefix;
5458
@@ -107,14 +111,14 @@
108112 if ( $showAs == 'list' ) {
109113 $r .= $this->formatList( $this->children, $this->children_start_char );
110114 } else {
111 - $ct = getCategoryTree();
 115+ $ct = $this->getCategoryTree();
112116
113 - foreach ( $this->child_titles as $title ) {
114 - $r .= $ct->renderNode( $title );
 117+ foreach ( $this->child_cats as $cat ) {
 118+ $r .= $ct->renderNodeInfo( $cat->getTitle(), $cat );
115119 }
116120 }
117121 return $r;
118 - }
 122+ }*/
119123
120124 function makeShowAsLink( $targetValue, $currentValue ) {
121125 $msg = htmlspecialchars( CategoryTree::msg( "show-$targetValue" ) );
@@ -127,13 +131,13 @@
128132 }
129133
130134 function clearCategoryState() {
131 - $this->child_titles = array();
 135+ $this->child_cats = array();
132136 parent::clearCategoryState();
133137 }
134138
135139 function finaliseCategoryState() {
136140 if( $this->flip ) {
137 - $this->child_titles = array_reverse( $this->child_titles );
 141+ $this->child_cats = array_reverse( $this->child_cats );
138142 }
139143 parent::finaliseCategoryState();
140144 }
Index: trunk/extensions/CategoryTree/CategoryTreePage.php
@@ -40,7 +40,7 @@
4141 * @param $par Parameters passed to the page
4242 */
4343 function execute( $par ) {
44 - global $wgRequest, $wgOut, $wgMakeBotPrivileged, $wgUser, $wgCategoryTreeDefaultOptions;
 44+ global $wgRequest, $wgOut, $wgCategoryTreeDefaultOptions, $wgCategoryTreeSpecialPageOptions;
4545
4646 $this->setHeaders();
4747
@@ -56,6 +56,9 @@
5757
5858 # grab all known options from the request. Normalization is done by the CategoryTree class
5959 foreach ( $wgCategoryTreeDefaultOptions as $option => $default ) {
 60+ if ( isset( $wgCategoryTreeSpecialPageOptions[$option] ) )
 61+ $default = $wgCategoryTreeSpecialPageOptions[$option];
 62+
6063 $options[$option] = $wgRequest->getVal( $option, $default );
6164 }
6265
Index: trunk/extensions/CategoryTree/CategoryTree.i18n.php
@@ -34,8 +34,11 @@
3535 'categorytree-expand' => 'expand',
3636 'categorytree-collapse-bullet' => '[<b>&#x2212;</b>]', # do not translate or duplicate this message to other languages
3737 'categorytree-expand-bullet' => '[<b>+</b>]', # do not translate or duplicate this message to other languages
 38+ 'categorytree-empty-bullet' => '[<b>&#215</b>]', # do not translate or duplicate this message to other languages
3839 'categorytree-page-bullet' => '&nbsp;', # do not translate or duplicate this message to other languages
3940
 41+ 'categorytree-member-counts' => 'contains $1 subcategories, $2 pages, and $3 files',
 42+
4043 'categorytree-load' => 'load',
4144 'categorytree-loading' => 'loading…',
4245 'categorytree-nothing-found' => 'nothing found',
@@ -596,6 +599,7 @@
597600 'categorytree-category' => 'Kategorie:',
598601 'categorytree-go' => 'Laden',
599602 'categorytree-parents' => 'Oberkategorien',
 603+ 'categorytree-member-counts' => 'enthält $1 Unterkategorien, $2 Seiten und $3 Dateien',
600604 'categorytree-mode-categories' => 'nur Kategorien',
601605 'categorytree-mode-pages' => 'Seiten außer Bilder',
602606 'categorytree-mode-all' => 'alle Seiten',
Index: trunk/extensions/CategoryTree/CategoryTree.php
@@ -32,12 +32,13 @@
3333 * This way, the cache does not need to be disabled. Default is false.
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.
36 - * $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
3836 * $wgCategoryTreeMaxDepth - maximum value for depth argument; An array that maps mode values to
3937 * the maximum depth acceptable for the depth option.
4038 * Per default, the "categories" mode has a max depth of 2,
4139 * all other modes have a max depth of 1.
 40+ * $wgCategoryTreeDefaultOptions - default options for the <categorytree> tag.
 41+ * $wgCategoryTreeCategoryPageOptions - options to apply on category pages.
 42+ * $wgCategoryTreeSpecialPageOptions - options to apply on Special:CategoryTree.
4243 */
4344
4445 $wgCategoryTreeMaxChildren = 200;
@@ -45,7 +46,7 @@
4647 $wgCategoryTreeDisableCache = true;
4748 $wgCategoryTreeDynamicTag = false;
4849 $wgCategoryTreeHTTPCache = false;
49 -$wgCategoryTreeUnifiedView = true;
 50+#$wgCategoryTreeUnifiedView = true;
5051 $wgCategoryTreeMaxDepth = array(CT_MODE_PAGES => 1, CT_MODE_ALL => 1, CT_MODE_CATEGORIES => 2);
5152
5253 $wgCategoryTreeExtPath = '/extensions/CategoryTree';
@@ -56,12 +57,17 @@
5758 $wgCategoryTreeDefaultOptions = array(); #Default values for most options. ADD NEW OPTIONS HERE!
5859 $wgCategoryTreeDefaultOptions['mode'] = NULL; # will be set to $wgCategoryTreeDefaultMode in efCategoryTree(); compatibility quirk
5960 $wgCategoryTreeDefaultOptions['hideprefix'] = NULL; # will be set to $wgCategoryTreeDefaultMode in efCategoryTree(); compatibility quirk
 61+$wgCategoryTreeDefaultOptions['showcount'] = false;
6062 #TODO: hideprefix: always, never, catonly, catonly_if_onlycat
6163
6264 $wgCategoryTreeCategoryPageMode = CT_MODE_CATEGORIES;
6365 $wgCategoryTreeCategoryPageOptions = array(); #Options to be used for category pages
6466 $wgCategoryTreeCategoryPageOptions['mode'] = NULL; # will be set to $wgCategoryTreeDefaultMode in efCategoryTree(); compatibility quirk
 67+$wgCategoryTreeCategoryPageOptions['showcount'] = true;
6568
 69+$wgCategoryTreeSpecialPageOptions = array(); #Options to be used for Special:CategoryTree
 70+$wgCategoryTreeSpecialPageOptions['showcount'] = true;
 71+
6672 /**
6773 * Register extension setup hook and credits
6874 */
Index: trunk/extensions/CategoryTree/CategoryTreeFunctions.php
@@ -32,6 +32,7 @@
3333
3434 $this->mOptions['mode'] = self::decodeMode( $this->mOptions['mode'] );
3535 $this->mOptions['hideprefix'] = self::decodeBoolean( $this->mOptions['hideprefix'] );
 36+ $this->mOptions['showcount'] = self::decodeBoolean( $this->mOptions['showcount'] );
3637 }
3738
3839 function getOption( $name ) {
@@ -66,7 +67,7 @@
6768 if ( is_int( $value ) ) return ( $value > 0 );
6869
6970 $value = trim( strtolower( $value ) );
70 - if ( is_numeric( $value ) ) return ( (int)$mode > 0 );
 71+ if ( is_numeric( $value ) ) return ( (int)$value > 0 );
7172
7273 if ( $value == 'yes' || $value == 'y' || $value == 'true' || $value == 't' || $value == 'on' ) return true;
7374 else if ( $value == 'no' || $value == 'n' || $value == 'false' || $value == 'f' || $value == 'off' ) return false;
@@ -306,7 +307,7 @@
307308 * $title must be a Title object
308309 */
309310 function renderChildren( &$title, $depth=1 ) {
310 - global $wgCategoryTreeMaxChildren;
 311+ global $wgCategoryTreeMaxChildren, $wgVersion;
311312
312313 if( $title->getNamespace() != NS_CATEGORY ) {
313314 // Non-categories can't have children. :)
@@ -315,10 +316,6 @@
316317
317318 $dbr =& wfGetDB( DB_SLAVE );
318319
319 - #additional stuff to be used if "transaltion" by interwiki-links is desired
320 - $transFields = '';
321 - $transJoin = '';
322 - $transWhere = '';
323320
324321 $mode = $this->getOption('mode');
325322
@@ -327,14 +324,33 @@
328325 else if ( $mode == CT_MODE_PAGES ) $nsmatch = ' AND cat.page_namespace != ' . NS_IMAGE;
329326 else $nsmatch = ' AND cat.page_namespace = ' . NS_CATEGORY;
330327
 328+ #additional stuff to be used if "transaltion" by interwiki-links is desired
 329+ $transFields = '';
 330+ $transJoin = '';
 331+ $transWhere = '';
 332+
 333+ # fetch member count if possible
 334+ $doCount = version_compare( $wgVersion, "1.12", '>' );
 335+
 336+ $countFields = '';
 337+ $countJoin = '';
 338+
 339+ if ( $doCount ) {
 340+ $cat = $dbr->tableName( 'category' );
 341+ $countJoin = " LEFT JOIN $cat ON cat_title = page_title AND page_namespace = " . NS_CATEGORY;
 342+ $countFields = ', cat_id, cat_title, cat_subcats, cat_pages, cat_files';
 343+ }
 344+
331345 $page = $dbr->tableName( 'page' );
332346 $categorylinks = $dbr->tableName( 'categorylinks' );
333347
334348 $sql = "SELECT cat.page_namespace, cat.page_title
335349 $transFields
 350+ $countFields
336351 FROM $page as cat
337352 JOIN $categorylinks ON cl_from = cat.page_id
338353 $transJoin
 354+ $countJoin
339355 WHERE cl_to = " . $dbr->addQuotes( $title->getDBkey() ) . "
340356 $nsmatch
341357 "./*AND cat.page_is_redirect = 0*/"
@@ -348,14 +364,20 @@
349365 $categories= '';
350366 $other= '';
351367
352 - while ( $row = $dbr->fetchRow( $res ) ) {
 368+ while ( $row = $dbr->fetchObject( $res ) ) {
353369 #TODO: translation support; ideally added to Title object
354 - $t = Title::makeTitle( $row['page_namespace'], $row['page_title'] );
 370+ $t = Title::newFromRow( $row );
355371
356 - $s = $this->renderNode( $t, $depth-1, false );
 372+ $cat = NULL;
 373+
 374+ if ( $doCount && $row->page_namespace == NS_CATEGORY ) {
 375+ $cat = Category::newFromRow( $row, $t );
 376+ }
 377+
 378+ $s = $this->renderNodeInfo( $t, $cat, $depth-1, false );
357379 $s .= "\n\t\t";
358380
359 - if ($row['page_namespace'] == NS_CATEGORY) $categories .= $s;
 381+ if ($row->page_namespace == NS_CATEGORY) $categories .= $s;
360382 else $other .= $s;
361383 }
362384
@@ -394,9 +416,9 @@
395417
396418 $s= '';
397419
398 - while ( $row = $dbr->fetchRow( $res ) ) {
 420+ while ( $row = $dbr->fetchObject( $res ) ) {
399421 #TODO: translation support; ideally added to Title object
400 - $t = Title::makeTitle( $row['page_namespace'], $row['page_title'] );
 422+ $t = Title::newFromRow( $row );
401423
402424 #$trans = $title->getLocalizedText();
403425 $trans = ''; #place holder for when translated titles are available
@@ -424,8 +446,18 @@
425447 * Returns a string with a HTML represenation of the given page.
426448 * $title must be a Title object
427449 */
428 - function renderNode( &$title, $children = 0, $loadchildren = false ) {
429 - global $wgCategoryTreeDefaultMode;
 450+ function renderNode( $title, $children = 0, $loadchildren = false ) {
 451+ if ( $title->getNamespace() == NS_CATEGORY ) $cat = Category::newFromTitle( $title );
 452+ else $cat = NULL;
 453+
 454+ return $this->renderNodeInfo( $title, $cat, $children, $loadchildren );
 455+ }
 456+
 457+ /**
 458+ * Returns a string with a HTML represenation of the given page.
 459+ * $info must be an associative array, containing at least a Title object under the 'title' key.
 460+ */
 461+ function renderNodeInfo( $title, $cat, $children = 0, $loadchildren = false ) {
430462 static $uniq = 0;
431463
432464 $mode = $this->getOption('mode');
@@ -462,6 +494,7 @@
463495
464496 if ( ( $ns % 2 ) > 0 ) $labelClass .= ' CategoryTreeLabelTalk';
465497
 498+ $count = false;
466499 $s = '';
467500
468501 #NOTE: things in CategoryTree.js rely on the exact order of tags!
@@ -475,25 +508,37 @@
476509 $s .= Xml::openElement( 'span', $attr );
477510
478511 if ( $ns == NS_CATEGORY ) {
 512+ if ( $cat ) {
 513+ if ( $mode == CT_MODE_CATEGORIES ) $count = $cat->getSubcatCount();
 514+ else if ( $mode == CT_MODE_PAGES ) $count = $cat->getPageCount() - $cat->getFileCount();
 515+ else $count = $cat->getPageCount();
 516+ }
 517+
479518 $linkattr= array( 'href' => $wikiLink );
480519 if ( $load ) $linkattr[ 'id' ] = $load;
481520
482521 $linkattr[ 'class' ] = "CategoryTreeToggle";
483522
484 - if ( $children == 0 || $loadchildren ) {
 523+ if ( $count === 0 ) {
 524+ $tag = 'span';
 525+ $txt = $this->msg('empty-bullet');
 526+ }
 527+ else if ( $children == 0 || $loadchildren ) {
 528+ $tag = 'a';
485529 $txt = $this->msg('expand-bullet');
486530 $linkattr[ 'onclick' ] = "this.href='javascript:void(0)'; categoryTreeExpandNode('".Xml::escapeJsString($key)."',".$this->getOptionsAsJsStructure().",this);";
487531 # Don't load this message for ajax requests, so that we don't have to initialise $wgLang
488532 $linkattr[ 'title' ] = $this->mIsAjaxRequest ? '##LOAD##' : self::msg('expand');
489533 }
490534 else {
 535+ $tag = 'a';
491536 $txt = $this->msg('collapse-bullet');
492537 $linkattr[ 'onclick' ] = "this.href='javascript:void(0)'; categoryTreeCollapseNode('".Xml::escapeJsString($key)."',".$this->getOptionsAsJsStructure().",this);";
493538 $linkattr[ 'title' ] = self::msg('collapse');
494539 $linkattr[ 'class' ] .= ' CategoryTreeLoaded';
495540 }
496541
497 - $s .= Xml::openElement( 'a', $linkattr ) . $txt . Xml::closeElement( 'a' ) . ' ';
 542+ $s .= Xml::openElement( $tag, $linkattr ) . $txt . Xml::closeElement( $tag ) . ' ';
498543 } else {
499544 $s .= $this->msg('page-bullet');
500545 }
@@ -501,6 +546,17 @@
502547 $s .= Xml::closeElement( 'span' );
503548
504549 $s .= Xml::openElement( 'a', array( 'class' => $labelClass, 'href' => $wikiLink ) ) . $label . Xml::closeElement( 'a' );
 550+
 551+ if ( $count !== false && $this->getOption( 'showcount' ) ) {
 552+ $pages = $cat->getPageCount() - $cat->getSubcatCount() - $cat->getFileCount();
 553+
 554+ $attr = array(
 555+ 'title' => $this->msg( 'member-counts', $cat->getSubcatCount(), $pages , $cat->getFileCount() )
 556+ );
 557+
 558+ $s .= Xml::element( 'span', $attr, ' (' . $count . ')' );
 559+ }
 560+
505561 $s .= Xml::closeElement( 'div' );
506562 $s .= "\n\t\t";
507563 $s .= Xml::openElement( 'div', array( 'class' => 'CategoryTreeChildren', 'style' => $children > 0 ? "display:block" : "display:none" ) );

Follow-up revisions

RevisionCommit summaryAuthorDate
r47049Remove code that has been commented out since r36815.siebrand19:28, 9 February 2009

Past revisions this follows-up on

RevisionCommit summaryAuthorDate
r36814Make member counts from the new category table available for subcategories, b...daniel14:06, 30 June 2008

Status & tagging log