3 * Special handling for category description pages.
4 * Modelled after ImagePage.php.
9 if ( !defined( 'MEDIAWIKI' ) )
13 * Special handling for category description pages, showing pages,
14 * subcategories and file that belong to the category
16 class CategoryPage extends Article {
17 # Subclasses can change this to override the viewer class.
18 protected $mCategoryViewerClass = 'CategoryViewer';
21 global $wgRequest, $wgUser;
23 $diff = $wgRequest->getVal( 'diff' );
24 $diffOnly = $wgRequest->getBool( 'diffonly', $wgUser->getOption( 'diffonly' ) );
26 if ( isset( $diff ) && $diffOnly ) {
27 return parent::view();
30 if ( !wfRunHooks( 'CategoryPageView', array( &$this ) ) ) {
34 if ( NS_CATEGORY == $this->mTitle->getNamespace() ) {
35 $this->openShowCategory();
40 if ( NS_CATEGORY == $this->mTitle->getNamespace() ) {
41 $this->closeShowCategory();
46 * Don't return a 404 for categories in use.
47 * In use defined as: either the actual page exists
48 * or the category currently has members.
50 function hasViewableContent() {
51 if ( parent::hasViewableContent() ) {
54 $cat = Category::newFromTitle( $this->mTitle );
55 // If any of these are not 0, then has members
56 if ( $cat->getPageCount()
57 || $cat->getSubcatCount()
58 || $cat->getFileCount()
66 function openShowCategory() {
70 function closeShowCategory() {
71 global $wgOut, $wgRequest;
73 $from = $until = array();
74 foreach ( array( 'page', 'subcat', 'file' ) as $type ) {
75 $from[$type] = $wgRequest->getVal( "{$type}from" );
76 $until[$type] = $wgRequest->getVal( "{$type}until" );
79 $viewer = new $this->mCategoryViewerClass( $this->mTitle, $from, $until, $wgRequest->getValues() );
80 $wgOut->addHTML( $viewer->getHTML() );
84 class CategoryViewer {
85 var $title, $limit, $from, $until,
86 $articles, $articles_start_char,
87 $children, $children_start_char,
88 $showGallery, $gallery,
89 $imgsNoGalley, $imgsNoGallery_start_char,
91 # Category object for this page
93 # The original query array, to be used in generating paging links.
96 function __construct( $title, $from = '', $until = '', $query = array() ) {
97 global $wgCategoryPagingLimit;
98 $this->title = $title;
100 $this->until = $until;
101 $this->limit = $wgCategoryPagingLimit;
102 $this->cat = Category::newFromTitle( $title );
103 $this->query = $query;
104 $this->collation = Collation::singleton();
105 unset( $this->query['title'] );
109 * Format the category data list.
111 * @return string HTML output
113 public function getHTML() {
114 global $wgOut, $wgCategoryMagicGallery, $wgContLang;
115 wfProfileIn( __METHOD__ );
117 $this->showGallery = $wgCategoryMagicGallery && !$wgOut->mNoGallery;
119 $this->clearCategoryState();
120 $this->doCategoryQuery();
121 $this->finaliseCategoryState();
123 $r = $this->getSubcategorySection() .
124 $this->getPagesSection() .
125 $this->getImageSection();
128 // If there is no category content to display, only
129 // show the top part of the navigation links.
130 // FIXME: cannot be completely suppressed because it
131 // is unknown if 'until' or 'from' makes this
133 $r = $r . $this->getCategoryTop();
135 $r = $this->getCategoryTop() .
137 $this->getCategoryBottom();
140 // Give a proper message if category is empty
142 $r = wfMsgExt( 'category-empty', array( 'parse' ) );
145 wfProfileOut( __METHOD__ );
146 return $wgContLang->convert( $r );
149 function clearCategoryState() {
150 $this->articles = array();
151 $this->articles_start_char = array();
152 $this->children = array();
153 $this->children_start_char = array();
154 if ( $this->showGallery ) {
155 $this->gallery = new ImageGallery();
156 $this->gallery->setHideBadImages();
158 $this->imgsNoGallery = array();
159 $this->imgsNoGallery_start_char = array();
164 if ( !$this->skin ) {
166 $this->skin = $wgUser->getSkin();
172 * Add a subcategory to the internal lists, using a Category object
174 function addSubcategoryObject( Category $cat, $sortkey, $pageLength ) {
175 // Subcategory; strip the 'Category' namespace from the link text.
176 $title = $cat->getTitle();
178 $link = $this->getSkin()->link( $title, $title->getText() );
179 if ( $title->isRedirect() ) {
180 // This didn't used to add redirect-in-category, but might
181 // as well be consistent with the rest of the sections
182 // on a category page.
183 $link = '<span class="redirect-in-category">' . $link . '</span>';
185 $this->children[] = $link;
187 $this->children_start_char[] =
188 $this->getSubcategorySortChar( $cat->getTitle(), $sortkey );
192 * Add a subcategory to the internal lists, using a title object
193 * @deprecated kept for compatibility, please use addSubcategoryObject instead
195 function addSubcategory( Title $title, $sortkey, $pageLength ) {
196 $this->addSubcategoryObject( Category::newFromTitle( $title ), $sortkey, $pageLength );
200 * Get the character to be used for sorting subcategories.
201 * If there's a link from Category:A to Category:B, the sortkey of the resulting
202 * entry in the categorylinks table is Category:A, not A, which it SHOULD be.
203 * Workaround: If sortkey == "Category:".$title, than use $title for sorting,
204 * else use sortkey...
206 * @param Title $title
207 * @param string $sortkey The human-readable sortkey (before transforming to icu or whatever).
209 function getSubcategorySortChar( $title, $sortkey ) {
212 if ( $title->getPrefixedText() == $sortkey ) {
213 $word = $title->getDBkey();
218 $firstChar = $this->collation->getFirstLetter( $word );
220 return $wgContLang->convert( $firstChar );
224 * Add a page in the image namespace
226 function addImage( Title $title, $sortkey, $pageLength, $isRedirect = false ) {
228 if ( $this->showGallery ) {
229 $flip = $this->flip['file'];
231 $this->gallery->insert( $title );
233 $this->gallery->add( $title );
236 $link = $this->getSkin()->link( $title );
238 // This seems kind of pointless given 'mw-redirect' class,
239 // but keeping for back-compatibility with user css.
240 $link = '<span class="redirect-in-category">' . $link . '</span>';
242 $this->imgsNoGallery[] = $link;
244 $this->imgsNoGallery_start_char[] = $wgContLang->convert(
245 $this->collation->getFirstLetter( $sortkey ) );
250 * Add a miscellaneous page
252 function addPage( $title, $sortkey, $pageLength, $isRedirect = false ) {
255 $link = $this->getSkin()->link( $title );
257 // This seems kind of pointless given 'mw-redirect' class,
258 // but keeping for back-compatiability with user css.
259 $link = '<span class="redirect-in-category">' . $link . '</span>';
261 $this->articles[] = $link;
263 $this->articles_start_char[] = $wgContLang->convert(
264 $this->collation->getFirstLetter( $sortkey ) );
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 );
272 if ( $this->flip['page'] ) {
273 $this->articles = array_reverse( $this->articles );
274 $this->articles_start_char = array_reverse( $this->articles_start_char );
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 );
282 function doCategoryQuery() {
283 $dbr = wfGetDB( DB_SLAVE, 'category' );
285 $this->nextPage = array(
290 $this->flip = array( 'page' => false, 'subcat' => false, 'file' => false );
292 foreach ( array( 'page', 'subcat', 'file' ) as $type ) {
293 # Get the sortkeys for start/end, if applicable. Note that if
294 # the collation in the database differs from the one
295 # set in $wgCategoryCollation, pagination might go totally haywire.
296 $extraConds = array( 'cl_type' => $type );
297 if ( $this->from[$type] !== null ) {
298 $extraConds[] = 'cl_sortkey >= '
299 . $dbr->addQuotes( $this->collation->getSortKey( $this->from[$type] ) );
300 } elseif ( $this->until[$type] !== null ) {
301 $extraConds[] = 'cl_sortkey < '
302 . $dbr->addQuotes( $this->collation->getSortKey( $this->until[$type] ) );
303 $this->flip[$type] = true;
307 array( 'page', 'categorylinks', 'category' ),
308 array( 'page_id', 'page_title', 'page_namespace', 'page_len',
309 'page_is_redirect', 'cl_sortkey', 'cat_id', 'cat_title',
310 'cat_subcats', 'cat_pages', 'cat_files',
311 'cl_sortkey_prefix', 'cl_collation' ),
312 array( 'cl_to' => $this->title->getDBkey() ) + $extraConds,
315 'USE INDEX' => array( 'categorylinks' => 'cl_sortkey' ),
316 'LIMIT' => $this->limit + 1,
317 'ORDER BY' => $this->flip[$type] ? 'cl_sortkey DESC' : 'cl_sortkey',
320 'categorylinks' => array( 'INNER JOIN', 'cl_from = page_id' ),
321 'category' => array( 'LEFT JOIN', 'cat_title = page_title AND page_namespace = ' . NS_CATEGORY )
326 foreach ( $res as $row ) {
327 $title = Title::newFromRow( $row );
328 if ( $row->cl_collation === '' ) {
329 // Hack to make sure that while updating from 1.16 schema
330 // and db is inconsistent, that the sky doesn't fall.
331 // See r83544. Could perhaps be removed in a couple decades...
332 $humanSortkey = $row->cl_sortkey;
334 $humanSortkey = $title->getCategorySortkey( $row->cl_sortkey_prefix );
337 if ( ++$count > $this->limit ) {
338 # We've reached the one extra which shows that there
339 # are additional pages to be had. Stop here...
340 $this->nextPage[$type] = $humanSortkey;
344 if ( $title->getNamespace() == NS_CATEGORY ) {
345 $cat = Category::newFromRow( $row, $title );
346 $this->addSubcategoryObject( $cat, $humanSortkey, $row->page_len );
347 } elseif ( $title->getNamespace() == NS_FILE ) {
348 $this->addImage( $title, $humanSortkey, $row->page_len, $row->page_is_redirect );
350 $this->addPage( $title, $humanSortkey, $row->page_len, $row->page_is_redirect );
356 function getCategoryTop() {
357 $r = $this->getCategoryBottom();
360 : "<br style=\"clear:both;\"/>\n" . $r;
363 function getSubcategorySection() {
364 # Don't show subcategories section if there are none.
366 $rescnt = count( $this->children );
367 $dbcnt = $this->cat->getSubcatCount();
368 $countmsg = $this->getCountMessage( $rescnt, $dbcnt, 'subcat' );
371 # Showing subcategories
372 $r .= "<div id=\"mw-subcategories\">\n";
373 $r .= '<h2>' . wfMsg( 'subcategories' ) . "</h2>\n";
375 $r .= $this->getSectionPagingLinks( 'subcat' );
376 $r .= $this->formatList( $this->children, $this->children_start_char );
377 $r .= $this->getSectionPagingLinks( 'subcat' );
383 function getPagesSection() {
384 $ti = htmlspecialchars( $this->title->getText() );
385 # Don't show articles section if there are none.
388 # FIXME, here and in the other two sections: we don't need to bother
389 # with this rigamarole if the entire category contents fit on one page
390 # and have already been retrieved. We can just use $rescnt in that
391 # case and save a query and some logic.
392 $dbcnt = $this->cat->getPageCount() - $this->cat->getSubcatCount()
393 - $this->cat->getFileCount();
394 $rescnt = count( $this->articles );
395 $countmsg = $this->getCountMessage( $rescnt, $dbcnt, 'article' );
398 $r = "<div id=\"mw-pages\">\n";
399 $r .= '<h2>' . wfMsg( 'category_header', $ti ) . "</h2>\n";
401 $r .= $this->getSectionPagingLinks( 'page' );
402 $r .= $this->formatList( $this->articles, $this->articles_start_char );
403 $r .= $this->getSectionPagingLinks( 'page' );
409 function getImageSection() {
411 $rescnt = $this->showGallery ? $this->gallery->count() : count( $this->imgsNoGallery );
413 $dbcnt = $this->cat->getFileCount();
414 $countmsg = $this->getCountMessage( $rescnt, $dbcnt, 'file' );
416 $r .= "<div id=\"mw-category-media\">\n";
417 $r .= '<h2>' . wfMsg( 'category-media-header', htmlspecialchars( $this->title->getText() ) ) . "</h2>\n";
419 $r .= $this->getSectionPagingLinks( 'file' );
420 if ( $this->showGallery ) {
421 $r .= $this->gallery->toHTML();
423 $r .= $this->formatList( $this->imgsNoGallery, $this->imgsNoGallery_start_char );
425 $r .= $this->getSectionPagingLinks( 'file' );
432 * Get the paging links for a section (subcats/pages/files), to go at the top and bottom
435 * @param $type String: 'page', 'subcat', or 'file'
436 * @return String: HTML output, possibly empty if there are no other pages
438 private function getSectionPagingLinks( $type ) {
439 if ( $this->until[$type] !== null ) {
440 return $this->pagingLinks( $this->nextPage[$type], $this->until[$type], $type );
441 } elseif ( $this->nextPage[$type] !== null || $this->from[$type] !== null ) {
442 return $this->pagingLinks( $this->from[$type], $this->nextPage[$type], $type );
448 function getCategoryBottom() {
453 * Format a list of articles chunked by letter, either as a
454 * bullet list or a columnar format, depending on the length.
456 * @param $articles Array
457 * @param $articles_start_char Array
462 function formatList( $articles, $articles_start_char, $cutoff = 6 ) {
463 if ( count ( $articles ) > $cutoff ) {
464 return self::columnList( $articles, $articles_start_char );
465 } elseif ( count( $articles ) > 0 ) {
466 // for short lists of articles in categories.
467 return self::shortList( $articles, $articles_start_char );
473 * Format a list of articles chunked by letter in a three-column
474 * list, ordered vertically.
476 * TODO: Take the headers into account when creating columns, so they're
477 * more visually equal.
479 * More distant TODO: Scrap this and use CSS columns, whenever IE finally
482 * @param $articles Array
483 * @param $articles_start_char Array
487 static function columnList( $articles, $articles_start_char ) {
488 $columns = array_combine( $articles, $articles_start_char );
489 # Split into three columns
490 $columns = array_chunk( $columns, ceil( count( $columns ) / 3 ), true /* preserve keys */ );
492 $ret = '<table width="100%"><tr valign="top"><td>';
495 foreach ( $columns as $column ) {
496 $colContents = array();
498 # Kind of like array_flip() here, but we keep duplicates in an
499 # array instead of dropping them.
500 foreach ( $column as $article => $char ) {
501 if ( !isset( $colContents[$char] ) ) {
502 $colContents[$char] = array();
504 $colContents[$char][] = $article;
508 foreach ( $colContents as $char => $articles ) {
509 $ret .= '<h3>' . htmlspecialchars( $char );
510 if ( $first && $char === $prevchar ) {
511 # We're continuing a previous chunk at the top of a new
512 # column, so add " cont." after the letter.
513 $ret .= ' ' . wfMsgHtml( 'listingcontinuesabbrev' );
518 $ret .= implode( "</li>\n<li>", $articles );
519 $ret .= '</li></ul>';
525 $ret .= "</td>\n<td>";
528 $ret .= '</td></tr></table>';
533 * Format a list of articles chunked by letter in a bullet list.
534 * @param $articles Array
535 * @param $articles_start_char Array
539 static function shortList( $articles, $articles_start_char ) {
540 $r = '<h3>' . htmlspecialchars( $articles_start_char[0] ) . "</h3>\n";
541 $r .= '<ul><li>' . $articles[0] . '</li>';
542 for ( $index = 1; $index < count( $articles ); $index++ )
544 if ( $articles_start_char[$index] != $articles_start_char[$index - 1] )
546 $r .= "</ul><h3>" . htmlspecialchars( $articles_start_char[$index] ) . "</h3>\n<ul>";
549 $r .= "<li>{$articles[$index]}</li>";
556 * Create paging links, as a helper method to getSectionPagingLinks().
558 * @param $first String The 'until' parameter for the generated URL
559 * @param $last String The 'from' parameter for the genererated URL
560 * @param $type String A prefix for parameters, 'page' or 'subcat' or
562 * @return String HTML
564 private function pagingLinks( $first, $last, $type = '' ) {
566 $sk = $this->getSkin();
567 $limitText = $wgLang->formatNum( $this->limit );
569 $prevLink = wfMsgExt( 'prevn', array( 'escape', 'parsemag' ), $limitText );
571 if ( $first != '' ) {
572 $prevQuery = $this->query;
573 $prevQuery["{$type}until"] = $first;
574 unset( $prevQuery["{$type}from"] );
575 $prevLink = $sk->linkKnown(
583 $nextLink = wfMsgExt( 'nextn', array( 'escape', 'parsemag' ), $limitText );
586 $lastQuery = $this->query;
587 $lastQuery["{$type}from"] = $last;
588 unset( $lastQuery["{$type}until"] );
589 $nextLink = $sk->linkKnown(
597 return "($prevLink) ($nextLink)";
601 * What to do if the category table conflicts with the number of results
602 * returned? This function says what. Each type is considered independantly
603 * of the other types.
605 * Note for grepping: uses the messages category-article-count,
606 * category-article-count-limited, category-subcat-count,
607 * category-subcat-count-limited, category-file-count,
608 * category-file-count-limited.
610 * @param $rescnt Int: The number of items returned by our database query.
611 * @param $dbcnt Int: The number of items according to the category table.
612 * @param $type String: 'subcat', 'article', or 'file'
613 * @return String: A message giving the number of items, to output to HTML.
615 private function getCountMessage( $rescnt, $dbcnt, $type ) {
617 # There are three cases:
618 # 1) The category table figure seems sane. It might be wrong, but
619 # we can't do anything about it if we don't recalculate it on ev-
621 # 2) The category table figure isn't sane, like it's smaller than the
622 # number of actual results, *but* the number of results is less
623 # than $this->limit and there's no offset. In this case we still
624 # know the right figure.
625 # 3) We have no idea.
627 # Check if there's a "from" or "until" for anything
629 // This is a little ugly, but we seem to use different names
630 // for the paging types then for the messages.
631 if ( $type === 'article' ) {
632 $pagingType = 'page';
637 $fromOrUntil = false;
638 if ( $this->from[$pagingType] !== null || $this->until[$pagingType] !== null ) {
642 if ( $dbcnt == $rescnt || ( ( $rescnt == $this->limit || $fromOrUntil )
643 && $dbcnt > $rescnt ) )
645 # Case 1: seems sane.
647 } elseif ( $rescnt < $this->limit && !$fromOrUntil ) {
648 # Case 2: not sane, but salvageable. Use the number of results.
649 # Since there are fewer than 200, we can also take this opportunity
650 # to refresh the incorrect category table entry -- which should be
651 # quick due to the small number of entries.
653 $this->cat->refreshCounts();
655 # Case 3: hopeless. Don't give a total count at all.
656 return wfMsgExt( "category-$type-count-limited", 'parse',
657 $wgLang->formatNum( $rescnt ) );
660 "category-$type-count",
662 $wgLang->formatNum( $rescnt ),
663 $wgLang->formatNum( $totalcnt )