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
);
266 $dbr->freeResult( $res );
269 function getCategoryTop() {
271 if( $this->until
!= '' ) {
272 $r .= $this->pagingLinks( $this->title
, $this->nextPage
, $this->until
, $this->limit
);
273 } elseif( $this->nextPage
!= '' ||
$this->from
!= '' ) {
274 $r .= $this->pagingLinks( $this->title
, $this->from
, $this->nextPage
, $this->limit
);
278 : "<br style=\"clear:both;\"/>\n" . $r;
281 function getSubcategorySection() {
282 # Don't show subcategories section if there are none.
284 $rescnt = count( $this->children
);
285 $dbcnt = $this->cat
->getSubcatCount();
286 $countmsg = $this->getCountMessage( $rescnt, $dbcnt, 'subcat' );
288 # Showing subcategories
289 $r .= "<div id=\"mw-subcategories\">\n";
290 $r .= '<h2>' . wfMsg( 'subcategories' ) . "</h2>\n";
292 $r .= $this->formatList( $this->children
, $this->children_start_char
);
298 function getPagesSection() {
299 $ti = htmlspecialchars( $this->title
->getText() );
300 # Don't show articles section if there are none.
303 # FIXME, here and in the other two sections: we don't need to bother
304 # with this rigamarole if the entire category contents fit on one page
305 # and have already been retrieved. We can just use $rescnt in that
306 # case and save a query and some logic.
307 $dbcnt = $this->cat
->getPageCount() - $this->cat
->getSubcatCount()
308 - $this->cat
->getFileCount();
309 $rescnt = count( $this->articles
);
310 $countmsg = $this->getCountMessage( $rescnt, $dbcnt, 'article' );
313 $r = "<div id=\"mw-pages\">\n";
314 $r .= '<h2>' . wfMsg( 'category_header', $ti ) . "</h2>\n";
316 $r .= $this->formatList( $this->articles
, $this->articles_start_char
);
322 function getImageSection() {
323 if( $this->showGallery
&& ! $this->gallery
->isEmpty() ) {
324 $dbcnt = $this->cat
->getFileCount();
325 $rescnt = $this->gallery
->count();
326 $countmsg = $this->getCountMessage( $rescnt, $dbcnt, 'file' );
328 return "<div id=\"mw-category-media\">\n" .
329 '<h2>' . wfMsg( 'category-media-header', htmlspecialchars( $this->title
->getText() ) ) . "</h2>\n" .
330 $countmsg . $this->gallery
->toHTML() . "\n</div>";
336 function getCategoryBottom() {
337 if( $this->until
!= '' ) {
338 return $this->pagingLinks( $this->title
, $this->nextPage
, $this->until
, $this->limit
);
339 } elseif( $this->nextPage
!= '' ||
$this->from
!= '' ) {
340 return $this->pagingLinks( $this->title
, $this->from
, $this->nextPage
, $this->limit
);
347 * Format a list of articles chunked by letter, either as a
348 * bullet list or a columnar format, depending on the length.
350 * @param $articles Array
351 * @param $articles_start_char Array
356 function formatList( $articles, $articles_start_char, $cutoff = 6 ) {
357 if ( count ( $articles ) > $cutoff ) {
358 return $this->columnList( $articles, $articles_start_char );
359 } elseif ( count($articles) > 0) {
360 // for short lists of articles in categories.
361 return $this->shortList( $articles, $articles_start_char );
367 * Format a list of articles chunked by letter in a three-column
368 * list, ordered vertically.
370 * @param $articles Array
371 * @param $articles_start_char Array
375 function columnList( $articles, $articles_start_char ) {
376 // divide list into three equal chunks
377 $chunk = (int) ( count( $articles ) / 3 );
378 $remaining = count( $articles ) %
3;
380 // get and display header
381 $r = '<table width="100%"><tr valign="top">';
383 $prev_start_char = 'none';
385 // loop through the chunks
386 for( $startChunk = 0, $endChunk = $chunk, $chunkIndex = 0;
388 $chunkIndex++
, $startChunk = $endChunk, $endChunk +
= $remaining == 0 ?
$chunk : $chunk +
1 )
393 // output all articles in category
394 for ($index = $startChunk ;
395 $index < $endChunk && $index < count($articles);
398 // check for change of starting letter or begining of chunk
399 if ( ($index == $startChunk) ||
400 ($articles_start_char[$index] != $articles_start_char[$index - 1]) )
404 $atColumnTop = false;
409 if ( $articles_start_char[$index] == $prev_start_char )
410 $cont_msg = ' ' . wfMsgHtml( 'listingcontinuesabbrev' );
411 $r .= "<h3>" . htmlspecialchars( $articles_start_char[$index] ) . "$cont_msg</h3>\n<ul>";
412 $prev_start_char = $articles_start_char[$index];
415 $r .= "<li>{$articles[$index]}</li>";
417 if( !$atColumnTop ) {
424 $r .= '</tr></table>';
429 * Format a list of articles chunked by letter in a bullet list.
430 * @param $articles Array
431 * @param $articles_start_char Array
435 function shortList( $articles, $articles_start_char ) {
436 $r = '<h3>' . htmlspecialchars( $articles_start_char[0] ) . "</h3>\n";
437 $r .= '<ul><li>'.$articles[0].'</li>';
438 for ($index = 1; $index < count($articles); $index++
)
440 if ($articles_start_char[$index] != $articles_start_char[$index - 1])
442 $r .= "</ul><h3>" . htmlspecialchars( $articles_start_char[$index] ) . "</h3>\n<ul>";
445 $r .= "<li>{$articles[$index]}</li>";
452 * @param $title Title object
453 * @param $first String
454 * @param $last String
456 * @param $query Array: additional query options to pass
460 function pagingLinks( $title, $first, $last, $limit, $query = array() ) {
462 $sk = $this->getSkin();
463 $limitText = $wgLang->formatNum( $limit );
465 $prevLink = wfMsgExt( 'prevn', array( 'escape', 'parsemag' ), $limitText );
467 $query['until'] = $first;
468 $prevLink = $sk->link(
475 $nextLink = wfMsgExt( 'nextn', array( 'escape', 'parsemag' ), $limitText );
477 $query['from'] = $last;
478 $nextLink = $sk->link(
486 return "($prevLink) ($nextLink)";
490 * What to do if the category table conflicts with the number of results
491 * returned? This function says what. It works the same whether the
492 * things being counted are articles, subcategories, or files.
494 * Note for grepping: uses the messages category-article-count,
495 * category-article-count-limited, category-subcat-count,
496 * category-subcat-count-limited, category-file-count,
497 * category-file-count-limited.
499 * @param $rescnt Int: The number of items returned by our database query.
500 * @param $dbcnt Int: The number of items according to the category table.
501 * @param $type String: 'subcat', 'article', or 'file'
502 * @return String: A message giving the number of items, to output to HTML.
504 private function getCountMessage( $rescnt, $dbcnt, $type ) {
506 # There are three cases:
507 # 1) The category table figure seems sane. It might be wrong, but
508 # we can't do anything about it if we don't recalculate it on ev-
510 # 2) The category table figure isn't sane, like it's smaller than the
511 # number of actual results, *but* the number of results is less
512 # than $this->limit and there's no offset. In this case we still
513 # know the right figure.
514 # 3) We have no idea.
515 $totalrescnt = count( $this->articles
) +
count( $this->children
) +
516 ($this->showGallery ?
$this->gallery
->count() : 0);
517 if($dbcnt == $rescnt ||
(($totalrescnt == $this->limit ||
$this->from
518 ||
$this->until
) && $dbcnt > $rescnt)){
519 # Case 1: seems sane.
521 } elseif($totalrescnt < $this->limit
&& !$this->from
&& !$this->until
){
522 # Case 2: not sane, but salvageable. Use the number of results.
523 # Since there are fewer than 200, we can also take this opportunity
524 # to refresh the incorrect category table entry -- which should be
525 # quick due to the small number of entries.
527 $this->cat
->refreshCounts();
529 # Case 3: hopeless. Don't give a total count at all.
530 return wfMsgExt("category-$type-count-limited", 'parse',
531 $wgLang->formatNum( $rescnt ) );
533 return wfMsgExt( "category-$type-count", 'parse', $wgLang->formatNum( $rescnt ),
534 $wgLang->formatNum( $totalcnt ) );