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() {
73 $from = $until = array();
74 foreach ( array( 'page', 'subcat', 'file' ) as $type ) {
75 # Use $_GET instead of $wgRequest, because the latter helpfully
76 # normalizes Unicode, which removes nulls. TODO: do something
77 # smarter than passing nulls in URLs. :/
78 $from[$type] = isset( $_GET["{$type}from"] ) ?
$_GET["{$type}from"] : null;
79 $until[$type] = isset( $_GET["{$type}until"] ) ?
$_GET["{$type}until"] : null;
82 $viewer = new $this->mCategoryViewerClass( $this->mTitle
, $from, $until, $_GET );
83 $wgOut->addHTML( $viewer->getHTML() );
87 class CategoryViewer
{
88 var $title, $limit, $from, $until,
89 $articles, $articles_start_char,
90 $children, $children_start_char,
91 $showGallery, $gallery,
93 # Category object for this page
95 # The original query array, to be used in generating paging links.
98 function __construct( $title, $from = '', $until = '', $query = array() ) {
99 global $wgCategoryPagingLimit;
100 $this->title
= $title;
102 $this->until
= $until;
103 $this->limit
= $wgCategoryPagingLimit;
104 $this->cat
= Category
::newFromTitle( $title );
105 $this->query
= $query;
106 unset( $this->query
['title'] );
110 * Format the category data list.
112 * @return string HTML output
114 public function getHTML() {
115 global $wgOut, $wgCategoryMagicGallery, $wgContLang;
116 wfProfileIn( __METHOD__
);
118 $this->showGallery
= $wgCategoryMagicGallery && !$wgOut->mNoGallery
;
120 $this->clearCategoryState();
121 $this->doCategoryQuery();
122 $this->finaliseCategoryState();
124 $r = $this->getSubcategorySection() .
125 $this->getPagesSection() .
126 $this->getImageSection();
129 // If there is no category content to display, only
130 // show the top part of the navigation links.
131 // FIXME: cannot be completely suppressed because it
132 // is unknown if 'until' or 'from' makes this
134 $r = $r . $this->getCategoryTop();
136 $r = $this->getCategoryTop() .
138 $this->getCategoryBottom();
141 // Give a proper message if category is empty
143 $r = wfMsgExt( 'category-empty', array( 'parse' ) );
146 wfProfileOut( __METHOD__
);
147 return $wgContLang->convert( $r );
150 function clearCategoryState() {
151 $this->articles
= array();
152 $this->articles_start_char
= array();
153 $this->children
= array();
154 $this->children_start_char
= array();
155 if ( $this->showGallery
) {
156 $this->gallery
= new ImageGallery();
157 $this->gallery
->setHideBadImages();
162 if ( !$this->skin
) {
164 $this->skin
= $wgUser->getSkin();
170 * Add a subcategory to the internal lists, using a Category object
172 function addSubcategoryObject( Category
$cat, $sortkey, $pageLength ) {
173 // Subcategory; strip the 'Category' namespace from the link text.
174 $title = $cat->getTitle();
175 $this->children
[] = $this->getSkin()->link(
180 array( 'known', 'noclasses' )
183 $this->children_start_char
[] =
184 $this->getSubcategorySortChar( $cat->getTitle(), $sortkey );
188 * Add a subcategory to the internal lists, using a title object
189 * @deprecated kept for compatibility, please use addSubcategoryObject instead
191 function addSubcategory( Title
$title, $sortkey, $pageLength ) {
192 $this->addSubcategoryObject( Category
::newFromTitle( $title ), $sortkey, $pageLength );
196 * Get the character to be used for sorting subcategories.
197 * If there's a link from Category:A to Category:B, the sortkey of the resulting
198 * entry in the categorylinks table is Category:A, not A, which it SHOULD be.
199 * Workaround: If sortkey == "Category:".$title, than use $title for sorting,
200 * else use sortkey...
202 function getSubcategorySortChar( $title, $sortkey ) {
205 if ( $title->getPrefixedText() == $sortkey ) {
206 $word = $title->getDBkey();
211 $firstChar = $wgContLang->firstLetterForLists( $word );
213 return $wgContLang->convert( $firstChar );
217 * Add a page in the image namespace
219 function addImage( Title
$title, $sortkey, $pageLength, $isRedirect = false ) {
220 if ( $this->showGallery
) {
221 $flip = $this->flip
['file'];
223 $this->gallery
->insert( $title );
225 $this->gallery
->add( $title );
228 $this->addPage( $title, $sortkey, $pageLength, $isRedirect );
233 * Add a miscellaneous page
235 function addPage( $title, $sortkey, $pageLength, $isRedirect = false ) {
237 $this->articles
[] = $isRedirect
238 ?
'<span class="redirect-in-category">' .
239 $this->getSkin()->link(
244 array( 'known', 'noclasses' )
246 : $this->getSkin()->link( $title );
248 $this->articles_start_char
[] = $wgContLang->convert( $wgContLang->firstLetterForLists( $sortkey ) );
251 function finaliseCategoryState() {
252 if ( $this->flip
['subcat'] ) {
253 $this->children
= array_reverse( $this->children
);
254 $this->children_start_char
= array_reverse( $this->children_start_char
);
256 if ( $this->flip
['page'] ) {
257 $this->articles
= array_reverse( $this->articles
);
258 $this->articles_start_char
= array_reverse( $this->articles_start_char
);
262 function doCategoryQuery() {
265 $dbr = wfGetDB( DB_SLAVE
, 'category' );
267 $this->nextPage
= array(
272 $this->flip
= array( 'page' => false, 'subcat' => false, 'file' => false );
274 foreach ( array( 'page', 'subcat', 'file' ) as $type ) {
275 # Get the sortkeys for start/end, if applicable. Note that if
276 # the collation in the database differs from the one
277 # $wgContLang is using, pagination might go totally haywire.
278 $extraConds = array( 'cl_type' => $type );
279 if ( $this->from
[$type] !== null ) {
280 $extraConds[] = 'cl_sortkey >= '
281 . $dbr->addQuotes( $wgContLang->convertToSortkey( $this->from
[$type] ) );
282 } elseif ( $this->until
[$type] !== null ) {
283 $extraConds[] = 'cl_sortkey < '
284 . $dbr->addQuotes( $wgContLang->convertToSortkey( $this->until
[$type] ) );
285 $this->flip
[$type] = true;
289 array( 'page', 'categorylinks', 'category' ),
290 array( 'page_id', 'page_title', 'page_namespace', 'page_len',
291 'page_is_redirect', 'cl_sortkey', 'cat_id', 'cat_title',
292 'cat_subcats', 'cat_pages', 'cat_files', 'cl_sortkey_prefix' ),
293 array( 'cl_to' => $this->title
->getDBkey() ) +
$extraConds,
296 'USE INDEX' => array( 'categorylinks' => 'cl_sortkey' ),
297 'LIMIT' => $this->limit +
1,
298 'ORDER BY' => $this->flip
[$type] ?
'cl_sortkey DESC' : 'cl_sortkey',
301 'categorylinks' => array( 'INNER JOIN', 'cl_from = page_id' ),
302 'category' => array( 'LEFT JOIN', 'cat_title = page_title AND page_namespace = ' . NS_CATEGORY
)
307 foreach ( $res as $row ) {
308 $title = Title
::newFromRow( $row );
309 $rawSortkey = $title->getCategorySortkey( $row->cl_sortkey_prefix
);
311 if ( ++
$count > $this->limit
) {
312 # We've reached the one extra which shows that there
313 # are additional pages to be had. Stop here...
314 $this->nextPage
[$type] = $rawSortkey;
318 if ( $title->getNamespace() == NS_CATEGORY
) {
319 $cat = Category
::newFromRow( $row, $title );
320 $this->addSubcategoryObject( $cat, $rawSortkey, $row->page_len
);
321 } elseif ( $this->showGallery
&& $title->getNamespace() == NS_FILE
) {
322 $this->addImage( $title, $rawSortkey, $row->page_len
, $row->page_is_redirect
);
324 $this->addPage( $title, $rawSortkey, $row->page_len
, $row->page_is_redirect
);
330 function getCategoryTop() {
331 $r = $this->getCategoryBottom();
334 : "<br style=\"clear:both;\"/>\n" . $r;
337 function getSubcategorySection() {
338 # Don't show subcategories section if there are none.
340 $rescnt = count( $this->children
);
341 $dbcnt = $this->cat
->getSubcatCount();
342 $countmsg = $this->getCountMessage( $rescnt, $dbcnt, 'subcat' );
345 # Showing subcategories
346 $r .= "<div id=\"mw-subcategories\">\n";
347 $r .= '<h2>' . wfMsg( 'subcategories' ) . "</h2>\n";
349 $r .= $this->getSectionPagingLinks( 'subcat' );
350 $r .= $this->formatList( $this->children
, $this->children_start_char
);
351 $r .= $this->getSectionPagingLinks( 'subcat' );
357 function getPagesSection() {
358 $ti = htmlspecialchars( $this->title
->getText() );
359 # Don't show articles section if there are none.
362 # FIXME, here and in the other two sections: we don't need to bother
363 # with this rigamarole if the entire category contents fit on one page
364 # and have already been retrieved. We can just use $rescnt in that
365 # case and save a query and some logic.
366 $dbcnt = $this->cat
->getPageCount() - $this->cat
->getSubcatCount()
367 - $this->cat
->getFileCount();
368 $rescnt = count( $this->articles
);
369 $countmsg = $this->getCountMessage( $rescnt, $dbcnt, 'article' );
372 $r = "<div id=\"mw-pages\">\n";
373 $r .= '<h2>' . wfMsg( 'category_header', $ti ) . "</h2>\n";
375 $r .= $this->getSectionPagingLinks( 'page' );
376 $r .= $this->formatList( $this->articles
, $this->articles_start_char
);
377 $r .= $this->getSectionPagingLinks( 'page' );
383 function getImageSection() {
385 if ( $this->showGallery
&& ! $this->gallery
->isEmpty() ) {
386 $dbcnt = $this->cat
->getFileCount();
387 $rescnt = $this->gallery
->count();
388 $countmsg = $this->getCountMessage( $rescnt, $dbcnt, 'file' );
390 $r .= "<div id=\"mw-category-media\">\n";
391 $r .= '<h2>' . wfMsg( 'category-media-header', htmlspecialchars( $this->title
->getText() ) ) . "</h2>\n";
393 $r .= $this->getSectionPagingLinks( 'file' );
394 $r .= $this->gallery
->toHTML();
395 $r .= $this->getSectionPagingLinks( 'file' );
402 * Get the paging links for a section (subcats/pages/files), to go at the top and bottom
405 * @param $type String: 'page', 'subcat', or 'file'
406 * @return String: HTML output, possibly empty if there are no other pages
408 private function getSectionPagingLinks( $type ) {
409 if ( $this->until
[$type] !== null ) {
410 return $this->pagingLinks( $this->nextPage
[$type], $this->until
[$type], $type );
411 } elseif ( $this->nextPage
[$type] !== null ||
$this->from
[$type] !== null ) {
412 return $this->pagingLinks( $this->from
[$type], $this->nextPage
[$type], $type );
418 function getCategoryBottom() {
423 * Format a list of articles chunked by letter, either as a
424 * bullet list or a columnar format, depending on the length.
426 * @param $articles Array
427 * @param $articles_start_char Array
432 function formatList( $articles, $articles_start_char, $cutoff = 6 ) {
433 if ( count ( $articles ) > $cutoff ) {
434 return self
::columnList( $articles, $articles_start_char );
435 } elseif ( count( $articles ) > 0 ) {
436 // for short lists of articles in categories.
437 return self
::shortList( $articles, $articles_start_char );
443 * Format a list of articles chunked by letter in a three-column
444 * list, ordered vertically.
446 * TODO: Take the headers into account when creating columns, so they're
447 * more visually equal.
449 * More distant TODO: Scrap this and use CSS columns, whenever IE finally
452 * @param $articles Array
453 * @param $articles_start_char Array
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 */ );
462 $ret = '<table width="100%"><tr valign="top"><td>';
465 foreach ( $columns as $column ) {
466 $colContents = array();
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();
474 $colContents[$char][] = $article;
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' );
488 $ret .= implode( "</li>\n<li>", $articles );
489 $ret .= '</li></ul>';
495 $ret .= "</td>\n<td>";
498 $ret .= '</td></tr></table>';
503 * Format a list of articles chunked by letter in a bullet list.
504 * @param $articles Array
505 * @param $articles_start_char Array
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++
)
514 if ( $articles_start_char[$index] != $articles_start_char[$index - 1] )
516 $r .= "</ul><h3>" . htmlspecialchars( $articles_start_char[$index] ) . "</h3>\n<ul>";
519 $r .= "<li>{$articles[$index]}</li>";
526 * Create paging links, as a helper method to getSectionPagingLinks().
528 * @param $first String The 'until' parameter for the generated URL
529 * @param $last String The 'from' parameter for the genererated URL
530 * @param $type String A prefix for parameters, 'page' or 'subcat' or
532 * @return String HTML
534 private function pagingLinks( $first, $last, $type = '' ) {
536 $sk = $this->getSkin();
537 $limitText = $wgLang->formatNum( $this->limit
);
539 $prevLink = wfMsgExt( 'prevn', array( 'escape', 'parsemag' ), $limitText );
541 if ( $first != '' ) {
542 $prevQuery = $this->query
;
543 $prevQuery["{$type}until"] = $first;
544 unset( $prevQuery["{$type}from"] );
545 $prevLink = $sk->linkKnown(
553 $nextLink = wfMsgExt( 'nextn', array( 'escape', 'parsemag' ), $limitText );
556 $lastQuery = $this->query
;
557 $lastQuery["{$type}from"] = $last;
558 unset( $lastQuery["{$type}until"] );
559 $nextLink = $sk->linkKnown(
567 return "($prevLink) ($nextLink)";
571 * What to do if the category table conflicts with the number of results
572 * returned? This function says what. It works the same whether the
573 * things being counted are articles, subcategories, or files.
575 * Note for grepping: uses the messages category-article-count,
576 * category-article-count-limited, category-subcat-count,
577 * category-subcat-count-limited, category-file-count,
578 * category-file-count-limited.
580 * @param $rescnt Int: The number of items returned by our database query.
581 * @param $dbcnt Int: The number of items according to the category table.
582 * @param $type String: 'subcat', 'article', or 'file'
583 * @return String: A message giving the number of items, to output to HTML.
585 private function getCountMessage( $rescnt, $dbcnt, $type ) {
587 # There are three cases:
588 # 1) The category table figure seems sane. It might be wrong, but
589 # we can't do anything about it if we don't recalculate it on ev-
591 # 2) The category table figure isn't sane, like it's smaller than the
592 # number of actual results, *but* the number of results is less
593 # than $this->limit and there's no offset. In this case we still
594 # know the right figure.
595 # 3) We have no idea.
596 $totalrescnt = count( $this->articles
) +
count( $this->children
) +
597 ( $this->showGallery ?
$this->gallery
->count() : 0 );
599 # Check if there's a "from" or "until" for anything
600 $fromOrUntil = false;
601 foreach ( array( 'page', 'subcat', 'file' ) as $t ) {
602 if ( $this->from
[$t] !== null ||
$this->until
[$t] !== null ) {
608 if ( $dbcnt == $rescnt ||
( ( $totalrescnt == $this->limit ||
$fromOrUntil )
609 && $dbcnt > $rescnt ) )
611 # Case 1: seems sane.
613 } elseif ( $totalrescnt < $this->limit
&& !$fromOrUntil ) {
614 # Case 2: not sane, but salvageable. Use the number of results.
615 # Since there are fewer than 200, we can also take this opportunity
616 # to refresh the incorrect category table entry -- which should be
617 # quick due to the small number of entries.
619 $this->cat
->refreshCounts();
621 # Case 3: hopeless. Don't give a total count at all.
622 return wfMsgExt( "category-$type-count-limited", 'parse',
623 $wgLang->formatNum( $rescnt ) );
626 "category-$type-count",
628 $wgLang->formatNum( $rescnt ),
629 $wgLang->formatNum( $totalcnt )