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 public 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
422 function formatList( $articles, $articles_start_char, $cutoff = 6 ) {
423 if ( count ( $articles ) > $cutoff ) {
424 return self
::columnList( $articles, $articles_start_char );
425 } elseif ( count( $articles ) > 0 ) {
426 // for short lists of articles in categories.
427 return self
::shortList( $articles, $articles_start_char );
433 * Format a list of articles chunked by letter in a three-column
434 * list, ordered vertically.
436 * TODO: Take the headers into account when creating columns, so they're
437 * more visually equal.
439 * More distant TODO: Scrap this and use CSS columns, whenever IE finally
442 * @param $articles Array
443 * @param $articles_start_char Array
447 static function columnList( $articles, $articles_start_char ) {
448 $columns = array_combine( $articles, $articles_start_char );
449 # Split into three columns
450 $columns = array_chunk( $columns, ceil( count( $columns ) / 3 ), true /* preserve keys */ );
452 $ret = '<table width="100%"><tr valign="top"><td>';
455 foreach ( $columns as $column ) {
456 $colContents = array();
458 # Kind of like array_flip() here, but we keep duplicates in an
459 # array instead of dropping them.
460 foreach ( $column as $article => $char ) {
461 if ( !isset( $colContents[$char] ) ) {
462 $colContents[$char] = array();
464 $colContents[$char][] = $article;
468 foreach ( $colContents as $char => $articles ) {
469 $ret .= '<h3>' . htmlspecialchars( $char );
470 if ( $first && $char === $prevchar ) {
471 # We're continuing a previous chunk at the top of a new
472 # column, so add " cont." after the letter.
473 $ret .= ' ' . wfMsgHtml( 'listingcontinuesabbrev' );
478 $ret .= implode( "</li>\n<li>", $articles );
479 $ret .= '</li></ul>';
485 $ret .= "</td>\n<td>";
488 $ret .= '</td></tr></table>';
493 * Format a list of articles chunked by letter in a bullet list.
494 * @param $articles Array
495 * @param $articles_start_char Array
499 static function shortList( $articles, $articles_start_char ) {
500 $r = '<h3>' . htmlspecialchars( $articles_start_char[0] ) . "</h3>\n";
501 $r .= '<ul><li>' . $articles[0] . '</li>';
502 for ( $index = 1; $index < count( $articles ); $index++
)
504 if ( $articles_start_char[$index] != $articles_start_char[$index - 1] )
506 $r .= "</ul><h3>" . htmlspecialchars( $articles_start_char[$index] ) . "</h3>\n<ul>";
509 $r .= "<li>{$articles[$index]}</li>";
516 * Create paging links, as a helper method to getSectionPagingLinks().
518 * @param $first String The 'until' parameter for the generated URL
519 * @param $last String The 'from' parameter for the genererated URL
520 * @param $type String A prefix for parameters, 'page' or 'subcat' or
522 * @return String HTML
524 private function pagingLinks( $first, $last, $type = '' ) {
526 $sk = $this->getSkin();
527 $limitText = $wgLang->formatNum( $this->limit
);
529 $prevLink = wfMsgExt( 'prevn', array( 'escape', 'parsemag' ), $limitText );
531 if ( $first != '' ) {
532 $prevQuery = $this->query
;
533 $prevQuery["{$type}until"] = $first;
534 unset( $prevQuery["{$type}from"] );
535 $prevLink = $sk->linkKnown(
543 $nextLink = wfMsgExt( 'nextn', array( 'escape', 'parsemag' ), $limitText );
546 $lastQuery = $this->query
;
547 $lastQuery["{$type}from"] = $last;
548 unset( $lastQuery["{$type}until"] );
549 $nextLink = $sk->linkKnown(
557 return "($prevLink) ($nextLink)";
561 * What to do if the category table conflicts with the number of results
562 * returned? This function says what. It works the same whether the
563 * things being counted are articles, subcategories, or files.
565 * Note for grepping: uses the messages category-article-count,
566 * category-article-count-limited, category-subcat-count,
567 * category-subcat-count-limited, category-file-count,
568 * category-file-count-limited.
570 * @param $rescnt Int: The number of items returned by our database query.
571 * @param $dbcnt Int: The number of items according to the category table.
572 * @param $type String: 'subcat', 'article', or 'file'
573 * @return String: A message giving the number of items, to output to HTML.
575 private function getCountMessage( $rescnt, $dbcnt, $type ) {
577 # There are three cases:
578 # 1) The category table figure seems sane. It might be wrong, but
579 # we can't do anything about it if we don't recalculate it on ev-
581 # 2) The category table figure isn't sane, like it's smaller than the
582 # number of actual results, *but* the number of results is less
583 # than $this->limit and there's no offset. In this case we still
584 # know the right figure.
585 # 3) We have no idea.
586 $totalrescnt = count( $this->articles
) +
count( $this->children
) +
587 ( $this->showGallery ?
$this->gallery
->count() : 0 );
589 # Check if there's a "from" or "until" for anything
590 $fromOrUntil = false;
591 foreach ( array( 'page', 'subcat', 'file' ) as $t ) {
592 if ( $this->from
[$t] !== null ||
$this->until
[$t] !== null ) {
598 if ( $dbcnt == $rescnt ||
( ( $totalrescnt == $this->limit ||
$fromOrUntil )
599 && $dbcnt > $rescnt ) )
601 # Case 1: seems sane.
603 } elseif ( $totalrescnt < $this->limit
&& !$fromOrUntil ) {
604 # Case 2: not sane, but salvageable. Use the number of results.
605 # Since there are fewer than 200, we can also take this opportunity
606 # to refresh the incorrect category table entry -- which should be
607 # quick due to the small number of entries.
609 $this->cat
->refreshCounts();
611 # Case 3: hopeless. Don't give a total count at all.
612 return wfMsgExt( "category-$type-count-limited", 'parse',
613 $wgLang->formatNum( $rescnt ) );
616 "category-$type-count",
618 $wgLang->formatNum( $rescnt ),
619 $wgLang->formatNum( $totalcnt )