d955f1f052b56b6e5d6b26c6a42190529ef8e14c
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;
89 unset( $this->query
['title'] );
93 * Format the category data list.
95 * @return string HTML output
99 global $wgOut, $wgCategoryMagicGallery, $wgContLang;
100 wfProfileIn( __METHOD__
);
102 $this->showGallery
= $wgCategoryMagicGallery && !$wgOut->mNoGallery
;
104 $this->clearCategoryState();
105 $this->doCategoryQuery();
106 $this->finaliseCategoryState();
108 $r = $this->getSubcategorySection() .
109 $this->getPagesSection() .
110 $this->getImageSection();
113 // If there is no category content to display, only
114 // show the top part of the navigation links.
115 // FIXME: cannot be completely suppressed because it
116 // is unknown if 'until' or 'from' makes this
118 $r = $r . $this->getCategoryTop();
120 $r = $this->getCategoryTop() .
122 $this->getCategoryBottom();
125 // Give a proper message if category is empty
127 $r = wfMsgExt( 'category-empty', array( 'parse' ) );
130 wfProfileOut( __METHOD__
);
131 return $wgContLang->convert( $r );
134 function clearCategoryState() {
135 $this->articles
= array();
136 $this->articles_start_char
= array();
137 $this->children
= array();
138 $this->children_start_char
= array();
139 if ( $this->showGallery
) {
140 $this->gallery
= new ImageGallery();
141 $this->gallery
->setHideBadImages();
146 if ( !$this->skin
) {
148 $this->skin
= $wgUser->getSkin();
154 * Add a subcategory to the internal lists, using a Category object
156 function addSubcategoryObject( $cat, $sortkey, $pageLength ) {
157 $title = $cat->getTitle();
158 $this->addSubcategory( $title, $sortkey, $pageLength );
162 * Add a subcategory to the internal lists, using a title object
163 * @deprecated kept for compatibility, please use addSubcategoryObject instead
165 function addSubcategory( $title, $sortkey, $pageLength ) {
166 // Subcategory; strip the 'Category' namespace from the link text.
167 $this->children
[] = $this->getSkin()->link(
172 array( 'known', 'noclasses' )
175 $this->children_start_char
[] = $this->getSubcategorySortChar( $title, $sortkey );
179 * Get the character to be used for sorting subcategories.
180 * If there's a link from Category:A to Category:B, the sortkey of the resulting
181 * entry in the categorylinks table is Category:A, not A, which it SHOULD be.
182 * Workaround: If sortkey == "Category:".$title, than use $title for sorting,
183 * else use sortkey...
185 function getSubcategorySortChar( $title, $sortkey ) {
188 if ( $title->getPrefixedText() == $sortkey ) {
189 $word = $title->getDBkey();
194 $firstChar = $wgContLang->firstLetterForLists( $word );
196 return $wgContLang->convert( $firstChar );
200 * Add a page in the image namespace
202 function addImage( Title
$title, $sortkey, $pageLength, $isRedirect = false ) {
203 if ( $this->showGallery
) {
204 $flip = $this->flip
['file'];
206 $this->gallery
->insert( $title );
208 $this->gallery
->add( $title );
211 $this->addPage( $title, $sortkey, $pageLength, $isRedirect );
216 * Add a miscellaneous page
218 function addPage( $title, $sortkey, $pageLength, $isRedirect = false ) {
220 $this->articles
[] = $isRedirect
221 ?
'<span class="redirect-in-category">' .
222 $this->getSkin()->link(
227 array( 'known', 'noclasses' )
229 : $this->getSkin()->link( $title );
231 $this->articles_start_char
[] = $wgContLang->convert( $wgContLang->firstLetterForLists( $sortkey ) );
234 function finaliseCategoryState() {
235 if ( $this->flip
['subcat'] ) {
236 $this->children
= array_reverse( $this->children
);
237 $this->children_start_char
= array_reverse( $this->children_start_char
);
239 if ( $this->flip
['page'] ) {
240 $this->articles
= array_reverse( $this->articles
);
241 $this->articles_start_char
= array_reverse( $this->articles_start_char
);
245 function doCategoryQuery() {
248 $dbr = wfGetDB( DB_SLAVE
, 'category' );
250 $this->nextPage
= array(
255 $this->flip
= array( 'page' => false, 'subcat' => false, 'file' => false );
257 foreach ( array( 'page', 'subcat', 'file' ) as $type ) {
258 # Get the sortkeys for start/end, if applicable. Note that if
259 # the collation in the database differs from the one
260 # $wgContLang is using, pagination might go totally haywire.
261 $extraConds = array( 'cl_type' => $type );
262 if ( $this->from
[$type] !== null ) {
263 $extraConds[] = 'cl_sortkey >= '
264 . $dbr->addQuotes( $wgContLang->convertToSortkey( $this->from
[$type] ) );
265 } elseif ( $this->until
[$type] !== null ) {
266 $extraConds[] = 'cl_sortkey < '
267 . $dbr->addQuotes( $wgContLang->convertToSortkey( $this->until
[$type] ) );
268 $this->flip
[$type] = true;
272 array( 'page', 'categorylinks', 'category' ),
273 array( 'page_title', 'page_namespace', 'page_len',
274 'page_is_redirect', 'cl_sortkey', 'cat_id', 'cat_title',
275 'cat_subcats', 'cat_pages', 'cat_files', 'cl_sortkey_prefix' ),
276 array( 'cl_to' => $this->title
->getDBkey() ) +
$extraConds,
279 'USE INDEX' => array( 'categorylinks' => 'cl_sortkey' ),
280 'LIMIT' => $this->limit +
1,
281 'ORDER BY' => $this->flip
[$type] ?
'cl_sortkey DESC' : 'cl_sortkey',
284 'categorylinks' => array( 'INNER JOIN', 'cl_from = page_id' ),
285 'category' => array( 'LEFT JOIN', 'cat_title = page_title AND page_namespace = ' . NS_CATEGORY
)
290 foreach ( $res as $row ) {
291 $title = Title
::newFromRow( $row );
292 $rawSortkey = $title->getCategorySortkey( $row->cl_sortkey_prefix
);
294 if ( ++
$count > $this->limit
) {
295 # We've reached the one extra which shows that there
296 # are additional pages to be had. Stop here...
297 $this->nextPage
[$type] = $rawSortkey;
301 if ( $title->getNamespace() == NS_CATEGORY
) {
302 $cat = Category
::newFromRow( $row, $title );
303 $this->addSubcategoryObject( $cat, $rawSortkey, $row->page_len
);
304 } elseif ( $this->showGallery
&& $title->getNamespace() == NS_FILE
) {
305 $this->addImage( $title, $rawSortkey, $row->page_len
, $row->page_is_redirect
);
307 $this->addPage( $title, $rawSortkey, $row->page_len
, $row->page_is_redirect
);
313 function getCategoryTop() {
314 $r = $this->getCategoryBottom();
317 : "<br style=\"clear:both;\"/>\n" . $r;
320 function getSubcategorySection() {
321 # Don't show subcategories section if there are none.
323 $rescnt = count( $this->children
);
324 $dbcnt = $this->cat
->getSubcatCount();
325 $countmsg = $this->getCountMessage( $rescnt, $dbcnt, 'subcat' );
328 # Showing subcategories
329 $r .= "<div id=\"mw-subcategories\">\n";
330 $r .= '<h2>' . wfMsg( 'subcategories' ) . "</h2>\n";
332 $r .= $this->getSectionPagingLinks( 'subcat' );
333 $r .= $this->formatList( $this->children
, $this->children_start_char
);
334 $r .= $this->getSectionPagingLinks( 'subcat' );
340 function getPagesSection() {
341 $ti = htmlspecialchars( $this->title
->getText() );
342 # Don't show articles section if there are none.
345 # FIXME, here and in the other two sections: we don't need to bother
346 # with this rigamarole if the entire category contents fit on one page
347 # and have already been retrieved. We can just use $rescnt in that
348 # case and save a query and some logic.
349 $dbcnt = $this->cat
->getPageCount() - $this->cat
->getSubcatCount()
350 - $this->cat
->getFileCount();
351 $rescnt = count( $this->articles
);
352 $countmsg = $this->getCountMessage( $rescnt, $dbcnt, 'article' );
355 $r = "<div id=\"mw-pages\">\n";
356 $r .= '<h2>' . wfMsg( 'category_header', $ti ) . "</h2>\n";
358 $r .= $this->getSectionPagingLinks( 'page' );
359 $r .= $this->formatList( $this->articles
, $this->articles_start_char
);
360 $r .= $this->getSectionPagingLinks( 'page' );
366 function getImageSection() {
368 if ( $this->showGallery
&& ! $this->gallery
->isEmpty() ) {
369 $dbcnt = $this->cat
->getFileCount();
370 $rescnt = $this->gallery
->count();
371 $countmsg = $this->getCountMessage( $rescnt, $dbcnt, 'file' );
373 $r .= "<div id=\"mw-category-media\">\n";
374 $r .= '<h2>' . wfMsg( 'category-media-header', htmlspecialchars( $this->title
->getText() ) ) . "</h2>\n";
376 $r .= $this->getSectionPagingLinks( 'file' );
377 $r .= $this->gallery
->toHTML();
378 $r .= $this->getSectionPagingLinks( 'file' );
385 * Get the paging links for a section (subcats/pages/files), to go at the top and bottom
388 * @param $type String: 'page', 'subcat', or 'file'
389 * @return String: HTML output, possibly empty if there are no other pages
391 private function getSectionPagingLinks( $type ) {
392 if ( $this->until
[$type] !== null ) {
393 return $this->pagingLinks( $this->nextPage
[$type], $this->until
[$type], $type );
394 } elseif ( $this->nextPage
[$type] !== null ||
$this->from
[$type] !== null ) {
395 return $this->pagingLinks( $this->from
[$type], $this->nextPage
[$type], $type );
401 function getCategoryBottom() {
406 * Format a list of articles chunked by letter, either as a
407 * bullet list or a columnar format, depending on the length.
409 * @param $articles Array
410 * @param $articles_start_char Array
415 function formatList( $articles, $articles_start_char, $cutoff = 6 ) {
416 if ( count ( $articles ) > $cutoff ) {
417 return $this->columnList( $articles, $articles_start_char );
418 } elseif ( count( $articles ) > 0 ) {
419 // for short lists of articles in categories.
420 return $this->shortList( $articles, $articles_start_char );
426 * Format a list of articles chunked by letter in a three-column
427 * list, ordered vertically.
429 * TODO: Take the headers into account when creating columns, so they're
430 * more visually equal.
432 * More distant TODO: Scrap this and use CSS columns, whenever IE finally
435 * @param $articles Array
436 * @param $articles_start_char Array
440 function columnList( $articles, $articles_start_char ) {
441 $columns = array_combine( $articles, $articles_start_char );
442 # Split into three columns
443 $columns = array_chunk( $columns, ceil( count( $columns ) / 3 ), true /* preserve keys */ );
445 $ret = '<table width="100%"><tr valign="top"><td>';
448 foreach ( $columns as $column ) {
449 $colContents = array();
451 # Kind of like array_flip() here, but we keep duplicates in an
452 # array instead of dropping them.
453 foreach ( $column as $article => $char ) {
454 if ( !isset( $colContents[$char] ) ) {
455 $colContents[$char] = array();
457 $colContents[$char][] = $article;
461 foreach ( $colContents as $char => $articles ) {
462 $ret .= '<h3>' . htmlspecialchars( $char );
463 if ( $first && $char === $prevchar ) {
464 # We're continuing a previous chunk at the top of a new
465 # column, so add " cont." after the letter.
466 $ret .= ' ' . wfMsgHtml( 'listingcontinuesabbrev' );
471 $ret .= implode( "</li>\n<li>", $articles );
472 $ret .= '</li></ul>';
478 $ret .= "</td>\n<td>";
481 $ret .= '</td></tr></table>';
486 * Format a list of articles chunked by letter in a bullet list.
487 * @param $articles Array
488 * @param $articles_start_char Array
492 function shortList( $articles, $articles_start_char ) {
493 $r = '<h3>' . htmlspecialchars( $articles_start_char[0] ) . "</h3>\n";
494 $r .= '<ul><li>' . $articles[0] . '</li>';
495 for ( $index = 1; $index < count( $articles ); $index++
)
497 if ( $articles_start_char[$index] != $articles_start_char[$index - 1] )
499 $r .= "</ul><h3>" . htmlspecialchars( $articles_start_char[$index] ) . "</h3>\n<ul>";
502 $r .= "<li>{$articles[$index]}</li>";
509 * Create paging links, as a helper method to getSectionPagingLinks().
511 * @param $first String The 'until' parameter for the generated URL
512 * @param $last String The 'from' parameter for the genererated URL
513 * @param $type String A prefix for parameters, 'page' or 'subcat' or
515 * @return String HTML
517 private function pagingLinks( $first, $last, $type = '' ) {
519 $sk = $this->getSkin();
520 $limitText = $wgLang->formatNum( $this->limit
);
522 $prevLink = wfMsgExt( 'prevn', array( 'escape', 'parsemag' ), $limitText );
524 if ( $first != '' ) {
525 $prevQuery = $this->query
;
526 $prevQuery["{$type}until"] = $first;
527 unset( $prevQuery["{$type}from"] );
528 $prevLink = $sk->linkKnown(
536 $nextLink = wfMsgExt( 'nextn', array( 'escape', 'parsemag' ), $limitText );
539 $lastQuery = $this->query
;
540 $lastQuery["{$type}from"] = $last;
541 unset( $lastQuery["{$type}until"] );
542 $nextLink = $sk->linkKnown(
550 return "($prevLink) ($nextLink)";
554 * What to do if the category table conflicts with the number of results
555 * returned? This function says what. It works the same whether the
556 * things being counted are articles, subcategories, or files.
558 * Note for grepping: uses the messages category-article-count,
559 * category-article-count-limited, category-subcat-count,
560 * category-subcat-count-limited, category-file-count,
561 * category-file-count-limited.
563 * @param $rescnt Int: The number of items returned by our database query.
564 * @param $dbcnt Int: The number of items according to the category table.
565 * @param $type String: 'subcat', 'article', or 'file'
566 * @return String: A message giving the number of items, to output to HTML.
568 private function getCountMessage( $rescnt, $dbcnt, $type ) {
570 # There are three cases:
571 # 1) The category table figure seems sane. It might be wrong, but
572 # we can't do anything about it if we don't recalculate it on ev-
574 # 2) The category table figure isn't sane, like it's smaller than the
575 # number of actual results, *but* the number of results is less
576 # than $this->limit and there's no offset. In this case we still
577 # know the right figure.
578 # 3) We have no idea.
579 $totalrescnt = count( $this->articles
) +
count( $this->children
) +
580 ( $this->showGallery ?
$this->gallery
->count() : 0 );
582 if ( $dbcnt == $rescnt ||
( ( $totalrescnt == $this->limit ||
$this->from
583 ||
$this->until
) && $dbcnt > $rescnt ) )
585 # Case 1: seems sane.
587 } elseif ( $totalrescnt < $this->limit
&& !$this->from
&& !$this->until
) {
588 # Case 2: not sane, but salvageable. Use the number of results.
589 # Since there are fewer than 200, we can also take this opportunity
590 # to refresh the incorrect category table entry -- which should be
591 # quick due to the small number of entries.
593 $this->cat
->refreshCounts();
595 # Case 3: hopeless. Don't give a total count at all.
596 return wfMsgExt( "category-$type-count-limited", 'parse',
597 $wgLang->formatNum( $rescnt ) );
600 "category-$type-count",
602 $wgLang->formatNum( $rescnt ),
603 $wgLang->formatNum( $totalcnt )