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();
41 * This page should not be cached if 'from' or 'until' has been used
44 function isFileCacheable() {
47 return ( ! Article
::isFileCacheable()
48 ||
$wgRequest->getVal( 'from' )
49 ||
$wgRequest->getVal( 'until' )
53 function openShowCategory() {
57 function closeShowCategory() {
58 global $wgOut, $wgRequest;
59 $from = $wgRequest->getVal( 'from' );
60 $until = $wgRequest->getVal( 'until' );
62 $viewer = new CategoryViewer( $this->mTitle
, $from, $until );
63 $wgOut->addHTML( $viewer->getHTML() );
67 class CategoryViewer
{
68 var $title, $limit, $from, $until,
69 $articles, $articles_start_char,
70 $children, $children_start_char,
71 $showGallery, $gallery,
73 /** Category object for this page */
76 function __construct( $title, $from = '', $until = '' ) {
77 global $wgCategoryPagingLimit;
78 $this->title
= $title;
80 $this->until
= $until;
81 $this->limit
= $wgCategoryPagingLimit;
82 $this->cat
= Category
::newFromName( $title->getDBKey() );
86 * Format the category data list.
88 * @param string $from -- return only sort keys from this item on
89 * @param string $until -- don't return keys after this point.
90 * @return string HTML output
94 global $wgOut, $wgCategoryMagicGallery, $wgCategoryPagingLimit;
95 wfProfileIn( __METHOD__
);
97 $this->showGallery
= $wgCategoryMagicGallery && !$wgOut->mNoGallery
;
99 $this->clearCategoryState();
100 $this->doCategoryQuery();
101 $this->finaliseCategoryState();
103 $r = $this->getCategoryTop() .
104 $this->getSubcategorySection() .
105 $this->getPagesSection() .
106 $this->getImageSection() .
107 $this->getCategoryBottom();
109 // Give a proper message if category is empty
111 $r = wfMsgExt( 'category-empty', array( 'parse' ) );
114 wfProfileOut( __METHOD__
);
118 function clearCategoryState() {
119 $this->articles
= array();
120 $this->articles_start_char
= array();
121 $this->children
= array();
122 $this->children_start_char
= array();
123 if( $this->showGallery
) {
124 $this->gallery
= new ImageGallery();
125 $this->gallery
->setHideBadImages();
130 if ( !$this->skin
) {
132 $this->skin
= $wgUser->getSkin();
138 * Add a subcategory to the internal lists
140 function addSubcategory( $title, $sortkey, $pageLength ) {
142 // Subcategory; strip the 'Category' namespace from the link text.
143 $this->children
[] = $this->getSkin()->makeKnownLinkObj(
144 $title, $wgContLang->convertHtml( $title->getText() ) );
146 $this->children_start_char
[] = $this->getSubcategorySortChar( $title, $sortkey );
150 * Get the character to be used for sorting subcategories.
151 * If there's a link from Category:A to Category:B, the sortkey of the resulting
152 * entry in the categorylinks table is Category:A, not A, which it SHOULD be.
153 * Workaround: If sortkey == "Category:".$title, than use $title for sorting,
154 * else use sortkey...
156 function getSubcategorySortChar( $title, $sortkey ) {
159 if( $title->getPrefixedText() == $sortkey ) {
160 $firstChar = $wgContLang->firstChar( $title->getDBkey() );
162 $firstChar = $wgContLang->firstChar( $sortkey );
165 return $wgContLang->convert( $firstChar );
169 * Add a page in the image namespace
171 function addImage( Title
$title, $sortkey, $pageLength, $isRedirect = false ) {
172 if ( $this->showGallery
) {
173 $image = new Image( $title );
175 $this->gallery
->insert( $image );
177 $this->gallery
->add( $image );
180 $this->addPage( $title, $sortkey, $pageLength, $isRedirect );
185 * Add a miscellaneous page
187 function addPage( $title, $sortkey, $pageLength, $isRedirect = false ) {
189 $this->articles
[] = $isRedirect
190 ?
'<span class="redirect-in-category">' . $this->getSkin()->makeKnownLinkObj( $title ) . '</span>'
191 : $this->getSkin()->makeSizeLinkObj( $pageLength, $title );
192 $this->articles_start_char
[] = $wgContLang->convert( $wgContLang->firstChar( $sortkey ) );
195 function finaliseCategoryState() {
197 $this->children
= array_reverse( $this->children
);
198 $this->children_start_char
= array_reverse( $this->children_start_char
);
199 $this->articles
= array_reverse( $this->articles
);
200 $this->articles_start_char
= array_reverse( $this->articles_start_char
);
204 function doCategoryQuery() {
205 $dbr = wfGetDB( DB_SLAVE
);
206 if( $this->from
!= '' ) {
207 $pageCondition = 'cl_sortkey >= ' . $dbr->addQuotes( $this->from
);
209 } elseif( $this->until
!= '' ) {
210 $pageCondition = 'cl_sortkey < ' . $dbr->addQuotes( $this->until
);
213 $pageCondition = '1 = 1';
217 array( 'page', 'categorylinks' ),
218 array( 'page_title', 'page_namespace', 'page_len', 'page_is_redirect', 'cl_sortkey' ),
219 array( $pageCondition,
221 'cl_to' => $this->title
->getDBkey()),
222 #'page_is_redirect' => 0),
225 array( 'ORDER BY' => $this->flip ?
'cl_sortkey DESC' : 'cl_sortkey',
226 'USE INDEX' => 'cl_sortkey',
227 'LIMIT' => $this->limit +
1 ) );
230 $this->nextPage
= null;
231 while( $x = $dbr->fetchObject ( $res ) ) {
232 if( ++
$count > $this->limit
) {
233 // We've reached the one extra which shows that there are
234 // additional pages to be had. Stop here...
235 $this->nextPage
= $x->cl_sortkey
;
239 $title = Title
::makeTitle( $x->page_namespace
, $x->page_title
);
241 if( $title->getNamespace() == NS_CATEGORY
) {
242 $this->addSubcategory( $title, $x->cl_sortkey
, $x->page_len
);
243 } elseif( $this->showGallery
&& $title->getNamespace() == NS_IMAGE
) {
244 $this->addImage( $title, $x->cl_sortkey
, $x->page_len
, $x->page_is_redirect
);
246 $this->addPage( $title, $x->cl_sortkey
, $x->page_len
, $x->page_is_redirect
);
249 $dbr->freeResult( $res );
252 function getCategoryTop() {
254 if( $this->until
!= '' ) {
255 $r .= $this->pagingLinks( $this->title
, $this->nextPage
, $this->until
, $this->limit
);
256 } elseif( $this->nextPage
!= '' ||
$this->from
!= '' ) {
257 $r .= $this->pagingLinks( $this->title
, $this->from
, $this->nextPage
, $this->limit
);
261 : "<br style=\"clear:both;\"/>\n" . $r;
264 function getSubcategorySection() {
265 # Don't show subcategories section if there are none.
267 $rescnt = count( $this->children
);
268 $dbcnt = $this->cat
->getSubcatCount();
269 $countmsg = $this->getCountMessage( $rescnt, $dbcnt, 'subcat' );
271 # Showing subcategories
272 $r .= "<div id=\"mw-subcategories\">\n";
273 $r .= '<h2>' . wfMsg( 'subcategories' ) . "</h2>\n";
275 $r .= $this->formatList( $this->children
, $this->children_start_char
);
281 function getPagesSection() {
282 $ti = htmlspecialchars( $this->title
->getText() );
283 # Don't show articles section if there are none.
286 # FIXME, here and in the other two sections: we don't need to bother
287 # with this rigamarole if the entire category contents fit on one page
288 # and have already been retrieved. We can just use $rescnt in that
289 # case and save a query and some logic.
290 $dbcnt = $this->cat
->getPageCount() - $this->cat
->getSubcatCount()
291 - $this->cat
->getFileCount();
292 $rescnt = count( $this->articles
);
293 $countmsg = $this->getCountMessage( $rescnt, $dbcnt, 'article' );
296 $r = "<div id=\"mw-pages\">\n";
297 $r .= '<h2>' . wfMsg( 'category_header', $ti ) . "</h2>\n";
299 $r .= $this->formatList( $this->articles
, $this->articles_start_char
);
305 function getImageSection() {
306 if( $this->showGallery
&& ! $this->gallery
->isEmpty() ) {
307 $dbcnt = $this->cat
->getFileCount();
308 $rescnt = $this->gallery
->count();
309 $countmsg = $this->getCountMessage( $rescnt, $dbcnt, 'file' );
311 return "<div id=\"mw-category-media\">\n" .
312 '<h2>' . wfMsg( 'category-media-header', htmlspecialchars($this->title
->getText()) ) . "</h2>\n" .
313 $countmsg . $this->gallery
->toHTML() . "\n</div>";
319 function getCategoryBottom() {
320 if( $this->until
!= '' ) {
321 return $this->pagingLinks( $this->title
, $this->nextPage
, $this->until
, $this->limit
);
322 } elseif( $this->nextPage
!= '' ||
$this->from
!= '' ) {
323 return $this->pagingLinks( $this->title
, $this->from
, $this->nextPage
, $this->limit
);
330 * Format a list of articles chunked by letter, either as a
331 * bullet list or a columnar format, depending on the length.
333 * @param array $articles
334 * @param array $articles_start_char
339 function formatList( $articles, $articles_start_char, $cutoff = 6 ) {
340 if ( count ( $articles ) > $cutoff ) {
341 return $this->columnList( $articles, $articles_start_char );
342 } elseif ( count($articles) > 0) {
343 // for short lists of articles in categories.
344 return $this->shortList( $articles, $articles_start_char );
350 * Format a list of articles chunked by letter in a three-column
351 * list, ordered vertically.
353 * @param array $articles
354 * @param array $articles_start_char
358 function columnList( $articles, $articles_start_char ) {
359 // divide list into three equal chunks
360 $chunk = (int) (count ( $articles ) / 3);
362 // get and display header
363 $r = '<table width="100%"><tr valign="top">';
365 $prev_start_char = 'none';
367 // loop through the chunks
368 for($startChunk = 0, $endChunk = $chunk, $chunkIndex = 0;
370 $chunkIndex++
, $startChunk = $endChunk, $endChunk +
= $chunk +
1)
375 // output all articles in category
376 for ($index = $startChunk ;
377 $index < $endChunk && $index < count($articles);
380 // check for change of starting letter or begining of chunk
381 if ( ($index == $startChunk) ||
382 ($articles_start_char[$index] != $articles_start_char[$index - 1]) )
386 $atColumnTop = false;
391 if ( $articles_start_char[$index] == $prev_start_char )
392 $cont_msg = ' ' . wfMsgHtml( 'listingcontinuesabbrev' );
393 $r .= "<h3>" . htmlspecialchars( $articles_start_char[$index] ) . "$cont_msg</h3>\n<ul>";
394 $prev_start_char = $articles_start_char[$index];
397 $r .= "<li>{$articles[$index]}</li>";
399 if( !$atColumnTop ) {
406 $r .= '</tr></table>';
411 * Format a list of articles chunked by letter in a bullet list.
412 * @param array $articles
413 * @param array $articles_start_char
417 function shortList( $articles, $articles_start_char ) {
418 $r = '<h3>' . htmlspecialchars( $articles_start_char[0] ) . "</h3>\n";
419 $r .= '<ul><li>'.$articles[0].'</li>';
420 for ($index = 1; $index < count($articles); $index++
)
422 if ($articles_start_char[$index] != $articles_start_char[$index - 1])
424 $r .= "</ul><h3>" . htmlspecialchars( $articles_start_char[$index] ) . "</h3>\n<ul>";
427 $r .= "<li>{$articles[$index]}</li>";
434 * @param Title $title
435 * @param string $first
436 * @param string $last
438 * @param array $query - additional query options to pass
442 function pagingLinks( $title, $first, $last, $limit, $query = array() ) {
444 $sk = $this->getSkin();
445 $limitText = $wgLang->formatNum( $limit );
447 $prevLink = htmlspecialchars( wfMsg( 'prevn', $limitText ) );
449 $prevLink = $sk->makeLinkObj( $title, $prevLink,
450 wfArrayToCGI( $query +
array( 'until' => $first ) ) );
452 $nextLink = htmlspecialchars( wfMsg( 'nextn', $limitText ) );
454 $nextLink = $sk->makeLinkObj( $title, $nextLink,
455 wfArrayToCGI( $query +
array( 'from' => $last ) ) );
458 return "($prevLink) ($nextLink)";
462 * What to do if the category table conflicts with the number of results
463 * returned? This function says what. It works the same whether the
464 * things being counted are articles, subcategories, or files.
466 * Note for grepping: uses the messages category-article-count,
467 * category-article-count-limited, category-subcat-count,
468 * category-subcat-count-limited, category-file-count,
469 * category-file-count-limited.
471 * @param int $rescnt The number of items returned by our database query.
472 * @param int $dbcnt The number of items according to the category table.
473 * @param string $type 'subcat', 'article', or 'file'
474 * @return string A message giving the number of items, to output to HTML.
476 private function getCountMessage( $rescnt, $dbcnt, $type ) {
478 # There are three cases:
479 # 1) The category table figure seems sane. It might be wrong, but
480 # we can't do anything about it if we don't recalculate it on ev-
482 # 2) The category table figure isn't sane, like it's smaller than the
483 # number of actual results, *but* the number of results is less
484 # than $this->limit and there's no offset. In this case we still
485 # know the right figure.
486 # 3) We have no idea.
487 $totalrescnt = count( $this->articles
) +
count( $this->children
) +
488 ($this->showGallery ?
$this->gallery
->count() : 0);
489 if($dbcnt == $rescnt ||
(($totalrescnt == $this->limit ||
$this->from
490 ||
$this->until
) && $dbcnt > $rescnt)){
491 # Case 1: seems sane.
493 } elseif($totalrescnt < $this->limit
&& !$this->from
&& !$this->until
){
494 # Case 2: not sane, but salvageable.
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', $rescnt,
502 $wgLang->formatNum( $totalcnt ) );