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))) return;
25 if ( NS_CATEGORY
== $this->mTitle
->getNamespace() ) {
26 $this->openShowCategory();
31 # If the article we've just shown is in the "Image" namespace,
32 # follow it with the history list and link list for the image
35 if ( NS_CATEGORY
== $this->mTitle
->getNamespace() ) {
36 $this->closeShowCategory();
40 function openShowCategory() {
44 function closeShowCategory() {
45 global $wgOut, $wgRequest;
46 $from = $wgRequest->getVal( 'from' );
47 $until = $wgRequest->getVal( 'until' );
49 $viewer = new CategoryViewer( $this->mTitle
, $from, $until );
50 $wgOut->addHTML( $viewer->getHTML() );
54 class CategoryViewer
{
55 var $title, $limit, $from, $until,
56 $articles, $articles_start_char,
57 $children, $children_start_char,
58 $showGallery, $gallery,
60 /** Category object for this page */
63 function __construct( $title, $from = '', $until = '' ) {
64 global $wgCategoryPagingLimit;
65 $this->title
= $title;
67 $this->until
= $until;
68 $this->limit
= $wgCategoryPagingLimit;
69 $this->cat
= Category
::newFromName( $title->getDBKey() );
73 * Format the category data list.
75 * @param string $from -- return only sort keys from this item on
76 * @param string $until -- don't return keys after this point.
77 * @return string HTML output
81 global $wgOut, $wgCategoryMagicGallery, $wgCategoryPagingLimit;
82 wfProfileIn( __METHOD__
);
84 $this->showGallery
= $wgCategoryMagicGallery && !$wgOut->mNoGallery
;
86 $this->clearCategoryState();
87 $this->doCategoryQuery();
88 $this->finaliseCategoryState();
90 $r = $this->getCategoryTop() .
91 $this->getSubcategorySection() .
92 $this->getPagesSection() .
93 $this->getImageSection() .
94 $this->getCategoryBottom();
96 // Give a proper message if category is empty
98 $r = wfMsgExt( 'category-empty', array( 'parse' ) );
101 wfProfileOut( __METHOD__
);
105 function clearCategoryState() {
106 $this->articles
= array();
107 $this->articles_start_char
= array();
108 $this->children
= array();
109 $this->children_start_char
= array();
110 if( $this->showGallery
) {
111 $this->gallery
= new ImageGallery();
112 $this->gallery
->setHideBadImages();
117 if ( !$this->skin
) {
119 $this->skin
= $wgUser->getSkin();
125 * Add a subcategory to the internal lists, using a Category object
127 function addSubcategoryObject( $cat, $sortkey, $pageLength ) {
128 $title = $cat->getTitle();
129 $this->addSubcategory( $title, $sortkey, $pageLength );
133 * Add a subcategory to the internal lists, using a title object
134 * @deprectated kept for compatibility, please use addSubcategoryObject instead
136 function addSubcategory( $title, $sortkey, $pageLength ) {
138 // Subcategory; strip the 'Category' namespace from the link text.
139 $this->children
[] = $this->getSkin()->makeKnownLinkObj(
140 $title, $wgContLang->convertHtml( $title->getText() ) );
142 $this->children_start_char
[] = $this->getSubcategorySortChar( $title, $sortkey );
146 * Get the character to be used for sorting subcategories.
147 * If there's a link from Category:A to Category:B, the sortkey of the resulting
148 * entry in the categorylinks table is Category:A, not A, which it SHOULD be.
149 * Workaround: If sortkey == "Category:".$title, than use $title for sorting,
150 * else use sortkey...
152 function getSubcategorySortChar( $title, $sortkey ) {
155 if( $title->getPrefixedText() == $sortkey ) {
156 $firstChar = $wgContLang->firstChar( $title->getDBkey() );
158 $firstChar = $wgContLang->firstChar( $sortkey );
161 return $wgContLang->convert( $firstChar );
165 * Add a page in the image namespace
167 function addImage( Title
$title, $sortkey, $pageLength, $isRedirect = false ) {
168 if ( $this->showGallery
) {
170 $this->gallery
->insert( $title );
172 $this->gallery
->add( $title );
175 $this->addPage( $title, $sortkey, $pageLength, $isRedirect );
180 * Add a miscellaneous page
182 function addPage( $title, $sortkey, $pageLength, $isRedirect = false ) {
184 $this->articles
[] = $isRedirect
185 ?
'<span class="redirect-in-category">' . $this->getSkin()->makeKnownLinkObj( $title ) . '</span>'
186 : $this->getSkin()->makeSizeLinkObj( $pageLength, $title );
187 $this->articles_start_char
[] = $wgContLang->convert( $wgContLang->firstChar( $sortkey ) );
190 function finaliseCategoryState() {
192 $this->children
= array_reverse( $this->children
);
193 $this->children_start_char
= array_reverse( $this->children_start_char
);
194 $this->articles
= array_reverse( $this->articles
);
195 $this->articles_start_char
= array_reverse( $this->articles_start_char
);
199 function doCategoryQuery() {
200 $dbr = wfGetDB( DB_SLAVE
);
201 if( $this->from
!= '' ) {
202 $pageCondition = 'cl_sortkey >= ' . $dbr->addQuotes( $this->from
);
204 } elseif( $this->until
!= '' ) {
205 $pageCondition = 'cl_sortkey < ' . $dbr->addQuotes( $this->until
);
208 $pageCondition = '1 = 1';
212 array( 'page', 'categorylinks', 'category' ),
213 array( 'page_title', 'page_namespace', 'page_len', 'page_is_redirect', 'cl_sortkey',
214 'cat_id', 'cat_title', 'cat_subcats', 'cat_pages', 'cat_files' ),
215 array( $pageCondition,
216 'cl_to' => $this->title
->getDBkey() ),
218 array( 'ORDER BY' => $this->flip ?
'cl_sortkey DESC' : 'cl_sortkey',
219 'USE INDEX' => array( 'categorylinks' => 'cl_sortkey' ),
220 'LIMIT' => $this->limit +
1 ),
221 array( 'categorylinks' => array( 'INNER JOIN', 'cl_from = page_id' ),
222 'category' => array( 'LEFT JOIN', 'cat_title = page_title AND page_namespace = ' . NS_CATEGORY
) ) );
225 $this->nextPage
= null;
226 while( $x = $dbr->fetchObject ( $res ) ) {
227 if( ++
$count > $this->limit
) {
228 // We've reached the one extra which shows that there are
229 // additional pages to be had. Stop here...
230 $this->nextPage
= $x->cl_sortkey
;
234 $title = Title
::makeTitle( $x->page_namespace
, $x->page_title
);
236 if( $title->getNamespace() == NS_CATEGORY
) {
237 $cat = Category
::newFromRow( $x, $title );
238 $this->addSubcategoryObject( $cat, $x->cl_sortkey
, $x->page_len
);
239 } elseif( $this->showGallery
&& $title->getNamespace() == NS_IMAGE
) {
240 $this->addImage( $title, $x->cl_sortkey
, $x->page_len
, $x->page_is_redirect
);
242 $this->addPage( $title, $x->cl_sortkey
, $x->page_len
, $x->page_is_redirect
);
245 $dbr->freeResult( $res );
248 function getCategoryTop() {
250 if( $this->until
!= '' ) {
251 $r .= $this->pagingLinks( $this->title
, $this->nextPage
, $this->until
, $this->limit
);
252 } elseif( $this->nextPage
!= '' ||
$this->from
!= '' ) {
253 $r .= $this->pagingLinks( $this->title
, $this->from
, $this->nextPage
, $this->limit
);
257 : "<br style=\"clear:both;\"/>\n" . $r;
260 function getSubcategorySection() {
261 # Don't show subcategories section if there are none.
263 $rescnt = count( $this->children
);
264 $dbcnt = $this->cat
->getSubcatCount();
265 $countmsg = $this->getCountMessage( $rescnt, $dbcnt, 'subcat' );
267 # Showing subcategories
268 $r .= "<div id=\"mw-subcategories\">\n";
269 $r .= '<h2>' . wfMsg( 'subcategories' ) . "</h2>\n";
271 $r .= $this->formatList( $this->children
, $this->children_start_char
);
277 function getPagesSection() {
278 $ti = htmlspecialchars( $this->title
->getText() );
279 # Don't show articles section if there are none.
282 # FIXME, here and in the other two sections: we don't need to bother
283 # with this rigamarole if the entire category contents fit on one page
284 # and have already been retrieved. We can just use $rescnt in that
285 # case and save a query and some logic.
286 $dbcnt = $this->cat
->getPageCount() - $this->cat
->getSubcatCount()
287 - $this->cat
->getFileCount();
288 $rescnt = count( $this->articles
);
289 $countmsg = $this->getCountMessage( $rescnt, $dbcnt, 'article' );
292 $r = "<div id=\"mw-pages\">\n";
293 $r .= '<h2>' . wfMsg( 'category_header', $ti ) . "</h2>\n";
295 $r .= $this->formatList( $this->articles
, $this->articles_start_char
);
301 function getImageSection() {
302 if( $this->showGallery
&& ! $this->gallery
->isEmpty() ) {
303 $dbcnt = $this->cat
->getFileCount();
304 $rescnt = $this->gallery
->count();
305 $countmsg = $this->getCountMessage( $rescnt, $dbcnt, 'file' );
307 return "<div id=\"mw-category-media\">\n" .
308 '<h2>' . wfMsg( 'category-media-header', htmlspecialchars($this->title
->getText()) ) . "</h2>\n" .
309 $countmsg . $this->gallery
->toHTML() . "\n</div>";
315 function getCategoryBottom() {
316 if( $this->until
!= '' ) {
317 return $this->pagingLinks( $this->title
, $this->nextPage
, $this->until
, $this->limit
);
318 } elseif( $this->nextPage
!= '' ||
$this->from
!= '' ) {
319 return $this->pagingLinks( $this->title
, $this->from
, $this->nextPage
, $this->limit
);
326 * Format a list of articles chunked by letter, either as a
327 * bullet list or a columnar format, depending on the length.
329 * @param array $articles
330 * @param array $articles_start_char
335 function formatList( $articles, $articles_start_char, $cutoff = 6 ) {
336 if ( count ( $articles ) > $cutoff ) {
337 return $this->columnList( $articles, $articles_start_char );
338 } elseif ( count($articles) > 0) {
339 // for short lists of articles in categories.
340 return $this->shortList( $articles, $articles_start_char );
346 * Format a list of articles chunked by letter in a three-column
347 * list, ordered vertically.
349 * @param array $articles
350 * @param array $articles_start_char
354 function columnList( $articles, $articles_start_char ) {
355 // divide list into three equal chunks
356 $chunk = (int) (count ( $articles ) / 3);
358 // get and display header
359 $r = '<table width="100%"><tr valign="top">';
361 $prev_start_char = 'none';
363 // loop through the chunks
364 for($startChunk = 0, $endChunk = $chunk, $chunkIndex = 0;
366 $chunkIndex++
, $startChunk = $endChunk, $endChunk +
= $chunk +
1)
371 // output all articles in category
372 for ($index = $startChunk ;
373 $index < $endChunk && $index < count($articles);
376 // check for change of starting letter or begining of chunk
377 if ( ($index == $startChunk) ||
378 ($articles_start_char[$index] != $articles_start_char[$index - 1]) )
382 $atColumnTop = false;
387 if ( $articles_start_char[$index] == $prev_start_char )
388 $cont_msg = ' ' . wfMsgHtml( 'listingcontinuesabbrev' );
389 $r .= "<h3>" . htmlspecialchars( $articles_start_char[$index] ) . "$cont_msg</h3>\n<ul>";
390 $prev_start_char = $articles_start_char[$index];
393 $r .= "<li>{$articles[$index]}</li>";
395 if( !$atColumnTop ) {
402 $r .= '</tr></table>';
407 * Format a list of articles chunked by letter in a bullet list.
408 * @param array $articles
409 * @param array $articles_start_char
413 function shortList( $articles, $articles_start_char ) {
414 $r = '<h3>' . htmlspecialchars( $articles_start_char[0] ) . "</h3>\n";
415 $r .= '<ul><li>'.$articles[0].'</li>';
416 for ($index = 1; $index < count($articles); $index++
)
418 if ($articles_start_char[$index] != $articles_start_char[$index - 1])
420 $r .= "</ul><h3>" . htmlspecialchars( $articles_start_char[$index] ) . "</h3>\n<ul>";
423 $r .= "<li>{$articles[$index]}</li>";
430 * @param Title $title
431 * @param string $first
432 * @param string $last
434 * @param array $query - additional query options to pass
438 function pagingLinks( $title, $first, $last, $limit, $query = array() ) {
440 $sk = $this->getSkin();
441 $limitText = $wgLang->formatNum( $limit );
443 $prevLink = htmlspecialchars( wfMsg( 'prevn', $limitText ) );
445 $prevLink = $sk->makeLinkObj( $title, $prevLink,
446 wfArrayToCGI( $query +
array( 'until' => $first ) ) );
448 $nextLink = htmlspecialchars( wfMsg( 'nextn', $limitText ) );
450 $nextLink = $sk->makeLinkObj( $title, $nextLink,
451 wfArrayToCGI( $query +
array( 'from' => $last ) ) );
454 return "($prevLink) ($nextLink)";
458 * What to do if the category table conflicts with the number of results
459 * returned? This function says what. It works the same whether the
460 * things being counted are articles, subcategories, or files.
462 * Note for grepping: uses the messages category-article-count,
463 * category-article-count-limited, category-subcat-count,
464 * category-subcat-count-limited, category-file-count,
465 * category-file-count-limited.
467 * @param int $rescnt The number of items returned by our database query.
468 * @param int $dbcnt The number of items according to the category table.
469 * @param string $type 'subcat', 'article', or 'file'
470 * @return string A message giving the number of items, to output to HTML.
472 private function getCountMessage( $rescnt, $dbcnt, $type ) {
474 # There are three cases:
475 # 1) The category table figure seems sane. It might be wrong, but
476 # we can't do anything about it if we don't recalculate it on ev-
478 # 2) The category table figure isn't sane, like it's smaller than the
479 # number of actual results, *but* the number of results is less
480 # than $this->limit and there's no offset. In this case we still
481 # know the right figure.
482 # 3) We have no idea.
483 $totalrescnt = count( $this->articles
) +
count( $this->children
) +
484 ($this->showGallery ?
$this->gallery
->count() : 0);
485 if($dbcnt == $rescnt ||
(($totalrescnt == $this->limit ||
$this->from
486 ||
$this->until
) && $dbcnt > $rescnt)){
487 # Case 1: seems sane.
489 } elseif($totalrescnt < $this->limit
&& !$this->from
&& !$this->until
){
490 # Case 2: not sane, but salvageable. Use the number of results.
491 # Since there are fewer than 200, we can also take this opportunity
492 # to refresh the incorrect category table entry -- which should be
493 # quick due to the small number of entries.
495 $this->cat
->refreshCounts();
497 # Case 3: hopeless. Don't give a total count at all.
498 return wfMsgExt("category-$type-count-limited", 'parse',
499 $wgLang->formatNum( $rescnt ) );
501 return wfMsgExt( "category-$type-count", 'parse', $wgLang->formatNum( $rescnt ),
502 $wgLang->formatNum( $totalcnt ) );