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 Article
::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;
50 function openShowCategory() {
54 function closeShowCategory() {
55 global $wgOut, $wgRequest;
56 $from = $wgRequest->getVal( 'from' );
57 $until = $wgRequest->getVal( 'until' );
59 $viewer = new CategoryViewer( $this->mTitle
, $from, $until );
60 $wgOut->addHTML( $viewer->getHTML() );
64 class CategoryViewer
{
65 var $title, $limit, $from, $until,
66 $articles, $articles_start_char,
67 $children, $children_start_char,
68 $showGallery, $gallery,
70 /** Category object for this page */
73 function __construct( $title, $from = '', $until = '' ) {
74 global $wgCategoryPagingLimit;
75 $this->title
= $title;
77 $this->until
= $until;
78 $this->limit
= $wgCategoryPagingLimit;
79 $this->cat
= Category
::newFromTitle( $title );
83 * Format the category data list.
85 * @return string HTML output
89 global $wgOut, $wgCategoryMagicGallery, $wgCategoryPagingLimit;
90 wfProfileIn( __METHOD__
);
92 $this->showGallery
= $wgCategoryMagicGallery && !$wgOut->mNoGallery
;
94 $this->clearCategoryState();
95 $this->doCategoryQuery();
96 $this->finaliseCategoryState();
98 $r = $this->getCategoryTop() .
99 $this->getSubcategorySection() .
100 $this->getPagesSection() .
101 $this->getImageSection() .
102 $this->getCategoryBottom();
104 // Give a proper message if category is empty
106 $r = wfMsgExt( 'category-empty', array( 'parse' ) );
109 wfProfileOut( __METHOD__
);
113 function clearCategoryState() {
114 $this->articles
= array();
115 $this->articles_start_char
= array();
116 $this->children
= array();
117 $this->children_start_char
= array();
118 if( $this->showGallery
) {
119 $this->gallery
= new ImageGallery();
120 $this->gallery
->setHideBadImages();
125 if ( !$this->skin
) {
127 $this->skin
= $wgUser->getSkin();
133 * Add a subcategory to the internal lists, using a Category object
135 function addSubcategoryObject( $cat, $sortkey, $pageLength ) {
136 $title = $cat->getTitle();
137 $this->addSubcategory( $title, $sortkey, $pageLength );
141 * Add a subcategory to the internal lists, using a title object
142 * @deprecated kept for compatibility, please use addSubcategoryObject instead
144 function addSubcategory( $title, $sortkey, $pageLength ) {
146 // Subcategory; strip the 'Category' namespace from the link text.
147 $this->children
[] = $this->getSkin()->link(
149 $wgContLang->convertHtml( $title->getText() ),
152 array( 'known', 'noclasses' )
155 $this->children_start_char
[] = $this->getSubcategorySortChar( $title, $sortkey );
159 * Get the character to be used for sorting subcategories.
160 * If there's a link from Category:A to Category:B, the sortkey of the resulting
161 * entry in the categorylinks table is Category:A, not A, which it SHOULD be.
162 * Workaround: If sortkey == "Category:".$title, than use $title for sorting,
163 * else use sortkey...
165 function getSubcategorySortChar( $title, $sortkey ) {
168 if( $title->getPrefixedText() == $sortkey ) {
169 $firstChar = $wgContLang->firstChar( $title->getDBkey() );
171 $firstChar = $wgContLang->firstChar( $sortkey );
174 return $wgContLang->convert( $firstChar );
178 * Add a page in the image namespace
180 function addImage( Title
$title, $sortkey, $pageLength, $isRedirect = false ) {
181 if ( $this->showGallery
) {
183 $this->gallery
->insert( $title );
185 $this->gallery
->add( $title );
188 $this->addPage( $title, $sortkey, $pageLength, $isRedirect );
193 * Add a miscellaneous page
195 function addPage( $title, $sortkey, $pageLength, $isRedirect = false ) {
197 $titletext = $wgContLang->convertHtml( $title->getPrefixedText() );
198 $this->articles
[] = $isRedirect
199 ?
'<span class="redirect-in-category">' .
200 $this->getSkin()->link(
205 array( 'known', 'noclasses' )
207 : $this->getSkin()->makeSizeLinkObj( $pageLength, $title, $titletext );
208 $this->articles_start_char
[] = $wgContLang->convert( $wgContLang->firstChar( $sortkey ) );
211 function finaliseCategoryState() {
213 $this->children
= array_reverse( $this->children
);
214 $this->children_start_char
= array_reverse( $this->children_start_char
);
215 $this->articles
= array_reverse( $this->articles
);
216 $this->articles_start_char
= array_reverse( $this->articles_start_char
);
220 function doCategoryQuery() {
221 $dbr = wfGetDB( DB_SLAVE
, 'category' );
222 if( $this->from
!= '' ) {
223 $pageCondition = 'cl_sortkey >= ' . $dbr->addQuotes( $this->from
);
225 } elseif( $this->until
!= '' ) {
226 $pageCondition = 'cl_sortkey < ' . $dbr->addQuotes( $this->until
);
229 $pageCondition = '1 = 1';
233 array( 'page', 'categorylinks', 'category' ),
234 array( 'page_title', 'page_namespace', 'page_len', 'page_is_redirect', 'cl_sortkey',
235 'cat_id', 'cat_title', 'cat_subcats', 'cat_pages', 'cat_files' ),
236 array( $pageCondition, 'cl_to' => $this->title
->getDBkey() ),
238 array( 'ORDER BY' => $this->flip ?
'cl_sortkey DESC' : 'cl_sortkey',
239 'USE INDEX' => array( 'categorylinks' => 'cl_sortkey' ),
240 'LIMIT' => $this->limit +
1 ),
241 array( 'categorylinks' => array( 'INNER JOIN', 'cl_from = page_id' ),
242 'category' => array( 'LEFT JOIN', 'cat_title = page_title AND page_namespace = ' . NS_CATEGORY
) )
246 $this->nextPage
= null;
247 while( $x = $dbr->fetchObject ( $res ) ) {
248 if( ++
$count > $this->limit
) {
249 // We've reached the one extra which shows that there are
250 // additional pages to be had. Stop here...
251 $this->nextPage
= $x->cl_sortkey
;
255 $title = Title
::makeTitle( $x->page_namespace
, $x->page_title
);
257 if( $title->getNamespace() == NS_CATEGORY
) {
258 $cat = Category
::newFromRow( $x, $title );
259 $this->addSubcategoryObject( $cat, $x->cl_sortkey
, $x->page_len
);
260 } elseif( $this->showGallery
&& $title->getNamespace() == NS_FILE
) {
261 $this->addImage( $title, $x->cl_sortkey
, $x->page_len
, $x->page_is_redirect
);
263 $this->addPage( $title, $x->cl_sortkey
, $x->page_len
, $x->page_is_redirect
);
268 function getCategoryTop() {
269 $r = $this->getCategoryBottom();
272 : "<br style=\"clear:both;\"/>\n" . $r;
275 function getSubcategorySection() {
276 # Don't show subcategories section if there are none.
278 $rescnt = count( $this->children
);
279 $dbcnt = $this->cat
->getSubcatCount();
280 $countmsg = $this->getCountMessage( $rescnt, $dbcnt, 'subcat' );
282 # Showing subcategories
283 $r .= "<div id=\"mw-subcategories\">\n";
284 $r .= '<h2>' . wfMsg( 'subcategories' ) . "</h2>\n";
286 $r .= $this->formatList( $this->children
, $this->children_start_char
);
292 function getPagesSection() {
293 $ti = htmlspecialchars( $this->title
->getText() );
294 # Don't show articles section if there are none.
297 # FIXME, here and in the other two sections: we don't need to bother
298 # with this rigamarole if the entire category contents fit on one page
299 # and have already been retrieved. We can just use $rescnt in that
300 # case and save a query and some logic.
301 $dbcnt = $this->cat
->getPageCount() - $this->cat
->getSubcatCount()
302 - $this->cat
->getFileCount();
303 $rescnt = count( $this->articles
);
304 $countmsg = $this->getCountMessage( $rescnt, $dbcnt, 'article' );
307 $r = "<div id=\"mw-pages\">\n";
308 $r .= '<h2>' . wfMsg( 'category_header', $ti ) . "</h2>\n";
310 $r .= $this->formatList( $this->articles
, $this->articles_start_char
);
316 function getImageSection() {
317 if( $this->showGallery
&& ! $this->gallery
->isEmpty() ) {
318 $dbcnt = $this->cat
->getFileCount();
319 $rescnt = $this->gallery
->count();
320 $countmsg = $this->getCountMessage( $rescnt, $dbcnt, 'file' );
322 return "<div id=\"mw-category-media\">\n" .
323 '<h2>' . wfMsg( 'category-media-header', htmlspecialchars( $this->title
->getText() ) ) . "</h2>\n" .
324 $countmsg . $this->gallery
->toHTML() . "\n</div>";
330 function getCategoryBottom() {
331 if( $this->until
!= '' ) {
332 return $this->pagingLinks( $this->title
, $this->nextPage
, $this->until
, $this->limit
);
333 } elseif( $this->nextPage
!= '' ||
$this->from
!= '' ) {
334 return $this->pagingLinks( $this->title
, $this->from
, $this->nextPage
, $this->limit
);
341 * Format a list of articles chunked by letter, either as a
342 * bullet list or a columnar format, depending on the length.
344 * @param $articles Array
345 * @param $articles_start_char Array
350 function formatList( $articles, $articles_start_char, $cutoff = 6 ) {
351 if ( count ( $articles ) > $cutoff ) {
352 return $this->columnList( $articles, $articles_start_char );
353 } elseif ( count($articles) > 0) {
354 // for short lists of articles in categories.
355 return $this->shortList( $articles, $articles_start_char );
361 * Format a list of articles chunked by letter in a three-column
362 * list, ordered vertically.
364 * @param $articles Array
365 * @param $articles_start_char Array
369 function columnList( $articles, $articles_start_char ) {
370 // divide list into three equal chunks
371 $chunk = (int) ( count( $articles ) / 3 );
372 $remaining = count( $articles ) %
3;
374 // get and display header
375 $r = '<table width="100%"><tr valign="top">';
377 $prev_start_char = 'none';
379 // loop through the chunks
380 for( $startChunk = 0, $endChunk = $chunk, $chunkIndex = 0;
382 $chunkIndex++
, $startChunk = $endChunk, $endChunk +
= $remaining == 0 ?
$chunk : $chunk +
1 )
387 // output all articles in category
388 for ($index = $startChunk ;
389 $index < $endChunk && $index < count($articles);
392 // check for change of starting letter or begining of chunk
393 if ( ($index == $startChunk) ||
394 ($articles_start_char[$index] != $articles_start_char[$index - 1]) )
398 $atColumnTop = false;
403 if ( $articles_start_char[$index] == $prev_start_char )
404 $cont_msg = ' ' . wfMsgHtml( 'listingcontinuesabbrev' );
405 $r .= "<h3>" . htmlspecialchars( $articles_start_char[$index] ) . "$cont_msg</h3>\n<ul>";
406 $prev_start_char = $articles_start_char[$index];
409 $r .= "<li>{$articles[$index]}</li>";
411 if( !$atColumnTop ) {
418 $r .= '</tr></table>';
423 * Format a list of articles chunked by letter in a bullet list.
424 * @param $articles Array
425 * @param $articles_start_char Array
429 function shortList( $articles, $articles_start_char ) {
430 $r = '<h3>' . htmlspecialchars( $articles_start_char[0] ) . "</h3>\n";
431 $r .= '<ul><li>'.$articles[0].'</li>';
432 for ($index = 1; $index < count($articles); $index++
)
434 if ($articles_start_char[$index] != $articles_start_char[$index - 1])
436 $r .= "</ul><h3>" . htmlspecialchars( $articles_start_char[$index] ) . "</h3>\n<ul>";
439 $r .= "<li>{$articles[$index]}</li>";
446 * @param $title Title object
447 * @param $first String
448 * @param $last String
450 * @param $query Array: additional query options to pass
454 function pagingLinks( $title, $first, $last, $limit, $query = array() ) {
456 $sk = $this->getSkin();
457 $limitText = $wgLang->formatNum( $limit );
459 $prevLink = wfMsgExt( 'prevn', array( 'escape', 'parsemag' ), $limitText );
462 $prevQuery['until'] = $first;
463 $prevLink = $sk->link(
470 $nextLink = wfMsgExt( 'nextn', array( 'escape', 'parsemag' ), $limitText );
473 $lastQuery['from'] = $last;
474 $nextLink = $sk->link(
482 return "($prevLink) ($nextLink)";
486 * What to do if the category table conflicts with the number of results
487 * returned? This function says what. It works the same whether the
488 * things being counted are articles, subcategories, or files.
490 * Note for grepping: uses the messages category-article-count,
491 * category-article-count-limited, category-subcat-count,
492 * category-subcat-count-limited, category-file-count,
493 * category-file-count-limited.
495 * @param $rescnt Int: The number of items returned by our database query.
496 * @param $dbcnt Int: The number of items according to the category table.
497 * @param $type String: 'subcat', 'article', or 'file'
498 * @return String: A message giving the number of items, to output to HTML.
500 private function getCountMessage( $rescnt, $dbcnt, $type ) {
502 # There are three cases:
503 # 1) The category table figure seems sane. It might be wrong, but
504 # we can't do anything about it if we don't recalculate it on ev-
506 # 2) The category table figure isn't sane, like it's smaller than the
507 # number of actual results, *but* the number of results is less
508 # than $this->limit and there's no offset. In this case we still
509 # know the right figure.
510 # 3) We have no idea.
511 $totalrescnt = count( $this->articles
) +
count( $this->children
) +
512 ($this->showGallery ?
$this->gallery
->count() : 0);
513 if($dbcnt == $rescnt ||
(($totalrescnt == $this->limit ||
$this->from
514 ||
$this->until
) && $dbcnt > $rescnt)){
515 # Case 1: seems sane.
517 } elseif($totalrescnt < $this->limit
&& !$this->from
&& !$this->until
){
518 # Case 2: not sane, but salvageable. Use the number of results.
519 # Since there are fewer than 200, we can also take this opportunity
520 # to refresh the incorrect category table entry -- which should be
521 # quick due to the small number of entries.
523 $this->cat
->refreshCounts();
525 # Case 3: hopeless. Don't give a total count at all.
526 return wfMsgExt("category-$type-count-limited", 'parse',
527 $wgLang->formatNum( $rescnt ) );
529 return wfMsgExt( "category-$type-count", 'parse', $wgLang->formatNum( $rescnt ),
530 $wgLang->formatNum( $totalcnt ) );