c9f4564aae795ee8fc5bb67cb1fc50678a3418bd
3 * Class for viewing MediaWiki 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';
20 protected function newPage( Title
$title ) {
21 // Overload mPage with a category-specific page
22 return new WikiCategoryPage( $title );
26 * Constructor from a page id
27 * @param $id Int article ID to load
29 public static function newFromID( $id ) {
30 $t = Title
::newFromID( $id );
31 # @todo FIXME: Doesn't inherit right
32 return $t == null ?
null : new self( $t );
33 # return $t == null ? null : new static( $t ); // PHP 5.3
37 global $wgRequest, $wgUser;
39 $diff = $wgRequest->getVal( 'diff' );
40 $diffOnly = $wgRequest->getBool( 'diffonly', $wgUser->getOption( 'diffonly' ) );
42 if ( isset( $diff ) && $diffOnly ) {
43 return parent
::view();
46 if ( !wfRunHooks( 'CategoryPageView', array( &$this ) ) ) {
50 if ( NS_CATEGORY
== $this->mTitle
->getNamespace() ) {
51 $this->openShowCategory();
56 if ( NS_CATEGORY
== $this->mTitle
->getNamespace() ) {
57 $this->closeShowCategory();
61 function openShowCategory() {
65 function closeShowCategory() {
66 global $wgOut, $wgRequest;
68 // Use these as defaults for back compat --catrope
69 $oldFrom = $wgRequest->getVal( 'from' );
70 $oldUntil = $wgRequest->getVal( 'until' );
72 $from = $until = array();
73 foreach ( array( 'page', 'subcat', 'file' ) as $type ) {
74 $from[$type] = $wgRequest->getVal( "{$type}from", $oldFrom );
75 $until[$type] = $wgRequest->getVal( "{$type}until", $oldUntil );
78 $viewer = new $this->mCategoryViewerClass( $this->mTitle
, $from, $until, $wgRequest->getValues() );
79 $wgOut->addHTML( $viewer->getHTML() );
83 class CategoryViewer
{
84 var $limit, $from, $until,
85 $articles, $articles_start_char,
86 $children, $children_start_char,
87 $showGallery, $imgsNoGalley,
88 $imgsNoGallery_start_char,
89 $skin, $imgsNoGallery;
117 * Category object for this page
123 * The original query array, to be used in generating paging links.
128 function __construct( $title, $from = '', $until = '', $query = array() ) {
129 global $wgCategoryPagingLimit;
130 $this->title
= $title;
132 $this->until
= $until;
133 $this->limit
= $wgCategoryPagingLimit;
134 $this->cat
= Category
::newFromTitle( $title );
135 $this->query
= $query;
136 $this->collation
= Collation
::singleton();
137 unset( $this->query
['title'] );
141 * Format the category data list.
143 * @return string HTML output
145 public function getHTML() {
146 global $wgOut, $wgCategoryMagicGallery, $wgContLang, $wgBetterDirectionality;
147 wfProfileIn( __METHOD__
);
149 $this->showGallery
= $wgCategoryMagicGallery && !$wgOut->mNoGallery
;
151 $this->clearCategoryState();
152 $this->doCategoryQuery();
153 $this->finaliseCategoryState();
155 $r = $this->getSubcategorySection() .
156 $this->getPagesSection() .
157 $this->getImageSection();
160 // If there is no category content to display, only
161 // show the top part of the navigation links.
162 // @todo FIXME: Cannot be completely suppressed because it
163 // is unknown if 'until' or 'from' makes this
165 $r = $r . $this->getCategoryTop();
167 $r = $this->getCategoryTop() .
169 $this->getCategoryBottom();
172 // Give a proper message if category is empty
174 $r = wfMsgExt( 'category-empty', array( 'parse' ) );
177 if( $wgBetterDirectionality ) {
178 $pageLang = $this->title
->getPageLanguage();
179 $langAttribs = array( 'lang' => $pageLang->getCode(), 'dir' => $pageLang->getDir() );
180 # close the previous div, show the headings in user language,
181 # then open a new div with the page content language again
182 $r = '</div>' . $r . Html
::openElement( 'div', $langAttribs );
185 wfProfileOut( __METHOD__
);
186 return $wgContLang->convert( $r );
189 function clearCategoryState() {
190 $this->articles
= array();
191 $this->articles_start_char
= array();
192 $this->children
= array();
193 $this->children_start_char
= array();
194 if ( $this->showGallery
) {
195 $this->gallery
= new ImageGallery();
196 $this->gallery
->setHideBadImages();
198 $this->imgsNoGallery
= array();
199 $this->imgsNoGallery_start_char
= array();
207 if ( !$this->skin
) {
209 $this->skin
= $wgUser->getSkin();
215 * Add a subcategory to the internal lists, using a Category object
217 function addSubcategoryObject( Category
$cat, $sortkey, $pageLength ) {
218 // Subcategory; strip the 'Category' namespace from the link text.
219 $title = $cat->getTitle();
221 $link = $this->getSkin()->link( $title, $title->getText() );
222 if ( $title->isRedirect() ) {
223 // This didn't used to add redirect-in-category, but might
224 // as well be consistent with the rest of the sections
225 // on a category page.
226 $link = '<span class="redirect-in-category">' . $link . '</span>';
228 $this->children
[] = $link;
230 $this->children_start_char
[] =
231 $this->getSubcategorySortChar( $cat->getTitle(), $sortkey );
235 * Add a subcategory to the internal lists, using a title object
236 * @deprecated since 1.17 kept for compatibility, please use addSubcategoryObject instead
238 function addSubcategory( Title
$title, $sortkey, $pageLength ) {
239 $this->addSubcategoryObject( Category
::newFromTitle( $title ), $sortkey, $pageLength );
243 * Get the character to be used for sorting subcategories.
244 * If there's a link from Category:A to Category:B, the sortkey of the resulting
245 * entry in the categorylinks table is Category:A, not A, which it SHOULD be.
246 * Workaround: If sortkey == "Category:".$title, than use $title for sorting,
247 * else use sortkey...
249 * @param Title $title
250 * @param string $sortkey The human-readable sortkey (before transforming to icu or whatever).
252 function getSubcategorySortChar( $title, $sortkey ) {
255 if ( $title->getPrefixedText() == $sortkey ) {
256 $word = $title->getDBkey();
261 $firstChar = $this->collation
->getFirstLetter( $word );
263 return $wgContLang->convert( $firstChar );
267 * Add a page in the image namespace
269 function addImage( Title
$title, $sortkey, $pageLength, $isRedirect = false ) {
271 if ( $this->showGallery
) {
272 $flip = $this->flip
['file'];
274 $this->gallery
->insert( $title );
276 $this->gallery
->add( $title );
279 $link = $this->getSkin()->link( $title );
281 // This seems kind of pointless given 'mw-redirect' class,
282 // but keeping for back-compatibility with user css.
283 $link = '<span class="redirect-in-category">' . $link . '</span>';
285 $this->imgsNoGallery
[] = $link;
287 $this->imgsNoGallery_start_char
[] = $wgContLang->convert(
288 $this->collation
->getFirstLetter( $sortkey ) );
293 * Add a miscellaneous page
295 function addPage( $title, $sortkey, $pageLength, $isRedirect = false ) {
298 $link = $this->getSkin()->link( $title );
300 // This seems kind of pointless given 'mw-redirect' class,
301 // but keeping for back-compatiability with user css.
302 $link = '<span class="redirect-in-category">' . $link . '</span>';
304 $this->articles
[] = $link;
306 $this->articles_start_char
[] = $wgContLang->convert(
307 $this->collation
->getFirstLetter( $sortkey ) );
310 function finaliseCategoryState() {
311 if ( $this->flip
['subcat'] ) {
312 $this->children
= array_reverse( $this->children
);
313 $this->children_start_char
= array_reverse( $this->children_start_char
);
315 if ( $this->flip
['page'] ) {
316 $this->articles
= array_reverse( $this->articles
);
317 $this->articles_start_char
= array_reverse( $this->articles_start_char
);
319 if ( !$this->showGallery
&& $this->flip
['file'] ) {
320 $this->imgsNoGallery
= array_reverse( $this->imgsNoGallery
);
321 $this->imgsNoGallery_start_char
= array_reverse( $this->imgsNoGallery_start_char
);
325 function doCategoryQuery() {
326 $dbr = wfGetDB( DB_SLAVE
, 'category' );
328 $this->nextPage
= array(
333 $this->flip
= array( 'page' => false, 'subcat' => false, 'file' => false );
335 foreach ( array( 'page', 'subcat', 'file' ) as $type ) {
336 # Get the sortkeys for start/end, if applicable. Note that if
337 # the collation in the database differs from the one
338 # set in $wgCategoryCollation, pagination might go totally haywire.
339 $extraConds = array( 'cl_type' => $type );
340 if ( $this->from
[$type] !== null ) {
341 $extraConds[] = 'cl_sortkey >= '
342 . $dbr->addQuotes( $this->collation
->getSortKey( $this->from
[$type] ) );
343 } elseif ( $this->until
[$type] !== null ) {
344 $extraConds[] = 'cl_sortkey < '
345 . $dbr->addQuotes( $this->collation
->getSortKey( $this->until
[$type] ) );
346 $this->flip
[$type] = true;
350 array( 'page', 'categorylinks', 'category' ),
351 array( 'page_id', 'page_title', 'page_namespace', 'page_len',
352 'page_is_redirect', 'cl_sortkey', 'cat_id', 'cat_title',
353 'cat_subcats', 'cat_pages', 'cat_files',
354 'cl_sortkey_prefix', 'cl_collation' ),
355 array_merge( array( 'cl_to' => $this->title
->getDBkey() ), $extraConds ),
358 'USE INDEX' => array( 'categorylinks' => 'cl_sortkey' ),
359 'LIMIT' => $this->limit +
1,
360 'ORDER BY' => $this->flip
[$type] ?
'cl_sortkey DESC' : 'cl_sortkey',
363 'categorylinks' => array( 'INNER JOIN', 'cl_from = page_id' ),
364 'category' => array( 'LEFT JOIN', 'cat_title = page_title AND page_namespace = ' . NS_CATEGORY
)
369 foreach ( $res as $row ) {
370 $title = Title
::newFromRow( $row );
371 if ( $row->cl_collation
=== '' ) {
372 // Hack to make sure that while updating from 1.16 schema
373 // and db is inconsistent, that the sky doesn't fall.
374 // See r83544. Could perhaps be removed in a couple decades...
375 $humanSortkey = $row->cl_sortkey
;
377 $humanSortkey = $title->getCategorySortkey( $row->cl_sortkey_prefix
);
380 if ( ++
$count > $this->limit
) {
381 # We've reached the one extra which shows that there
382 # are additional pages to be had. Stop here...
383 $this->nextPage
[$type] = $humanSortkey;
387 if ( $title->getNamespace() == NS_CATEGORY
) {
388 $cat = Category
::newFromRow( $row, $title );
389 $this->addSubcategoryObject( $cat, $humanSortkey, $row->page_len
);
390 } elseif ( $title->getNamespace() == NS_FILE
) {
391 $this->addImage( $title, $humanSortkey, $row->page_len
, $row->page_is_redirect
);
393 $this->addPage( $title, $humanSortkey, $row->page_len
, $row->page_is_redirect
);
399 function getCategoryTop() {
400 $r = $this->getCategoryBottom();
403 : "<br style=\"clear:both;\"/>\n" . $r;
406 function getSubcategorySection() {
407 # Don't show subcategories section if there are none.
409 $rescnt = count( $this->children
);
410 $dbcnt = $this->cat
->getSubcatCount();
411 $countmsg = $this->getCountMessage( $rescnt, $dbcnt, 'subcat' );
414 # Showing subcategories
415 $r .= "<div id=\"mw-subcategories\">\n";
416 $r .= '<h2>' . wfMsg( 'subcategories' ) . "</h2>\n";
418 $r .= $this->getSectionPagingLinks( 'subcat' );
419 $r .= $this->formatList( $this->children
, $this->children_start_char
);
420 $r .= $this->getSectionPagingLinks( 'subcat' );
426 function getPagesSection() {
427 $ti = htmlspecialchars( $this->title
->getText() );
428 # Don't show articles section if there are none.
431 # @todo FIXME: Here and in the other two sections: we don't need to bother
432 # with this rigamarole if the entire category contents fit on one page
433 # and have already been retrieved. We can just use $rescnt in that
434 # case and save a query and some logic.
435 $dbcnt = $this->cat
->getPageCount() - $this->cat
->getSubcatCount()
436 - $this->cat
->getFileCount();
437 $rescnt = count( $this->articles
);
438 $countmsg = $this->getCountMessage( $rescnt, $dbcnt, 'article' );
441 $r = "<div id=\"mw-pages\">\n";
442 $r .= '<h2>' . wfMsg( 'category_header', $ti ) . "</h2>\n";
444 $r .= $this->getSectionPagingLinks( 'page' );
445 $r .= $this->formatList( $this->articles
, $this->articles_start_char
);
446 $r .= $this->getSectionPagingLinks( 'page' );
452 function getImageSection() {
454 $rescnt = $this->showGallery ?
$this->gallery
->count() : count( $this->imgsNoGallery
);
456 $dbcnt = $this->cat
->getFileCount();
457 $countmsg = $this->getCountMessage( $rescnt, $dbcnt, 'file' );
459 $r .= "<div id=\"mw-category-media\">\n";
460 $r .= '<h2>' . wfMsg( 'category-media-header', htmlspecialchars( $this->title
->getText() ) ) . "</h2>\n";
462 $r .= $this->getSectionPagingLinks( 'file' );
463 if ( $this->showGallery
) {
464 $r .= $this->gallery
->toHTML();
466 $r .= $this->formatList( $this->imgsNoGallery
, $this->imgsNoGallery_start_char
);
468 $r .= $this->getSectionPagingLinks( 'file' );
475 * Get the paging links for a section (subcats/pages/files), to go at the top and bottom
478 * @param $type String: 'page', 'subcat', or 'file'
479 * @return String: HTML output, possibly empty if there are no other pages
481 private function getSectionPagingLinks( $type ) {
482 if ( $this->until
[$type] !== null ) {
483 return $this->pagingLinks( $this->nextPage
[$type], $this->until
[$type], $type );
484 } elseif ( $this->nextPage
[$type] !== null ||
$this->from
[$type] !== null ) {
485 return $this->pagingLinks( $this->from
[$type], $this->nextPage
[$type], $type );
491 function getCategoryBottom() {
496 * Format a list of articles chunked by letter, either as a
497 * bullet list or a columnar format, depending on the length.
499 * @param $articles Array
500 * @param $articles_start_char Array
505 function formatList( $articles, $articles_start_char, $cutoff = 6 ) {
506 global $wgBetterDirectionality;
509 if ( count ( $articles ) > $cutoff ) {
510 $list = self
::columnList( $articles, $articles_start_char );
511 } elseif ( count( $articles ) > 0 ) {
512 // for short lists of articles in categories.
513 $list = self
::shortList( $articles, $articles_start_char );
516 if( $wgBetterDirectionality ) {
517 $pageLang = $this->title
->getPageLanguage();
518 $attribs = array( 'lang' => $pageLang->getCode(), 'dir' => $pageLang->getDir(),
519 'class' => 'mw-content-'.$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(
628 $this->addFragmentToTitle( $this->title
, $type ),
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(
642 $this->addFragmentToTitle( $this->title
, $type ),
649 return "($prevLink) ($nextLink)";
653 * Takes a title, and adds the fragment identifier that
654 * corresponds to the correct segment of the category.
656 * @param Title $title: The title (usually $this->title)
657 * @param String $section: Which section
659 private function addFragmentToTitle( $title, $section ) {
660 switch ( $section ) {
662 $fragment = 'mw-pages';
665 $fragment = 'mw-subcategories';
668 $fragment = 'mw-category-media';
671 throw new MWException( __METHOD__
.
672 " Invalid section $section." );
675 return Title
::makeTitle( $title->getNamespace(),
676 $title->getDBkey(), $fragment );
679 * What to do if the category table conflicts with the number of results
680 * returned? This function says what. Each type is considered independently
681 * of the other types.
683 * Note for grepping: uses the messages category-article-count,
684 * category-article-count-limited, category-subcat-count,
685 * category-subcat-count-limited, category-file-count,
686 * category-file-count-limited.
688 * @param $rescnt Int: The number of items returned by our database query.
689 * @param $dbcnt Int: The number of items according to the category table.
690 * @param $type String: 'subcat', 'article', or 'file'
691 * @return String: A message giving the number of items, to output to HTML.
693 private function getCountMessage( $rescnt, $dbcnt, $type ) {
695 # There are three cases:
696 # 1) The category table figure seems sane. It might be wrong, but
697 # we can't do anything about it if we don't recalculate it on ev-
699 # 2) The category table figure isn't sane, like it's smaller than the
700 # number of actual results, *but* the number of results is less
701 # than $this->limit and there's no offset. In this case we still
702 # know the right figure.
703 # 3) We have no idea.
705 # Check if there's a "from" or "until" for anything
707 // This is a little ugly, but we seem to use different names
708 // for the paging types then for the messages.
709 if ( $type === 'article' ) {
710 $pagingType = 'page';
715 $fromOrUntil = false;
716 if ( $this->from
[$pagingType] !== null ||
$this->until
[$pagingType] !== null ) {
720 if ( $dbcnt == $rescnt ||
( ( $rescnt == $this->limit ||
$fromOrUntil )
721 && $dbcnt > $rescnt ) ) {
722 # Case 1: seems sane.
724 } elseif ( $rescnt < $this->limit
&& !$fromOrUntil ) {
725 # Case 2: not sane, but salvageable. Use the number of results.
726 # Since there are fewer than 200, we can also take this opportunity
727 # to refresh the incorrect category table entry -- which should be
728 # quick due to the small number of entries.
730 $this->cat
->refreshCounts();
732 # Case 3: hopeless. Don't give a total count at all.
733 return wfMsgExt( "category-$type-count-limited", 'parse',
734 $wgLang->formatNum( $rescnt ) );
737 "category-$type-count",
739 $wgLang->formatNum( $rescnt ),
740 $wgLang->formatNum( $totalcnt )