3 * Special handling for category description pages.
4 * Modelled after ImagePage.php.
9 if ( !defined( 'MEDIAWIKI' ) )
13 * Special handling for category description pages, showing pages,
14 * subcategories and file that belong to the category
16 class CategoryPage
extends Article
{
17 # Subclasses can change this to override the viewer class.
18 protected $mCategoryViewerClass = 'CategoryViewer';
21 global $wgRequest, $wgUser;
23 $diff = $wgRequest->getVal( 'diff' );
24 $diffOnly = $wgRequest->getBool( 'diffonly', $wgUser->getOption( 'diffonly' ) );
26 if ( isset( $diff ) && $diffOnly ) {
27 return parent
::view();
30 if ( !wfRunHooks( 'CategoryPageView', array( &$this ) ) ) {
34 if ( NS_CATEGORY
== $this->mTitle
->getNamespace() ) {
35 $this->openShowCategory();
40 if ( NS_CATEGORY
== $this->mTitle
->getNamespace() ) {
41 $this->closeShowCategory();
46 * Don't return a 404 for categories in use.
47 * In use defined as: either the actual page exists
48 * or the category currently has members.
50 function hasViewableContent() {
51 if ( parent
::hasViewableContent() ) {
54 $cat = Category
::newFromTitle( $this->mTitle
);
55 // If any of these are not 0, then has members
56 if ( $cat->getPageCount()
57 ||
$cat->getSubcatCount()
58 ||
$cat->getFileCount()
66 function openShowCategory() {
70 function closeShowCategory() {
71 global $wgOut, $wgRequest;
73 // Use these as defaults for back compat --catrope
74 $oldFrom = $wgRequest->getVal( 'from' );
75 $oldUntil = $wgRequest->getVal( 'until' );
77 $from = $until = array();
78 foreach ( array( 'page', 'subcat', 'file' ) as $type ) {
79 $from[$type] = $wgRequest->getVal( "{$type}from", $oldFrom );
80 $until[$type] = $wgRequest->getVal( "{$type}until", $oldUntil );
83 $viewer = new $this->mCategoryViewerClass( $this->mTitle
, $from, $until, $wgRequest->getValues() );
84 $wgOut->addHTML( $viewer->getHTML() );
88 class CategoryViewer
{
89 var $limit, $from, $until,
90 $articles, $articles_start_char,
91 $children, $children_start_char,
92 $showGallery, $imgsNoGalley,
93 $imgsNoGallery_start_char,
94 $skin, $imgsNoGallery;
122 * Category object for this page
128 * The original query array, to be used in generating paging links.
133 function __construct( $title, $from = '', $until = '', $query = array() ) {
134 global $wgCategoryPagingLimit;
135 $this->title
= $title;
137 $this->until
= $until;
138 $this->limit
= $wgCategoryPagingLimit;
139 $this->cat
= Category
::newFromTitle( $title );
140 $this->query
= $query;
141 $this->collation
= Collation
::singleton();
142 unset( $this->query
['title'] );
146 * Format the category data list.
148 * @return string HTML output
150 public function getHTML() {
151 global $wgOut, $wgCategoryMagicGallery, $wgContLang;
152 wfProfileIn( __METHOD__
);
154 $this->showGallery
= $wgCategoryMagicGallery && !$wgOut->mNoGallery
;
156 $this->clearCategoryState();
157 $this->doCategoryQuery();
158 $this->finaliseCategoryState();
160 $r = $this->getSubcategorySection() .
161 $this->getPagesSection() .
162 $this->getImageSection();
165 // If there is no category content to display, only
166 // show the top part of the navigation links.
167 // @todo FIXME: Cannot be completely suppressed because it
168 // is unknown if 'until' or 'from' makes this
170 $r = $r . $this->getCategoryTop();
172 $r = $this->getCategoryTop() .
174 $this->getCategoryBottom();
177 // Give a proper message if category is empty
179 $r = wfMsgExt( 'category-empty', array( 'parse' ) );
182 global $wgBetterDirectionality, $wgLang;
183 if( $wgBetterDirectionality ) {
184 $langAttribs = array( 'lang' => $wgLang->getCode(), 'dir' => $wgLang->getDir() );
185 $r = Html
::rawElement( 'div', $langAttribs, $r );
188 wfProfileOut( __METHOD__
);
189 return $wgContLang->convert( $r );
192 function clearCategoryState() {
193 $this->articles
= array();
194 $this->articles_start_char
= array();
195 $this->children
= array();
196 $this->children_start_char
= array();
197 if ( $this->showGallery
) {
198 $this->gallery
= new ImageGallery();
199 $this->gallery
->setHideBadImages();
201 $this->imgsNoGallery
= array();
202 $this->imgsNoGallery_start_char
= array();
210 if ( !$this->skin
) {
212 $this->skin
= $wgUser->getSkin();
218 * Add a subcategory to the internal lists, using a Category object
220 function addSubcategoryObject( Category
$cat, $sortkey, $pageLength ) {
221 // Subcategory; strip the 'Category' namespace from the link text.
222 $title = $cat->getTitle();
224 $link = $this->getSkin()->link( $title, $title->getText() );
225 if ( $title->isRedirect() ) {
226 // This didn't used to add redirect-in-category, but might
227 // as well be consistent with the rest of the sections
228 // on a category page.
229 $link = '<span class="redirect-in-category">' . $link . '</span>';
231 $this->children
[] = $link;
233 $this->children_start_char
[] =
234 $this->getSubcategorySortChar( $cat->getTitle(), $sortkey );
238 * Add a subcategory to the internal lists, using a title object
239 * @deprecated since 1.17 kept for compatibility, please use addSubcategoryObject instead
241 function addSubcategory( Title
$title, $sortkey, $pageLength ) {
242 $this->addSubcategoryObject( Category
::newFromTitle( $title ), $sortkey, $pageLength );
246 * Get the character to be used for sorting subcategories.
247 * If there's a link from Category:A to Category:B, the sortkey of the resulting
248 * entry in the categorylinks table is Category:A, not A, which it SHOULD be.
249 * Workaround: If sortkey == "Category:".$title, than use $title for sorting,
250 * else use sortkey...
252 * @param Title $title
253 * @param string $sortkey The human-readable sortkey (before transforming to icu or whatever).
255 function getSubcategorySortChar( $title, $sortkey ) {
258 if ( $title->getPrefixedText() == $sortkey ) {
259 $word = $title->getDBkey();
264 $firstChar = $this->collation
->getFirstLetter( $word );
266 return $wgContLang->convert( $firstChar );
270 * Add a page in the image namespace
272 function addImage( Title
$title, $sortkey, $pageLength, $isRedirect = false ) {
274 if ( $this->showGallery
) {
275 $flip = $this->flip
['file'];
277 $this->gallery
->insert( $title );
279 $this->gallery
->add( $title );
282 $link = $this->getSkin()->link( $title );
284 // This seems kind of pointless given 'mw-redirect' class,
285 // but keeping for back-compatibility with user css.
286 $link = '<span class="redirect-in-category">' . $link . '</span>';
288 $this->imgsNoGallery
[] = $link;
290 $this->imgsNoGallery_start_char
[] = $wgContLang->convert(
291 $this->collation
->getFirstLetter( $sortkey ) );
296 * Add a miscellaneous page
298 function addPage( $title, $sortkey, $pageLength, $isRedirect = false ) {
301 $link = $this->getSkin()->link( $title );
303 // This seems kind of pointless given 'mw-redirect' class,
304 // but keeping for back-compatiability with user css.
305 $link = '<span class="redirect-in-category">' . $link . '</span>';
307 $this->articles
[] = $link;
309 $this->articles_start_char
[] = $wgContLang->convert(
310 $this->collation
->getFirstLetter( $sortkey ) );
313 function finaliseCategoryState() {
314 if ( $this->flip
['subcat'] ) {
315 $this->children
= array_reverse( $this->children
);
316 $this->children_start_char
= array_reverse( $this->children_start_char
);
318 if ( $this->flip
['page'] ) {
319 $this->articles
= array_reverse( $this->articles
);
320 $this->articles_start_char
= array_reverse( $this->articles_start_char
);
322 if ( !$this->showGallery
&& $this->flip
['file'] ) {
323 $this->imgsNoGallery
= array_reverse( $this->imgsNoGallery
);
324 $this->imgsNoGallery_start_char
= array_reverse( $this->imgsNoGallery_start_char
);
328 function doCategoryQuery() {
329 $dbr = wfGetDB( DB_SLAVE
, 'category' );
331 $this->nextPage
= array(
336 $this->flip
= array( 'page' => false, 'subcat' => false, 'file' => false );
338 foreach ( array( 'page', 'subcat', 'file' ) as $type ) {
339 # Get the sortkeys for start/end, if applicable. Note that if
340 # the collation in the database differs from the one
341 # set in $wgCategoryCollation, pagination might go totally haywire.
342 $extraConds = array( 'cl_type' => $type );
343 if ( $this->from
[$type] !== null ) {
344 $extraConds[] = 'cl_sortkey >= '
345 . $dbr->addQuotes( $this->collation
->getSortKey( $this->from
[$type] ) );
346 } elseif ( $this->until
[$type] !== null ) {
347 $extraConds[] = 'cl_sortkey < '
348 . $dbr->addQuotes( $this->collation
->getSortKey( $this->until
[$type] ) );
349 $this->flip
[$type] = true;
353 array( 'page', 'categorylinks', 'category' ),
354 array( 'page_id', 'page_title', 'page_namespace', 'page_len',
355 'page_is_redirect', 'cl_sortkey', 'cat_id', 'cat_title',
356 'cat_subcats', 'cat_pages', 'cat_files',
357 'cl_sortkey_prefix', 'cl_collation' ),
358 array_merge( array( 'cl_to' => $this->title
->getDBkey() ), $extraConds ),
361 'USE INDEX' => array( 'categorylinks' => 'cl_sortkey' ),
362 'LIMIT' => $this->limit +
1,
363 'ORDER BY' => $this->flip
[$type] ?
'cl_sortkey DESC' : 'cl_sortkey',
366 'categorylinks' => array( 'INNER JOIN', 'cl_from = page_id' ),
367 'category' => array( 'LEFT JOIN', 'cat_title = page_title AND page_namespace = ' . NS_CATEGORY
)
372 foreach ( $res as $row ) {
373 $title = Title
::newFromRow( $row );
374 if ( $row->cl_collation
=== '' ) {
375 // Hack to make sure that while updating from 1.16 schema
376 // and db is inconsistent, that the sky doesn't fall.
377 // See r83544. Could perhaps be removed in a couple decades...
378 $humanSortkey = $row->cl_sortkey
;
380 $humanSortkey = $title->getCategorySortkey( $row->cl_sortkey_prefix
);
383 if ( ++
$count > $this->limit
) {
384 # We've reached the one extra which shows that there
385 # are additional pages to be had. Stop here...
386 $this->nextPage
[$type] = $humanSortkey;
390 if ( $title->getNamespace() == NS_CATEGORY
) {
391 $cat = Category
::newFromRow( $row, $title );
392 $this->addSubcategoryObject( $cat, $humanSortkey, $row->page_len
);
393 } elseif ( $title->getNamespace() == NS_FILE
) {
394 $this->addImage( $title, $humanSortkey, $row->page_len
, $row->page_is_redirect
);
396 $this->addPage( $title, $humanSortkey, $row->page_len
, $row->page_is_redirect
);
402 function getCategoryTop() {
403 $r = $this->getCategoryBottom();
406 : "<br style=\"clear:both;\"/>\n" . $r;
409 function getSubcategorySection() {
410 # Don't show subcategories section if there are none.
412 $rescnt = count( $this->children
);
413 $dbcnt = $this->cat
->getSubcatCount();
414 $countmsg = $this->getCountMessage( $rescnt, $dbcnt, 'subcat' );
417 # Showing subcategories
418 $r .= "<div id=\"mw-subcategories\">\n";
419 $r .= '<h2>' . wfMsg( 'subcategories' ) . "</h2>\n";
421 $r .= $this->getSectionPagingLinks( 'subcat' );
422 $r .= $this->formatList( $this->children
, $this->children_start_char
);
423 $r .= $this->getSectionPagingLinks( 'subcat' );
429 function getPagesSection() {
430 $ti = htmlspecialchars( $this->title
->getText() );
431 # Don't show articles section if there are none.
434 # @todo FIXME: Here and in the other two sections: we don't need to bother
435 # with this rigamarole if the entire category contents fit on one page
436 # and have already been retrieved. We can just use $rescnt in that
437 # case and save a query and some logic.
438 $dbcnt = $this->cat
->getPageCount() - $this->cat
->getSubcatCount()
439 - $this->cat
->getFileCount();
440 $rescnt = count( $this->articles
);
441 $countmsg = $this->getCountMessage( $rescnt, $dbcnt, 'article' );
444 $r = "<div id=\"mw-pages\">\n";
445 $r .= '<h2>' . wfMsg( 'category_header', $ti ) . "</h2>\n";
447 $r .= $this->getSectionPagingLinks( 'page' );
448 $r .= $this->formatList( $this->articles
, $this->articles_start_char
);
449 $r .= $this->getSectionPagingLinks( 'page' );
455 function getImageSection() {
457 $rescnt = $this->showGallery ?
$this->gallery
->count() : count( $this->imgsNoGallery
);
459 $dbcnt = $this->cat
->getFileCount();
460 $countmsg = $this->getCountMessage( $rescnt, $dbcnt, 'file' );
462 $r .= "<div id=\"mw-category-media\">\n";
463 $r .= '<h2>' . wfMsg( 'category-media-header', htmlspecialchars( $this->title
->getText() ) ) . "</h2>\n";
465 $r .= $this->getSectionPagingLinks( 'file' );
466 if ( $this->showGallery
) {
467 $r .= $this->gallery
->toHTML();
469 $r .= $this->formatList( $this->imgsNoGallery
, $this->imgsNoGallery_start_char
);
471 $r .= $this->getSectionPagingLinks( 'file' );
478 * Get the paging links for a section (subcats/pages/files), to go at the top and bottom
481 * @param $type String: 'page', 'subcat', or 'file'
482 * @return String: HTML output, possibly empty if there are no other pages
484 private function getSectionPagingLinks( $type ) {
485 if ( $this->until
[$type] !== null ) {
486 return $this->pagingLinks( $this->nextPage
[$type], $this->until
[$type], $type );
487 } elseif ( $this->nextPage
[$type] !== null ||
$this->from
[$type] !== null ) {
488 return $this->pagingLinks( $this->from
[$type], $this->nextPage
[$type], $type );
494 function getCategoryBottom() {
499 * Format a list of articles chunked by letter, either as a
500 * bullet list or a columnar format, depending on the length.
502 * @param $articles Array
503 * @param $articles_start_char Array
508 function formatList( $articles, $articles_start_char, $cutoff = 6 ) {
510 if ( count ( $articles ) > $cutoff ) {
511 $list = self
::columnList( $articles, $articles_start_char );
512 } elseif ( count( $articles ) > 0 ) {
513 // for short lists of articles in categories.
514 $list = self
::shortList( $articles, $articles_start_char );
516 global $wgBetterDirectionality, $wgTitle;
517 if( $wgBetterDirectionality ) {
518 $pageLang = $wgTitle->getPageLanguage();
519 $attribs = array( 'lang' => $pageLang->getCode(), 'dir' => $pageLang->getDir() );
520 $list = Html
::rawElement( 'div', $attribs, $list );
527 * Format a list of articles chunked by letter in a three-column
528 * list, ordered vertically.
530 * TODO: Take the headers into account when creating columns, so they're
531 * more visually equal.
533 * More distant TODO: Scrap this and use CSS columns, whenever IE finally
536 * @param $articles Array
537 * @param $articles_start_char Array
541 static function columnList( $articles, $articles_start_char ) {
542 $columns = array_combine( $articles, $articles_start_char );
543 # Split into three columns
544 $columns = array_chunk( $columns, ceil( count( $columns ) / 3 ), true /* preserve keys */ );
546 $ret = '<table width="100%"><tr valign="top"><td>';
549 foreach ( $columns as $column ) {
550 $colContents = array();
552 # Kind of like array_flip() here, but we keep duplicates in an
553 # array instead of dropping them.
554 foreach ( $column as $article => $char ) {
555 if ( !isset( $colContents[$char] ) ) {
556 $colContents[$char] = array();
558 $colContents[$char][] = $article;
562 foreach ( $colContents as $char => $articles ) {
563 $ret .= '<h3>' . htmlspecialchars( $char );
564 if ( $first && $char === $prevchar ) {
565 # We're continuing a previous chunk at the top of a new
566 # column, so add " cont." after the letter.
567 $ret .= ' ' . wfMsgHtml( 'listingcontinuesabbrev' );
572 $ret .= implode( "</li>\n<li>", $articles );
573 $ret .= '</li></ul>';
579 $ret .= "</td>\n<td>";
582 $ret .= '</td></tr></table>';
587 * Format a list of articles chunked by letter in a bullet list.
588 * @param $articles Array
589 * @param $articles_start_char Array
593 static function shortList( $articles, $articles_start_char ) {
594 $r = '<h3>' . htmlspecialchars( $articles_start_char[0] ) . "</h3>\n";
595 $r .= '<ul><li>' . $articles[0] . '</li>';
596 for ( $index = 1; $index < count( $articles ); $index++
) {
597 if ( $articles_start_char[$index] != $articles_start_char[$index - 1] ) {
598 $r .= "</ul><h3>" . htmlspecialchars( $articles_start_char[$index] ) . "</h3>\n<ul>";
601 $r .= "<li>{$articles[$index]}</li>";
608 * Create paging links, as a helper method to getSectionPagingLinks().
610 * @param $first String The 'until' parameter for the generated URL
611 * @param $last String The 'from' parameter for the genererated URL
612 * @param $type String A prefix for parameters, 'page' or 'subcat' or
614 * @return String HTML
616 private function pagingLinks( $first, $last, $type = '' ) {
618 $sk = $this->getSkin();
619 $limitText = $wgLang->formatNum( $this->limit
);
621 $prevLink = wfMsgExt( 'prevn', array( 'escape', 'parsemag' ), $limitText );
623 if ( $first != '' ) {
624 $prevQuery = $this->query
;
625 $prevQuery["{$type}until"] = $first;
626 unset( $prevQuery["{$type}from"] );
627 $prevLink = $sk->linkKnown(
635 $nextLink = wfMsgExt( 'nextn', array( 'escape', 'parsemag' ), $limitText );
638 $lastQuery = $this->query
;
639 $lastQuery["{$type}from"] = $last;
640 unset( $lastQuery["{$type}until"] );
641 $nextLink = $sk->linkKnown(
649 return "($prevLink) ($nextLink)";
653 * What to do if the category table conflicts with the number of results
654 * returned? This function says what. Each type is considered independently
655 * of the other types.
657 * Note for grepping: uses the messages category-article-count,
658 * category-article-count-limited, category-subcat-count,
659 * category-subcat-count-limited, category-file-count,
660 * category-file-count-limited.
662 * @param $rescnt Int: The number of items returned by our database query.
663 * @param $dbcnt Int: The number of items according to the category table.
664 * @param $type String: 'subcat', 'article', or 'file'
665 * @return String: A message giving the number of items, to output to HTML.
667 private function getCountMessage( $rescnt, $dbcnt, $type ) {
669 # There are three cases:
670 # 1) The category table figure seems sane. It might be wrong, but
671 # we can't do anything about it if we don't recalculate it on ev-
673 # 2) The category table figure isn't sane, like it's smaller than the
674 # number of actual results, *but* the number of results is less
675 # than $this->limit and there's no offset. In this case we still
676 # know the right figure.
677 # 3) We have no idea.
679 # Check if there's a "from" or "until" for anything
681 // This is a little ugly, but we seem to use different names
682 // for the paging types then for the messages.
683 if ( $type === 'article' ) {
684 $pagingType = 'page';
689 $fromOrUntil = false;
690 if ( $this->from
[$pagingType] !== null ||
$this->until
[$pagingType] !== null ) {
694 if ( $dbcnt == $rescnt ||
( ( $rescnt == $this->limit ||
$fromOrUntil )
695 && $dbcnt > $rescnt ) ) {
696 # Case 1: seems sane.
698 } elseif ( $rescnt < $this->limit
&& !$fromOrUntil ) {
699 # Case 2: not sane, but salvageable. Use the number of results.
700 # Since there are fewer than 200, we can also take this opportunity
701 # to refresh the incorrect category table entry -- which should be
702 # quick due to the small number of entries.
704 $this->cat
->refreshCounts();
706 # Case 3: hopeless. Don't give a total count at all.
707 return wfMsgExt( "category-$type-count-limited", 'parse',
708 $wgLang->formatNum( $rescnt ) );
711 "category-$type-count",
713 $wgLang->formatNum( $rescnt ),
714 $wgLang->formatNum( $totalcnt )