3 * Special handling for category description pages
4 * Modelled after ImagePage.php
8 if ( !defined( 'MEDIAWIKI' ) )
13 class CategoryPage
extends Article
{
15 global $wgRequest, $wgUser;
17 $diff = $wgRequest->getVal( 'diff' );
18 $diffOnly = $wgRequest->getBool( 'diffonly', $wgUser->getOption( 'diffonly' ) );
20 if ( isset( $diff ) && $diffOnly )
21 return parent
::view();
23 if ( !wfRunHooks( 'CategoryPageView', array( &$this ) ) )
26 if ( NS_CATEGORY
== $this->mTitle
->getNamespace() ) {
27 $this->openShowCategory();
32 if ( NS_CATEGORY
== $this->mTitle
->getNamespace() ) {
33 $this->closeShowCategory();
38 * Don't return a 404 for categories in use.
40 function hasViewableContent() {
41 if ( parent
::hasViewableContent() ) {
44 $cat = Category
::newFromTitle( $this->mTitle
);
45 return $cat->getId() != 0;
49 function openShowCategory() {
53 function closeShowCategory() {
54 global $wgOut, $wgRequest;
56 $from = $until = array();
57 foreach ( array( 'page', 'subcat', 'file' ) as $type ) {
58 # Use $_GET instead of $wgRequest, because the latter helpfully
59 # normalizes Unicode, which removes nulls. TODO: do something
60 # smarter than passing nulls in URLs. :/
61 $from[$type] = isset( $_GET["{$type}from"] ) ?
$_GET["{$type}from"] : null;
62 $until[$type] = isset( $_GET["{$type}until"] ) ?
$_GET["{$type}until"] : null;
65 $viewer = new CategoryViewer( $this->mTitle
, $from, $until, $_GET );
66 $wgOut->addHTML( $viewer->getHTML() );
70 class CategoryViewer
{
71 var $title, $limit, $from, $until,
72 $articles, $articles_start_char,
73 $children, $children_start_char,
74 $showGallery, $gallery,
76 # Category object for this page
78 # The original query array, to be used in generating paging links.
81 function __construct( $title, $from = '', $until = '', $query = array() ) {
82 global $wgCategoryPagingLimit;
83 $this->title
= $title;
85 $this->until
= $until;
86 $this->limit
= $wgCategoryPagingLimit;
87 $this->cat
= Category
::newFromTitle( $title );
88 $this->query
= $query;
92 * Format the category data list.
94 * @return string HTML output
98 global $wgOut, $wgCategoryMagicGallery, $wgContLang;
99 wfProfileIn( __METHOD__
);
101 $this->showGallery
= $wgCategoryMagicGallery && !$wgOut->mNoGallery
;
103 $this->clearCategoryState();
104 $this->doCategoryQuery();
105 $this->finaliseCategoryState();
107 $r = $this->getSubcategorySection() .
108 $this->getPagesSection() .
109 $this->getImageSection();
112 // If there is no category content to display, only
113 // show the top part of the navigation links.
114 // FIXME: cannot be completely suppressed because it
115 // is unknown if 'until' or 'from' makes this
117 $r = $r . $this->getCategoryTop();
119 $r = $this->getCategoryTop() .
121 $this->getCategoryBottom();
124 // Give a proper message if category is empty
126 $r = wfMsgExt( 'category-empty', array( 'parse' ) );
129 wfProfileOut( __METHOD__
);
130 return $wgContLang->convert( $r );
133 function clearCategoryState() {
134 $this->articles
= array();
135 $this->articles_start_char
= array();
136 $this->children
= array();
137 $this->children_start_char
= array();
138 if ( $this->showGallery
) {
139 $this->gallery
= new ImageGallery();
140 $this->gallery
->setHideBadImages();
145 if ( !$this->skin
) {
147 $this->skin
= $wgUser->getSkin();
153 * Add a subcategory to the internal lists, using a Category object
155 function addSubcategoryObject( $cat, $sortkey, $pageLength ) {
156 $title = $cat->getTitle();
157 $this->addSubcategory( $title, $sortkey, $pageLength );
161 * Add a subcategory to the internal lists, using a title object
162 * @deprecated kept for compatibility, please use addSubcategoryObject instead
164 function addSubcategory( $title, $sortkey, $pageLength ) {
165 // Subcategory; strip the 'Category' namespace from the link text.
166 $this->children
[] = $this->getSkin()->link(
171 array( 'known', 'noclasses' )
174 $this->children_start_char
[] = $this->getSubcategorySortChar( $title, $sortkey );
178 * Get the character to be used for sorting subcategories.
179 * If there's a link from Category:A to Category:B, the sortkey of the resulting
180 * entry in the categorylinks table is Category:A, not A, which it SHOULD be.
181 * Workaround: If sortkey == "Category:".$title, than use $title for sorting,
182 * else use sortkey...
184 function getSubcategorySortChar( $title, $sortkey ) {
187 if ( $title->getPrefixedText() == $sortkey ) {
188 $word = $title->getDBkey();
193 $firstChar = $wgContLang->firstLetterForLists( $word );
195 return $wgContLang->convert( $firstChar );
199 * Add a page in the image namespace
201 function addImage( Title
$title, $sortkey, $pageLength, $isRedirect = false ) {
202 if ( $this->showGallery
) {
203 $flip = $this->flip
['file'];
205 $this->gallery
->insert( $title );
207 $this->gallery
->add( $title );
210 $this->addPage( $title, $sortkey, $pageLength, $isRedirect );
215 * Add a miscellaneous page
217 function addPage( $title, $sortkey, $pageLength, $isRedirect = false ) {
219 $this->articles
[] = $isRedirect
220 ?
'<span class="redirect-in-category">' .
221 $this->getSkin()->link(
226 array( 'known', 'noclasses' )
228 : $this->getSkin()->link( $title );
230 $this->articles_start_char
[] = $wgContLang->convert( $wgContLang->firstLetterForLists( $sortkey ) );
233 function finaliseCategoryState() {
234 if ( $this->flip
['subcat'] ) {
235 $this->children
= array_reverse( $this->children
);
236 $this->children_start_char
= array_reverse( $this->children_start_char
);
238 if ( $this->flip
['page'] ) {
239 $this->articles
= array_reverse( $this->articles
);
240 $this->articles_start_char
= array_reverse( $this->articles_start_char
);
244 function doCategoryQuery() {
247 $dbr = wfGetDB( DB_SLAVE
, 'category' );
249 $tables = array( 'page', 'categorylinks', 'category' );
250 $fields = array( 'page_title', 'page_namespace', 'page_len',
251 'page_is_redirect', 'cl_sortkey', 'cat_id', 'cat_title',
252 'cat_subcats', 'cat_pages', 'cat_files' );
253 $conds = array( 'cl_to' => $this->title
->getDBkey() );
255 'USE INDEX' => array( 'categorylinks' => 'cl_sortkey' ),
256 'LIMIT' => $this->limit +
1,
258 $joins = array( 'categorylinks' => array( 'INNER JOIN', 'cl_from = page_id' ),
259 'category' => array( 'LEFT JOIN', 'cat_title = page_title AND page_namespace = ' . NS_CATEGORY
) );
261 $this->nextPage
= array(
266 $this->flip
= array( 'page' => false, 'subcat' => false, 'file' => false );
268 foreach ( array( 'page', 'subcat', 'file' ) as $type ) {
269 # Get the sortkeys for start/end, if applicable. Note that if
270 # the collation in the database differs from the one
271 # $wgContLang is using, pagination might go totally haywire.
272 $extraConds = array( 'cl_type' => $type );
273 if ( $this->from
[$type] !== null ) {
274 $extraConds[] = 'cl_sortkey >= '
275 . $dbr->addQuotes( $wgContLang->convertToSortkey( $this->from
[$type] ) );
276 } elseif ( $this->until
[$type] !== null ) {
277 $extraConds[] = 'cl_sortkey < '
278 . $dbr->addQuotes( $wgContLang->convertToSortkey( $this->until
[$type] ) );
279 $this->flip
[$type] = true;
284 array_merge( $fields, array( 'cl_sortkey_prefix' ) ),
285 $conds +
$extraConds,
287 $opts +
array( 'ORDER BY' => $this->flip
[$type] ?
'cl_sortkey DESC' : 'cl_sortkey' ),
292 foreach ( $res as $row ) {
293 $title = Title
::newFromRow( $row );
294 $rawSortkey = $title->getCategorySortkey( $row->cl_sortkey_prefix
);
296 if ( ++
$count > $this->limit
) {
297 # We've reached the one extra which shows that there
298 # are additional pages to be had. Stop here...
299 $this->nextPage
[$type] = $rawSortkey;
303 if ( $title->getNamespace() == NS_CATEGORY
) {
304 $cat = Category
::newFromRow( $row, $title );
305 $this->addSubcategoryObject( $cat, $rawSortkey, $row->page_len
);
306 } elseif ( $this->showGallery
&& $title->getNamespace() == NS_FILE
) {
307 $this->addImage( $title, $rawSortkey, $row->page_len
, $row->page_is_redirect
);
309 $this->addPage( $title, $rawSortkey, $row->page_len
, $row->page_is_redirect
);
315 function getCategoryTop() {
316 $r = $this->getCategoryBottom();
319 : "<br style=\"clear:both;\"/>\n" . $r;
322 function getSubcategorySection() {
323 # Don't show subcategories section if there are none.
325 $rescnt = count( $this->children
);
326 $dbcnt = $this->cat
->getSubcatCount();
327 $countmsg = $this->getCountMessage( $rescnt, $dbcnt, 'subcat' );
330 # Showing subcategories
331 $r .= "<div id=\"mw-subcategories\">\n";
332 $r .= '<h2>' . wfMsg( 'subcategories' ) . "</h2>\n";
334 $r .= $this->getSectionPagingLinks( 'subcat' );
335 $r .= $this->formatList( $this->children
, $this->children_start_char
);
336 $r .= $this->getSectionPagingLinks( 'subcat' );
342 function getPagesSection() {
343 $ti = htmlspecialchars( $this->title
->getText() );
344 # Don't show articles section if there are none.
347 # FIXME, here and in the other two sections: we don't need to bother
348 # with this rigamarole if the entire category contents fit on one page
349 # and have already been retrieved. We can just use $rescnt in that
350 # case and save a query and some logic.
351 $dbcnt = $this->cat
->getPageCount() - $this->cat
->getSubcatCount()
352 - $this->cat
->getFileCount();
353 $rescnt = count( $this->articles
);
354 $countmsg = $this->getCountMessage( $rescnt, $dbcnt, 'article' );
357 $r = "<div id=\"mw-pages\">\n";
358 $r .= '<h2>' . wfMsg( 'category_header', $ti ) . "</h2>\n";
360 $r .= $this->getSectionPagingLinks( 'page' );
361 $r .= $this->formatList( $this->articles
, $this->articles_start_char
);
362 $r .= $this->getSectionPagingLinks( 'page' );
368 function getImageSection() {
370 if ( $this->showGallery
&& ! $this->gallery
->isEmpty() ) {
371 $dbcnt = $this->cat
->getFileCount();
372 $rescnt = $this->gallery
->count();
373 $countmsg = $this->getCountMessage( $rescnt, $dbcnt, 'file' );
375 $r .= "<div id=\"mw-category-media\">\n";
376 $r .= '<h2>' . wfMsg( 'category-media-header', htmlspecialchars( $this->title
->getText() ) ) . "</h2>\n";
378 $r .= $this->getSectionPagingLinks( 'file' );
379 $r .= $this->gallery
->toHTML();
380 $r .= $this->getSectionPagingLinks( 'file' );
387 * Get the paging links for a section (subcats/pages/files), to go at the top and bottom
390 * @param $type String: 'page', 'subcat', or 'file'
391 * @return String: HTML output, possibly empty if there are no other pages
393 private function getSectionPagingLinks( $type ) {
394 if ( $this->until
[$type] !== null ) {
395 return $this->pagingLinks( $this->nextPage
[$type], $this->until
[$type], $type );
396 } elseif ( $this->nextPage
[$type] !== null ||
$this->from
[$type] !== null ) {
397 return $this->pagingLinks( $this->from
[$type], $this->nextPage
[$type], $type );
403 function getCategoryBottom() {
408 * Format a list of articles chunked by letter, either as a
409 * bullet list or a columnar format, depending on the length.
411 * @param $articles Array
412 * @param $articles_start_char Array
417 function formatList( $articles, $articles_start_char, $cutoff = 6 ) {
418 if ( count ( $articles ) > $cutoff ) {
419 return $this->columnList( $articles, $articles_start_char );
420 } elseif ( count( $articles ) > 0 ) {
421 // for short lists of articles in categories.
422 return $this->shortList( $articles, $articles_start_char );
428 * Format a list of articles chunked by letter in a three-column
429 * list, ordered vertically.
431 * TODO: Take the headers into account when creating columns, so they're
432 * more visually equal.
434 * More distant TODO: Scrap this and use CSS columns, whenever IE finally
437 * @param $articles Array
438 * @param $articles_start_char Array
442 function columnList( $articles, $articles_start_char ) {
443 $columns = array_combine( $articles, $articles_start_char );
444 # Split into three columns
445 $columns = array_chunk( $columns, ceil( count( $columns ) / 3 ), true /* preserve keys */ );
447 $ret = '<table width="100%"><tr valign="top"><td>';
450 foreach ( $columns as $column ) {
451 $colContents = array();
453 # Kind of like array_flip() here, but we keep duplicates in an
454 # array instead of dropping them.
455 foreach ( $column as $article => $char ) {
456 if ( !isset( $colContents[$char] ) ) {
457 $colContents[$char] = array();
459 $colContents[$char][] = $article;
463 foreach ( $colContents as $char => $articles ) {
464 $ret .= '<h3>' . htmlspecialchars( $char );
465 if ( $first && $char === $prevchar ) {
466 # We're continuing a previous chunk at the top of a new
467 # column, so add " cont." after the letter.
468 $ret .= ' ' . wfMsgHtml( 'listingcontinuesabbrev' );
473 $ret .= implode( "</li>\n<li>", $articles );
474 $ret .= '</li></ul>';
480 $ret .= "</td>\n<td>";
483 $ret .= '</td></tr></table>';
488 * Format a list of articles chunked by letter in a bullet list.
489 * @param $articles Array
490 * @param $articles_start_char Array
494 function shortList( $articles, $articles_start_char ) {
495 $r = '<h3>' . htmlspecialchars( $articles_start_char[0] ) . "</h3>\n";
496 $r .= '<ul><li>' . $articles[0] . '</li>';
497 for ( $index = 1; $index < count( $articles ); $index++
)
499 if ( $articles_start_char[$index] != $articles_start_char[$index - 1] )
501 $r .= "</ul><h3>" . htmlspecialchars( $articles_start_char[$index] ) . "</h3>\n<ul>";
504 $r .= "<li>{$articles[$index]}</li>";
511 * Create paging links, as a helper method to getSectionPagingLinks().
513 * @param $first String The 'until' parameter for the generated URL
514 * @param $last String The 'from' parameter for the genererated URL
515 * @param $type String A prefix for parameters, 'page' or 'subcat' or
517 * @return String HTML
519 private function pagingLinks( $first, $last, $type = '' ) {
521 $sk = $this->getSkin();
522 $limitText = $wgLang->formatNum( $this->limit
);
524 $prevLink = wfMsgExt( 'prevn', array( 'escape', 'parsemag' ), $limitText );
526 if ( $first != '' ) {
527 $prevQuery = $this->query
;
528 $prevQuery["{$type}until"] = $first;
529 unset( $prevQuery["{$type}from"] );
530 $prevLink = $sk->linkKnown(
538 $nextLink = wfMsgExt( 'nextn', array( 'escape', 'parsemag' ), $limitText );
541 $lastQuery = $this->query
;
542 $lastQuery["{$type}from"] = $last;
543 unset( $lastQuery["{$type}until"] );
544 $nextLink = $sk->linkKnown(
552 return "($prevLink) ($nextLink)";
556 * What to do if the category table conflicts with the number of results
557 * returned? This function says what. It works the same whether the
558 * things being counted are articles, subcategories, or files.
560 * Note for grepping: uses the messages category-article-count,
561 * category-article-count-limited, category-subcat-count,
562 * category-subcat-count-limited, category-file-count,
563 * category-file-count-limited.
565 * @param $rescnt Int: The number of items returned by our database query.
566 * @param $dbcnt Int: The number of items according to the category table.
567 * @param $type String: 'subcat', 'article', or 'file'
568 * @return String: A message giving the number of items, to output to HTML.
570 private function getCountMessage( $rescnt, $dbcnt, $type ) {
572 # There are three cases:
573 # 1) The category table figure seems sane. It might be wrong, but
574 # we can't do anything about it if we don't recalculate it on ev-
576 # 2) The category table figure isn't sane, like it's smaller than the
577 # number of actual results, *but* the number of results is less
578 # than $this->limit and there's no offset. In this case we still
579 # know the right figure.
580 # 3) We have no idea.
581 $totalrescnt = count( $this->articles
) +
count( $this->children
) +
582 ( $this->showGallery ?
$this->gallery
->count() : 0 );
584 if ( $dbcnt == $rescnt ||
( ( $totalrescnt == $this->limit ||
$this->from
585 ||
$this->until
) && $dbcnt > $rescnt ) )
587 # Case 1: seems sane.
589 } elseif ( $totalrescnt < $this->limit
&& !$this->from
&& !$this->until
) {
590 # Case 2: not sane, but salvageable. Use the number of results.
591 # Since there are fewer than 200, we can also take this opportunity
592 # to refresh the incorrect category table entry -- which should be
593 # quick due to the small number of entries.
595 $this->cat
->refreshCounts();
597 # Case 3: hopeless. Don't give a total count at all.
598 return wfMsgExt( "category-$type-count-limited", 'parse',
599 $wgLang->formatNum( $rescnt ) );
602 "category-$type-count",
604 $wgLang->formatNum( $rescnt ),
605 $wgLang->formatNum( $totalcnt )