Index: trunk/extensions/CategoryTree/CategoryTreeFunctions.php |
— | — | @@ -424,79 +424,49 @@ |
425 | 425 | $mode = $this->getOption( 'mode' ); |
426 | 426 | $namespaces = $this->getOption( 'namespaces' ); |
427 | 427 | |
| 428 | + $tables = array( 'page', 'categorylinks' ); |
| 429 | + $fields = array( 'page_namespace', 'page_title', 'cl_to', 'cl_from' ); |
| 430 | + $where = array(); |
| 431 | + $joins = array(); |
| 432 | + |
428 | 433 | 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(); |
438 | 439 | |
439 | 440 | # namespace filter. |
440 | 441 | if ( $namespaces ) { |
441 | 442 | # 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 ) ); |
444 | 447 | } else { |
445 | | - $nsmatch = ' AND cat.page_namespace IN ( ' . implode( ', ', $namespaces ) . ') '; |
| 448 | + $where['page_namespace'] = NS_CATEGORY; |
446 | 449 | } |
447 | 450 | } |
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 | | - } |
457 | 451 | } |
458 | 452 | |
459 | | - # additional stuff to be used if "transaltion" by interwiki-links is desired |
460 | | - $transFields = ''; |
461 | | - $transJoin = ''; |
462 | | - $transWhere = ''; |
463 | | - |
464 | 453 | # fetch member count if possible |
465 | 454 | $doCount = !$inverse && $wgCategoryTreeUseCategoryTable; |
466 | 455 | |
467 | | - $countFields = ''; |
468 | | - $countJoin = ''; |
469 | | - |
470 | 456 | 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 ); |
474 | 460 | } |
475 | 461 | |
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 ); |
478 | 465 | |
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 | | - |
496 | 466 | # collect categories separately from other pages |
497 | 467 | $categories = ''; |
498 | 468 | $other = ''; |
499 | 469 | |
500 | | - while ( $row = $dbr->fetchObject( $res ) ) { |
| 470 | + foreach ( $res as $row ) { |
501 | 471 | # NOTE: in inverse mode, the page record may be null, because we use a right join. |
502 | 472 | # happens for categories with no category page (red cat links) |
503 | 473 | if ( $inverse && $row->page_title === null ) { |
— | — | @@ -512,7 +482,7 @@ |
513 | 483 | $cat = Category::newFromRow( $row, $t ); |
514 | 484 | } |
515 | 485 | |
516 | | - $s = $this->renderNodeInfo( $t, $cat, $depth -1, false ); |
| 486 | + $s = $this->renderNodeInfo( $t, $cat, $depth - 1, false ); |
517 | 487 | $s .= "\n\t\t"; |
518 | 488 | |
519 | 489 | if ( $row->page_namespace == NS_CATEGORY ) { |
— | — | @@ -522,8 +492,6 @@ |
523 | 493 | } |
524 | 494 | } |
525 | 495 | |
526 | | - $dbr->freeResult( $res ); |
527 | | - |
528 | 496 | return $categories . $other; |
529 | 497 | } |
530 | 498 | |