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 * @return string HTML output
79 global $wgOut, $wgCategoryMagicGallery, $wgCategoryPagingLimit;
80 wfProfileIn( __METHOD__
);
82 $this->showGallery
= $wgCategoryMagicGallery && !$wgOut->mNoGallery
;
84 $this->clearCategoryState();
85 $this->doCategoryQuery();
86 $this->finaliseCategoryState();
88 $r = $this->getCategoryTop() .
89 $this->getSubcategorySection() .
90 $this->getPagesSection() .
91 $this->getImageSection() .
92 $this->getCategoryBottom();
94 // Give a proper message if category is empty
96 $r = wfMsgExt( 'category-empty', array( 'parse' ) );
99 wfProfileOut( __METHOD__
);
103 function clearCategoryState() {
104 $this->articles
= array();
105 $this->articles_start_char
= array();
106 $this->children
= array();
107 $this->children_start_char
= array();
108 if( $this->showGallery
) {
109 $this->gallery
= new ImageGallery();
110 $this->gallery
->setHideBadImages();
115 if ( !$this->skin
) {
117 $this->skin
= $wgUser->getSkin();
123 * Add a subcategory to the internal lists, using a Category object
125 function addSubcategoryObject( $cat, $sortkey, $pageLength ) {
126 $title = $cat->getTitle();
127 $this->addSubcategory( $title, $sortkey, $pageLength );
131 * Add a subcategory to the internal lists, using a title object
132 * @deprecated kept for compatibility, please use addSubcategoryObject instead
134 function addSubcategory( $title, $sortkey, $pageLength ) {
136 // Subcategory; strip the 'Category' namespace from the link text.
137 $this->children
[] = $this->getSkin()->makeKnownLinkObj(
138 $title, $wgContLang->convertHtml( $title->getText() ) );
140 $this->children_start_char
[] = $this->getSubcategorySortChar( $title, $sortkey );
144 * Get the character to be used for sorting subcategories.
145 * If there's a link from Category:A to Category:B, the sortkey of the resulting
146 * entry in the categorylinks table is Category:A, not A, which it SHOULD be.
147 * Workaround: If sortkey == "Category:".$title, than use $title for sorting,
148 * else use sortkey...
150 function getSubcategorySortChar( $title, $sortkey ) {
153 if( $title->getPrefixedText() == $sortkey ) {
154 $firstChar = $wgContLang->firstChar( $title->getDBkey() );
156 $firstChar = $wgContLang->firstChar( $sortkey );
159 return $wgContLang->convert( $firstChar );
163 * Add a page in the image namespace
165 function addImage( Title
$title, $sortkey, $pageLength, $isRedirect = false ) {
166 if ( $this->showGallery
) {
168 $this->gallery
->insert( $title );
170 $this->gallery
->add( $title );
173 $this->addPage( $title, $sortkey, $pageLength, $isRedirect );
178 * Add a miscellaneous page
180 function addPage( $title, $sortkey, $pageLength, $isRedirect = false ) {
182 $this->articles
[] = $isRedirect
183 ?
'<span class="redirect-in-category">' . $this->getSkin()->makeKnownLinkObj( $title ) . '</span>'
184 : $this->getSkin()->makeSizeLinkObj( $pageLength, $title );
185 $this->articles_start_char
[] = $wgContLang->convert( $wgContLang->firstChar( $sortkey ) );
188 function finaliseCategoryState() {
190 $this->children
= array_reverse( $this->children
);
191 $this->children_start_char
= array_reverse( $this->children_start_char
);
192 $this->articles
= array_reverse( $this->articles
);
193 $this->articles_start_char
= array_reverse( $this->articles_start_char
);
197 function doCategoryQuery() {
198 $dbr = wfGetDB( DB_SLAVE
);
199 if( $this->from
!= '' ) {
200 $pageCondition = 'cl_sortkey >= ' . $dbr->addQuotes( $this->from
);
202 } elseif( $this->until
!= '' ) {
203 $pageCondition = 'cl_sortkey < ' . $dbr->addQuotes( $this->until
);
206 $pageCondition = '1 = 1';
210 array( 'page', 'categorylinks', 'category' ),
211 array( 'page_title', 'page_namespace', 'page_len', 'page_is_redirect', 'cl_sortkey',
212 'cat_id', 'cat_title', 'cat_subcats', 'cat_pages', 'cat_files' ),
213 array( $pageCondition,
214 'cl_to' => $this->title
->getDBkey() ),
216 array( 'ORDER BY' => $this->flip ?
'cl_sortkey DESC' : 'cl_sortkey',
217 'USE INDEX' => array( 'categorylinks' => 'cl_sortkey' ),
218 'LIMIT' => $this->limit +
1 ),
219 array( 'categorylinks' => array( 'INNER JOIN', 'cl_from = page_id' ),
220 'category' => array( 'LEFT JOIN', 'cat_title = page_title AND page_namespace = ' . NS_CATEGORY
) ) );
223 $this->nextPage
= null;
224 while( $x = $dbr->fetchObject ( $res ) ) {
225 if( ++
$count > $this->limit
) {
226 // We've reached the one extra which shows that there are
227 // additional pages to be had. Stop here...
228 $this->nextPage
= $x->cl_sortkey
;
232 $title = Title
::makeTitle( $x->page_namespace
, $x->page_title
);
234 if( $title->getNamespace() == NS_CATEGORY
) {
235 $cat = Category
::newFromRow( $x, $title );
236 $this->addSubcategoryObject( $cat, $x->cl_sortkey
, $x->page_len
);
237 } elseif( $this->showGallery
&& $title->getNamespace() == NS_FILE
) {
238 $this->addImage( $title, $x->cl_sortkey
, $x->page_len
, $x->page_is_redirect
);
240 $this->addPage( $title, $x->cl_sortkey
, $x->page_len
, $x->page_is_redirect
);
243 $dbr->freeResult( $res );
246 function getCategoryTop() {
248 if( $this->until
!= '' ) {
249 $r .= $this->pagingLinks( $this->title
, $this->nextPage
, $this->until
, $this->limit
);
250 } elseif( $this->nextPage
!= '' ||
$this->from
!= '' ) {
251 $r .= $this->pagingLinks( $this->title
, $this->from
, $this->nextPage
, $this->limit
);
255 : "<br style=\"clear:both;\"/>\n" . $r;
258 function getSubcategorySection() {
259 # Don't show subcategories section if there are none.
261 $rescnt = count( $this->children
);
262 $dbcnt = $this->cat
->getSubcatCount();
263 $countmsg = $this->getCountMessage( $rescnt, $dbcnt, 'subcat' );
265 # Showing subcategories
266 $r .= "<div id=\"mw-subcategories\">\n";
267 $r .= '<h2>' . wfMsg( 'subcategories' ) . "</h2>\n";
269 $r .= $this->formatList( $this->children
, $this->children_start_char
);
275 function getPagesSection() {
276 $ti = htmlspecialchars( $this->title
->getText() );
277 # Don't show articles section if there are none.
280 # FIXME, here and in the other two sections: we don't need to bother
281 # with this rigamarole if the entire category contents fit on one page
282 # and have already been retrieved. We can just use $rescnt in that
283 # case and save a query and some logic.
284 $dbcnt = $this->cat
->getPageCount() - $this->cat
->getSubcatCount()
285 - $this->cat
->getFileCount();
286 $rescnt = count( $this->articles
);
287 $countmsg = $this->getCountMessage( $rescnt, $dbcnt, 'article' );
290 $r = "<div id=\"mw-pages\">\n";
291 $r .= '<h2>' . wfMsg( 'category_header', $ti ) . "</h2>\n";
293 $r .= $this->formatList( $this->articles
, $this->articles_start_char
);
299 function getImageSection() {
300 if( $this->showGallery
&& ! $this->gallery
->isEmpty() ) {
301 $dbcnt = $this->cat
->getFileCount();
302 $rescnt = $this->gallery
->count();
303 $countmsg = $this->getCountMessage( $rescnt, $dbcnt, 'file' );
305 return "<div id=\"mw-category-media\">\n" .
306 '<h2>' . wfMsg( 'category-media-header', htmlspecialchars($this->title
->getText()) ) . "</h2>\n" .
307 $countmsg . $this->gallery
->toHTML() . "\n</div>";
313 function getCategoryBottom() {
314 if( $this->until
!= '' ) {
315 return $this->pagingLinks( $this->title
, $this->nextPage
, $this->until
, $this->limit
);
316 } elseif( $this->nextPage
!= '' ||
$this->from
!= '' ) {
317 return $this->pagingLinks( $this->title
, $this->from
, $this->nextPage
, $this->limit
);
324 * Format a list of articles chunked by letter, either as a
325 * bullet list or a columnar format, depending on the length.
327 * @param $articles Array
328 * @param $articles_start_char Array
333 function formatList( $articles, $articles_start_char, $cutoff = 6 ) {
334 if ( count ( $articles ) > $cutoff ) {
335 return $this->columnList( $articles, $articles_start_char );
336 } elseif ( count($articles) > 0) {
337 // for short lists of articles in categories.
338 return $this->shortList( $articles, $articles_start_char );
344 * Format a list of articles chunked by letter in a three-column
345 * list, ordered vertically.
347 * @param $articles Array
348 * @param $articles_start_char Array
352 function columnList( $articles, $articles_start_char ) {
353 // divide list into three equal chunks
354 $chunk = (int) (count ( $articles ) / 3);
356 // get and display header
357 $r = '<table width="100%"><tr valign="top">';
359 $prev_start_char = 'none';
361 // loop through the chunks
362 for($startChunk = 0, $endChunk = $chunk, $chunkIndex = 0;
364 $chunkIndex++
, $startChunk = $endChunk, $endChunk +
= $chunk +
1)
369 // output all articles in category
370 for ($index = $startChunk ;
371 $index < $endChunk && $index < count($articles);
374 // check for change of starting letter or begining of chunk
375 if ( ($index == $startChunk) ||
376 ($articles_start_char[$index] != $articles_start_char[$index - 1]) )
380 $atColumnTop = false;
385 if ( $articles_start_char[$index] == $prev_start_char )
386 $cont_msg = ' ' . wfMsgHtml( 'listingcontinuesabbrev' );
387 $r .= "<h3>" . htmlspecialchars( $articles_start_char[$index] ) . "$cont_msg</h3>\n<ul>";
388 $prev_start_char = $articles_start_char[$index];
391 $r .= "<li>{$articles[$index]}</li>";
393 if( !$atColumnTop ) {
400 $r .= '</tr></table>';
405 * Format a list of articles chunked by letter in a bullet list.
406 * @param $articles Array
407 * @param $articles_start_char Array
411 function shortList( $articles, $articles_start_char ) {
412 $r = '<h3>' . htmlspecialchars( $articles_start_char[0] ) . "</h3>\n";
413 $r .= '<ul><li>'.$articles[0].'</li>';
414 for ($index = 1; $index < count($articles); $index++
)
416 if ($articles_start_char[$index] != $articles_start_char[$index - 1])
418 $r .= "</ul><h3>" . htmlspecialchars( $articles_start_char[$index] ) . "</h3>\n<ul>";
421 $r .= "<li>{$articles[$index]}</li>";
428 * @param $title Title object
429 * @param $first String
430 * @param $last String
432 * @param $query Array: additional query options to pass
436 function pagingLinks( $title, $first, $last, $limit, $query = array() ) {
438 $sk = $this->getSkin();
439 $limitText = $wgLang->formatNum( $limit );
441 $prevLink = htmlspecialchars( wfMsg( 'prevn', $limitText ) );
443 $prevLink = $sk->makeLinkObj( $title, $prevLink,
444 wfArrayToCGI( $query +
array( 'until' => $first ) ) );
446 $nextLink = htmlspecialchars( wfMsg( 'nextn', $limitText ) );
448 $nextLink = $sk->makeLinkObj( $title, $nextLink,
449 wfArrayToCGI( $query +
array( 'from' => $last ) ) );
452 return "($prevLink) ($nextLink)";
456 * What to do if the category table conflicts with the number of results
457 * returned? This function says what. It works the same whether the
458 * things being counted are articles, subcategories, or files.
460 * Note for grepping: uses the messages category-article-count,
461 * category-article-count-limited, category-subcat-count,
462 * category-subcat-count-limited, category-file-count,
463 * category-file-count-limited.
465 * @param $rescnt Int: The number of items returned by our database query.
466 * @param $dbcnt Int: The number of items according to the category table.
467 * @param $type String: 'subcat', 'article', or 'file'
468 * @return String: A message giving the number of items, to output to HTML.
470 private function getCountMessage( $rescnt, $dbcnt, $type ) {
472 # There are three cases:
473 # 1) The category table figure seems sane. It might be wrong, but
474 # we can't do anything about it if we don't recalculate it on ev-
476 # 2) The category table figure isn't sane, like it's smaller than the
477 # number of actual results, *but* the number of results is less
478 # than $this->limit and there's no offset. In this case we still
479 # know the right figure.
480 # 3) We have no idea.
481 $totalrescnt = count( $this->articles
) +
count( $this->children
) +
482 ($this->showGallery ?
$this->gallery
->count() : 0);
483 if($dbcnt == $rescnt ||
(($totalrescnt == $this->limit ||
$this->from
484 ||
$this->until
) && $dbcnt > $rescnt)){
485 # Case 1: seems sane.
487 } elseif($totalrescnt < $this->limit
&& !$this->from
&& !$this->until
){
488 # Case 2: not sane, but salvageable. Use the number of results.
489 # Since there are fewer than 200, we can also take this opportunity
490 # to refresh the incorrect category table entry -- which should be
491 # quick due to the small number of entries.
493 $this->cat
->refreshCounts();
495 # Case 3: hopeless. Don't give a total count at all.
496 return wfMsgExt("category-$type-count-limited", 'parse',
497 $wgLang->formatNum( $rescnt ) );
499 return wfMsgExt( "category-$type-count", 'parse', $wgLang->formatNum( $rescnt ),
500 $wgLang->formatNum( $totalcnt ) );