3 if ( !defined( 'MEDIAWIKI' ) )
6 class CategoryViewer
extends ContextSource
{
7 var $limit, $from, $until,
8 $articles, $articles_start_char,
9 $children, $children_start_char,
10 $showGallery, $imgsNoGalley,
11 $imgsNoGallery_start_char,
40 * Category object for this page
46 * The original query array, to be used in generating paging links.
54 * @since 1.19 $context is a second, required parameter
56 * @param $context IContextSource
58 * @param $until String
61 function __construct( $title, IContextSource
$context, $from = '', $until = '', $query = array() ) {
62 global $wgCategoryPagingLimit;
63 $this->title
= $title;
64 $this->setContext( $context );
66 $this->until
= $until;
67 $this->limit
= $wgCategoryPagingLimit;
68 $this->cat
= Category
::newFromTitle( $title );
69 $this->query
= $query;
70 $this->collation
= Collation
::singleton();
71 unset( $this->query
['title'] );
75 * Format the category data list.
77 * @return string HTML output
79 public function getHTML() {
80 global $wgCategoryMagicGallery;
81 wfProfileIn( __METHOD__
);
83 $this->showGallery
= $wgCategoryMagicGallery && !$this->getOutput()->mNoGallery
;
85 $this->clearCategoryState();
86 $this->doCategoryQuery();
87 $this->finaliseCategoryState();
89 $r = $this->getSubcategorySection() .
90 $this->getPagesSection() .
91 $this->getImageSection();
94 // If there is no category content to display, only
95 // show the top part of the navigation links.
96 // @todo FIXME: Cannot be completely suppressed because it
97 // is unknown if 'until' or 'from' makes this
99 $r = $r . $this->getCategoryTop();
101 $r = $this->getCategoryTop() .
103 $this->getCategoryBottom();
106 // Give a proper message if category is empty
108 $r = wfMsgExt( 'category-empty', array( 'parse' ) );
111 $lang = $this->getLang();
112 $langAttribs = array( 'lang' => $lang->getCode(), 'dir' => $lang->getDir() );
113 # put a div around the headings which are in the user language
114 $r = Html
::openElement( 'div', $langAttribs ) . $r . '</div>';
116 wfProfileOut( __METHOD__
);
120 function clearCategoryState() {
121 $this->articles
= array();
122 $this->articles_start_char
= array();
123 $this->children
= array();
124 $this->children_start_char
= array();
125 if ( $this->showGallery
) {
126 $this->gallery
= new ImageGallery();
127 $this->gallery
->setHideBadImages();
129 $this->imgsNoGallery
= array();
130 $this->imgsNoGallery_start_char
= array();
135 * Add a subcategory to the internal lists, using a Category object
137 function addSubcategoryObject( Category
$cat, $sortkey, $pageLength ) {
138 // Subcategory; strip the 'Category' namespace from the link text.
139 $title = $cat->getTitle();
141 $link = Linker
::link( $title, htmlspecialchars( $title->getText() ) );
142 if ( $title->isRedirect() ) {
143 // This didn't used to add redirect-in-category, but might
144 // as well be consistent with the rest of the sections
145 // on a category page.
146 $link = '<span class="redirect-in-category">' . $link . '</span>';
148 $this->children
[] = $link;
150 $this->children_start_char
[] =
151 $this->getSubcategorySortChar( $cat->getTitle(), $sortkey );
155 * Add a subcategory to the internal lists, using a title object
156 * @deprecated since 1.17 kept for compatibility, please use addSubcategoryObject instead
158 function addSubcategory( Title
$title, $sortkey, $pageLength ) {
159 $this->addSubcategoryObject( Category
::newFromTitle( $title ), $sortkey, $pageLength );
163 * Get the character to be used for sorting subcategories.
164 * If there's a link from Category:A to Category:B, the sortkey of the resulting
165 * entry in the categorylinks table is Category:A, not A, which it SHOULD be.
166 * Workaround: If sortkey == "Category:".$title, than use $title for sorting,
167 * else use sortkey...
169 * @param Title $title
170 * @param string $sortkey The human-readable sortkey (before transforming to icu or whatever).
172 function getSubcategorySortChar( $title, $sortkey ) {
175 if ( $title->getPrefixedText() == $sortkey ) {
176 $word = $title->getDBkey();
181 $firstChar = $this->collation
->getFirstLetter( $word );
183 return $wgContLang->convert( $firstChar );
187 * Add a page in the image namespace
189 function addImage( Title
$title, $sortkey, $pageLength, $isRedirect = false ) {
191 if ( $this->showGallery
) {
192 $flip = $this->flip
['file'];
194 $this->gallery
->insert( $title );
196 $this->gallery
->add( $title );
199 $link = Linker
::link( $title );
201 // This seems kind of pointless given 'mw-redirect' class,
202 // but keeping for back-compatibility with user css.
203 $link = '<span class="redirect-in-category">' . $link . '</span>';
205 $this->imgsNoGallery
[] = $link;
207 $this->imgsNoGallery_start_char
[] = $wgContLang->convert(
208 $this->collation
->getFirstLetter( $sortkey ) );
213 * Add a miscellaneous page
215 function addPage( $title, $sortkey, $pageLength, $isRedirect = false ) {
218 $link = Linker
::link( $title );
220 // This seems kind of pointless given 'mw-redirect' class,
221 // but keeping for back-compatiability with user css.
222 $link = '<span class="redirect-in-category">' . $link . '</span>';
224 $this->articles
[] = $link;
226 $this->articles_start_char
[] = $wgContLang->convert(
227 $this->collation
->getFirstLetter( $sortkey ) );
230 function finaliseCategoryState() {
231 if ( $this->flip
['subcat'] ) {
232 $this->children
= array_reverse( $this->children
);
233 $this->children_start_char
= array_reverse( $this->children_start_char
);
235 if ( $this->flip
['page'] ) {
236 $this->articles
= array_reverse( $this->articles
);
237 $this->articles_start_char
= array_reverse( $this->articles_start_char
);
239 if ( !$this->showGallery
&& $this->flip
['file'] ) {
240 $this->imgsNoGallery
= array_reverse( $this->imgsNoGallery
);
241 $this->imgsNoGallery_start_char
= array_reverse( $this->imgsNoGallery_start_char
);
245 function doCategoryQuery() {
246 $dbr = wfGetDB( DB_SLAVE
, 'category' );
248 $this->nextPage
= array(
253 $this->flip
= array( 'page' => false, 'subcat' => false, 'file' => false );
255 foreach ( array( 'page', 'subcat', 'file' ) as $type ) {
256 # Get the sortkeys for start/end, if applicable. Note that if
257 # the collation in the database differs from the one
258 # set in $wgCategoryCollation, pagination might go totally haywire.
259 $extraConds = array( 'cl_type' => $type );
260 if ( $this->from
[$type] !== null ) {
261 $extraConds[] = 'cl_sortkey >= '
262 . $dbr->addQuotes( $this->collation
->getSortKey( $this->from
[$type] ) );
263 } elseif ( $this->until
[$type] !== null ) {
264 $extraConds[] = 'cl_sortkey < '
265 . $dbr->addQuotes( $this->collation
->getSortKey( $this->until
[$type] ) );
266 $this->flip
[$type] = true;
270 array( 'page', 'categorylinks', 'category' ),
271 array( 'page_id', 'page_title', 'page_namespace', 'page_len',
272 'page_is_redirect', 'cl_sortkey', 'cat_id', 'cat_title',
273 'cat_subcats', 'cat_pages', 'cat_files',
274 'cl_sortkey_prefix', 'cl_collation' ),
275 array_merge( array( 'cl_to' => $this->title
->getDBkey() ), $extraConds ),
278 'USE INDEX' => array( 'categorylinks' => 'cl_sortkey' ),
279 'LIMIT' => $this->limit +
1,
280 'ORDER BY' => $this->flip
[$type] ?
'cl_sortkey DESC' : 'cl_sortkey',
283 'categorylinks' => array( 'INNER JOIN', 'cl_from = page_id' ),
284 'category' => array( 'LEFT JOIN', 'cat_title = page_title AND page_namespace = ' . NS_CATEGORY
)
289 foreach ( $res as $row ) {
290 $title = Title
::newFromRow( $row );
291 if ( $row->cl_collation
=== '' ) {
292 // Hack to make sure that while updating from 1.16 schema
293 // and db is inconsistent, that the sky doesn't fall.
294 // See r83544. Could perhaps be removed in a couple decades...
295 $humanSortkey = $row->cl_sortkey
;
297 $humanSortkey = $title->getCategorySortkey( $row->cl_sortkey_prefix
);
300 if ( ++
$count > $this->limit
) {
301 # We've reached the one extra which shows that there
302 # are additional pages to be had. Stop here...
303 $this->nextPage
[$type] = $humanSortkey;
307 if ( $title->getNamespace() == NS_CATEGORY
) {
308 $cat = Category
::newFromRow( $row, $title );
309 $this->addSubcategoryObject( $cat, $humanSortkey, $row->page_len
);
310 } elseif ( $title->getNamespace() == NS_FILE
) {
311 $this->addImage( $title, $humanSortkey, $row->page_len
, $row->page_is_redirect
);
313 $this->addPage( $title, $humanSortkey, $row->page_len
, $row->page_is_redirect
);
319 function getCategoryTop() {
320 $r = $this->getCategoryBottom();
323 : "<br style=\"clear:both;\"/>\n" . $r;
326 function getSubcategorySection() {
327 # Don't show subcategories section if there are none.
329 $rescnt = count( $this->children
);
330 $dbcnt = $this->cat
->getSubcatCount();
331 $countmsg = $this->getCountMessage( $rescnt, $dbcnt, 'subcat' );
334 # Showing subcategories
335 $r .= "<div id=\"mw-subcategories\">\n";
336 $r .= '<h2>' . wfMsg( 'subcategories' ) . "</h2>\n";
338 $r .= $this->getSectionPagingLinks( 'subcat' );
339 $r .= $this->formatList( $this->children
, $this->children_start_char
);
340 $r .= $this->getSectionPagingLinks( 'subcat' );
346 function getPagesSection() {
347 $ti = htmlspecialchars( $this->title
->getText() );
348 # Don't show articles section if there are none.
351 # @todo FIXME: Here and in the other two sections: we don't need to bother
352 # with this rigamarole if the entire category contents fit on one page
353 # and have already been retrieved. We can just use $rescnt in that
354 # case and save a query and some logic.
355 $dbcnt = $this->cat
->getPageCount() - $this->cat
->getSubcatCount()
356 - $this->cat
->getFileCount();
357 $rescnt = count( $this->articles
);
358 $countmsg = $this->getCountMessage( $rescnt, $dbcnt, 'article' );
361 $r = "<div id=\"mw-pages\">\n";
362 $r .= '<h2>' . wfMsg( 'category_header', $ti ) . "</h2>\n";
364 $r .= $this->getSectionPagingLinks( 'page' );
365 $r .= $this->formatList( $this->articles
, $this->articles_start_char
);
366 $r .= $this->getSectionPagingLinks( 'page' );
372 function getImageSection() {
374 $rescnt = $this->showGallery ?
$this->gallery
->count() : count( $this->imgsNoGallery
);
376 $dbcnt = $this->cat
->getFileCount();
377 $countmsg = $this->getCountMessage( $rescnt, $dbcnt, 'file' );
379 $r .= "<div id=\"mw-category-media\">\n";
380 $r .= '<h2>' . wfMsg( 'category-media-header', htmlspecialchars( $this->title
->getText() ) ) . "</h2>\n";
382 $r .= $this->getSectionPagingLinks( 'file' );
383 if ( $this->showGallery
) {
384 $r .= $this->gallery
->toHTML();
386 $r .= $this->formatList( $this->imgsNoGallery
, $this->imgsNoGallery_start_char
);
388 $r .= $this->getSectionPagingLinks( 'file' );
395 * Get the paging links for a section (subcats/pages/files), to go at the top and bottom
398 * @param $type String: 'page', 'subcat', or 'file'
399 * @return String: HTML output, possibly empty if there are no other pages
401 private function getSectionPagingLinks( $type ) {
402 if ( $this->until
[$type] !== null ) {
403 return $this->pagingLinks( $this->nextPage
[$type], $this->until
[$type], $type );
404 } elseif ( $this->nextPage
[$type] !== null ||
$this->from
[$type] !== null ) {
405 return $this->pagingLinks( $this->from
[$type], $this->nextPage
[$type], $type );
411 function getCategoryBottom() {
416 * Format a list of articles chunked by letter, either as a
417 * bullet list or a columnar format, depending on the length.
419 * @param $articles Array
420 * @param $articles_start_char Array
425 function formatList( $articles, $articles_start_char, $cutoff = 6 ) {
427 if ( count ( $articles ) > $cutoff ) {
428 $list = self
::columnList( $articles, $articles_start_char );
429 } elseif ( count( $articles ) > 0 ) {
430 // for short lists of articles in categories.
431 $list = self
::shortList( $articles, $articles_start_char );
434 $pageLang = $this->title
->getPageLanguage();
435 $attribs = array( 'lang' => $pageLang->getCode(), 'dir' => $pageLang->getDir(),
436 'class' => 'mw-content-'.$pageLang->getDir() );
437 $list = Html
::rawElement( 'div', $attribs, $list );
443 * Format a list of articles chunked by letter in a three-column
444 * list, ordered vertically.
446 * TODO: Take the headers into account when creating columns, so they're
447 * more visually equal.
449 * More distant TODO: Scrap this and use CSS columns, whenever IE finally
452 * @param $articles Array
453 * @param $articles_start_char Array
457 static function columnList( $articles, $articles_start_char ) {
458 $columns = array_combine( $articles, $articles_start_char );
459 # Split into three columns
460 $columns = array_chunk( $columns, ceil( count( $columns ) / 3 ), true /* preserve keys */ );
462 $ret = '<table width="100%"><tr valign="top"><td>';
465 foreach ( $columns as $column ) {
466 $colContents = array();
468 # Kind of like array_flip() here, but we keep duplicates in an
469 # array instead of dropping them.
470 foreach ( $column as $article => $char ) {
471 if ( !isset( $colContents[$char] ) ) {
472 $colContents[$char] = array();
474 $colContents[$char][] = $article;
478 foreach ( $colContents as $char => $articles ) {
479 $ret .= '<h3>' . htmlspecialchars( $char );
480 if ( $first && $char === $prevchar ) {
481 # We're continuing a previous chunk at the top of a new
482 # column, so add " cont." after the letter.
483 $ret .= ' ' . wfMsgHtml( 'listingcontinuesabbrev' );
488 $ret .= implode( "</li>\n<li>", $articles );
489 $ret .= '</li></ul>';
495 $ret .= "</td>\n<td>";
498 $ret .= '</td></tr></table>';
503 * Format a list of articles chunked by letter in a bullet list.
504 * @param $articles Array
505 * @param $articles_start_char Array
509 static function shortList( $articles, $articles_start_char ) {
510 $r = '<h3>' . htmlspecialchars( $articles_start_char[0] ) . "</h3>\n";
511 $r .= '<ul><li>' . $articles[0] . '</li>';
512 for ( $index = 1; $index < count( $articles ); $index++
) {
513 if ( $articles_start_char[$index] != $articles_start_char[$index - 1] ) {
514 $r .= "</ul><h3>" . htmlspecialchars( $articles_start_char[$index] ) . "</h3>\n<ul>";
517 $r .= "<li>{$articles[$index]}</li>";
524 * Create paging links, as a helper method to getSectionPagingLinks().
526 * @param $first String The 'until' parameter for the generated URL
527 * @param $last String The 'from' parameter for the genererated URL
528 * @param $type String A prefix for parameters, 'page' or 'subcat' or
530 * @return String HTML
532 private function pagingLinks( $first, $last, $type = '' ) {
533 $prevLink = wfMessage( 'prevn' )->numParams( $this->limit
)->escaped();
535 if ( $first != '' ) {
536 $prevQuery = $this->query
;
537 $prevQuery["{$type}until"] = $first;
538 unset( $prevQuery["{$type}from"] );
539 $prevLink = Linker
::linkKnown(
540 $this->addFragmentToTitle( $this->title
, $type ),
547 $nextLink = wfMessage( 'nextn' )->numParams( $this->limit
)->escaped();
550 $lastQuery = $this->query
;
551 $lastQuery["{$type}from"] = $last;
552 unset( $lastQuery["{$type}until"] );
553 $nextLink = Linker
::linkKnown(
554 $this->addFragmentToTitle( $this->title
, $type ),
561 return "($prevLink) ($nextLink)";
565 * Takes a title, and adds the fragment identifier that
566 * corresponds to the correct segment of the category.
568 * @param Title $title: The title (usually $this->title)
569 * @param String $section: Which section
571 private function addFragmentToTitle( $title, $section ) {
572 switch ( $section ) {
574 $fragment = 'mw-pages';
577 $fragment = 'mw-subcategories';
580 $fragment = 'mw-category-media';
583 throw new MWException( __METHOD__
.
584 " Invalid section $section." );
587 return Title
::makeTitle( $title->getNamespace(),
588 $title->getDBkey(), $fragment );
591 * What to do if the category table conflicts with the number of results
592 * returned? This function says what. Each type is considered independently
593 * of the other types.
595 * Note for grepping: uses the messages category-article-count,
596 * category-article-count-limited, category-subcat-count,
597 * category-subcat-count-limited, category-file-count,
598 * category-file-count-limited.
600 * @param $rescnt Int: The number of items returned by our database query.
601 * @param $dbcnt Int: The number of items according to the category table.
602 * @param $type String: 'subcat', 'article', or 'file'
603 * @return String: A message giving the number of items, to output to HTML.
605 private function getCountMessage( $rescnt, $dbcnt, $type ) {
606 # There are three cases:
607 # 1) The category table figure seems sane. It might be wrong, but
608 # we can't do anything about it if we don't recalculate it on ev-
610 # 2) The category table figure isn't sane, like it's smaller than the
611 # number of actual results, *but* the number of results is less
612 # than $this->limit and there's no offset. In this case we still
613 # know the right figure.
614 # 3) We have no idea.
616 # Check if there's a "from" or "until" for anything
618 // This is a little ugly, but we seem to use different names
619 // for the paging types then for the messages.
620 if ( $type === 'article' ) {
621 $pagingType = 'page';
626 $fromOrUntil = false;
627 if ( $this->from
[$pagingType] !== null ||
$this->until
[$pagingType] !== null ) {
631 if ( $dbcnt == $rescnt ||
( ( $rescnt == $this->limit ||
$fromOrUntil )
632 && $dbcnt > $rescnt ) ) {
633 # Case 1: seems sane.
635 } elseif ( $rescnt < $this->limit
&& !$fromOrUntil ) {
636 # Case 2: not sane, but salvageable. Use the number of results.
637 # Since there are fewer than 200, we can also take this opportunity
638 # to refresh the incorrect category table entry -- which should be
639 # quick due to the small number of entries.
641 $this->cat
->refreshCounts();
643 # Case 3: hopeless. Don't give a total count at all.
644 return wfMessage( "category-$type-count-limited" )->numParams( $rescnt )->parseAsBlock();
646 return wfMessage( "category-$type-count" )->numParams( $rescnt, $totalcnt )->parseAsBlock();