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();
29 if ( !wfRunHooks( 'CategoryPageView', array( &$this ) ) )
32 if ( NS_CATEGORY
== $this->mTitle
->getNamespace() ) {
33 $this->openShowCategory();
38 if ( NS_CATEGORY
== $this->mTitle
->getNamespace() ) {
39 $this->closeShowCategory();
44 * Don't return a 404 for categories in use.
46 function hasViewableContent() {
47 if ( parent
::hasViewableContent() ) {
50 $cat = Category
::newFromTitle( $this->mTitle
);
51 return $cat->getId() != 0;
55 function openShowCategory() {
59 function closeShowCategory() {
62 $from = $until = array();
63 foreach ( array( 'page', 'subcat', 'file' ) as $type ) {
64 # Use $_GET instead of $wgRequest, because the latter helpfully
65 # normalizes Unicode, which removes nulls. TODO: do something
66 # smarter than passing nulls in URLs. :/
67 $from[$type] = isset( $_GET["{$type}from"] ) ?
$_GET["{$type}from"] : null;
68 $until[$type] = isset( $_GET["{$type}until"] ) ?
$_GET["{$type}until"] : null;
71 $viewer = new $this->mCategoryViewerClass( $this->mTitle
, $from, $until, $_GET );
72 $wgOut->addHTML( $viewer->getHTML() );
76 class CategoryViewer
{
77 var $title, $limit, $from, $until,
78 $articles, $articles_start_char,
79 $children, $children_start_char,
80 $showGallery, $gallery,
82 # Category object for this page
84 # The original query array, to be used in generating paging links.
87 function __construct( $title, $from = '', $until = '', $query = array() ) {
88 global $wgCategoryPagingLimit;
89 $this->title
= $title;
91 $this->until
= $until;
92 $this->limit
= $wgCategoryPagingLimit;
93 $this->cat
= Category
::newFromTitle( $title );
94 $this->query
= $query;
95 unset( $this->query
['title'] );
99 * Format the category data list.
101 * @return string HTML output
105 global $wgOut, $wgCategoryMagicGallery, $wgContLang;
106 wfProfileIn( __METHOD__
);
108 $this->showGallery
= $wgCategoryMagicGallery && !$wgOut->mNoGallery
;
110 $this->clearCategoryState();
111 $this->doCategoryQuery();
112 $this->finaliseCategoryState();
114 $r = $this->getSubcategorySection() .
115 $this->getPagesSection() .
116 $this->getImageSection();
119 // If there is no category content to display, only
120 // show the top part of the navigation links.
121 // FIXME: cannot be completely suppressed because it
122 // is unknown if 'until' or 'from' makes this
124 $r = $r . $this->getCategoryTop();
126 $r = $this->getCategoryTop() .
128 $this->getCategoryBottom();
131 // Give a proper message if category is empty
133 $r = wfMsgExt( 'category-empty', array( 'parse' ) );
136 wfProfileOut( __METHOD__
);
137 return $wgContLang->convert( $r );
140 function clearCategoryState() {
141 $this->articles
= array();
142 $this->articles_start_char
= array();
143 $this->children
= array();
144 $this->children_start_char
= array();
145 if ( $this->showGallery
) {
146 $this->gallery
= new ImageGallery();
147 $this->gallery
->setHideBadImages();
152 if ( !$this->skin
) {
154 $this->skin
= $wgUser->getSkin();
160 * Add a subcategory to the internal lists, using a Category object
162 function addSubcategoryObject( Category
$cat, $sortkey, $pageLength ) {
163 // Subcategory; strip the 'Category' namespace from the link text.
164 $this->children
[] = $this->getSkin()->link(
169 array( 'known', 'noclasses' )
172 $this->children_start_char
[] = $this->getSubcategorySortChar( $title, $sortkey );
176 * Add a subcategory to the internal lists, using a title object
177 * @deprecated kept for compatibility, please use addSubcategoryObject instead
179 function addSubcategory( Title
$title, $sortkey, $pageLength ) {
180 $this->addSubcategoryObject( Category
::newFromTitle( $title ), $sortkey, $pageLength );
184 * Get the character to be used for sorting subcategories.
185 * If there's a link from Category:A to Category:B, the sortkey of the resulting
186 * entry in the categorylinks table is Category:A, not A, which it SHOULD be.
187 * Workaround: If sortkey == "Category:".$title, than use $title for sorting,
188 * else use sortkey...
190 function getSubcategorySortChar( $title, $sortkey ) {
193 if ( $title->getPrefixedText() == $sortkey ) {
194 $word = $title->getDBkey();
199 $firstChar = $wgContLang->firstLetterForLists( $word );
201 return $wgContLang->convert( $firstChar );
205 * Add a page in the image namespace
207 function addImage( Title
$title, $sortkey, $pageLength, $isRedirect = false ) {
208 if ( $this->showGallery
) {
209 $flip = $this->flip
['file'];
211 $this->gallery
->insert( $title );
213 $this->gallery
->add( $title );
216 $this->addPage( $title, $sortkey, $pageLength, $isRedirect );
221 * Add a miscellaneous page
223 function addPage( $title, $sortkey, $pageLength, $isRedirect = false ) {
225 $this->articles
[] = $isRedirect
226 ?
'<span class="redirect-in-category">' .
227 $this->getSkin()->link(
232 array( 'known', 'noclasses' )
234 : $this->getSkin()->link( $title );
236 $this->articles_start_char
[] = $wgContLang->convert( $wgContLang->firstLetterForLists( $sortkey ) );
239 function finaliseCategoryState() {
240 if ( $this->flip
['subcat'] ) {
241 $this->children
= array_reverse( $this->children
);
242 $this->children_start_char
= array_reverse( $this->children_start_char
);
244 if ( $this->flip
['page'] ) {
245 $this->articles
= array_reverse( $this->articles
);
246 $this->articles_start_char
= array_reverse( $this->articles_start_char
);
250 function doCategoryQuery() {
253 $dbr = wfGetDB( DB_SLAVE
, 'category' );
255 $this->nextPage
= array(
260 $this->flip
= array( 'page' => false, 'subcat' => false, 'file' => false );
262 foreach ( array( 'page', 'subcat', 'file' ) as $type ) {
263 # Get the sortkeys for start/end, if applicable. Note that if
264 # the collation in the database differs from the one
265 # $wgContLang is using, pagination might go totally haywire.
266 $extraConds = array( 'cl_type' => $type );
267 if ( $this->from
[$type] !== null ) {
268 $extraConds[] = 'cl_sortkey >= '
269 . $dbr->addQuotes( $wgContLang->convertToSortkey( $this->from
[$type] ) );
270 } elseif ( $this->until
[$type] !== null ) {
271 $extraConds[] = 'cl_sortkey < '
272 . $dbr->addQuotes( $wgContLang->convertToSortkey( $this->until
[$type] ) );
273 $this->flip
[$type] = true;
277 array( 'page', 'categorylinks', 'category' ),
278 array( 'page_id', 'page_title', 'page_namespace', 'page_len',
279 'page_is_redirect', 'cl_sortkey', 'cat_id', 'cat_title',
280 'cat_subcats', 'cat_pages', 'cat_files', 'cl_sortkey_prefix' ),
281 array( 'cl_to' => $this->title
->getDBkey() ) +
$extraConds,
284 'USE INDEX' => array( 'categorylinks' => 'cl_sortkey' ),
285 'LIMIT' => $this->limit +
1,
286 'ORDER BY' => $this->flip
[$type] ?
'cl_sortkey DESC' : 'cl_sortkey',
289 'categorylinks' => array( 'INNER JOIN', 'cl_from = page_id' ),
290 'category' => array( 'LEFT JOIN', 'cat_title = page_title AND page_namespace = ' . NS_CATEGORY
)
295 foreach ( $res as $row ) {
296 $title = Title
::newFromRow( $row );
297 $rawSortkey = $title->getCategorySortkey( $row->cl_sortkey_prefix
);
299 if ( ++
$count > $this->limit
) {
300 # We've reached the one extra which shows that there
301 # are additional pages to be had. Stop here...
302 $this->nextPage
[$type] = $rawSortkey;
306 if ( $title->getNamespace() == NS_CATEGORY
) {
307 $cat = Category
::newFromRow( $row, $title );
308 $this->addSubcategoryObject( $cat, $rawSortkey, $row->page_len
);
309 } elseif ( $this->showGallery
&& $title->getNamespace() == NS_FILE
) {
310 $this->addImage( $title, $rawSortkey, $row->page_len
, $row->page_is_redirect
);
312 $this->addPage( $title, $rawSortkey, $row->page_len
, $row->page_is_redirect
);
318 function getCategoryTop() {
319 $r = $this->getCategoryBottom();
322 : "<br style=\"clear:both;\"/>\n" . $r;
325 function getSubcategorySection() {
326 # Don't show subcategories section if there are none.
328 $rescnt = count( $this->children
);
329 $dbcnt = $this->cat
->getSubcatCount();
330 $countmsg = $this->getCountMessage( $rescnt, $dbcnt, 'subcat' );
333 # Showing subcategories
334 $r .= "<div id=\"mw-subcategories\">\n";
335 $r .= '<h2>' . wfMsg( 'subcategories' ) . "</h2>\n";
337 $r .= $this->getSectionPagingLinks( 'subcat' );
338 $r .= $this->formatList( $this->children
, $this->children_start_char
);
339 $r .= $this->getSectionPagingLinks( 'subcat' );
345 function getPagesSection() {
346 $ti = htmlspecialchars( $this->title
->getText() );
347 # Don't show articles section if there are none.
350 # FIXME, here and in the other two sections: we don't need to bother
351 # with this rigamarole if the entire category contents fit on one page
352 # and have already been retrieved. We can just use $rescnt in that
353 # case and save a query and some logic.
354 $dbcnt = $this->cat
->getPageCount() - $this->cat
->getSubcatCount()
355 - $this->cat
->getFileCount();
356 $rescnt = count( $this->articles
);
357 $countmsg = $this->getCountMessage( $rescnt, $dbcnt, 'article' );
360 $r = "<div id=\"mw-pages\">\n";
361 $r .= '<h2>' . wfMsg( 'category_header', $ti ) . "</h2>\n";
363 $r .= $this->getSectionPagingLinks( 'page' );
364 $r .= $this->formatList( $this->articles
, $this->articles_start_char
);
365 $r .= $this->getSectionPagingLinks( 'page' );
371 function getImageSection() {
373 if ( $this->showGallery
&& ! $this->gallery
->isEmpty() ) {
374 $dbcnt = $this->cat
->getFileCount();
375 $rescnt = $this->gallery
->count();
376 $countmsg = $this->getCountMessage( $rescnt, $dbcnt, 'file' );
378 $r .= "<div id=\"mw-category-media\">\n";
379 $r .= '<h2>' . wfMsg( 'category-media-header', htmlspecialchars( $this->title
->getText() ) ) . "</h2>\n";
381 $r .= $this->getSectionPagingLinks( 'file' );
382 $r .= $this->gallery
->toHTML();
383 $r .= $this->getSectionPagingLinks( 'file' );
390 * Get the paging links for a section (subcats/pages/files), to go at the top and bottom
393 * @param $type String: 'page', 'subcat', or 'file'
394 * @return String: HTML output, possibly empty if there are no other pages
396 private function getSectionPagingLinks( $type ) {
397 if ( $this->until
[$type] !== null ) {
398 return $this->pagingLinks( $this->nextPage
[$type], $this->until
[$type], $type );
399 } elseif ( $this->nextPage
[$type] !== null ||
$this->from
[$type] !== null ) {
400 return $this->pagingLinks( $this->from
[$type], $this->nextPage
[$type], $type );
406 function getCategoryBottom() {
411 * Format a list of articles chunked by letter, either as a
412 * bullet list or a columnar format, depending on the length.
414 * @param $articles Array
415 * @param $articles_start_char Array
420 function formatList( $articles, $articles_start_char, $cutoff = 6 ) {
421 if ( count ( $articles ) > $cutoff ) {
422 return $this->columnList( $articles, $articles_start_char );
423 } elseif ( count( $articles ) > 0 ) {
424 // for short lists of articles in categories.
425 return $this->shortList( $articles, $articles_start_char );
431 * Format a list of articles chunked by letter in a three-column
432 * list, ordered vertically.
434 * TODO: Take the headers into account when creating columns, so they're
435 * more visually equal.
437 * More distant TODO: Scrap this and use CSS columns, whenever IE finally
440 * @param $articles Array
441 * @param $articles_start_char Array
445 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
497 function shortList( $articles, $articles_start_char ) {
498 $r = '<h3>' . htmlspecialchars( $articles_start_char[0] ) . "</h3>\n";
499 $r .= '<ul><li>' . $articles[0] . '</li>';
500 for ( $index = 1; $index < count( $articles ); $index++
)
502 if ( $articles_start_char[$index] != $articles_start_char[$index - 1] )
504 $r .= "</ul><h3>" . htmlspecialchars( $articles_start_char[$index] ) . "</h3>\n<ul>";
507 $r .= "<li>{$articles[$index]}</li>";
514 * Create paging links, as a helper method to getSectionPagingLinks().
516 * @param $first String The 'until' parameter for the generated URL
517 * @param $last String The 'from' parameter for the genererated URL
518 * @param $type String A prefix for parameters, 'page' or 'subcat' or
520 * @return String HTML
522 private function pagingLinks( $first, $last, $type = '' ) {
524 $sk = $this->getSkin();
525 $limitText = $wgLang->formatNum( $this->limit
);
527 $prevLink = wfMsgExt( 'prevn', array( 'escape', 'parsemag' ), $limitText );
529 if ( $first != '' ) {
530 $prevQuery = $this->query
;
531 $prevQuery["{$type}until"] = $first;
532 unset( $prevQuery["{$type}from"] );
533 $prevLink = $sk->linkKnown(
541 $nextLink = wfMsgExt( 'nextn', array( 'escape', 'parsemag' ), $limitText );
544 $lastQuery = $this->query
;
545 $lastQuery["{$type}from"] = $last;
546 unset( $lastQuery["{$type}until"] );
547 $nextLink = $sk->linkKnown(
555 return "($prevLink) ($nextLink)";
559 * What to do if the category table conflicts with the number of results
560 * returned? This function says what. It works the same whether the
561 * things being counted are articles, subcategories, or files.
563 * Note for grepping: uses the messages category-article-count,
564 * category-article-count-limited, category-subcat-count,
565 * category-subcat-count-limited, category-file-count,
566 * category-file-count-limited.
568 * @param $rescnt Int: The number of items returned by our database query.
569 * @param $dbcnt Int: The number of items according to the category table.
570 * @param $type String: 'subcat', 'article', or 'file'
571 * @return String: A message giving the number of items, to output to HTML.
573 private function getCountMessage( $rescnt, $dbcnt, $type ) {
575 # There are three cases:
576 # 1) The category table figure seems sane. It might be wrong, but
577 # we can't do anything about it if we don't recalculate it on ev-
579 # 2) The category table figure isn't sane, like it's smaller than the
580 # number of actual results, *but* the number of results is less
581 # than $this->limit and there's no offset. In this case we still
582 # know the right figure.
583 # 3) We have no idea.
584 $totalrescnt = count( $this->articles
) +
count( $this->children
) +
585 ( $this->showGallery ?
$this->gallery
->count() : 0 );
587 # Check if there's a "from" or "until" for anything
588 $fromOrUntil = false;
589 foreach ( array( 'page', 'subcat', 'file' ) as $type ) {
590 if ( $this->from
[$type] !== null ||
$this->until
[$type] !== null ) {
596 if ( $dbcnt == $rescnt ||
( ( $totalrescnt == $this->limit ||
$fromOrUntil )
597 && $dbcnt > $rescnt ) )
599 # Case 1: seems sane.
601 } elseif ( $totalrescnt < $this->limit
&& !$fromOrUntil ) {
602 # Case 2: not sane, but salvageable. Use the number of results.
603 # Since there are fewer than 200, we can also take this opportunity
604 # to refresh the incorrect category table entry -- which should be
605 # quick due to the small number of entries.
607 $this->cat
->refreshCounts();
609 # Case 3: hopeless. Don't give a total count at all.
610 return wfMsgExt( "category-$type-count-limited", 'parse',
611 $wgLang->formatNum( $rescnt ) );
614 "category-$type-count",
616 $wgLang->formatNum( $rescnt ),
617 $wgLang->formatNum( $totalcnt )