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