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.
48 function hasViewableContent() {
49 if ( parent
::hasViewableContent() ) {
52 $cat = Category
::newFromTitle( $this->mTitle
);
53 return $cat->getId() != 0;
57 function openShowCategory() {
61 function closeShowCategory() {
64 $from = $until = array();
65 foreach ( array( 'page', 'subcat', 'file' ) as $type ) {
66 # Use $_GET instead of $wgRequest, because the latter helpfully
67 # normalizes Unicode, which removes nulls. TODO: do something
68 # smarter than passing nulls in URLs. :/
69 $from[$type] = isset( $_GET["{$type}from"] ) ?
$_GET["{$type}from"] : null;
70 $until[$type] = isset( $_GET["{$type}until"] ) ?
$_GET["{$type}until"] : null;
73 $viewer = new $this->mCategoryViewerClass( $this->mTitle
, $from, $until, $_GET );
74 $wgOut->addHTML( $viewer->getHTML() );
78 class CategoryViewer
{
79 var $title, $limit, $from, $until,
80 $articles, $articles_start_char,
81 $children, $children_start_char,
82 $showGallery, $gallery,
84 # Category object for this page
86 # The original query array, to be used in generating paging links.
89 function __construct( $title, $from = '', $until = '', $query = array() ) {
90 global $wgCategoryPagingLimit;
91 $this->title
= $title;
93 $this->until
= $until;
94 $this->limit
= $wgCategoryPagingLimit;
95 $this->cat
= Category
::newFromTitle( $title );
96 $this->query
= $query;
97 unset( $this->query
['title'] );
101 * Format the category data list.
103 * @return string HTML output
105 private function getHTML() {
106 global $wgOut, $wgCategoryMagicGallery, $wgContLang;
107 wfProfileIn( __METHOD__
);
109 $this->showGallery
= $wgCategoryMagicGallery && !$wgOut->mNoGallery
;
111 $this->clearCategoryState();
112 $this->doCategoryQuery();
113 $this->finaliseCategoryState();
115 $r = $this->getSubcategorySection() .
116 $this->getPagesSection() .
117 $this->getImageSection();
120 // If there is no category content to display, only
121 // show the top part of the navigation links.
122 // FIXME: cannot be completely suppressed because it
123 // is unknown if 'until' or 'from' makes this
125 $r = $r . $this->getCategoryTop();
127 $r = $this->getCategoryTop() .
129 $this->getCategoryBottom();
132 // Give a proper message if category is empty
134 $r = wfMsgExt( 'category-empty', array( 'parse' ) );
137 wfProfileOut( __METHOD__
);
138 return $wgContLang->convert( $r );
141 function clearCategoryState() {
142 $this->articles
= array();
143 $this->articles_start_char
= array();
144 $this->children
= array();
145 $this->children_start_char
= array();
146 if ( $this->showGallery
) {
147 $this->gallery
= new ImageGallery();
148 $this->gallery
->setHideBadImages();
153 if ( !$this->skin
) {
155 $this->skin
= $wgUser->getSkin();
161 * Add a subcategory to the internal lists, using a Category object
163 function addSubcategoryObject( Category
$cat, $sortkey, $pageLength ) {
164 // Subcategory; strip the 'Category' namespace from the link text.
165 $this->children
[] = $this->getSkin()->link(
170 array( 'known', 'noclasses' )
173 $this->children_start_char
[] =
174 $this->getSubcategorySortChar( $cat->getTitle(), $sortkey );
178 * Add a subcategory to the internal lists, using a title object
179 * @deprecated kept for compatibility, please use addSubcategoryObject instead
181 function addSubcategory( Title
$title, $sortkey, $pageLength ) {
182 $this->addSubcategoryObject( Category
::newFromTitle( $title ), $sortkey, $pageLength );
186 * Get the character to be used for sorting subcategories.
187 * If there's a link from Category:A to Category:B, the sortkey of the resulting
188 * entry in the categorylinks table is Category:A, not A, which it SHOULD be.
189 * Workaround: If sortkey == "Category:".$title, than use $title for sorting,
190 * else use sortkey...
192 function getSubcategorySortChar( $title, $sortkey ) {
195 if ( $title->getPrefixedText() == $sortkey ) {
196 $word = $title->getDBkey();
201 $firstChar = $wgContLang->firstLetterForLists( $word );
203 return $wgContLang->convert( $firstChar );
207 * Add a page in the image namespace
209 function addImage( Title
$title, $sortkey, $pageLength, $isRedirect = false ) {
210 if ( $this->showGallery
) {
211 $flip = $this->flip
['file'];
213 $this->gallery
->insert( $title );
215 $this->gallery
->add( $title );
218 $this->addPage( $title, $sortkey, $pageLength, $isRedirect );
223 * Add a miscellaneous page
225 function addPage( $title, $sortkey, $pageLength, $isRedirect = false ) {
227 $this->articles
[] = $isRedirect
228 ?
'<span class="redirect-in-category">' .
229 $this->getSkin()->link(
234 array( 'known', 'noclasses' )
236 : $this->getSkin()->link( $title );
238 $this->articles_start_char
[] = $wgContLang->convert( $wgContLang->firstLetterForLists( $sortkey ) );
241 function finaliseCategoryState() {
242 if ( $this->flip
['subcat'] ) {
243 $this->children
= array_reverse( $this->children
);
244 $this->children_start_char
= array_reverse( $this->children_start_char
);
246 if ( $this->flip
['page'] ) {
247 $this->articles
= array_reverse( $this->articles
);
248 $this->articles_start_char
= array_reverse( $this->articles_start_char
);
252 function doCategoryQuery() {
255 $dbr = wfGetDB( DB_SLAVE
, 'category' );
257 $this->nextPage
= array(
262 $this->flip
= array( 'page' => false, 'subcat' => false, 'file' => false );
264 foreach ( array( 'page', 'subcat', 'file' ) as $type ) {
265 # Get the sortkeys for start/end, if applicable. Note that if
266 # the collation in the database differs from the one
267 # $wgContLang is using, pagination might go totally haywire.
268 $extraConds = array( 'cl_type' => $type );
269 if ( $this->from
[$type] !== null ) {
270 $extraConds[] = 'cl_sortkey >= '
271 . $dbr->addQuotes( $wgContLang->convertToSortkey( $this->from
[$type] ) );
272 } elseif ( $this->until
[$type] !== null ) {
273 $extraConds[] = 'cl_sortkey < '
274 . $dbr->addQuotes( $wgContLang->convertToSortkey( $this->until
[$type] ) );
275 $this->flip
[$type] = true;
279 array( 'page', 'categorylinks', 'category' ),
280 array( 'page_id', 'page_title', 'page_namespace', 'page_len',
281 'page_is_redirect', 'cl_sortkey', 'cat_id', 'cat_title',
282 'cat_subcats', 'cat_pages', 'cat_files', 'cl_sortkey_prefix' ),
283 array( 'cl_to' => $this->title
->getDBkey() ) +
$extraConds,
286 'USE INDEX' => array( 'categorylinks' => 'cl_sortkey' ),
287 'LIMIT' => $this->limit +
1,
288 'ORDER BY' => $this->flip
[$type] ?
'cl_sortkey DESC' : 'cl_sortkey',
291 'categorylinks' => array( 'INNER JOIN', 'cl_from = page_id' ),
292 'category' => array( 'LEFT JOIN', 'cat_title = page_title AND page_namespace = ' . NS_CATEGORY
)
297 foreach ( $res as $row ) {
298 $title = Title
::newFromRow( $row );
299 $rawSortkey = $title->getCategorySortkey( $row->cl_sortkey_prefix
);
301 if ( ++
$count > $this->limit
) {
302 # We've reached the one extra which shows that there
303 # are additional pages to be had. Stop here...
304 $this->nextPage
[$type] = $rawSortkey;
308 if ( $title->getNamespace() == NS_CATEGORY
) {
309 $cat = Category
::newFromRow( $row, $title );
310 $this->addSubcategoryObject( $cat, $rawSortkey, $row->page_len
);
311 } elseif ( $this->showGallery
&& $title->getNamespace() == NS_FILE
) {
312 $this->addImage( $title, $rawSortkey, $row->page_len
, $row->page_is_redirect
);
314 $this->addPage( $title, $rawSortkey, $row->page_len
, $row->page_is_redirect
);
320 function getCategoryTop() {
321 $r = $this->getCategoryBottom();
324 : "<br style=\"clear:both;\"/>\n" . $r;
327 function getSubcategorySection() {
328 # Don't show subcategories section if there are none.
330 $rescnt = count( $this->children
);
331 $dbcnt = $this->cat
->getSubcatCount();
332 $countmsg = $this->getCountMessage( $rescnt, $dbcnt, 'subcat' );
335 # Showing subcategories
336 $r .= "<div id=\"mw-subcategories\">\n";
337 $r .= '<h2>' . wfMsg( 'subcategories' ) . "</h2>\n";
339 $r .= $this->getSectionPagingLinks( 'subcat' );
340 $r .= $this->formatList( $this->children
, $this->children_start_char
);
341 $r .= $this->getSectionPagingLinks( 'subcat' );
347 function getPagesSection() {
348 $ti = htmlspecialchars( $this->title
->getText() );
349 # Don't show articles section if there are none.
352 # FIXME, here and in the other two sections: we don't need to bother
353 # with this rigamarole if the entire category contents fit on one page
354 # and have already been retrieved. We can just use $rescnt in that
355 # case and save a query and some logic.
356 $dbcnt = $this->cat
->getPageCount() - $this->cat
->getSubcatCount()
357 - $this->cat
->getFileCount();
358 $rescnt = count( $this->articles
);
359 $countmsg = $this->getCountMessage( $rescnt, $dbcnt, 'article' );
362 $r = "<div id=\"mw-pages\">\n";
363 $r .= '<h2>' . wfMsg( 'category_header', $ti ) . "</h2>\n";
365 $r .= $this->getSectionPagingLinks( 'page' );
366 $r .= $this->formatList( $this->articles
, $this->articles_start_char
);
367 $r .= $this->getSectionPagingLinks( 'page' );
373 function getImageSection() {
375 if ( $this->showGallery
&& ! $this->gallery
->isEmpty() ) {
376 $dbcnt = $this->cat
->getFileCount();
377 $rescnt = $this->gallery
->count();
378 $countmsg = $this->getCountMessage( $rescnt, $dbcnt, 'file' );
380 $r .= "<div id=\"mw-category-media\">\n";
381 $r .= '<h2>' . wfMsg( 'category-media-header', htmlspecialchars( $this->title
->getText() ) ) . "</h2>\n";
383 $r .= $this->getSectionPagingLinks( 'file' );
384 $r .= $this->gallery
->toHTML();
385 $r .= $this->getSectionPagingLinks( 'file' );
392 * Get the paging links for a section (subcats/pages/files), to go at the top and bottom
395 * @param $type String: 'page', 'subcat', or 'file'
396 * @return String: HTML output, possibly empty if there are no other pages
398 private function getSectionPagingLinks( $type ) {
399 if ( $this->until
[$type] !== null ) {
400 return $this->pagingLinks( $this->nextPage
[$type], $this->until
[$type], $type );
401 } elseif ( $this->nextPage
[$type] !== null ||
$this->from
[$type] !== null ) {
402 return $this->pagingLinks( $this->from
[$type], $this->nextPage
[$type], $type );
408 function getCategoryBottom() {
413 * Format a list of articles chunked by letter, either as a
414 * bullet list or a columnar format, depending on the length.
416 * @param $articles Array
417 * @param $articles_start_char Array
421 private function formatList( $articles, $articles_start_char, $cutoff = 6 ) {
422 if ( count ( $articles ) > $cutoff ) {
423 return self
::columnList( $articles, $articles_start_char );
424 } elseif ( count( $articles ) > 0 ) {
425 // for short lists of articles in categories.
426 return self
::shortList( $articles, $articles_start_char );
432 * Format a list of articles chunked by letter in a three-column
433 * list, ordered vertically.
435 * TODO: Take the headers into account when creating columns, so they're
436 * more visually equal.
438 * More distant TODO: Scrap this and use CSS columns, whenever IE finally
441 * @param $articles Array
442 * @param $articles_start_char Array
445 private static function columnList( $articles, $articles_start_char ) {
446 $columns = array_combine( $articles, $articles_start_char );
447 # Split into three columns
448 $columns = array_chunk( $columns, ceil( count( $columns ) / 3 ), true /* preserve keys */ );
450 $ret = '<table width="100%"><tr valign="top"><td>';
453 foreach ( $columns as $column ) {
454 $colContents = array();
456 # Kind of like array_flip() here, but we keep duplicates in an
457 # array instead of dropping them.
458 foreach ( $column as $article => $char ) {
459 if ( !isset( $colContents[$char] ) ) {
460 $colContents[$char] = array();
462 $colContents[$char][] = $article;
466 foreach ( $colContents as $char => $articles ) {
467 $ret .= '<h3>' . htmlspecialchars( $char );
468 if ( $first && $char === $prevchar ) {
469 # We're continuing a previous chunk at the top of a new
470 # column, so add " cont." after the letter.
471 $ret .= ' ' . wfMsgHtml( 'listingcontinuesabbrev' );
476 $ret .= implode( "</li>\n<li>", $articles );
477 $ret .= '</li></ul>';
483 $ret .= "</td>\n<td>";
486 $ret .= '</td></tr></table>';
491 * Format a list of articles chunked by letter in a bullet list.
492 * @param $articles Array
493 * @param $articles_start_char Array
496 private static function shortList( $articles, $articles_start_char ) {
497 $r = '<h3>' . htmlspecialchars( $articles_start_char[0] ) . "</h3>\n";
498 $r .= '<ul><li>' . $articles[0] . '</li>';
499 for ( $index = 1; $index < count( $articles ); $index++
)
501 if ( $articles_start_char[$index] != $articles_start_char[$index - 1] )
503 $r .= "</ul><h3>" . htmlspecialchars( $articles_start_char[$index] ) . "</h3>\n<ul>";
506 $r .= "<li>{$articles[$index]}</li>";
513 * Create paging links, as a helper method to getSectionPagingLinks().
515 * @param $first String The 'until' parameter for the generated URL
516 * @param $last String The 'from' parameter for the genererated URL
517 * @param $type String A prefix for parameters, 'page' or 'subcat' or
519 * @return String HTML
521 private function pagingLinks( $first, $last, $type = '' ) {
523 $sk = $this->getSkin();
524 $limitText = $wgLang->formatNum( $this->limit
);
526 $prevLink = wfMsgExt( 'prevn', array( 'escape', 'parsemag' ), $limitText );
528 if ( $first != '' ) {
529 $prevQuery = $this->query
;
530 $prevQuery["{$type}until"] = $first;
531 unset( $prevQuery["{$type}from"] );
532 $prevLink = $sk->linkKnown(
540 $nextLink = wfMsgExt( 'nextn', array( 'escape', 'parsemag' ), $limitText );
543 $lastQuery = $this->query
;
544 $lastQuery["{$type}from"] = $last;
545 unset( $lastQuery["{$type}until"] );
546 $nextLink = $sk->linkKnown(
554 return "($prevLink) ($nextLink)";
558 * What to do if the category table conflicts with the number of results
559 * returned? This function says what. It works the same whether the
560 * things being counted are articles, subcategories, or files.
562 * Note for grepping: uses the messages category-article-count,
563 * category-article-count-limited, category-subcat-count,
564 * category-subcat-count-limited, category-file-count,
565 * category-file-count-limited.
567 * @param $rescnt Int: The number of items returned by our database query.
568 * @param $dbcnt Int: The number of items according to the category table.
569 * @param $type String: 'subcat', 'article', or 'file'
570 * @return String: A message giving the number of items, to output to HTML.
572 private function getCountMessage( $rescnt, $dbcnt, $type ) {
574 # There are three cases:
575 # 1) The category table figure seems sane. It might be wrong, but
576 # we can't do anything about it if we don't recalculate it on ev-
578 # 2) The category table figure isn't sane, like it's smaller than the
579 # number of actual results, *but* the number of results is less
580 # than $this->limit and there's no offset. In this case we still
581 # know the right figure.
582 # 3) We have no idea.
583 $totalrescnt = count( $this->articles
) +
count( $this->children
) +
584 ( $this->showGallery ?
$this->gallery
->count() : 0 );
586 # Check if there's a "from" or "until" for anything
587 $fromOrUntil = false;
588 foreach ( array( 'page', 'subcat', 'file' ) as $t ) {
589 if ( $this->from
[$t] !== null ||
$this->until
[$t] !== null ) {
595 if ( $dbcnt == $rescnt ||
( ( $totalrescnt == $this->limit ||
$fromOrUntil )
596 && $dbcnt > $rescnt ) )
598 # Case 1: seems sane.
600 } elseif ( $totalrescnt < $this->limit
&& !$fromOrUntil ) {
601 # Case 2: not sane, but salvageable. Use the number of results.
602 # Since there are fewer than 200, we can also take this opportunity
603 # to refresh the incorrect category table entry -- which should be
604 # quick due to the small number of entries.
606 $this->cat
->refreshCounts();
608 # Case 3: hopeless. Don't give a total count at all.
609 return wfMsgExt( "category-$type-count-limited", 'parse',
610 $wgLang->formatNum( $rescnt ) );
613 "category-$type-count",
615 $wgLang->formatNum( $rescnt ),
616 $wgLang->formatNum( $totalcnt )