r71052 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r71051‎ | r71052 | r71053 >
Date:22:38, 13 August 2010
Author:simetrical
Status:ok
Tags:
Comment:
Use DB abstraction CategoryTree::renderChildren()

Formerly pieced it all together with strings. This seems not to have
broken anything for basic usage, but I didn't test thoroughly. Also
fixed some random style errors and unused variables while I was there.
Modified paths:
  • /trunk/extensions/CategoryTree/CategoryTreeFunctions.php (modified) (history)

Diff [purge]

Index: trunk/extensions/CategoryTree/CategoryTreeFunctions.php
@@ -424,79 +424,49 @@
425425 $mode = $this->getOption( 'mode' );
426426 $namespaces = $this->getOption( 'namespaces' );
427427
 428+ $tables = array( 'page', 'categorylinks' );
 429+ $fields = array( 'page_namespace', 'page_title', 'cl_to', 'cl_from' );
 430+ $where = array();
 431+ $joins = array();
 432+
428433 if ( $inverse ) {
429 - $ctJoinCond = ' cl_to = cat.page_title AND cat.page_namespace = ' . NS_CATEGORY;
430 - $ctWhere = ' cl_from = ' . $title->getArticleId();
431 - $ctJoin = ' RIGHT JOIN ';
432 - $nsmatch = '';
433 - }
434 - else {
435 - $ctJoinCond = ' cl_from = cat.page_id ';
436 - $ctWhere = ' cl_to = ' . $dbr->addQuotes( $title->getDBkey() );
437 - $ctJoin = ' JOIN ';
 434+ $joins['categorylinks'] = array( 'RIGHT JOIN', 'cl_to = page_title AND page_namespace = ' . NS_CATEGORY );
 435+ $where['cl_from'] = $title->getArticleId();
 436+ } else {
 437+ $joins['categorylinks'] = array( 'JOIN', 'cl_from = page_id' );
 438+ $where['cl_to'] = $title->getDBkey();
438439
439440 # namespace filter.
440441 if ( $namespaces ) {
441442 # NOTE: we assume that the $namespaces array contains only integers! decodeNamepsaces makes it so.
442 - if ( sizeof( $namespaces ) === 1 ) {
443 - $nsmatch = ' AND cat.page_namespace = ' . $namespaces[0] . ' ';
 443+ $where['page_namespace'] = $namespaces;
 444+ } elseif ( $mode != CT_MODE_ALL ) {
 445+ if ( $mode == CT_MODE_PAGES ) {
 446+ $where = array_merge( $where, array( 'page_namespace != ' . NS_IMAGE ) );
444447 } else {
445 - $nsmatch = ' AND cat.page_namespace IN ( ' . implode( ', ', $namespaces ) . ') ';
 448+ $where['page_namespace'] = NS_CATEGORY;
446449 }
447450 }
448 - else {
449 - if ( $mode == CT_MODE_ALL ) {
450 - $nsmatch = '';
451 - } else if ( $mode == CT_MODE_PAGES ) {
452 - $nsmatch = ' AND cat.page_namespace != ' . NS_IMAGE;
453 - } else {
454 - $nsmatch = ' AND cat.page_namespace = ' . NS_CATEGORY;
455 - }
456 - }
457451 }
458452
459 - # additional stuff to be used if "transaltion" by interwiki-links is desired
460 - $transFields = '';
461 - $transJoin = '';
462 - $transWhere = '';
463 -
464453 # fetch member count if possible
465454 $doCount = !$inverse && $wgCategoryTreeUseCategoryTable;
466455
467 - $countFields = '';
468 - $countJoin = '';
469 -
470456 if ( $doCount ) {
471 - $cat = $dbr->tableName( 'category' );
472 - $countJoin = " LEFT JOIN $cat ON cat_title = page_title AND page_namespace = " . NS_CATEGORY;
473 - $countFields = ', cat_id, cat_title, cat_subcats, cat_pages, cat_files';
 457+ $tables = array_merge( $tables, array( 'category' ) );
 458+ $fields = array_merge( $fields, array( 'cat_id', 'cat_title', 'cat_subcats', 'cat_pages', 'cat_files' ) );
 459+ $joins['category'] = array( 'LEFT JOIN', 'cat_title = page_title AND page_namespace = ' . NS_CATEGORY );
474460 }
475461
476 - $page = $dbr->tableName( 'page' );
477 - $categorylinks = $dbr->tableName( 'categorylinks' );
 462+ $res = $dbr->select( $tables, $fields, $where, __METHOD__,
 463+ array( 'ORDER BY' => 'cl_sortkey', 'LIMIT' => $wgCategoryTreeMaxChildren ),
 464+ $joins );
478465
479 - $sql = "SELECT cat.page_namespace, cat.page_title,
480 - cl_to, cl_from
481 - $transFields
482 - $countFields
483 - FROM $page as cat
484 - $ctJoin $categorylinks ON $ctJoinCond
485 - $transJoin
486 - $countJoin
487 - WHERE $ctWhere
488 - $nsmatch
489 - " . /*AND cat.page_is_redirect = 0*/"
490 - $transWhere
491 - ORDER BY cl_sortkey";
492 - $sql = $dbr->limitResult( $sql, (int)$wgCategoryTreeMaxChildren );
493 -
494 - $res = $dbr->query( $sql, __METHOD__ );
495 -
496466 # collect categories separately from other pages
497467 $categories = '';
498468 $other = '';
499469
500 - while ( $row = $dbr->fetchObject( $res ) ) {
 470+ foreach ( $res as $row ) {
501471 # NOTE: in inverse mode, the page record may be null, because we use a right join.
502472 # happens for categories with no category page (red cat links)
503473 if ( $inverse && $row->page_title === null ) {
@@ -512,7 +482,7 @@
513483 $cat = Category::newFromRow( $row, $t );
514484 }
515485
516 - $s = $this->renderNodeInfo( $t, $cat, $depth -1, false );
 486+ $s = $this->renderNodeInfo( $t, $cat, $depth - 1, false );
517487 $s .= "\n\t\t";
518488
519489 if ( $row->page_namespace == NS_CATEGORY ) {
@@ -522,8 +492,6 @@
523493 }
524494 }
525495
526 - $dbr->freeResult( $res );
527 -
528496 return $categories . $other;
529497 }
530498

Status & tagging log