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 ) {
175 global $wgContLang, $wgExperimentalCategorySort;
177 if ( $title->getPrefixedText() == $sortkey ) {
178 $word = $title->getDBkey();
183 if ( $wgExperimentalCategorySort ) {
184 $firstChar = $wgContLang->firstLetterForLists( $word );
186 $firstChar = $wgContLang->firstChar( $word );
189 return $wgContLang->convert( $firstChar );
193 * Add a page in the image namespace
195 function addImage( Title
$title, $sortkey, $pageLength, $isRedirect = false ) {
196 if ( $this->showGallery
) {
198 $this->gallery
->insert( $title );
200 $this->gallery
->add( $title );
203 $this->addPage( $title, $sortkey, $pageLength, $isRedirect );
208 * Add a miscellaneous page
210 function addPage( $title, $sortkey, $pageLength, $isRedirect = false ) {
211 global $wgContLang, $wgExperimentalCategorySort;
212 $this->articles
[] = $isRedirect
213 ?
'<span class="redirect-in-category">' .
214 $this->getSkin()->link(
219 array( 'known', 'noclasses' )
221 : $this->getSkin()->makeSizeLinkObj( $pageLength, $title );
223 if ( $wgExperimentalCategorySort ) {
224 $this->articles_start_char
[] = $wgContLang->convert( $wgContLang->firstLetterForLists( $sortkey ) );
226 $this->articles_start_char
[] = $wgContLang->convert( $wgContLang->firstChar( $sortkey ) );
230 function finaliseCategoryState() {
232 $this->children
= array_reverse( $this->children
);
233 $this->children_start_char
= array_reverse( $this->children_start_char
);
234 $this->articles
= array_reverse( $this->articles
);
235 $this->articles_start_char
= array_reverse( $this->articles_start_char
);
239 function doCategoryQuery() {
240 global $wgExperimentalCategorySort;
242 $dbr = wfGetDB( DB_SLAVE
, 'category' );
243 if ( $this->from
!= '' ) {
244 $pageCondition = 'cl_sortkey >= ' . $dbr->addQuotes( $this->from
);
246 } elseif ( $this->until
!= '' ) {
247 $pageCondition = 'cl_sortkey < ' . $dbr->addQuotes( $this->until
);
250 $pageCondition = '1 = 1';
254 $tables = array( 'page', 'categorylinks', 'category' );
255 $fields = array( 'page_title', 'page_namespace', 'page_len',
256 'page_is_redirect', 'cl_sortkey', 'cat_id', 'cat_title',
257 'cat_subcats', 'cat_pages', 'cat_files' );
258 $conds = array( 'cl_to' => $this->title
->getDBkey() );
259 $opts = array( 'ORDER BY' => $this->flip ?
'cl_sortkey DESC' :
260 'cl_sortkey', 'USE INDEX' => array( 'categorylinks' => 'cl_sortkey' ) );
261 $joins = array( 'categorylinks' => array( 'INNER JOIN', 'cl_from = page_id' ),
262 'category' => array( 'LEFT JOIN', 'cat_title = page_title AND page_namespace = ' . NS_CATEGORY
) );
264 if ( $wgExperimentalCategorySort ) {
265 # Copy-pasted from below, but that's okay, because the stuff below
266 # will be deleted when this becomes the default.
268 $this->nextPage
= null;
270 foreach ( array( 'page', 'subcat', 'file' ) as $type ) {
273 array_merge( $fields, array( 'cl_raw_sortkey' ) ),
274 $conds +
array( 'cl_type' => $type ) +
( $type == 'page' ?
array( $pageCondition ) : array() ),
276 $opts +
( $type == 'page' ?
array( 'LIMIT' => $this->limit +
1 ) : array() ),
280 foreach ( $res as $row ) {
281 if ( $type == 'page' && ++
$count > $this->limit
) {
282 # We've reached the one extra which shows that there
283 # are additional pages to be had. Stop here...
284 $this->nextPage
= $row->cl_sortkey
;
288 $title = Title
::newFromRow( $row );
290 if ( $title->getNamespace() == NS_CATEGORY
) {
291 $cat = Category
::newFromRow( $row, $title );
292 $this->addSubcategoryObject( $cat, $row->cl_raw_sortkey
, $row->page_len
);
293 } elseif ( $this->showGallery
&& $title->getNamespace() == NS_FILE
) {
294 $this->addImage( $title, $row->cl_raw_sortkey
, $row->page_len
, $row->page_is_redirect
);
296 $this->addPage( $title, $row->cl_raw_sortkey
, $row->page_len
, $row->page_is_redirect
);
304 # Non-$wgExperimentalCategorySort stuff
309 $conds +
array( $pageCondition ),
311 $opts +
array( 'LIMIT' => $this->limit +
1 ),
316 $this->nextPage
= null;
318 foreach ( $res as $row ) {
319 if ( ++
$count > $this->limit
) {
320 // We've reached the one extra which shows that there are
321 // additional pages to be had. Stop here...
322 $this->nextPage
= $row->cl_sortkey
;
326 $title = Title
::newFromRow( $row );
328 if ( $title->getNamespace() == NS_CATEGORY
) {
329 $cat = Category
::newFromRow( $row, $title );
330 $this->addSubcategoryObject( $cat, $row->cl_sortkey
, $row->page_len
);
331 } elseif ( $this->showGallery
&& $title->getNamespace() == NS_FILE
) {
332 $this->addImage( $title, $row->cl_sortkey
, $row->page_len
, $row->page_is_redirect
);
334 $this->addPage( $title, $row->cl_sortkey
, $row->page_len
, $row->page_is_redirect
);
339 function getCategoryTop() {
340 $r = $this->getCategoryBottom();
343 : "<br style=\"clear:both;\"/>\n" . $r;
346 function getSubcategorySection() {
347 # Don't show subcategories section if there are none.
349 $rescnt = count( $this->children
);
350 $dbcnt = $this->cat
->getSubcatCount();
351 $countmsg = $this->getCountMessage( $rescnt, $dbcnt, 'subcat' );
354 # Showing subcategories
355 $r .= "<div id=\"mw-subcategories\">\n";
356 $r .= '<h2>' . wfMsg( 'subcategories' ) . "</h2>\n";
358 $r .= $this->formatList( $this->children
, $this->children_start_char
);
364 function getPagesSection() {
365 $ti = htmlspecialchars( $this->title
->getText() );
366 # Don't show articles section if there are none.
369 # FIXME, here and in the other two sections: we don't need to bother
370 # with this rigamarole if the entire category contents fit on one page
371 # and have already been retrieved. We can just use $rescnt in that
372 # case and save a query and some logic.
373 $dbcnt = $this->cat
->getPageCount() - $this->cat
->getSubcatCount()
374 - $this->cat
->getFileCount();
375 $rescnt = count( $this->articles
);
376 $countmsg = $this->getCountMessage( $rescnt, $dbcnt, 'article' );
379 $r = "<div id=\"mw-pages\">\n";
380 $r .= '<h2>' . wfMsg( 'category_header', $ti ) . "</h2>\n";
382 $r .= $this->formatList( $this->articles
, $this->articles_start_char
);
388 function getImageSection() {
389 if ( $this->showGallery
&& ! $this->gallery
->isEmpty() ) {
390 $dbcnt = $this->cat
->getFileCount();
391 $rescnt = $this->gallery
->count();
392 $countmsg = $this->getCountMessage( $rescnt, $dbcnt, 'file' );
394 return "<div id=\"mw-category-media\">\n" .
395 '<h2>' . wfMsg( 'category-media-header', htmlspecialchars( $this->title
->getText() ) ) . "</h2>\n" .
396 $countmsg . $this->gallery
->toHTML() . "\n</div>";
402 function getCategoryBottom() {
403 if ( $this->until
!= '' ) {
404 return $this->pagingLinks( $this->title
, $this->nextPage
, $this->until
, $this->limit
);
405 } elseif ( $this->nextPage
!= '' ||
$this->from
!= '' ) {
406 return $this->pagingLinks( $this->title
, $this->from
, $this->nextPage
, $this->limit
);
413 * Format a list of articles chunked by letter, either as a
414 * bullet list or a columnar format, depending on the length.
416 * @param $articles Array
417 * @param $articles_start_char Array
422 function formatList( $articles, $articles_start_char, $cutoff = 6 ) {
423 if ( count ( $articles ) > $cutoff ) {
424 return $this->columnList( $articles, $articles_start_char );
425 } elseif ( count( $articles ) > 0 ) {
426 // for short lists of articles in categories.
427 return $this->shortList( $articles, $articles_start_char );
433 * Format a list of articles chunked by letter in a three-column
434 * list, ordered vertically.
436 * TODO: Take the headers into account when creating columns, so they're
437 * more visually equal.
439 * More distant TODO: Scrap this and use CSS columns, whenever IE finally
442 * @param $articles Array
443 * @param $articles_start_char Array
447 function columnList( $articles, $articles_start_char ) {
448 $columns = array_combine( $articles, $articles_start_char );
449 # Split into three columns
450 $columns = array_chunk( $columns, ceil( count( $columns ) / 3 ), true /* preserve keys */ );
452 $ret = '<table width="100%"><tr valign="top"><td>';
455 foreach ( $columns as $column ) {
456 $colContents = array();
458 # Kind of like array_flip() here, but we keep duplicates in an
459 # array instead of dropping them.
460 foreach ( $column as $article => $char ) {
461 if ( !isset( $colContents[$char] ) ) {
462 $colContents[$char] = array();
464 $colContents[$char][] = $article;
468 foreach ( $colContents as $char => $articles ) {
469 $ret .= '<h3>' . htmlspecialchars( $char );
470 if ( $first && $char === $prevchar ) {
471 # We're continuing a previous chunk at the top of a new
472 # column, so add " cont." after the letter.
473 $ret .= ' ' . wfMsgHtml( 'listingcontinuesabbrev' );
478 $ret .= implode( "</li>\n<li>", $articles );
479 $ret .= '</li></ul>';
485 $ret .= "</td>\n<td>";
488 $ret .= '</td></tr></table>';
493 * Format a list of articles chunked by letter in a bullet list.
494 * @param $articles Array
495 * @param $articles_start_char Array
499 function shortList( $articles, $articles_start_char ) {
500 $r = '<h3>' . htmlspecialchars( $articles_start_char[0] ) . "</h3>\n";
501 $r .= '<ul><li>' . $articles[0] . '</li>';
502 for ( $index = 1; $index < count( $articles ); $index++
)
504 if ( $articles_start_char[$index] != $articles_start_char[$index - 1] )
506 $r .= "</ul><h3>" . htmlspecialchars( $articles_start_char[$index] ) . "</h3>\n<ul>";
509 $r .= "<li>{$articles[$index]}</li>";
516 * @param $title Title object
517 * @param $first String
518 * @param $last String
520 * @param $query Array: additional query options to pass
524 function pagingLinks( $title, $first, $last, $limit, $query = array() ) {
526 $sk = $this->getSkin();
527 $limitText = $wgLang->formatNum( $limit );
529 $prevLink = wfMsgExt( 'prevn', array( 'escape', 'parsemag' ), $limitText );
531 if ( $first != '' ) {
533 $prevQuery['until'] = $first;
534 $prevLink = $sk->linkKnown(
542 $nextLink = wfMsgExt( 'nextn', array( 'escape', 'parsemag' ), $limitText );
546 $lastQuery['from'] = $last;
547 $nextLink = $sk->linkKnown(
555 return "($prevLink) ($nextLink)";
559 * What to do if the category table conflicts with the number of results
560 * returned? This function says what. It works the same whether the
561 * things being counted are articles, subcategories, or files.
563 * Note for grepping: uses the messages category-article-count,
564 * category-article-count-limited, category-subcat-count,
565 * category-subcat-count-limited, category-file-count,
566 * category-file-count-limited.
568 * @param $rescnt Int: The number of items returned by our database query.
569 * @param $dbcnt Int: The number of items according to the category table.
570 * @param $type String: 'subcat', 'article', or 'file'
571 * @return String: A message giving the number of items, to output to HTML.
573 private function getCountMessage( $rescnt, $dbcnt, $type ) {
575 # There are three cases:
576 # 1) The category table figure seems sane. It might be wrong, but
577 # we can't do anything about it if we don't recalculate it on ev-
579 # 2) The category table figure isn't sane, like it's smaller than the
580 # number of actual results, *but* the number of results is less
581 # than $this->limit and there's no offset. In this case we still
582 # know the right figure.
583 # 3) We have no idea.
584 $totalrescnt = count( $this->articles
) +
count( $this->children
) +
585 ( $this->showGallery ?
$this->gallery
->count() : 0 );
587 if ( $dbcnt == $rescnt ||
( ( $totalrescnt == $this->limit ||
$this->from
588 ||
$this->until
) && $dbcnt > $rescnt ) )
590 # Case 1: seems sane.
592 } elseif ( $totalrescnt < $this->limit
&& !$this->from
&& !$this->until
) {
593 # Case 2: not sane, but salvageable. Use the number of results.
594 # Since there are fewer than 200, we can also take this opportunity
595 # to refresh the incorrect category table entry -- which should be
596 # quick due to the small number of entries.
598 $this->cat
->refreshCounts();
600 # Case 3: hopeless. Don't give a total count at all.
601 return wfMsgExt( "category-$type-count-limited", 'parse',
602 $wgLang->formatNum( $rescnt ) );
605 "category-$type-count",
607 $wgLang->formatNum( $rescnt ),
608 $wgLang->formatNum( $totalcnt )