7e898481e9d3eed78be39316e048c802ce1c658d
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 $request = $this->getContext()->getRequest();
38 $diff = $request->getVal( 'diff' );
39 $diffOnly = $request->getBool( 'diffonly',
40 $this->getContext()->getUser()->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 // Use these as defaults for back compat --catrope
67 $request = $this->getContext()->getRequest();
68 $oldFrom = $request->getVal( 'from' );
69 $oldUntil = $request->getVal( 'until' );
71 $reqArray = $request->getValues();
73 $from = $until = array();
74 foreach ( array( 'page', 'subcat', 'file' ) as $type ) {
75 $from[$type] = $request->getVal( "{$type}from", $oldFrom );
76 $until[$type] = $request->getVal( "{$type}until", $oldUntil );
78 // Do not want old-style from/until propagating in nav links.
79 if ( !isset( $reqArray["{$type}from"] ) && isset( $reqArray["from"] ) ) {
80 $reqArray["{$type}from"] = $reqArray["from"];
82 if ( !isset( $reqArray["{$type}to"] ) && isset( $reqArray["to"] ) ) {
83 $reqArray["{$type}to"] = $reqArray["to"];
87 unset( $reqArray["from"] );
88 unset( $reqArray["to"] );
90 $viewer = new $this->mCategoryViewerClass( $this->mTitle
, $this->getContext(), $from, $until, $reqArray );
91 $this->getContext()->getOutput()->addHTML( $viewer->getHTML() );
95 class CategoryViewer
{
96 var $limit, $from, $until,
97 $articles, $articles_start_char,
98 $children, $children_start_char,
99 $showGallery, $imgsNoGalley,
100 $imgsNoGallery_start_char,
129 * Category object for this page
135 * The original query array, to be used in generating paging links.
142 * @var IContextSource
149 * @since 1.19 $context is a second, required parameter
150 * @param $title Title
151 * @param $context IContextSource
152 * @param $from String
153 * @param $until String
154 * @param $query Array
156 function __construct( $title, IContextSource
$context, $from = '', $until = '', $query = array() ) {
157 global $wgCategoryPagingLimit;
158 $this->title
= $title;
159 $this->context
= $context;
161 $this->until
= $until;
162 $this->limit
= $wgCategoryPagingLimit;
163 $this->cat
= Category
::newFromTitle( $title );
164 $this->query
= $query;
165 $this->collation
= Collation
::singleton();
166 unset( $this->query
['title'] );
170 * Format the category data list.
172 * @return string HTML output
174 public function getHTML() {
175 global $wgCategoryMagicGallery;
176 wfProfileIn( __METHOD__
);
178 $this->showGallery
= $wgCategoryMagicGallery && !$this->context
->getOutput()->mNoGallery
;
180 $this->clearCategoryState();
181 $this->doCategoryQuery();
182 $this->finaliseCategoryState();
184 $r = $this->getSubcategorySection() .
185 $this->getPagesSection() .
186 $this->getImageSection();
189 // If there is no category content to display, only
190 // show the top part of the navigation links.
191 // @todo FIXME: Cannot be completely suppressed because it
192 // is unknown if 'until' or 'from' makes this
194 $r = $r . $this->getCategoryTop();
196 $r = $this->getCategoryTop() .
198 $this->getCategoryBottom();
201 // Give a proper message if category is empty
203 $r = wfMsgExt( 'category-empty', array( 'parse' ) );
206 $lang = $this->getLang();
207 $langAttribs = array( 'lang' => $lang->getCode(), 'dir' => $lang->getDir() );
208 # put a div around the headings which are in the user language
209 $r = Html
::openElement( 'div', $langAttribs ) . $r . '</div>';
211 wfProfileOut( __METHOD__
);
215 function clearCategoryState() {
216 $this->articles
= array();
217 $this->articles_start_char
= array();
218 $this->children
= array();
219 $this->children_start_char
= array();
220 if ( $this->showGallery
) {
221 $this->gallery
= new ImageGallery();
222 $this->gallery
->setHideBadImages();
224 $this->imgsNoGallery
= array();
225 $this->imgsNoGallery_start_char
= array();
230 * Add a subcategory to the internal lists, using a Category object
232 function addSubcategoryObject( Category
$cat, $sortkey, $pageLength ) {
233 // Subcategory; strip the 'Category' namespace from the link text.
234 $title = $cat->getTitle();
236 $link = Linker
::link( $title, htmlspecialchars( $title->getText() ) );
237 if ( $title->isRedirect() ) {
238 // This didn't used to add redirect-in-category, but might
239 // as well be consistent with the rest of the sections
240 // on a category page.
241 $link = '<span class="redirect-in-category">' . $link . '</span>';
243 $this->children
[] = $link;
245 $this->children_start_char
[] =
246 $this->getSubcategorySortChar( $cat->getTitle(), $sortkey );
250 * Add a subcategory to the internal lists, using a title object
251 * @deprecated since 1.17 kept for compatibility, please use addSubcategoryObject instead
253 function addSubcategory( Title
$title, $sortkey, $pageLength ) {
254 $this->addSubcategoryObject( Category
::newFromTitle( $title ), $sortkey, $pageLength );
258 * Get the character to be used for sorting subcategories.
259 * If there's a link from Category:A to Category:B, the sortkey of the resulting
260 * entry in the categorylinks table is Category:A, not A, which it SHOULD be.
261 * Workaround: If sortkey == "Category:".$title, than use $title for sorting,
262 * else use sortkey...
264 * @param Title $title
265 * @param string $sortkey The human-readable sortkey (before transforming to icu or whatever).
267 function getSubcategorySortChar( $title, $sortkey ) {
268 if ( $title->getPrefixedText() == $sortkey ) {
269 $word = $title->getDBkey();
274 $firstChar = $this->collation
->getFirstLetter( $word );
276 return $this->getLang()->convert( $firstChar );
280 * Add a page in the image namespace
282 function addImage( Title
$title, $sortkey, $pageLength, $isRedirect = false ) {
283 if ( $this->showGallery
) {
284 $flip = $this->flip
['file'];
286 $this->gallery
->insert( $title );
288 $this->gallery
->add( $title );
291 $link = Linker
::link( $title );
293 // This seems kind of pointless given 'mw-redirect' class,
294 // but keeping for back-compatibility with user css.
295 $link = '<span class="redirect-in-category">' . $link . '</span>';
297 $this->imgsNoGallery
[] = $link;
299 $this->imgsNoGallery_start_char
[] = $this->getLang()->convert(
300 $this->collation
->getFirstLetter( $sortkey ) );
305 * Add a miscellaneous page
307 function addPage( $title, $sortkey, $pageLength, $isRedirect = false ) {
308 $link = Linker
::link( $title );
310 // This seems kind of pointless given 'mw-redirect' class,
311 // but keeping for back-compatiability with user css.
312 $link = '<span class="redirect-in-category">' . $link . '</span>';
314 $this->articles
[] = $link;
316 $this->articles_start_char
[] = $this->getLang()->convert(
317 $this->collation
->getFirstLetter( $sortkey ) );
320 function finaliseCategoryState() {
321 if ( $this->flip
['subcat'] ) {
322 $this->children
= array_reverse( $this->children
);
323 $this->children_start_char
= array_reverse( $this->children_start_char
);
325 if ( $this->flip
['page'] ) {
326 $this->articles
= array_reverse( $this->articles
);
327 $this->articles_start_char
= array_reverse( $this->articles_start_char
);
329 if ( !$this->showGallery
&& $this->flip
['file'] ) {
330 $this->imgsNoGallery
= array_reverse( $this->imgsNoGallery
);
331 $this->imgsNoGallery_start_char
= array_reverse( $this->imgsNoGallery_start_char
);
335 function doCategoryQuery() {
336 $dbr = wfGetDB( DB_SLAVE
, 'category' );
338 $this->nextPage
= array(
343 $this->flip
= array( 'page' => false, 'subcat' => false, 'file' => false );
345 foreach ( array( 'page', 'subcat', 'file' ) as $type ) {
346 # Get the sortkeys for start/end, if applicable. Note that if
347 # the collation in the database differs from the one
348 # set in $wgCategoryCollation, pagination might go totally haywire.
349 $extraConds = array( 'cl_type' => $type );
350 if ( $this->from
[$type] !== null ) {
351 $extraConds[] = 'cl_sortkey >= '
352 . $dbr->addQuotes( $this->collation
->getSortKey( $this->from
[$type] ) );
353 } elseif ( $this->until
[$type] !== null ) {
354 $extraConds[] = 'cl_sortkey < '
355 . $dbr->addQuotes( $this->collation
->getSortKey( $this->until
[$type] ) );
356 $this->flip
[$type] = true;
360 array( 'page', 'categorylinks', 'category' ),
361 array( 'page_id', 'page_title', 'page_namespace', 'page_len',
362 'page_is_redirect', 'cl_sortkey', 'cat_id', 'cat_title',
363 'cat_subcats', 'cat_pages', 'cat_files',
364 'cl_sortkey_prefix', 'cl_collation' ),
365 array_merge( array( 'cl_to' => $this->title
->getDBkey() ), $extraConds ),
368 'USE INDEX' => array( 'categorylinks' => 'cl_sortkey' ),
369 'LIMIT' => $this->limit +
1,
370 'ORDER BY' => $this->flip
[$type] ?
'cl_sortkey DESC' : 'cl_sortkey',
373 'categorylinks' => array( 'INNER JOIN', 'cl_from = page_id' ),
374 'category' => array( 'LEFT JOIN', 'cat_title = page_title AND page_namespace = ' . NS_CATEGORY
)
379 foreach ( $res as $row ) {
380 $title = Title
::newFromRow( $row );
381 if ( $row->cl_collation
=== '' ) {
382 // Hack to make sure that while updating from 1.16 schema
383 // and db is inconsistent, that the sky doesn't fall.
384 // See r83544. Could perhaps be removed in a couple decades...
385 $humanSortkey = $row->cl_sortkey
;
387 $humanSortkey = $title->getCategorySortkey( $row->cl_sortkey_prefix
);
390 if ( ++
$count > $this->limit
) {
391 # We've reached the one extra which shows that there
392 # are additional pages to be had. Stop here...
393 $this->nextPage
[$type] = $humanSortkey;
397 if ( $title->getNamespace() == NS_CATEGORY
) {
398 $cat = Category
::newFromRow( $row, $title );
399 $this->addSubcategoryObject( $cat, $humanSortkey, $row->page_len
);
400 } elseif ( $title->getNamespace() == NS_FILE
) {
401 $this->addImage( $title, $humanSortkey, $row->page_len
, $row->page_is_redirect
);
403 $this->addPage( $title, $humanSortkey, $row->page_len
, $row->page_is_redirect
);
409 function getCategoryTop() {
410 $r = $this->getCategoryBottom();
413 : "<br style=\"clear:both;\"/>\n" . $r;
416 function getSubcategorySection() {
417 # Don't show subcategories section if there are none.
419 $rescnt = count( $this->children
);
420 $dbcnt = $this->cat
->getSubcatCount();
421 $countmsg = $this->getCountMessage( $rescnt, $dbcnt, 'subcat' );
424 # Showing subcategories
425 $r .= "<div id=\"mw-subcategories\">\n";
426 $r .= '<h2>' . wfMsg( 'subcategories' ) . "</h2>\n";
428 $r .= $this->getSectionPagingLinks( 'subcat' );
429 $r .= $this->formatList( $this->children
, $this->children_start_char
);
430 $r .= $this->getSectionPagingLinks( 'subcat' );
436 function getPagesSection() {
437 $ti = htmlspecialchars( $this->title
->getText() );
438 # Don't show articles section if there are none.
441 # @todo FIXME: Here and in the other two sections: we don't need to bother
442 # with this rigamarole if the entire category contents fit on one page
443 # and have already been retrieved. We can just use $rescnt in that
444 # case and save a query and some logic.
445 $dbcnt = $this->cat
->getPageCount() - $this->cat
->getSubcatCount()
446 - $this->cat
->getFileCount();
447 $rescnt = count( $this->articles
);
448 $countmsg = $this->getCountMessage( $rescnt, $dbcnt, 'article' );
451 $r = "<div id=\"mw-pages\">\n";
452 $r .= '<h2>' . wfMsg( 'category_header', $ti ) . "</h2>\n";
454 $r .= $this->getSectionPagingLinks( 'page' );
455 $r .= $this->formatList( $this->articles
, $this->articles_start_char
);
456 $r .= $this->getSectionPagingLinks( 'page' );
462 function getImageSection() {
464 $rescnt = $this->showGallery ?
$this->gallery
->count() : count( $this->imgsNoGallery
);
466 $dbcnt = $this->cat
->getFileCount();
467 $countmsg = $this->getCountMessage( $rescnt, $dbcnt, 'file' );
469 $r .= "<div id=\"mw-category-media\">\n";
470 $r .= '<h2>' . wfMsg( 'category-media-header', htmlspecialchars( $this->title
->getText() ) ) . "</h2>\n";
472 $r .= $this->getSectionPagingLinks( 'file' );
473 if ( $this->showGallery
) {
474 $r .= $this->gallery
->toHTML();
476 $r .= $this->formatList( $this->imgsNoGallery
, $this->imgsNoGallery_start_char
);
478 $r .= $this->getSectionPagingLinks( 'file' );
485 * Get the paging links for a section (subcats/pages/files), to go at the top and bottom
488 * @param $type String: 'page', 'subcat', or 'file'
489 * @return String: HTML output, possibly empty if there are no other pages
491 private function getSectionPagingLinks( $type ) {
492 if ( $this->until
[$type] !== null ) {
493 return $this->pagingLinks( $this->nextPage
[$type], $this->until
[$type], $type );
494 } elseif ( $this->nextPage
[$type] !== null ||
$this->from
[$type] !== null ) {
495 return $this->pagingLinks( $this->from
[$type], $this->nextPage
[$type], $type );
501 function getCategoryBottom() {
506 * Format a list of articles chunked by letter, either as a
507 * bullet list or a columnar format, depending on the length.
509 * @param $articles Array
510 * @param $articles_start_char Array
515 function formatList( $articles, $articles_start_char, $cutoff = 6 ) {
517 if ( count ( $articles ) > $cutoff ) {
518 $list = self
::columnList( $articles, $articles_start_char );
519 } elseif ( count( $articles ) > 0 ) {
520 // for short lists of articles in categories.
521 $list = self
::shortList( $articles, $articles_start_char );
524 $pageLang = $this->title
->getPageLanguage();
525 $attribs = array( 'lang' => $pageLang->getCode(), 'dir' => $pageLang->getDir(),
526 'class' => 'mw-content-'.$pageLang->getDir() );
527 $list = Html
::rawElement( 'div', $attribs, $list );
533 * Format a list of articles chunked by letter in a three-column
534 * list, ordered vertically.
536 * TODO: Take the headers into account when creating columns, so they're
537 * more visually equal.
539 * More distant TODO: Scrap this and use CSS columns, whenever IE finally
542 * @param $articles Array
543 * @param $articles_start_char Array
547 static function columnList( $articles, $articles_start_char ) {
548 $columns = array_combine( $articles, $articles_start_char );
549 # Split into three columns
550 $columns = array_chunk( $columns, ceil( count( $columns ) / 3 ), true /* preserve keys */ );
552 $ret = '<table width="100%"><tr valign="top"><td>';
555 foreach ( $columns as $column ) {
556 $colContents = array();
558 # Kind of like array_flip() here, but we keep duplicates in an
559 # array instead of dropping them.
560 foreach ( $column as $article => $char ) {
561 if ( !isset( $colContents[$char] ) ) {
562 $colContents[$char] = array();
564 $colContents[$char][] = $article;
568 foreach ( $colContents as $char => $articles ) {
569 $ret .= '<h3>' . htmlspecialchars( $char );
570 if ( $first && $char === $prevchar ) {
571 # We're continuing a previous chunk at the top of a new
572 # column, so add " cont." after the letter.
573 $ret .= ' ' . wfMsgHtml( 'listingcontinuesabbrev' );
578 $ret .= implode( "</li>\n<li>", $articles );
579 $ret .= '</li></ul>';
585 $ret .= "</td>\n<td>";
588 $ret .= '</td></tr></table>';
593 * Format a list of articles chunked by letter in a bullet list.
594 * @param $articles Array
595 * @param $articles_start_char Array
599 static function shortList( $articles, $articles_start_char ) {
600 $r = '<h3>' . htmlspecialchars( $articles_start_char[0] ) . "</h3>\n";
601 $r .= '<ul><li>' . $articles[0] . '</li>';
602 for ( $index = 1; $index < count( $articles ); $index++
) {
603 if ( $articles_start_char[$index] != $articles_start_char[$index - 1] ) {
604 $r .= "</ul><h3>" . htmlspecialchars( $articles_start_char[$index] ) . "</h3>\n<ul>";
607 $r .= "<li>{$articles[$index]}</li>";
614 * Create paging links, as a helper method to getSectionPagingLinks().
616 * @param $first String The 'until' parameter for the generated URL
617 * @param $last String The 'from' parameter for the genererated URL
618 * @param $type String A prefix for parameters, 'page' or 'subcat' or
620 * @return String HTML
622 private function pagingLinks( $first, $last, $type = '' ) {
625 $limitText = $this->getLang()->formatNum( $this->limit
);
627 $prevLink = wfMsgExt( 'prevn', array( 'escape', 'parsemag' ), $limitText );
629 if ( $first != '' ) {
630 $prevQuery = $this->query
;
631 $prevQuery["{$type}until"] = $first;
632 unset( $prevQuery["{$type}from"] );
633 $prevLink = Linker
::linkKnown(
634 $this->addFragmentToTitle( $this->title
, $type ),
641 $nextLink = wfMsgExt( 'nextn', array( 'escape', 'parsemag' ), $limitText );
644 $lastQuery = $this->query
;
645 $lastQuery["{$type}from"] = $last;
646 unset( $lastQuery["{$type}until"] );
647 $nextLink = Linker
::linkKnown(
648 $this->addFragmentToTitle( $this->title
, $type ),
655 return "($prevLink) ($nextLink)";
659 * Takes a title, and adds the fragment identifier that
660 * corresponds to the correct segment of the category.
662 * @param Title $title: The title (usually $this->title)
663 * @param String $section: Which section
665 private function addFragmentToTitle( $title, $section ) {
666 switch ( $section ) {
668 $fragment = 'mw-pages';
671 $fragment = 'mw-subcategories';
674 $fragment = 'mw-category-media';
677 throw new MWException( __METHOD__
.
678 " Invalid section $section." );
681 return Title
::makeTitle( $title->getNamespace(),
682 $title->getDBkey(), $fragment );
685 * What to do if the category table conflicts with the number of results
686 * returned? This function says what. Each type is considered independently
687 * of the other types.
689 * Note for grepping: uses the messages category-article-count,
690 * category-article-count-limited, category-subcat-count,
691 * category-subcat-count-limited, category-file-count,
692 * category-file-count-limited.
694 * @param $rescnt Int: The number of items returned by our database query.
695 * @param $dbcnt Int: The number of items according to the category table.
696 * @param $type String: 'subcat', 'article', or 'file'
697 * @return String: A message giving the number of items, to output to HTML.
699 private function getCountMessage( $rescnt, $dbcnt, $type ) {
700 # There are three cases:
701 # 1) The category table figure seems sane. It might be wrong, but
702 # we can't do anything about it if we don't recalculate it on ev-
704 # 2) The category table figure isn't sane, like it's smaller than the
705 # number of actual results, *but* the number of results is less
706 # than $this->limit and there's no offset. In this case we still
707 # know the right figure.
708 # 3) We have no idea.
710 # Check if there's a "from" or "until" for anything
712 // This is a little ugly, but we seem to use different names
713 // for the paging types then for the messages.
714 if ( $type === 'article' ) {
715 $pagingType = 'page';
720 $fromOrUntil = false;
721 if ( $this->from
[$pagingType] !== null ||
$this->until
[$pagingType] !== null ) {
725 if ( $dbcnt == $rescnt ||
( ( $rescnt == $this->limit ||
$fromOrUntil )
726 && $dbcnt > $rescnt ) ) {
727 # Case 1: seems sane.
729 } elseif ( $rescnt < $this->limit
&& !$fromOrUntil ) {
730 # Case 2: not sane, but salvageable. Use the number of results.
731 # Since there are fewer than 200, we can also take this opportunity
732 # to refresh the incorrect category table entry -- which should be
733 # quick due to the small number of entries.
735 $this->cat
->refreshCounts();
737 # Case 3: hopeless. Don't give a total count at all.
738 return wfMsgExt( "category-$type-count-limited", 'parse',
739 $this->getLang()->formatNum( $rescnt ) );
742 "category-$type-count",
744 $this->getLang()->formatNum( $rescnt ),
745 $this->getLang()->formatNum( $totalcnt )
750 * Returns a language object from the context
754 protected function getLang() {
755 return $this->context
->getLang();