Followup r63914, function must match parent
[mediawiki.git] / includes / CategoryPage.php
blob56f85faabeaa2c2a27d66329c823c6d251c8c3da
1 <?php
2 /**
3 * Special handling for category description pages
4 * Modelled after ImagePage.php
6 */
8 if ( !defined( 'MEDIAWIKI' ) )
9 die( 1 );
11 /**
13 class CategoryPage extends Article {
14 function view() {
15 global $wgRequest, $wgUser;
17 $diff = $wgRequest->getVal( 'diff' );
18 $diffOnly = $wgRequest->getBool( 'diffonly', $wgUser->getOption( 'diffonly' ) );
20 if ( isset( $diff ) && $diffOnly )
21 return Article::view();
23 if ( !wfRunHooks( 'CategoryPageView', array( &$this ) ) )
24 return;
26 if ( NS_CATEGORY == $this->mTitle->getNamespace() ) {
27 $this->openShowCategory();
30 Article::view();
32 if ( NS_CATEGORY == $this->mTitle->getNamespace() ) {
33 $this->closeShowCategory();
37 /**
38 * Don't return a 404 for categories in use.
40 function hasViewableContent() {
41 if ( parent::hasViewableContent() ) {
42 return true;
43 } else {
44 $cat = Category::newFromTitle( $this->mTitle );
45 return $cat->getId() != 0;
49 function openShowCategory() {
50 # For overloading
53 function closeShowCategory() {
54 global $wgOut, $wgRequest;
55 $from = $wgRequest->getVal( 'from' );
56 $until = $wgRequest->getVal( 'until' );
58 $viewer = new CategoryViewer( $this->mTitle, $from, $until );
59 $wgOut->addHTML( $viewer->getHTML() );
63 class CategoryViewer {
64 var $title, $limit, $from, $until,
65 $articles, $articles_start_char,
66 $children, $children_start_char,
67 $showGallery, $gallery,
68 $skin;
69 /** Category object for this page */
70 private $cat;
72 function __construct( $title, $from = '', $until = '' ) {
73 global $wgCategoryPagingLimit;
74 $this->title = $title;
75 $this->from = $from;
76 $this->until = $until;
77 $this->limit = $wgCategoryPagingLimit;
78 $this->cat = Category::newFromTitle( $title );
81 /**
82 * Format the category data list.
84 * @return string HTML output
85 * @private
87 function getHTML() {
88 global $wgOut, $wgCategoryMagicGallery, $wgCategoryPagingLimit, $wgContLang;
89 wfProfileIn( __METHOD__ );
91 $this->showGallery = $wgCategoryMagicGallery && !$wgOut->mNoGallery;
93 $this->clearCategoryState();
94 $this->doCategoryQuery();
95 $this->finaliseCategoryState();
97 $r = $this->getSubcategorySection() .
98 $this->getPagesSection() .
99 $this->getImageSection();
101 if ( $r == '' ) {
102 // If there is no category content to display, only
103 // show the top part of the navigation links.
104 // FIXME: cannot be completely suppressed because it
105 // is unknown if 'until' or 'from' makes this
106 // give 0 results.
107 $r = $r . $this->getCategoryTop();
108 } else {
109 $r = $this->getCategoryTop() .
110 $r .
111 $this->getCategoryBottom();
114 // Give a proper message if category is empty
115 if ( $r == '' ) {
116 $r = wfMsgExt( 'category-empty', array( 'parse' ) );
119 wfProfileOut( __METHOD__ );
120 return $wgContLang->convert( $r );
123 function clearCategoryState() {
124 $this->articles = array();
125 $this->articles_start_char = array();
126 $this->children = array();
127 $this->children_start_char = array();
128 if ( $this->showGallery ) {
129 $this->gallery = new ImageGallery();
130 $this->gallery->setHideBadImages();
134 function getSkin() {
135 if ( !$this->skin ) {
136 global $wgUser;
137 $this->skin = $wgUser->getSkin();
139 return $this->skin;
143 * Add a subcategory to the internal lists, using a Category object
145 function addSubcategoryObject( $cat, $sortkey, $pageLength ) {
146 $title = $cat->getTitle();
147 $this->addSubcategory( $title, $sortkey, $pageLength );
151 * Add a subcategory to the internal lists, using a title object
152 * @deprecated kept for compatibility, please use addSubcategoryObject instead
154 function addSubcategory( $title, $sortkey, $pageLength ) {
155 // Subcategory; strip the 'Category' namespace from the link text.
156 $this->children[] = $this->getSkin()->link(
157 $title,
158 null,
159 array(),
160 array(),
161 array( 'known', 'noclasses' )
164 $this->children_start_char[] = $this->getSubcategorySortChar( $title, $sortkey );
168 * Get the character to be used for sorting subcategories.
169 * If there's a link from Category:A to Category:B, the sortkey of the resulting
170 * entry in the categorylinks table is Category:A, not A, which it SHOULD be.
171 * Workaround: If sortkey == "Category:".$title, than use $title for sorting,
172 * else use sortkey...
174 function getSubcategorySortChar( $title, $sortkey ) {
175 global $wgContLang;
177 if ( $title->getPrefixedText() == $sortkey ) {
178 $firstChar = $wgContLang->firstChar( $title->getDBkey() );
179 } else {
180 $firstChar = $wgContLang->firstChar( $sortkey );
183 return $wgContLang->convert( $firstChar );
187 * Add a page in the image namespace
189 function addImage( Title $title, $sortkey, $pageLength, $isRedirect = false ) {
190 if ( $this->showGallery ) {
191 if ( $this->flip ) {
192 $this->gallery->insert( $title );
193 } else {
194 $this->gallery->add( $title );
196 } else {
197 $this->addPage( $title, $sortkey, $pageLength, $isRedirect );
202 * Add a miscellaneous page
204 function addPage( $title, $sortkey, $pageLength, $isRedirect = false ) {
205 global $wgContLang;
206 $this->articles[] = $isRedirect
207 ? '<span class="redirect-in-category">' .
208 $this->getSkin()->link(
209 $title,
210 null,
211 array(),
212 array(),
213 array( 'known', 'noclasses' )
214 ) . '</span>'
215 : $this->getSkin()->makeSizeLinkObj( $pageLength, $title );
216 $this->articles_start_char[] = $wgContLang->convert( $wgContLang->firstChar( $sortkey ) );
219 function finaliseCategoryState() {
220 if ( $this->flip ) {
221 $this->children = array_reverse( $this->children );
222 $this->children_start_char = array_reverse( $this->children_start_char );
223 $this->articles = array_reverse( $this->articles );
224 $this->articles_start_char = array_reverse( $this->articles_start_char );
228 function doCategoryQuery() {
229 $dbr = wfGetDB( DB_SLAVE, 'category' );
230 if ( $this->from != '' ) {
231 $pageCondition = 'cl_sortkey >= ' . $dbr->addQuotes( $this->from );
232 $this->flip = false;
233 } elseif ( $this->until != '' ) {
234 $pageCondition = 'cl_sortkey < ' . $dbr->addQuotes( $this->until );
235 $this->flip = true;
236 } else {
237 $pageCondition = '1 = 1';
238 $this->flip = false;
241 $res = $dbr->select(
242 array( 'page', 'categorylinks', 'category' ),
243 array( 'page_title', 'page_namespace', 'page_len', 'page_is_redirect', 'cl_sortkey',
244 'cat_id', 'cat_title', 'cat_subcats', 'cat_pages', 'cat_files' ),
245 array( $pageCondition, 'cl_to' => $this->title->getDBkey() ),
246 __METHOD__,
247 array( 'ORDER BY' => $this->flip ? 'cl_sortkey DESC' : 'cl_sortkey',
248 'USE INDEX' => array( 'categorylinks' => 'cl_sortkey' ),
249 'LIMIT' => $this->limit + 1 ),
250 array( 'categorylinks' => array( 'INNER JOIN', 'cl_from = page_id' ),
251 'category' => array( 'LEFT JOIN', 'cat_title = page_title AND page_namespace = ' . NS_CATEGORY ) )
254 $count = 0;
255 $this->nextPage = null;
257 while ( $x = $dbr->fetchObject ( $res ) ) {
258 if ( ++$count > $this->limit ) {
259 // We've reached the one extra which shows that there are
260 // additional pages to be had. Stop here...
261 $this->nextPage = $x->cl_sortkey;
262 break;
265 $title = Title::makeTitle( $x->page_namespace, $x->page_title );
267 if ( $title->getNamespace() == NS_CATEGORY ) {
268 $cat = Category::newFromRow( $x, $title );
269 $this->addSubcategoryObject( $cat, $x->cl_sortkey, $x->page_len );
270 } elseif ( $this->showGallery && $title->getNamespace() == NS_FILE ) {
271 $this->addImage( $title, $x->cl_sortkey, $x->page_len, $x->page_is_redirect );
272 } else {
273 $this->addPage( $title, $x->cl_sortkey, $x->page_len, $x->page_is_redirect );
278 function getCategoryTop() {
279 $r = $this->getCategoryBottom();
280 return $r === ''
281 ? $r
282 : "<br style=\"clear:both;\"/>\n" . $r;
285 function getSubcategorySection() {
286 # Don't show subcategories section if there are none.
287 $r = '';
288 $rescnt = count( $this->children );
289 $dbcnt = $this->cat->getSubcatCount();
290 $countmsg = $this->getCountMessage( $rescnt, $dbcnt, 'subcat' );
292 if ( $rescnt > 0 ) {
293 # Showing subcategories
294 $r .= "<div id=\"mw-subcategories\">\n";
295 $r .= '<h2>' . wfMsg( 'subcategories' ) . "</h2>\n";
296 $r .= $countmsg;
297 $r .= $this->formatList( $this->children, $this->children_start_char );
298 $r .= "\n</div>";
300 return $r;
303 function getPagesSection() {
304 $ti = htmlspecialchars( $this->title->getText() );
305 # Don't show articles section if there are none.
306 $r = '';
308 # FIXME, here and in the other two sections: we don't need to bother
309 # with this rigamarole if the entire category contents fit on one page
310 # and have already been retrieved. We can just use $rescnt in that
311 # case and save a query and some logic.
312 $dbcnt = $this->cat->getPageCount() - $this->cat->getSubcatCount()
313 - $this->cat->getFileCount();
314 $rescnt = count( $this->articles );
315 $countmsg = $this->getCountMessage( $rescnt, $dbcnt, 'article' );
317 if ( $rescnt > 0 ) {
318 $r = "<div id=\"mw-pages\">\n";
319 $r .= '<h2>' . wfMsg( 'category_header', $ti ) . "</h2>\n";
320 $r .= $countmsg;
321 $r .= $this->formatList( $this->articles, $this->articles_start_char );
322 $r .= "\n</div>";
324 return $r;
327 function getImageSection() {
328 if ( $this->showGallery && ! $this->gallery->isEmpty() ) {
329 $dbcnt = $this->cat->getFileCount();
330 $rescnt = $this->gallery->count();
331 $countmsg = $this->getCountMessage( $rescnt, $dbcnt, 'file' );
333 return "<div id=\"mw-category-media\">\n" .
334 '<h2>' . wfMsg( 'category-media-header', htmlspecialchars( $this->title->getText() ) ) . "</h2>\n" .
335 $countmsg . $this->gallery->toHTML() . "\n</div>";
336 } else {
337 return '';
341 function getCategoryBottom() {
342 if ( $this->until != '' ) {
343 return $this->pagingLinks( $this->title, $this->nextPage, $this->until, $this->limit );
344 } elseif ( $this->nextPage != '' || $this->from != '' ) {
345 return $this->pagingLinks( $this->title, $this->from, $this->nextPage, $this->limit );
346 } else {
347 return '';
352 * Format a list of articles chunked by letter, either as a
353 * bullet list or a columnar format, depending on the length.
355 * @param $articles Array
356 * @param $articles_start_char Array
357 * @param $cutoff Int
358 * @return String
359 * @private
361 function formatList( $articles, $articles_start_char, $cutoff = 6 ) {
362 if ( count ( $articles ) > $cutoff ) {
363 return $this->columnList( $articles, $articles_start_char );
364 } elseif ( count( $articles ) > 0 ) {
365 // for short lists of articles in categories.
366 return $this->shortList( $articles, $articles_start_char );
368 return '';
372 * Format a list of articles chunked by letter in a three-column
373 * list, ordered vertically.
375 * TODO: Take the headers into account when creating columns, so they're
376 * more visually equal.
378 * More distant TODO: Scrap this and use CSS columns, whenever IE finally
379 * supports those.
381 * @param $articles Array
382 * @param $articles_start_char Array
383 * @return String
384 * @private
386 function columnList( $articles, $articles_start_char ) {
387 $columns = array_combine( $articles, $articles_start_char );
388 # Split into three columns
389 $columns = array_chunk( $columns, ceil( count( $columns ) / 3 ), true /* preserve keys */ );
391 $ret = '<table width="100%"><tr valign="top"><td>';
392 $prevchar = null;
394 foreach ( $columns as $column ) {
395 $colContents = array();
397 # Kind of like array_flip() here, but we keep duplicates in an
398 # array instead of dropping them.
399 foreach ( $column as $article => $char ) {
400 if ( !isset( $colContents[$char] ) ) {
401 $colContents[$char] = array();
403 $colContents[$char][] = $article;
406 $first = true;
407 foreach ( $colContents as $char => $articles ) {
408 $ret .= '<h3>' . htmlspecialchars( $char );
409 if ( $first && $char === $prevchar ) {
410 # We're continuing a previous chunk at the top of a new
411 # column, so add " cont." after the letter.
412 $ret .= ' ' . wfMsgHtml( 'listingcontinuesabbrev' );
414 $ret .= "</h3>\n";
416 $ret .= '<ul><li>';
417 $ret .= implode( "</li>\n<li>", $articles );
418 $ret .= '</li></ul>';
420 $first = false;
421 $prevchar = $char;
424 $ret .= "</td>\n<td>";
427 $ret .= '</td></tr></table>';
428 return $ret;
432 * Format a list of articles chunked by letter in a bullet list.
433 * @param $articles Array
434 * @param $articles_start_char Array
435 * @return String
436 * @private
438 function shortList( $articles, $articles_start_char ) {
439 $r = '<h3>' . htmlspecialchars( $articles_start_char[0] ) . "</h3>\n";
440 $r .= '<ul><li>' . $articles[0] . '</li>';
441 for ( $index = 1; $index < count( $articles ); $index++ )
443 if ( $articles_start_char[$index] != $articles_start_char[$index - 1] )
445 $r .= "</ul><h3>" . htmlspecialchars( $articles_start_char[$index] ) . "</h3>\n<ul>";
448 $r .= "<li>{$articles[$index]}</li>";
450 $r .= '</ul>';
451 return $r;
455 * @param $title Title object
456 * @param $first String
457 * @param $last String
458 * @param $limit Int
459 * @param $query Array: additional query options to pass
460 * @return String
461 * @private
463 function pagingLinks( $title, $first, $last, $limit, $query = array() ) {
464 global $wgLang;
465 $sk = $this->getSkin();
466 $limitText = $wgLang->formatNum( $limit );
468 $prevLink = wfMsgExt( 'prevn', array( 'escape', 'parsemag' ), $limitText );
470 if ( $first != '' ) {
471 $prevQuery = $query;
472 $prevQuery['until'] = $first;
473 $prevLink = $sk->linkKnown(
474 $title,
475 $prevLink,
476 array(),
477 $prevQuery
481 $nextLink = wfMsgExt( 'nextn', array( 'escape', 'parsemag' ), $limitText );
483 if ( $last != '' ) {
484 $lastQuery = $query;
485 $lastQuery['from'] = $last;
486 $nextLink = $sk->linkKnown(
487 $title,
488 $nextLink,
489 array(),
490 $lastQuery
494 return "($prevLink) ($nextLink)";
498 * What to do if the category table conflicts with the number of results
499 * returned? This function says what. It works the same whether the
500 * things being counted are articles, subcategories, or files.
502 * Note for grepping: uses the messages category-article-count,
503 * category-article-count-limited, category-subcat-count,
504 * category-subcat-count-limited, category-file-count,
505 * category-file-count-limited.
507 * @param $rescnt Int: The number of items returned by our database query.
508 * @param $dbcnt Int: The number of items according to the category table.
509 * @param $type String: 'subcat', 'article', or 'file'
510 * @return String: A message giving the number of items, to output to HTML.
512 private function getCountMessage( $rescnt, $dbcnt, $type ) {
513 global $wgLang;
514 # There are three cases:
515 # 1) The category table figure seems sane. It might be wrong, but
516 # we can't do anything about it if we don't recalculate it on ev-
517 # ery category view.
518 # 2) The category table figure isn't sane, like it's smaller than the
519 # number of actual results, *but* the number of results is less
520 # than $this->limit and there's no offset. In this case we still
521 # know the right figure.
522 # 3) We have no idea.
523 $totalrescnt = count( $this->articles ) + count( $this->children ) +
524 ( $this->showGallery ? $this->gallery->count() : 0 );
526 if ( $dbcnt == $rescnt || ( ( $totalrescnt == $this->limit || $this->from
527 || $this->until ) && $dbcnt > $rescnt ) )
529 # Case 1: seems sane.
530 $totalcnt = $dbcnt;
531 } elseif ( $totalrescnt < $this->limit && !$this->from && !$this->until ) {
532 # Case 2: not sane, but salvageable. Use the number of results.
533 # Since there are fewer than 200, we can also take this opportunity
534 # to refresh the incorrect category table entry -- which should be
535 # quick due to the small number of entries.
536 $totalcnt = $rescnt;
537 $this->cat->refreshCounts();
538 } else {
539 # Case 3: hopeless. Don't give a total count at all.
540 return wfMsgExt( "category-$type-count-limited", 'parse',
541 $wgLang->formatNum( $rescnt ) );
543 return wfMsgExt(
544 "category-$type-count",
545 'parse',
546 $wgLang->formatNum( $rescnt ),
547 $wgLang->formatNum( $totalcnt )