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 * Don't return a 404 for categories in use.
43 function hasViewableContent() {
44 if( parent
::hasViewableContent() ) {
47 $cat = Category
::newFromTitle( $this->mTitle
);
48 return $cat->getId() != 0;
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 * @return string HTML output
92 global $wgOut, $wgCategoryMagicGallery, $wgCategoryPagingLimit;
93 wfProfileIn( __METHOD__
);
95 $this->showGallery
= $wgCategoryMagicGallery && !$wgOut->mNoGallery
;
97 $this->clearCategoryState();
98 $this->doCategoryQuery();
99 $this->finaliseCategoryState();
101 $r = $this->getCategoryTop() .
102 $this->getSubcategorySection() .
103 $this->getPagesSection() .
104 $this->getImageSection() .
105 $this->getCategoryBottom();
107 // Give a proper message if category is empty
109 $r = wfMsgExt( 'category-empty', array( 'parse' ) );
112 wfProfileOut( __METHOD__
);
116 function clearCategoryState() {
117 $this->articles
= array();
118 $this->articles_start_char
= array();
119 $this->children
= array();
120 $this->children_start_char
= array();
121 if( $this->showGallery
) {
122 $this->gallery
= new ImageGallery();
123 $this->gallery
->setHideBadImages();
128 if ( !$this->skin
) {
130 $this->skin
= $wgUser->getSkin();
136 * Add a subcategory to the internal lists, using a Category object
138 function addSubcategoryObject( $cat, $sortkey, $pageLength ) {
139 $title = $cat->getTitle();
140 $this->addSubcategory( $title, $sortkey, $pageLength );
144 * Add a subcategory to the internal lists, using a title object
145 * @deprecated kept for compatibility, please use addSubcategoryObject instead
147 function addSubcategory( $title, $sortkey, $pageLength ) {
149 // Subcategory; strip the 'Category' namespace from the link text.
150 $this->children
[] = $this->getSkin()->makeKnownLinkObj(
151 $title, $wgContLang->convertHtml( $title->getText() ) );
153 $this->children_start_char
[] = $this->getSubcategorySortChar( $title, $sortkey );
157 * Get the character to be used for sorting subcategories.
158 * If there's a link from Category:A to Category:B, the sortkey of the resulting
159 * entry in the categorylinks table is Category:A, not A, which it SHOULD be.
160 * Workaround: If sortkey == "Category:".$title, than use $title for sorting,
161 * else use sortkey...
163 function getSubcategorySortChar( $title, $sortkey ) {
166 if( $title->getPrefixedText() == $sortkey ) {
167 $firstChar = $wgContLang->firstChar( $title->getDBkey() );
169 $firstChar = $wgContLang->firstChar( $sortkey );
172 return $wgContLang->convert( $firstChar );
176 * Add a page in the image namespace
178 function addImage( Title
$title, $sortkey, $pageLength, $isRedirect = false ) {
179 if ( $this->showGallery
) {
181 $this->gallery
->insert( $title );
183 $this->gallery
->add( $title );
186 $this->addPage( $title, $sortkey, $pageLength, $isRedirect );
191 * Add a miscellaneous page
193 function addPage( $title, $sortkey, $pageLength, $isRedirect = false ) {
195 $this->articles
[] = $isRedirect
196 ?
'<span class="redirect-in-category">' . $this->getSkin()->makeKnownLinkObj( $title ) . '</span>'
197 : $this->getSkin()->makeSizeLinkObj( $pageLength, $title );
198 $this->articles_start_char
[] = $wgContLang->convert( $wgContLang->firstChar( $sortkey ) );
201 function finaliseCategoryState() {
203 $this->children
= array_reverse( $this->children
);
204 $this->children_start_char
= array_reverse( $this->children_start_char
);
205 $this->articles
= array_reverse( $this->articles
);
206 $this->articles_start_char
= array_reverse( $this->articles_start_char
);
210 function doCategoryQuery() {
211 $dbr = wfGetDB( DB_SLAVE
);
212 if( $this->from
!= '' ) {
213 $pageCondition = 'cl_sortkey >= ' . $dbr->addQuotes( $this->from
);
215 } elseif( $this->until
!= '' ) {
216 $pageCondition = 'cl_sortkey < ' . $dbr->addQuotes( $this->until
);
219 $pageCondition = '1 = 1';
223 array( 'page', 'categorylinks', 'category' ),
224 array( 'page_title', 'page_namespace', 'page_len', 'page_is_redirect', 'cl_sortkey',
225 'cat_id', 'cat_title', 'cat_subcats', 'cat_pages', 'cat_files' ),
226 array( $pageCondition, 'cl_to' => $this->title
->getDBkey() ),
228 array( 'ORDER BY' => $this->flip ?
'cl_sortkey DESC' : 'cl_sortkey',
229 'USE INDEX' => array( 'categorylinks' => 'cl_sortkey' ),
230 'LIMIT' => $this->limit +
1 ),
231 array( 'categorylinks' => array( 'INNER JOIN', 'cl_from = page_id' ),
232 'category' => array( 'LEFT JOIN', 'cat_title = page_title AND page_namespace = ' . NS_CATEGORY
) )
236 $this->nextPage
= null;
237 while( $x = $dbr->fetchObject ( $res ) ) {
238 if( ++
$count > $this->limit
) {
239 // We've reached the one extra which shows that there are
240 // additional pages to be had. Stop here...
241 $this->nextPage
= $x->cl_sortkey
;
245 $title = Title
::makeTitle( $x->page_namespace
, $x->page_title
);
247 if( $title->getNamespace() == NS_CATEGORY
) {
248 $cat = Category
::newFromRow( $x, $title );
249 $this->addSubcategoryObject( $cat, $x->cl_sortkey
, $x->page_len
);
250 } elseif( $this->showGallery
&& $title->getNamespace() == NS_FILE
) {
251 $this->addImage( $title, $x->cl_sortkey
, $x->page_len
, $x->page_is_redirect
);
253 $this->addPage( $title, $x->cl_sortkey
, $x->page_len
, $x->page_is_redirect
);
256 $dbr->freeResult( $res );
259 function getCategoryTop() {
261 if( $this->until
!= '' ) {
262 $r .= $this->pagingLinks( $this->title
, $this->nextPage
, $this->until
, $this->limit
);
263 } elseif( $this->nextPage
!= '' ||
$this->from
!= '' ) {
264 $r .= $this->pagingLinks( $this->title
, $this->from
, $this->nextPage
, $this->limit
);
268 : "<br style=\"clear:both;\"/>\n" . $r;
271 function getSubcategorySection() {
272 # Don't show subcategories section if there are none.
274 $rescnt = count( $this->children
);
275 $dbcnt = $this->cat
->getSubcatCount();
276 $countmsg = $this->getCountMessage( $rescnt, $dbcnt, 'subcat' );
278 # Showing subcategories
279 $r .= "<div id=\"mw-subcategories\">\n";
280 $r .= '<h2>' . wfMsg( 'subcategories' ) . "</h2>\n";
282 $r .= $this->formatList( $this->children
, $this->children_start_char
);
288 function getPagesSection() {
289 $ti = htmlspecialchars( $this->title
->getText() );
290 # Don't show articles section if there are none.
293 # FIXME, here and in the other two sections: we don't need to bother
294 # with this rigamarole if the entire category contents fit on one page
295 # and have already been retrieved. We can just use $rescnt in that
296 # case and save a query and some logic.
297 $dbcnt = $this->cat
->getPageCount() - $this->cat
->getSubcatCount()
298 - $this->cat
->getFileCount();
299 $rescnt = count( $this->articles
);
300 $countmsg = $this->getCountMessage( $rescnt, $dbcnt, 'article' );
303 $r = "<div id=\"mw-pages\">\n";
304 $r .= '<h2>' . wfMsg( 'category_header', $ti ) . "</h2>\n";
306 $r .= $this->formatList( $this->articles
, $this->articles_start_char
);
312 function getImageSection() {
313 if( $this->showGallery
&& ! $this->gallery
->isEmpty() ) {
314 $dbcnt = $this->cat
->getFileCount();
315 $rescnt = $this->gallery
->count();
316 $countmsg = $this->getCountMessage( $rescnt, $dbcnt, 'file' );
318 return "<div id=\"mw-category-media\">\n" .
319 '<h2>' . wfMsg( 'category-media-header', htmlspecialchars($this->title
->getText()) ) . "</h2>\n" .
320 $countmsg . $this->gallery
->toHTML() . "\n</div>";
326 function getCategoryBottom() {
327 if( $this->until
!= '' ) {
328 return $this->pagingLinks( $this->title
, $this->nextPage
, $this->until
, $this->limit
);
329 } elseif( $this->nextPage
!= '' ||
$this->from
!= '' ) {
330 return $this->pagingLinks( $this->title
, $this->from
, $this->nextPage
, $this->limit
);
337 * Format a list of articles chunked by letter, either as a
338 * bullet list or a columnar format, depending on the length.
340 * @param $articles Array
341 * @param $articles_start_char Array
346 function formatList( $articles, $articles_start_char, $cutoff = 6 ) {
347 if ( count ( $articles ) > $cutoff ) {
348 return $this->columnList( $articles, $articles_start_char );
349 } elseif ( count($articles) > 0) {
350 // for short lists of articles in categories.
351 return $this->shortList( $articles, $articles_start_char );
357 * Format a list of articles chunked by letter in a three-column
358 * list, ordered vertically.
360 * @param $articles Array
361 * @param $articles_start_char Array
365 function columnList( $articles, $articles_start_char ) {
366 // divide list into three equal chunks
367 $chunk = (int) (count ( $articles ) / 3);
369 // get and display header
370 $r = '<table width="100%"><tr valign="top">';
372 $prev_start_char = 'none';
374 // loop through the chunks
375 for($startChunk = 0, $endChunk = $chunk, $chunkIndex = 0;
377 $chunkIndex++
, $startChunk = $endChunk, $endChunk +
= $chunk +
1)
382 // output all articles in category
383 for ($index = $startChunk ;
384 $index < $endChunk && $index < count($articles);
387 // check for change of starting letter or begining of chunk
388 if ( ($index == $startChunk) ||
389 ($articles_start_char[$index] != $articles_start_char[$index - 1]) )
393 $atColumnTop = false;
398 if ( $articles_start_char[$index] == $prev_start_char )
399 $cont_msg = ' ' . wfMsgHtml( 'listingcontinuesabbrev' );
400 $r .= "<h3>" . htmlspecialchars( $articles_start_char[$index] ) . "$cont_msg</h3>\n<ul>";
401 $prev_start_char = $articles_start_char[$index];
404 $r .= "<li>{$articles[$index]}</li>";
406 if( !$atColumnTop ) {
413 $r .= '</tr></table>';
418 * Format a list of articles chunked by letter in a bullet list.
419 * @param $articles Array
420 * @param $articles_start_char Array
424 function shortList( $articles, $articles_start_char ) {
425 $r = '<h3>' . htmlspecialchars( $articles_start_char[0] ) . "</h3>\n";
426 $r .= '<ul><li>'.$articles[0].'</li>';
427 for ($index = 1; $index < count($articles); $index++
)
429 if ($articles_start_char[$index] != $articles_start_char[$index - 1])
431 $r .= "</ul><h3>" . htmlspecialchars( $articles_start_char[$index] ) . "</h3>\n<ul>";
434 $r .= "<li>{$articles[$index]}</li>";
441 * @param $title Title object
442 * @param $first String
443 * @param $last String
445 * @param $query Array: additional query options to pass
449 function pagingLinks( $title, $first, $last, $limit, $query = array() ) {
451 $sk = $this->getSkin();
452 $limitText = $wgLang->formatNum( $limit );
454 $prevLink = htmlspecialchars( wfMsg( 'prevn', $limitText ) );
456 $prevLink = $sk->makeLinkObj( $title, $prevLink,
457 wfArrayToCGI( $query +
array( 'until' => $first ) ) );
459 $nextLink = htmlspecialchars( wfMsg( 'nextn', $limitText ) );
461 $nextLink = $sk->makeLinkObj( $title, $nextLink,
462 wfArrayToCGI( $query +
array( 'from' => $last ) ) );
465 return "($prevLink) ($nextLink)";
469 * What to do if the category table conflicts with the number of results
470 * returned? This function says what. It works the same whether the
471 * things being counted are articles, subcategories, or files.
473 * Note for grepping: uses the messages category-article-count,
474 * category-article-count-limited, category-subcat-count,
475 * category-subcat-count-limited, category-file-count,
476 * category-file-count-limited.
478 * @param $rescnt Int: The number of items returned by our database query.
479 * @param $dbcnt Int: The number of items according to the category table.
480 * @param $type String: 'subcat', 'article', or 'file'
481 * @return String: A message giving the number of items, to output to HTML.
483 private function getCountMessage( $rescnt, $dbcnt, $type ) {
485 # There are three cases:
486 # 1) The category table figure seems sane. It might be wrong, but
487 # we can't do anything about it if we don't recalculate it on ev-
489 # 2) The category table figure isn't sane, like it's smaller than the
490 # number of actual results, *but* the number of results is less
491 # than $this->limit and there's no offset. In this case we still
492 # know the right figure.
493 # 3) We have no idea.
494 $totalrescnt = count( $this->articles
) +
count( $this->children
) +
495 ($this->showGallery ?
$this->gallery
->count() : 0);
496 if($dbcnt == $rescnt ||
(($totalrescnt == $this->limit ||
$this->from
497 ||
$this->until
) && $dbcnt > $rescnt)){
498 # Case 1: seems sane.
500 } elseif($totalrescnt < $this->limit
&& !$this->from
&& !$this->until
){
501 # Case 2: not sane, but salvageable. Use the number of results.
502 # Since there are fewer than 200, we can also take this opportunity
503 # to refresh the incorrect category table entry -- which should be
504 # quick due to the small number of entries.
506 $this->cat
->refreshCounts();
508 # Case 3: hopeless. Don't give a total count at all.
509 return wfMsgExt("category-$type-count-limited", 'parse',
510 $wgLang->formatNum( $rescnt ) );
512 return wfMsgExt( "category-$type-count", 'parse', $wgLang->formatNum( $rescnt ),
513 $wgLang->formatNum( $totalcnt ) );