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