c9f4564aae795ee8fc5bb67cb1fc50678a3418bd
[lhc/web/wiklou.git] / includes / CategoryPage.php
1 <?php
2 /**
3 * Class for viewing MediaWiki category description pages.
4 * Modelled after ImagePage.php.
5 *
6 * @file
7 */
8
9 if ( !defined( 'MEDIAWIKI' ) )
10 die( 1 );
11
12 /**
13 * Special handling for category description pages, showing pages,
14 * subcategories and file that belong to the category
15 */
16 class CategoryPage extends Article {
17 # Subclasses can change this to override the viewer class.
18 protected $mCategoryViewerClass = 'CategoryViewer';
19
20 protected function newPage( Title $title ) {
21 // Overload mPage with a category-specific page
22 return new WikiCategoryPage( $title );
23 }
24
25 /**
26 * Constructor from a page id
27 * @param $id Int article ID to load
28 */
29 public static function newFromID( $id ) {
30 $t = Title::newFromID( $id );
31 # @todo FIXME: Doesn't inherit right
32 return $t == null ? null : new self( $t );
33 # return $t == null ? null : new static( $t ); // PHP 5.3
34 }
35
36 function view() {
37 global $wgRequest, $wgUser;
38
39 $diff = $wgRequest->getVal( 'diff' );
40 $diffOnly = $wgRequest->getBool( 'diffonly', $wgUser->getOption( 'diffonly' ) );
41
42 if ( isset( $diff ) && $diffOnly ) {
43 return parent::view();
44 }
45
46 if ( !wfRunHooks( 'CategoryPageView', array( &$this ) ) ) {
47 return;
48 }
49
50 if ( NS_CATEGORY == $this->mTitle->getNamespace() ) {
51 $this->openShowCategory();
52 }
53
54 parent::view();
55
56 if ( NS_CATEGORY == $this->mTitle->getNamespace() ) {
57 $this->closeShowCategory();
58 }
59 }
60
61 function openShowCategory() {
62 # For overloading
63 }
64
65 function closeShowCategory() {
66 global $wgOut, $wgRequest;
67
68 // Use these as defaults for back compat --catrope
69 $oldFrom = $wgRequest->getVal( 'from' );
70 $oldUntil = $wgRequest->getVal( 'until' );
71
72 $from = $until = array();
73 foreach ( array( 'page', 'subcat', 'file' ) as $type ) {
74 $from[$type] = $wgRequest->getVal( "{$type}from", $oldFrom );
75 $until[$type] = $wgRequest->getVal( "{$type}until", $oldUntil );
76 }
77
78 $viewer = new $this->mCategoryViewerClass( $this->mTitle, $from, $until, $wgRequest->getValues() );
79 $wgOut->addHTML( $viewer->getHTML() );
80 }
81 }
82
83 class CategoryViewer {
84 var $limit, $from, $until,
85 $articles, $articles_start_char,
86 $children, $children_start_char,
87 $showGallery, $imgsNoGalley,
88 $imgsNoGallery_start_char,
89 $skin, $imgsNoGallery;
90
91 /**
92 * @var
93 */
94 var $nextPage;
95
96 /**
97 * @var Array
98 */
99 var $flip;
100
101 /**
102 * @var Title
103 */
104 var $title;
105
106 /**
107 * @var Collation
108 */
109 var $collation;
110
111 /**
112 * @var ImageGallery
113 */
114 var $gallery;
115
116 /**
117 * Category object for this page
118 * @var Category
119 */
120 private $cat;
121
122 /**
123 * The original query array, to be used in generating paging links.
124 * @var array
125 */
126 private $query;
127
128 function __construct( $title, $from = '', $until = '', $query = array() ) {
129 global $wgCategoryPagingLimit;
130 $this->title = $title;
131 $this->from = $from;
132 $this->until = $until;
133 $this->limit = $wgCategoryPagingLimit;
134 $this->cat = Category::newFromTitle( $title );
135 $this->query = $query;
136 $this->collation = Collation::singleton();
137 unset( $this->query['title'] );
138 }
139
140 /**
141 * Format the category data list.
142 *
143 * @return string HTML output
144 */
145 public function getHTML() {
146 global $wgOut, $wgCategoryMagicGallery, $wgContLang, $wgBetterDirectionality;
147 wfProfileIn( __METHOD__ );
148
149 $this->showGallery = $wgCategoryMagicGallery && !$wgOut->mNoGallery;
150
151 $this->clearCategoryState();
152 $this->doCategoryQuery();
153 $this->finaliseCategoryState();
154
155 $r = $this->getSubcategorySection() .
156 $this->getPagesSection() .
157 $this->getImageSection();
158
159 if ( $r == '' ) {
160 // If there is no category content to display, only
161 // show the top part of the navigation links.
162 // @todo FIXME: Cannot be completely suppressed because it
163 // is unknown if 'until' or 'from' makes this
164 // give 0 results.
165 $r = $r . $this->getCategoryTop();
166 } else {
167 $r = $this->getCategoryTop() .
168 $r .
169 $this->getCategoryBottom();
170 }
171
172 // Give a proper message if category is empty
173 if ( $r == '' ) {
174 $r = wfMsgExt( 'category-empty', array( 'parse' ) );
175 }
176
177 if( $wgBetterDirectionality ) {
178 $pageLang = $this->title->getPageLanguage();
179 $langAttribs = array( 'lang' => $pageLang->getCode(), 'dir' => $pageLang->getDir() );
180 # close the previous div, show the headings in user language,
181 # then open a new div with the page content language again
182 $r = '</div>' . $r . Html::openElement( 'div', $langAttribs );
183 }
184
185 wfProfileOut( __METHOD__ );
186 return $wgContLang->convert( $r );
187 }
188
189 function clearCategoryState() {
190 $this->articles = array();
191 $this->articles_start_char = array();
192 $this->children = array();
193 $this->children_start_char = array();
194 if ( $this->showGallery ) {
195 $this->gallery = new ImageGallery();
196 $this->gallery->setHideBadImages();
197 } else {
198 $this->imgsNoGallery = array();
199 $this->imgsNoGallery_start_char = array();
200 }
201 }
202
203 /**
204 * @return Skin
205 */
206 function getSkin() {
207 if ( !$this->skin ) {
208 global $wgUser;
209 $this->skin = $wgUser->getSkin();
210 }
211 return $this->skin;
212 }
213
214 /**
215 * Add a subcategory to the internal lists, using a Category object
216 */
217 function addSubcategoryObject( Category $cat, $sortkey, $pageLength ) {
218 // Subcategory; strip the 'Category' namespace from the link text.
219 $title = $cat->getTitle();
220
221 $link = $this->getSkin()->link( $title, $title->getText() );
222 if ( $title->isRedirect() ) {
223 // This didn't used to add redirect-in-category, but might
224 // as well be consistent with the rest of the sections
225 // on a category page.
226 $link = '<span class="redirect-in-category">' . $link . '</span>';
227 }
228 $this->children[] = $link;
229
230 $this->children_start_char[] =
231 $this->getSubcategorySortChar( $cat->getTitle(), $sortkey );
232 }
233
234 /**
235 * Add a subcategory to the internal lists, using a title object
236 * @deprecated since 1.17 kept for compatibility, please use addSubcategoryObject instead
237 */
238 function addSubcategory( Title $title, $sortkey, $pageLength ) {
239 $this->addSubcategoryObject( Category::newFromTitle( $title ), $sortkey, $pageLength );
240 }
241
242 /**
243 * Get the character to be used for sorting subcategories.
244 * If there's a link from Category:A to Category:B, the sortkey of the resulting
245 * entry in the categorylinks table is Category:A, not A, which it SHOULD be.
246 * Workaround: If sortkey == "Category:".$title, than use $title for sorting,
247 * else use sortkey...
248 *
249 * @param Title $title
250 * @param string $sortkey The human-readable sortkey (before transforming to icu or whatever).
251 */
252 function getSubcategorySortChar( $title, $sortkey ) {
253 global $wgContLang;
254
255 if ( $title->getPrefixedText() == $sortkey ) {
256 $word = $title->getDBkey();
257 } else {
258 $word = $sortkey;
259 }
260
261 $firstChar = $this->collation->getFirstLetter( $word );
262
263 return $wgContLang->convert( $firstChar );
264 }
265
266 /**
267 * Add a page in the image namespace
268 */
269 function addImage( Title $title, $sortkey, $pageLength, $isRedirect = false ) {
270 global $wgContLang;
271 if ( $this->showGallery ) {
272 $flip = $this->flip['file'];
273 if ( $flip ) {
274 $this->gallery->insert( $title );
275 } else {
276 $this->gallery->add( $title );
277 }
278 } else {
279 $link = $this->getSkin()->link( $title );
280 if ( $isRedirect ) {
281 // This seems kind of pointless given 'mw-redirect' class,
282 // but keeping for back-compatibility with user css.
283 $link = '<span class="redirect-in-category">' . $link . '</span>';
284 }
285 $this->imgsNoGallery[] = $link;
286
287 $this->imgsNoGallery_start_char[] = $wgContLang->convert(
288 $this->collation->getFirstLetter( $sortkey ) );
289 }
290 }
291
292 /**
293 * Add a miscellaneous page
294 */
295 function addPage( $title, $sortkey, $pageLength, $isRedirect = false ) {
296 global $wgContLang;
297
298 $link = $this->getSkin()->link( $title );
299 if ( $isRedirect ) {
300 // This seems kind of pointless given 'mw-redirect' class,
301 // but keeping for back-compatiability with user css.
302 $link = '<span class="redirect-in-category">' . $link . '</span>';
303 }
304 $this->articles[] = $link;
305
306 $this->articles_start_char[] = $wgContLang->convert(
307 $this->collation->getFirstLetter( $sortkey ) );
308 }
309
310 function finaliseCategoryState() {
311 if ( $this->flip['subcat'] ) {
312 $this->children = array_reverse( $this->children );
313 $this->children_start_char = array_reverse( $this->children_start_char );
314 }
315 if ( $this->flip['page'] ) {
316 $this->articles = array_reverse( $this->articles );
317 $this->articles_start_char = array_reverse( $this->articles_start_char );
318 }
319 if ( !$this->showGallery && $this->flip['file'] ) {
320 $this->imgsNoGallery = array_reverse( $this->imgsNoGallery );
321 $this->imgsNoGallery_start_char = array_reverse( $this->imgsNoGallery_start_char );
322 }
323 }
324
325 function doCategoryQuery() {
326 $dbr = wfGetDB( DB_SLAVE, 'category' );
327
328 $this->nextPage = array(
329 'page' => null,
330 'subcat' => null,
331 'file' => null,
332 );
333 $this->flip = array( 'page' => false, 'subcat' => false, 'file' => false );
334
335 foreach ( array( 'page', 'subcat', 'file' ) as $type ) {
336 # Get the sortkeys for start/end, if applicable. Note that if
337 # the collation in the database differs from the one
338 # set in $wgCategoryCollation, pagination might go totally haywire.
339 $extraConds = array( 'cl_type' => $type );
340 if ( $this->from[$type] !== null ) {
341 $extraConds[] = 'cl_sortkey >= '
342 . $dbr->addQuotes( $this->collation->getSortKey( $this->from[$type] ) );
343 } elseif ( $this->until[$type] !== null ) {
344 $extraConds[] = 'cl_sortkey < '
345 . $dbr->addQuotes( $this->collation->getSortKey( $this->until[$type] ) );
346 $this->flip[$type] = true;
347 }
348
349 $res = $dbr->select(
350 array( 'page', 'categorylinks', 'category' ),
351 array( 'page_id', 'page_title', 'page_namespace', 'page_len',
352 'page_is_redirect', 'cl_sortkey', 'cat_id', 'cat_title',
353 'cat_subcats', 'cat_pages', 'cat_files',
354 'cl_sortkey_prefix', 'cl_collation' ),
355 array_merge( array( 'cl_to' => $this->title->getDBkey() ), $extraConds ),
356 __METHOD__,
357 array(
358 'USE INDEX' => array( 'categorylinks' => 'cl_sortkey' ),
359 'LIMIT' => $this->limit + 1,
360 'ORDER BY' => $this->flip[$type] ? 'cl_sortkey DESC' : 'cl_sortkey',
361 ),
362 array(
363 'categorylinks' => array( 'INNER JOIN', 'cl_from = page_id' ),
364 'category' => array( 'LEFT JOIN', 'cat_title = page_title AND page_namespace = ' . NS_CATEGORY )
365 )
366 );
367
368 $count = 0;
369 foreach ( $res as $row ) {
370 $title = Title::newFromRow( $row );
371 if ( $row->cl_collation === '' ) {
372 // Hack to make sure that while updating from 1.16 schema
373 // and db is inconsistent, that the sky doesn't fall.
374 // See r83544. Could perhaps be removed in a couple decades...
375 $humanSortkey = $row->cl_sortkey;
376 } else {
377 $humanSortkey = $title->getCategorySortkey( $row->cl_sortkey_prefix );
378 }
379
380 if ( ++$count > $this->limit ) {
381 # We've reached the one extra which shows that there
382 # are additional pages to be had. Stop here...
383 $this->nextPage[$type] = $humanSortkey;
384 break;
385 }
386
387 if ( $title->getNamespace() == NS_CATEGORY ) {
388 $cat = Category::newFromRow( $row, $title );
389 $this->addSubcategoryObject( $cat, $humanSortkey, $row->page_len );
390 } elseif ( $title->getNamespace() == NS_FILE ) {
391 $this->addImage( $title, $humanSortkey, $row->page_len, $row->page_is_redirect );
392 } else {
393 $this->addPage( $title, $humanSortkey, $row->page_len, $row->page_is_redirect );
394 }
395 }
396 }
397 }
398
399 function getCategoryTop() {
400 $r = $this->getCategoryBottom();
401 return $r === ''
402 ? $r
403 : "<br style=\"clear:both;\"/>\n" . $r;
404 }
405
406 function getSubcategorySection() {
407 # Don't show subcategories section if there are none.
408 $r = '';
409 $rescnt = count( $this->children );
410 $dbcnt = $this->cat->getSubcatCount();
411 $countmsg = $this->getCountMessage( $rescnt, $dbcnt, 'subcat' );
412
413 if ( $rescnt > 0 ) {
414 # Showing subcategories
415 $r .= "<div id=\"mw-subcategories\">\n";
416 $r .= '<h2>' . wfMsg( 'subcategories' ) . "</h2>\n";
417 $r .= $countmsg;
418 $r .= $this->getSectionPagingLinks( 'subcat' );
419 $r .= $this->formatList( $this->children, $this->children_start_char );
420 $r .= $this->getSectionPagingLinks( 'subcat' );
421 $r .= "\n</div>";
422 }
423 return $r;
424 }
425
426 function getPagesSection() {
427 $ti = htmlspecialchars( $this->title->getText() );
428 # Don't show articles section if there are none.
429 $r = '';
430
431 # @todo FIXME: Here and in the other two sections: we don't need to bother
432 # with this rigamarole if the entire category contents fit on one page
433 # and have already been retrieved. We can just use $rescnt in that
434 # case and save a query and some logic.
435 $dbcnt = $this->cat->getPageCount() - $this->cat->getSubcatCount()
436 - $this->cat->getFileCount();
437 $rescnt = count( $this->articles );
438 $countmsg = $this->getCountMessage( $rescnt, $dbcnt, 'article' );
439
440 if ( $rescnt > 0 ) {
441 $r = "<div id=\"mw-pages\">\n";
442 $r .= '<h2>' . wfMsg( 'category_header', $ti ) . "</h2>\n";
443 $r .= $countmsg;
444 $r .= $this->getSectionPagingLinks( 'page' );
445 $r .= $this->formatList( $this->articles, $this->articles_start_char );
446 $r .= $this->getSectionPagingLinks( 'page' );
447 $r .= "\n</div>";
448 }
449 return $r;
450 }
451
452 function getImageSection() {
453 $r = '';
454 $rescnt = $this->showGallery ? $this->gallery->count() : count( $this->imgsNoGallery );
455 if ( $rescnt > 0 ) {
456 $dbcnt = $this->cat->getFileCount();
457 $countmsg = $this->getCountMessage( $rescnt, $dbcnt, 'file' );
458
459 $r .= "<div id=\"mw-category-media\">\n";
460 $r .= '<h2>' . wfMsg( 'category-media-header', htmlspecialchars( $this->title->getText() ) ) . "</h2>\n";
461 $r .= $countmsg;
462 $r .= $this->getSectionPagingLinks( 'file' );
463 if ( $this->showGallery ) {
464 $r .= $this->gallery->toHTML();
465 } else {
466 $r .= $this->formatList( $this->imgsNoGallery, $this->imgsNoGallery_start_char );
467 }
468 $r .= $this->getSectionPagingLinks( 'file' );
469 $r .= "\n</div>";
470 }
471 return $r;
472 }
473
474 /**
475 * Get the paging links for a section (subcats/pages/files), to go at the top and bottom
476 * of the output.
477 *
478 * @param $type String: 'page', 'subcat', or 'file'
479 * @return String: HTML output, possibly empty if there are no other pages
480 */
481 private function getSectionPagingLinks( $type ) {
482 if ( $this->until[$type] !== null ) {
483 return $this->pagingLinks( $this->nextPage[$type], $this->until[$type], $type );
484 } elseif ( $this->nextPage[$type] !== null || $this->from[$type] !== null ) {
485 return $this->pagingLinks( $this->from[$type], $this->nextPage[$type], $type );
486 } else {
487 return '';
488 }
489 }
490
491 function getCategoryBottom() {
492 return '';
493 }
494
495 /**
496 * Format a list of articles chunked by letter, either as a
497 * bullet list or a columnar format, depending on the length.
498 *
499 * @param $articles Array
500 * @param $articles_start_char Array
501 * @param $cutoff Int
502 * @return String
503 * @private
504 */
505 function formatList( $articles, $articles_start_char, $cutoff = 6 ) {
506 global $wgBetterDirectionality;
507
508 $list = '';
509 if ( count ( $articles ) > $cutoff ) {
510 $list = self::columnList( $articles, $articles_start_char );
511 } elseif ( count( $articles ) > 0 ) {
512 // for short lists of articles in categories.
513 $list = self::shortList( $articles, $articles_start_char );
514 }
515
516 if( $wgBetterDirectionality ) {
517 $pageLang = $this->title->getPageLanguage();
518 $attribs = array( 'lang' => $pageLang->getCode(), 'dir' => $pageLang->getDir(),
519 'class' => 'mw-content-'.$pageLang->getDir() );
520 $list = Html::rawElement( 'div', $attribs, $list );
521 }
522
523 return $list;
524 }
525
526 /**
527 * Format a list of articles chunked by letter in a three-column
528 * list, ordered vertically.
529 *
530 * TODO: Take the headers into account when creating columns, so they're
531 * more visually equal.
532 *
533 * More distant TODO: Scrap this and use CSS columns, whenever IE finally
534 * supports those.
535 *
536 * @param $articles Array
537 * @param $articles_start_char Array
538 * @return String
539 * @private
540 */
541 static function columnList( $articles, $articles_start_char ) {
542 $columns = array_combine( $articles, $articles_start_char );
543 # Split into three columns
544 $columns = array_chunk( $columns, ceil( count( $columns ) / 3 ), true /* preserve keys */ );
545
546 $ret = '<table width="100%"><tr valign="top"><td>';
547 $prevchar = null;
548
549 foreach ( $columns as $column ) {
550 $colContents = array();
551
552 # Kind of like array_flip() here, but we keep duplicates in an
553 # array instead of dropping them.
554 foreach ( $column as $article => $char ) {
555 if ( !isset( $colContents[$char] ) ) {
556 $colContents[$char] = array();
557 }
558 $colContents[$char][] = $article;
559 }
560
561 $first = true;
562 foreach ( $colContents as $char => $articles ) {
563 $ret .= '<h3>' . htmlspecialchars( $char );
564 if ( $first && $char === $prevchar ) {
565 # We're continuing a previous chunk at the top of a new
566 # column, so add " cont." after the letter.
567 $ret .= ' ' . wfMsgHtml( 'listingcontinuesabbrev' );
568 }
569 $ret .= "</h3>\n";
570
571 $ret .= '<ul><li>';
572 $ret .= implode( "</li>\n<li>", $articles );
573 $ret .= '</li></ul>';
574
575 $first = false;
576 $prevchar = $char;
577 }
578
579 $ret .= "</td>\n<td>";
580 }
581
582 $ret .= '</td></tr></table>';
583 return $ret;
584 }
585
586 /**
587 * Format a list of articles chunked by letter in a bullet list.
588 * @param $articles Array
589 * @param $articles_start_char Array
590 * @return String
591 * @private
592 */
593 static function shortList( $articles, $articles_start_char ) {
594 $r = '<h3>' . htmlspecialchars( $articles_start_char[0] ) . "</h3>\n";
595 $r .= '<ul><li>' . $articles[0] . '</li>';
596 for ( $index = 1; $index < count( $articles ); $index++ ) {
597 if ( $articles_start_char[$index] != $articles_start_char[$index - 1] ) {
598 $r .= "</ul><h3>" . htmlspecialchars( $articles_start_char[$index] ) . "</h3>\n<ul>";
599 }
600
601 $r .= "<li>{$articles[$index]}</li>";
602 }
603 $r .= '</ul>';
604 return $r;
605 }
606
607 /**
608 * Create paging links, as a helper method to getSectionPagingLinks().
609 *
610 * @param $first String The 'until' parameter for the generated URL
611 * @param $last String The 'from' parameter for the genererated URL
612 * @param $type String A prefix for parameters, 'page' or 'subcat' or
613 * 'file'
614 * @return String HTML
615 */
616 private function pagingLinks( $first, $last, $type = '' ) {
617 global $wgLang;
618 $sk = $this->getSkin();
619 $limitText = $wgLang->formatNum( $this->limit );
620
621 $prevLink = wfMsgExt( 'prevn', array( 'escape', 'parsemag' ), $limitText );
622
623 if ( $first != '' ) {
624 $prevQuery = $this->query;
625 $prevQuery["{$type}until"] = $first;
626 unset( $prevQuery["{$type}from"] );
627 $prevLink = $sk->linkKnown(
628 $this->addFragmentToTitle( $this->title, $type ),
629 $prevLink,
630 array(),
631 $prevQuery
632 );
633 }
634
635 $nextLink = wfMsgExt( 'nextn', array( 'escape', 'parsemag' ), $limitText );
636
637 if ( $last != '' ) {
638 $lastQuery = $this->query;
639 $lastQuery["{$type}from"] = $last;
640 unset( $lastQuery["{$type}until"] );
641 $nextLink = $sk->linkKnown(
642 $this->addFragmentToTitle( $this->title, $type ),
643 $nextLink,
644 array(),
645 $lastQuery
646 );
647 }
648
649 return "($prevLink) ($nextLink)";
650 }
651
652 /**
653 * Takes a title, and adds the fragment identifier that
654 * corresponds to the correct segment of the category.
655 *
656 * @param Title $title: The title (usually $this->title)
657 * @param String $section: Which section
658 */
659 private function addFragmentToTitle( $title, $section ) {
660 switch ( $section ) {
661 case 'page':
662 $fragment = 'mw-pages';
663 break;
664 case 'subcat':
665 $fragment = 'mw-subcategories';
666 break;
667 case 'file':
668 $fragment = 'mw-category-media';
669 break;
670 default:
671 throw new MWException( __METHOD__ .
672 " Invalid section $section." );
673 }
674
675 return Title::makeTitle( $title->getNamespace(),
676 $title->getDBkey(), $fragment );
677 }
678 /**
679 * What to do if the category table conflicts with the number of results
680 * returned? This function says what. Each type is considered independently
681 * of the other types.
682 *
683 * Note for grepping: uses the messages category-article-count,
684 * category-article-count-limited, category-subcat-count,
685 * category-subcat-count-limited, category-file-count,
686 * category-file-count-limited.
687 *
688 * @param $rescnt Int: The number of items returned by our database query.
689 * @param $dbcnt Int: The number of items according to the category table.
690 * @param $type String: 'subcat', 'article', or 'file'
691 * @return String: A message giving the number of items, to output to HTML.
692 */
693 private function getCountMessage( $rescnt, $dbcnt, $type ) {
694 global $wgLang;
695 # There are three cases:
696 # 1) The category table figure seems sane. It might be wrong, but
697 # we can't do anything about it if we don't recalculate it on ev-
698 # ery category view.
699 # 2) The category table figure isn't sane, like it's smaller than the
700 # number of actual results, *but* the number of results is less
701 # than $this->limit and there's no offset. In this case we still
702 # know the right figure.
703 # 3) We have no idea.
704
705 # Check if there's a "from" or "until" for anything
706
707 // This is a little ugly, but we seem to use different names
708 // for the paging types then for the messages.
709 if ( $type === 'article' ) {
710 $pagingType = 'page';
711 } else {
712 $pagingType = $type;
713 }
714
715 $fromOrUntil = false;
716 if ( $this->from[$pagingType] !== null || $this->until[$pagingType] !== null ) {
717 $fromOrUntil = true;
718 }
719
720 if ( $dbcnt == $rescnt || ( ( $rescnt == $this->limit || $fromOrUntil )
721 && $dbcnt > $rescnt ) ) {
722 # Case 1: seems sane.
723 $totalcnt = $dbcnt;
724 } elseif ( $rescnt < $this->limit && !$fromOrUntil ) {
725 # Case 2: not sane, but salvageable. Use the number of results.
726 # Since there are fewer than 200, we can also take this opportunity
727 # to refresh the incorrect category table entry -- which should be
728 # quick due to the small number of entries.
729 $totalcnt = $rescnt;
730 $this->cat->refreshCounts();
731 } else {
732 # Case 3: hopeless. Don't give a total count at all.
733 return wfMsgExt( "category-$type-count-limited", 'parse',
734 $wgLang->formatNum( $rescnt ) );
735 }
736 return wfMsgExt(
737 "category-$type-count",
738 'parse',
739 $wgLang->formatNum( $rescnt ),
740 $wgLang->formatNum( $totalcnt )
741 );
742 }
743 }