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