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,
92 $imgsNoGalley, $imgsNoGallery_start_char,
94 # Category object for this page
96 # The original query array, to be used in generating paging links.
99 function __construct( $title, $from = '', $until = '', $query = array() ) {
100 global $wgCategoryPagingLimit;
101 $this->title
= $title;
103 $this->until
= $until;
104 $this->limit
= $wgCategoryPagingLimit;
105 $this->cat
= Category
::newFromTitle( $title );
106 $this->query
= $query;
107 $this->collation
= Collation
::singleton();
108 unset( $this->query
['title'] );
112 * Format the category data list.
114 * @return string HTML output
116 public function getHTML() {
117 global $wgOut, $wgCategoryMagicGallery, $wgContLang;
118 wfProfileIn( __METHOD__
);
120 $this->showGallery
= $wgCategoryMagicGallery && !$wgOut->mNoGallery
;
122 $this->clearCategoryState();
123 $this->doCategoryQuery();
124 $this->finaliseCategoryState();
126 $r = $this->getSubcategorySection() .
127 $this->getPagesSection() .
128 $this->getImageSection();
131 // If there is no category content to display, only
132 // show the top part of the navigation links.
133 // FIXME: cannot be completely suppressed because it
134 // is unknown if 'until' or 'from' makes this
136 $r = $r . $this->getCategoryTop();
138 $r = $this->getCategoryTop() .
140 $this->getCategoryBottom();
143 // Give a proper message if category is empty
145 $r = wfMsgExt( 'category-empty', array( 'parse' ) );
148 wfProfileOut( __METHOD__
);
149 return $wgContLang->convert( $r );
152 function clearCategoryState() {
153 $this->articles
= array();
154 $this->articles_start_char
= array();
155 $this->children
= array();
156 $this->children_start_char
= array();
157 if ( $this->showGallery
) {
158 $this->gallery
= new ImageGallery();
159 $this->gallery
->setHideBadImages();
161 $this->imgsNoGallery
= array();
162 $this->imgsNoGallery_start_char
= array();
167 if ( !$this->skin
) {
169 $this->skin
= $wgUser->getSkin();
175 * Add a subcategory to the internal lists, using a Category object
177 function addSubcategoryObject( Category
$cat, $sortkey, $pageLength ) {
178 // Subcategory; strip the 'Category' namespace from the link text.
179 $title = $cat->getTitle();
181 $link = $this->getSkin()->link( $title, $title->getText() );
182 if ( $title->isRedirect() ) {
183 // This didn't used to add redirect-in-category, but might
184 // as well be consistent with the rest of the sections
185 // on a category page.
186 $link = '<span class="redirect-in-category">' . $link . '</span>';
188 $this->children
[] = $link;
190 $this->children_start_char
[] =
191 $this->getSubcategorySortChar( $cat->getTitle(), $sortkey );
195 * Add a subcategory to the internal lists, using a title object
196 * @deprecated kept for compatibility, please use addSubcategoryObject instead
198 function addSubcategory( Title
$title, $sortkey, $pageLength ) {
199 $this->addSubcategoryObject( Category
::newFromTitle( $title ), $sortkey, $pageLength );
203 * Get the character to be used for sorting subcategories.
204 * If there's a link from Category:A to Category:B, the sortkey of the resulting
205 * entry in the categorylinks table is Category:A, not A, which it SHOULD be.
206 * Workaround: If sortkey == "Category:".$title, than use $title for sorting,
207 * else use sortkey...
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', 'cl_sortkey_prefix' ),
311 array( 'cl_to' => $this->title
->getDBkey() ) +
$extraConds,
314 'USE INDEX' => array( 'categorylinks' => 'cl_sortkey' ),
315 'LIMIT' => $this->limit +
1,
316 'ORDER BY' => $this->flip
[$type] ?
'cl_sortkey DESC' : 'cl_sortkey',
319 'categorylinks' => array( 'INNER JOIN', 'cl_from = page_id' ),
320 'category' => array( 'LEFT JOIN', 'cat_title = page_title AND page_namespace = ' . NS_CATEGORY
)
325 foreach ( $res as $row ) {
326 $title = Title
::newFromRow( $row );
327 $rawSortkey = $title->getCategorySortkey( $row->cl_sortkey_prefix
);
329 if ( ++
$count > $this->limit
) {
330 # We've reached the one extra which shows that there
331 # are additional pages to be had. Stop here...
332 $this->nextPage
[$type] = $rawSortkey;
336 if ( $title->getNamespace() == NS_CATEGORY
) {
337 $cat = Category
::newFromRow( $row, $title );
338 $this->addSubcategoryObject( $cat, $rawSortkey, $row->page_len
);
339 } elseif ( $title->getNamespace() == NS_FILE
) {
340 $this->addImage( $title, $rawSortkey, $row->page_len
, $row->page_is_redirect
);
342 $this->addPage( $title, $rawSortkey, $row->page_len
, $row->page_is_redirect
);
348 function getCategoryTop() {
349 $r = $this->getCategoryBottom();
352 : "<br style=\"clear:both;\"/>\n" . $r;
355 function getSubcategorySection() {
356 # Don't show subcategories section if there are none.
358 $rescnt = count( $this->children
);
359 $dbcnt = $this->cat
->getSubcatCount();
360 $countmsg = $this->getCountMessage( $rescnt, $dbcnt, 'subcat' );
363 # Showing subcategories
364 $r .= "<div id=\"mw-subcategories\">\n";
365 $r .= '<h2>' . wfMsg( 'subcategories' ) . "</h2>\n";
367 $r .= $this->getSectionPagingLinks( 'subcat' );
368 $r .= $this->formatList( $this->children
, $this->children_start_char
);
369 $r .= $this->getSectionPagingLinks( 'subcat' );
375 function getPagesSection() {
376 $ti = htmlspecialchars( $this->title
->getText() );
377 # Don't show articles section if there are none.
380 # FIXME, here and in the other two sections: we don't need to bother
381 # with this rigamarole if the entire category contents fit on one page
382 # and have already been retrieved. We can just use $rescnt in that
383 # case and save a query and some logic.
384 $dbcnt = $this->cat
->getPageCount() - $this->cat
->getSubcatCount()
385 - $this->cat
->getFileCount();
386 $rescnt = count( $this->articles
);
387 $countmsg = $this->getCountMessage( $rescnt, $dbcnt, 'article' );
390 $r = "<div id=\"mw-pages\">\n";
391 $r .= '<h2>' . wfMsg( 'category_header', $ti ) . "</h2>\n";
393 $r .= $this->getSectionPagingLinks( 'page' );
394 $r .= $this->formatList( $this->articles
, $this->articles_start_char
);
395 $r .= $this->getSectionPagingLinks( 'page' );
401 function getImageSection() {
403 $rescnt = $this->showGallery ?
$this->gallery
->count() : count( $this->imgsNoGallery
);
405 $dbcnt = $this->cat
->getFileCount();
406 $countmsg = $this->getCountMessage( $rescnt, $dbcnt, 'file' );
408 $r .= "<div id=\"mw-category-media\">\n";
409 $r .= '<h2>' . wfMsg( 'category-media-header', htmlspecialchars( $this->title
->getText() ) ) . "</h2>\n";
411 $r .= $this->getSectionPagingLinks( 'file' );
412 if ( $this->showGallery
) {
413 $r .= $this->gallery
->toHTML();
415 $r .= $this->formatList( $this->imgsNoGallery
, $this->imgsNoGallery_start_char
);
417 $r .= $this->getSectionPagingLinks( 'file' );
424 * Get the paging links for a section (subcats/pages/files), to go at the top and bottom
427 * @param $type String: 'page', 'subcat', or 'file'
428 * @return String: HTML output, possibly empty if there are no other pages
430 private function getSectionPagingLinks( $type ) {
431 if ( $this->until
[$type] !== null ) {
432 return $this->pagingLinks( $this->nextPage
[$type], $this->until
[$type], $type );
433 } elseif ( $this->nextPage
[$type] !== null ||
$this->from
[$type] !== null ) {
434 return $this->pagingLinks( $this->from
[$type], $this->nextPage
[$type], $type );
440 function getCategoryBottom() {
445 * Format a list of articles chunked by letter, either as a
446 * bullet list or a columnar format, depending on the length.
448 * @param $articles Array
449 * @param $articles_start_char Array
454 function formatList( $articles, $articles_start_char, $cutoff = 6 ) {
455 if ( count ( $articles ) > $cutoff ) {
456 return self
::columnList( $articles, $articles_start_char );
457 } elseif ( count( $articles ) > 0 ) {
458 // for short lists of articles in categories.
459 return self
::shortList( $articles, $articles_start_char );
465 * Format a list of articles chunked by letter in a three-column
466 * list, ordered vertically.
468 * TODO: Take the headers into account when creating columns, so they're
469 * more visually equal.
471 * More distant TODO: Scrap this and use CSS columns, whenever IE finally
474 * @param $articles Array
475 * @param $articles_start_char Array
479 static function columnList( $articles, $articles_start_char ) {
480 $columns = array_combine( $articles, $articles_start_char );
481 # Split into three columns
482 $columns = array_chunk( $columns, ceil( count( $columns ) / 3 ), true /* preserve keys */ );
484 $ret = '<table width="100%"><tr valign="top"><td>';
487 foreach ( $columns as $column ) {
488 $colContents = array();
490 # Kind of like array_flip() here, but we keep duplicates in an
491 # array instead of dropping them.
492 foreach ( $column as $article => $char ) {
493 if ( !isset( $colContents[$char] ) ) {
494 $colContents[$char] = array();
496 $colContents[$char][] = $article;
500 foreach ( $colContents as $char => $articles ) {
501 $ret .= '<h3>' . htmlspecialchars( $char );
502 if ( $first && $char === $prevchar ) {
503 # We're continuing a previous chunk at the top of a new
504 # column, so add " cont." after the letter.
505 $ret .= ' ' . wfMsgHtml( 'listingcontinuesabbrev' );
510 $ret .= implode( "</li>\n<li>", $articles );
511 $ret .= '</li></ul>';
517 $ret .= "</td>\n<td>";
520 $ret .= '</td></tr></table>';
525 * Format a list of articles chunked by letter in a bullet list.
526 * @param $articles Array
527 * @param $articles_start_char Array
531 static function shortList( $articles, $articles_start_char ) {
532 $r = '<h3>' . htmlspecialchars( $articles_start_char[0] ) . "</h3>\n";
533 $r .= '<ul><li>' . $articles[0] . '</li>';
534 for ( $index = 1; $index < count( $articles ); $index++
)
536 if ( $articles_start_char[$index] != $articles_start_char[$index - 1] )
538 $r .= "</ul><h3>" . htmlspecialchars( $articles_start_char[$index] ) . "</h3>\n<ul>";
541 $r .= "<li>{$articles[$index]}</li>";
548 * Create paging links, as a helper method to getSectionPagingLinks().
550 * @param $first String The 'until' parameter for the generated URL
551 * @param $last String The 'from' parameter for the genererated URL
552 * @param $type String A prefix for parameters, 'page' or 'subcat' or
554 * @return String HTML
556 private function pagingLinks( $first, $last, $type = '' ) {
558 $sk = $this->getSkin();
559 $limitText = $wgLang->formatNum( $this->limit
);
561 $prevLink = wfMsgExt( 'prevn', array( 'escape', 'parsemag' ), $limitText );
563 if ( $first != '' ) {
564 $prevQuery = $this->query
;
565 $prevQuery["{$type}until"] = $first;
566 unset( $prevQuery["{$type}from"] );
567 $prevLink = $sk->linkKnown(
575 $nextLink = wfMsgExt( 'nextn', array( 'escape', 'parsemag' ), $limitText );
578 $lastQuery = $this->query
;
579 $lastQuery["{$type}from"] = $last;
580 unset( $lastQuery["{$type}until"] );
581 $nextLink = $sk->linkKnown(
589 return "($prevLink) ($nextLink)";
593 * What to do if the category table conflicts with the number of results
594 * returned? This function says what. Each type is considered independently
595 * of the other types.
597 * Note for grepping: uses the messages category-article-count,
598 * category-article-count-limited, category-subcat-count,
599 * category-subcat-count-limited, category-file-count,
600 * category-file-count-limited.
602 * @param $rescnt Int: The number of items returned by our database query.
603 * @param $dbcnt Int: The number of items according to the category table.
604 * @param $type String: 'subcat', 'article', or 'file'
605 * @return String: A message giving the number of items, to output to HTML.
607 private function getCountMessage( $rescnt, $dbcnt, $type ) {
609 # There are three cases:
610 # 1) The category table figure seems sane. It might be wrong, but
611 # we can't do anything about it if we don't recalculate it on ev-
613 # 2) The category table figure isn't sane, like it's smaller than the
614 # number of actual results, *but* the number of results is less
615 # than $this->limit and there's no offset. In this case we still
616 # know the right figure.
617 # 3) We have no idea.
619 # Check if there's a "from" or "until" for anything
621 // This is a little ugly, but we seem to use different names
622 // for the paging types then for the messages.
623 if ( $type === 'article' ) {
624 $pagingType = 'page';
629 $fromOrUntil = false;
630 if ( $this->from
[$pagingType] !== null ||
$this->until
[$pagingType] !== null ) {
634 if ( $dbcnt == $rescnt ||
( ( $rescnt == $this->limit ||
$fromOrUntil )
635 && $dbcnt > $rescnt ) )
637 # Case 1: seems sane.
639 } elseif ( $rescnt < $this->limit
&& !$fromOrUntil ) {
640 # Case 2: not sane, but salvageable. Use the number of results.
641 # Since there are fewer than 200, we can also take this opportunity
642 # to refresh the incorrect category table entry -- which should be
643 # quick due to the small number of entries.
645 $this->cat
->refreshCounts();
647 # Case 3: hopeless. Don't give a total count at all.
648 return wfMsgExt( "category-$type-count-limited", 'parse',
649 $wgLang->formatNum( $rescnt ) );
652 "category-$type-count",
654 $wgLang->formatNum( $rescnt ),
655 $wgLang->formatNum( $totalcnt )