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 Article
::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;
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,
69 /** Category object for this page */
72 function __construct( $title, $from = '', $until = '' ) {
73 global $wgCategoryPagingLimit;
74 $this->title
= $title;
76 $this->until
= $until;
77 $this->limit
= $wgCategoryPagingLimit;
78 $this->cat
= Category
::newFromTitle( $title );
82 * Format the category data list.
84 * @return string HTML output
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();
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
107 $r = $r . $this->getCategoryTop();
109 $r = $this->getCategoryTop() .
111 $this->getCategoryBottom();
114 // Give a proper message if category is empty
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();
135 if ( !$this->skin
) {
137 $this->skin
= $wgUser->getSkin();
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(
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 ) {
177 if ( $title->getPrefixedText() == $sortkey ) {
178 $firstChar = $wgContLang->firstChar( $title->getDBkey() );
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
) {
192 $this->gallery
->insert( $title );
194 $this->gallery
->add( $title );
197 $this->addPage( $title, $sortkey, $pageLength, $isRedirect );
202 * Add a miscellaneous page
204 function addPage( $title, $sortkey, $pageLength, $isRedirect = false ) {
206 $this->articles
[] = $isRedirect
207 ?
'<span class="redirect-in-category">' .
208 $this->getSkin()->link(
213 array( 'known', 'noclasses' )
215 : $this->getSkin()->makeSizeLinkObj( $pageLength, $title );
216 $this->articles_start_char
[] = $wgContLang->convert( $wgContLang->firstChar( $sortkey ) );
219 function finaliseCategoryState() {
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 global $wgExperimentalCategorySort;
231 $dbr = wfGetDB( DB_SLAVE
, 'category' );
232 if ( $this->from
!= '' ) {
233 $pageCondition = 'cl_sortkey >= ' . $dbr->addQuotes( $this->from
);
235 } elseif ( $this->until
!= '' ) {
236 $pageCondition = 'cl_sortkey < ' . $dbr->addQuotes( $this->until
);
239 $pageCondition = '1 = 1';
243 $tables = array( 'page', 'categorylinks', 'category' );
244 $fields = array( 'page_title', 'page_namespace', 'page_len',
245 'page_is_redirect', 'cl_sortkey', 'cat_id', 'cat_title',
246 'cat_subcats', 'cat_pages', 'cat_files' );
247 $conds = array( 'cl_to' => $this->title
->getDBkey() );
248 $opts = array( 'ORDER BY' => $this->flip ?
'cl_sortkey DESC' :
249 'cl_sortkey', 'USE INDEX' => array( 'categorylinks' => 'cl_sortkey' ) );
250 $joins = array( 'categorylinks' => array( 'INNER JOIN', 'cl_from = page_id' ),
251 'category' => array( 'LEFT JOIN', 'cat_title = page_title AND page_namespace = ' . NS_CATEGORY
) );
253 if ( $wgExperimentalCategorySort ) {
254 # Copy-pasted from below, but that's okay, because the stuff below
255 # will be deleted when this becomes the default.
257 $this->nextPage
= null;
259 foreach ( array( 'page', 'subcat', 'file' ) as $type ) {
263 $conds +
array( 'cl_type' => $type ) +
( $type == 'page' ?
array( $pageCondition ) : array() ),
265 $opts +
( $type == 'page' ?
array( 'LIMIT' => $this->limit +
1 ) : array() ),
269 foreach ( $res as $row ) {
270 if ( $type == 'page' && ++
$count > $this->limit
) {
271 # We've reached the one extra which shows that there
272 # are additional pages to be had. Stop here...
273 $this->nextPage
= $row->cl_sortkey
;
277 $title = Title
::newFromRow( $row );
279 if ( $title->getNamespace() == NS_CATEGORY
) {
280 $cat = Category
::newFromRow( $row, $title );
281 $this->addSubcategoryObject( $cat, $row->cl_sortkey
, $row->page_len
);
282 } elseif ( $this->showGallery
&& $title->getNamespace() == NS_FILE
) {
283 $this->addImage( $title, $row->cl_sortkey
, $row->page_len
, $row->page_is_redirect
);
285 $this->addPage( $title, $row->cl_sortkey
, $row->page_len
, $row->page_is_redirect
);
293 # Non-$wgExperimentalCategorySort stuff
298 $conds +
array( $pageCondition ),
300 $opts +
array( 'LIMIT' => $this->limit +
1 ),
305 $this->nextPage
= null;
307 foreach ( $res as $row ) {
308 if ( ++
$count > $this->limit
) {
309 // We've reached the one extra which shows that there are
310 // additional pages to be had. Stop here...
311 $this->nextPage
= $row->cl_sortkey
;
315 $title = Title
::newFromRow( $row );
317 if ( $title->getNamespace() == NS_CATEGORY
) {
318 $cat = Category
::newFromRow( $row, $title );
319 $this->addSubcategoryObject( $cat, $row->cl_sortkey
, $row->page_len
);
320 } elseif ( $this->showGallery
&& $title->getNamespace() == NS_FILE
) {
321 $this->addImage( $title, $row->cl_sortkey
, $row->page_len
, $row->page_is_redirect
);
323 $this->addPage( $title, $row->cl_sortkey
, $row->page_len
, $row->page_is_redirect
);
328 function getCategoryTop() {
329 $r = $this->getCategoryBottom();
332 : "<br style=\"clear:both;\"/>\n" . $r;
335 function getSubcategorySection() {
336 # Don't show subcategories section if there are none.
338 $rescnt = count( $this->children
);
339 $dbcnt = $this->cat
->getSubcatCount();
340 $countmsg = $this->getCountMessage( $rescnt, $dbcnt, 'subcat' );
343 # Showing subcategories
344 $r .= "<div id=\"mw-subcategories\">\n";
345 $r .= '<h2>' . wfMsg( 'subcategories' ) . "</h2>\n";
347 $r .= $this->formatList( $this->children
, $this->children_start_char
);
353 function getPagesSection() {
354 $ti = htmlspecialchars( $this->title
->getText() );
355 # Don't show articles section if there are none.
358 # FIXME, here and in the other two sections: we don't need to bother
359 # with this rigamarole if the entire category contents fit on one page
360 # and have already been retrieved. We can just use $rescnt in that
361 # case and save a query and some logic.
362 $dbcnt = $this->cat
->getPageCount() - $this->cat
->getSubcatCount()
363 - $this->cat
->getFileCount();
364 $rescnt = count( $this->articles
);
365 $countmsg = $this->getCountMessage( $rescnt, $dbcnt, 'article' );
368 $r = "<div id=\"mw-pages\">\n";
369 $r .= '<h2>' . wfMsg( 'category_header', $ti ) . "</h2>\n";
371 $r .= $this->formatList( $this->articles
, $this->articles_start_char
);
377 function getImageSection() {
378 if ( $this->showGallery
&& ! $this->gallery
->isEmpty() ) {
379 $dbcnt = $this->cat
->getFileCount();
380 $rescnt = $this->gallery
->count();
381 $countmsg = $this->getCountMessage( $rescnt, $dbcnt, 'file' );
383 return "<div id=\"mw-category-media\">\n" .
384 '<h2>' . wfMsg( 'category-media-header', htmlspecialchars( $this->title
->getText() ) ) . "</h2>\n" .
385 $countmsg . $this->gallery
->toHTML() . "\n</div>";
391 function getCategoryBottom() {
392 if ( $this->until
!= '' ) {
393 return $this->pagingLinks( $this->title
, $this->nextPage
, $this->until
, $this->limit
);
394 } elseif ( $this->nextPage
!= '' ||
$this->from
!= '' ) {
395 return $this->pagingLinks( $this->title
, $this->from
, $this->nextPage
, $this->limit
);
402 * Format a list of articles chunked by letter, either as a
403 * bullet list or a columnar format, depending on the length.
405 * @param $articles Array
406 * @param $articles_start_char Array
411 function formatList( $articles, $articles_start_char, $cutoff = 6 ) {
412 if ( count ( $articles ) > $cutoff ) {
413 return $this->columnList( $articles, $articles_start_char );
414 } elseif ( count( $articles ) > 0 ) {
415 // for short lists of articles in categories.
416 return $this->shortList( $articles, $articles_start_char );
422 * Format a list of articles chunked by letter in a three-column
423 * list, ordered vertically.
425 * TODO: Take the headers into account when creating columns, so they're
426 * more visually equal.
428 * More distant TODO: Scrap this and use CSS columns, whenever IE finally
431 * @param $articles Array
432 * @param $articles_start_char Array
436 function columnList( $articles, $articles_start_char ) {
437 $columns = array_combine( $articles, $articles_start_char );
438 # Split into three columns
439 $columns = array_chunk( $columns, ceil( count( $columns ) / 3 ), true /* preserve keys */ );
441 $ret = '<table width="100%"><tr valign="top"><td>';
444 foreach ( $columns as $column ) {
445 $colContents = array();
447 # Kind of like array_flip() here, but we keep duplicates in an
448 # array instead of dropping them.
449 foreach ( $column as $article => $char ) {
450 if ( !isset( $colContents[$char] ) ) {
451 $colContents[$char] = array();
453 $colContents[$char][] = $article;
457 foreach ( $colContents as $char => $articles ) {
458 $ret .= '<h3>' . htmlspecialchars( $char );
459 if ( $first && $char === $prevchar ) {
460 # We're continuing a previous chunk at the top of a new
461 # column, so add " cont." after the letter.
462 $ret .= ' ' . wfMsgHtml( 'listingcontinuesabbrev' );
467 $ret .= implode( "</li>\n<li>", $articles );
468 $ret .= '</li></ul>';
474 $ret .= "</td>\n<td>";
477 $ret .= '</td></tr></table>';
482 * Format a list of articles chunked by letter in a bullet list.
483 * @param $articles Array
484 * @param $articles_start_char Array
488 function shortList( $articles, $articles_start_char ) {
489 $r = '<h3>' . htmlspecialchars( $articles_start_char[0] ) . "</h3>\n";
490 $r .= '<ul><li>' . $articles[0] . '</li>';
491 for ( $index = 1; $index < count( $articles ); $index++
)
493 if ( $articles_start_char[$index] != $articles_start_char[$index - 1] )
495 $r .= "</ul><h3>" . htmlspecialchars( $articles_start_char[$index] ) . "</h3>\n<ul>";
498 $r .= "<li>{$articles[$index]}</li>";
505 * @param $title Title object
506 * @param $first String
507 * @param $last String
509 * @param $query Array: additional query options to pass
513 function pagingLinks( $title, $first, $last, $limit, $query = array() ) {
515 $sk = $this->getSkin();
516 $limitText = $wgLang->formatNum( $limit );
518 $prevLink = wfMsgExt( 'prevn', array( 'escape', 'parsemag' ), $limitText );
520 if ( $first != '' ) {
522 $prevQuery['until'] = $first;
523 $prevLink = $sk->linkKnown(
531 $nextLink = wfMsgExt( 'nextn', array( 'escape', 'parsemag' ), $limitText );
535 $lastQuery['from'] = $last;
536 $nextLink = $sk->linkKnown(
544 return "($prevLink) ($nextLink)";
548 * What to do if the category table conflicts with the number of results
549 * returned? This function says what. It works the same whether the
550 * things being counted are articles, subcategories, or files.
552 * Note for grepping: uses the messages category-article-count,
553 * category-article-count-limited, category-subcat-count,
554 * category-subcat-count-limited, category-file-count,
555 * category-file-count-limited.
557 * @param $rescnt Int: The number of items returned by our database query.
558 * @param $dbcnt Int: The number of items according to the category table.
559 * @param $type String: 'subcat', 'article', or 'file'
560 * @return String: A message giving the number of items, to output to HTML.
562 private function getCountMessage( $rescnt, $dbcnt, $type ) {
564 # There are three cases:
565 # 1) The category table figure seems sane. It might be wrong, but
566 # we can't do anything about it if we don't recalculate it on ev-
568 # 2) The category table figure isn't sane, like it's smaller than the
569 # number of actual results, *but* the number of results is less
570 # than $this->limit and there's no offset. In this case we still
571 # know the right figure.
572 # 3) We have no idea.
573 $totalrescnt = count( $this->articles
) +
count( $this->children
) +
574 ( $this->showGallery ?
$this->gallery
->count() : 0 );
576 if ( $dbcnt == $rescnt ||
( ( $totalrescnt == $this->limit ||
$this->from
577 ||
$this->until
) && $dbcnt > $rescnt ) )
579 # Case 1: seems sane.
581 } elseif ( $totalrescnt < $this->limit
&& !$this->from
&& !$this->until
) {
582 # Case 2: not sane, but salvageable. Use the number of results.
583 # Since there are fewer than 200, we can also take this opportunity
584 # to refresh the incorrect category table entry -- which should be
585 # quick due to the small number of entries.
587 $this->cat
->refreshCounts();
589 # Case 3: hopeless. Don't give a total count at all.
590 return wfMsgExt( "category-$type-count-limited", 'parse',
591 $wgLang->formatNum( $rescnt ) );
594 "category-$type-count",
596 $wgLang->formatNum( $rescnt ),
597 $wgLang->formatNum( $totalcnt )