Suppress display of duplicate getCategoryBottom() if there are no category members...
[lhc/web/wiklou.git] / includes / CategoryPage.php
1 <?php
2 /**
3 * Special handling for category description pages
4 * Modelled after ImagePage.php
5 *
6 */
7
8 if( !defined( 'MEDIAWIKI' ) )
9 die( 1 );
10
11 /**
12 */
13 class CategoryPage extends Article {
14 function view() {
15 global $wgRequest, $wgUser;
16
17 $diff = $wgRequest->getVal( 'diff' );
18 $diffOnly = $wgRequest->getBool( 'diffonly', $wgUser->getOption( 'diffonly' ) );
19
20 if ( isset( $diff ) && $diffOnly )
21 return Article::view();
22
23 if( !wfRunHooks( 'CategoryPageView', array( &$this ) ) )
24 return;
25
26 if ( NS_CATEGORY == $this->mTitle->getNamespace() ) {
27 $this->openShowCategory();
28 }
29
30 Article::view();
31
32 if ( NS_CATEGORY == $this->mTitle->getNamespace() ) {
33 $this->closeShowCategory();
34 }
35 }
36
37 /**
38 * Don't return a 404 for categories in use.
39 */
40 function hasViewableContent() {
41 if( parent::hasViewableContent() ) {
42 return true;
43 } else {
44 $cat = Category::newFromTitle( $this->mTitle );
45 return $cat->getId() != 0;
46 }
47
48 }
49
50 function openShowCategory() {
51 # For overloading
52 }
53
54 function closeShowCategory() {
55 global $wgOut, $wgRequest;
56 $from = $wgRequest->getVal( 'from' );
57 $until = $wgRequest->getVal( 'until' );
58
59 $viewer = new CategoryViewer( $this->mTitle, $from, $until );
60 $wgOut->addHTML( $viewer->getHTML() );
61 }
62 }
63
64 class CategoryViewer {
65 var $title, $limit, $from, $until,
66 $articles, $articles_start_char,
67 $children, $children_start_char,
68 $showGallery, $gallery,
69 $skin;
70 /** Category object for this page */
71 private $cat;
72
73 function __construct( $title, $from = '', $until = '' ) {
74 global $wgCategoryPagingLimit;
75 $this->title = $title;
76 $this->from = $from;
77 $this->until = $until;
78 $this->limit = $wgCategoryPagingLimit;
79 $this->cat = Category::newFromTitle( $title );
80 }
81
82 /**
83 * Format the category data list.
84 *
85 * @return string HTML output
86 * @private
87 */
88 function getHTML() {
89 global $wgOut, $wgCategoryMagicGallery, $wgCategoryPagingLimit;
90 wfProfileIn( __METHOD__ );
91
92 $this->showGallery = $wgCategoryMagicGallery && !$wgOut->mNoGallery;
93
94 $this->clearCategoryState();
95 $this->doCategoryQuery();
96 $this->finaliseCategoryState();
97
98 $r = $this->getSubcategorySection() .
99 $this->getPagesSection() .
100 $this->getImageSection();
101
102 if( $r == '' ) {
103 // If there is no category content to display, only
104 // show the top part of the navigation links.
105 // FIXME: cannot be completely suppressed because it
106 // is unknown if 'until' or 'from' makes this
107 // give 0 results.
108 $r = $r . $this->getCategoryTop();
109 } else {
110 $r = $this->getCategoryTop() .
111 $r .
112 $this->getCategoryBottom();
113 }
114
115 // Give a proper message if category is empty
116 if ( $r == '' ) {
117 $r = wfMsgExt( 'category-empty', array( 'parse' ) );
118 }
119
120 wfProfileOut( __METHOD__ );
121 return $r;
122 }
123
124 function clearCategoryState() {
125 $this->articles = array();
126 $this->articles_start_char = array();
127 $this->children = array();
128 $this->children_start_char = array();
129 if( $this->showGallery ) {
130 $this->gallery = new ImageGallery();
131 $this->gallery->setHideBadImages();
132 }
133 }
134
135 function getSkin() {
136 if ( !$this->skin ) {
137 global $wgUser;
138 $this->skin = $wgUser->getSkin();
139 }
140 return $this->skin;
141 }
142
143 /**
144 * Add a subcategory to the internal lists, using a Category object
145 */
146 function addSubcategoryObject( $cat, $sortkey, $pageLength ) {
147 $title = $cat->getTitle();
148 $this->addSubcategory( $title, $sortkey, $pageLength );
149 }
150
151 /**
152 * Add a subcategory to the internal lists, using a title object
153 * @deprecated kept for compatibility, please use addSubcategoryObject instead
154 */
155 function addSubcategory( $title, $sortkey, $pageLength ) {
156 global $wgContLang;
157 // Subcategory; strip the 'Category' namespace from the link text.
158 $this->children[] = $this->getSkin()->link(
159 $title,
160 $wgContLang->convertHtml( $title->getText() ),
161 array(),
162 array(),
163 array( 'known', 'noclasses' )
164 );
165
166 $this->children_start_char[] = $this->getSubcategorySortChar( $title, $sortkey );
167 }
168
169 /**
170 * Get the character to be used for sorting subcategories.
171 * If there's a link from Category:A to Category:B, the sortkey of the resulting
172 * entry in the categorylinks table is Category:A, not A, which it SHOULD be.
173 * Workaround: If sortkey == "Category:".$title, than use $title for sorting,
174 * else use sortkey...
175 */
176 function getSubcategorySortChar( $title, $sortkey ) {
177 global $wgContLang;
178
179 if( $title->getPrefixedText() == $sortkey ) {
180 $firstChar = $wgContLang->firstChar( $title->getDBkey() );
181 } else {
182 $firstChar = $wgContLang->firstChar( $sortkey );
183 }
184
185 return $wgContLang->convert( $firstChar );
186 }
187
188 /**
189 * Add a page in the image namespace
190 */
191 function addImage( Title $title, $sortkey, $pageLength, $isRedirect = false ) {
192 if ( $this->showGallery ) {
193 if( $this->flip ) {
194 $this->gallery->insert( $title );
195 } else {
196 $this->gallery->add( $title );
197 }
198 } else {
199 $this->addPage( $title, $sortkey, $pageLength, $isRedirect );
200 }
201 }
202
203 /**
204 * Add a miscellaneous page
205 */
206 function addPage( $title, $sortkey, $pageLength, $isRedirect = false ) {
207 global $wgContLang;
208 $titletext = $wgContLang->convertHtml( $title->getPrefixedText() );
209 $this->articles[] = $isRedirect
210 ? '<span class="redirect-in-category">' .
211 $this->getSkin()->link(
212 $title,
213 $titletext,
214 array(),
215 array(),
216 array( 'known', 'noclasses' )
217 ) . '</span>'
218 : $this->getSkin()->makeSizeLinkObj( $pageLength, $title, $titletext );
219 $this->articles_start_char[] = $wgContLang->convert( $wgContLang->firstChar( $sortkey ) );
220 }
221
222 function finaliseCategoryState() {
223 if( $this->flip ) {
224 $this->children = array_reverse( $this->children );
225 $this->children_start_char = array_reverse( $this->children_start_char );
226 $this->articles = array_reverse( $this->articles );
227 $this->articles_start_char = array_reverse( $this->articles_start_char );
228 }
229 }
230
231 function doCategoryQuery() {
232 $dbr = wfGetDB( DB_SLAVE, 'category' );
233 if( $this->from != '' ) {
234 $pageCondition = 'cl_sortkey >= ' . $dbr->addQuotes( $this->from );
235 $this->flip = false;
236 } elseif( $this->until != '' ) {
237 $pageCondition = 'cl_sortkey < ' . $dbr->addQuotes( $this->until );
238 $this->flip = true;
239 } else {
240 $pageCondition = '1 = 1';
241 $this->flip = false;
242 }
243 $res = $dbr->select(
244 array( 'page', 'categorylinks', 'category' ),
245 array( 'page_title', 'page_namespace', 'page_len', 'page_is_redirect', 'cl_sortkey',
246 'cat_id', 'cat_title', 'cat_subcats', 'cat_pages', 'cat_files' ),
247 array( $pageCondition, 'cl_to' => $this->title->getDBkey() ),
248 __METHOD__,
249 array( 'ORDER BY' => $this->flip ? 'cl_sortkey DESC' : 'cl_sortkey',
250 'USE INDEX' => array( 'categorylinks' => 'cl_sortkey' ),
251 'LIMIT' => $this->limit + 1 ),
252 array( 'categorylinks' => array( 'INNER JOIN', 'cl_from = page_id' ),
253 'category' => array( 'LEFT JOIN', 'cat_title = page_title AND page_namespace = ' . NS_CATEGORY ) )
254 );
255
256 $count = 0;
257 $this->nextPage = null;
258 while( $x = $dbr->fetchObject ( $res ) ) {
259 if( ++$count > $this->limit ) {
260 // We've reached the one extra which shows that there are
261 // additional pages to be had. Stop here...
262 $this->nextPage = $x->cl_sortkey;
263 break;
264 }
265
266 $title = Title::makeTitle( $x->page_namespace, $x->page_title );
267
268 if( $title->getNamespace() == NS_CATEGORY ) {
269 $cat = Category::newFromRow( $x, $title );
270 $this->addSubcategoryObject( $cat, $x->cl_sortkey, $x->page_len );
271 } elseif( $this->showGallery && $title->getNamespace() == NS_FILE ) {
272 $this->addImage( $title, $x->cl_sortkey, $x->page_len, $x->page_is_redirect );
273 } else {
274 $this->addPage( $title, $x->cl_sortkey, $x->page_len, $x->page_is_redirect );
275 }
276 }
277 }
278
279 function getCategoryTop() {
280 $r = $this->getCategoryBottom();
281 return $r === ''
282 ? $r
283 : "<br style=\"clear:both;\"/>\n" . $r;
284 }
285
286 function getSubcategorySection() {
287 # Don't show subcategories section if there are none.
288 $r = '';
289 $rescnt = count( $this->children );
290 $dbcnt = $this->cat->getSubcatCount();
291 $countmsg = $this->getCountMessage( $rescnt, $dbcnt, 'subcat' );
292 if( $rescnt > 0 ) {
293 # Showing subcategories
294 $r .= "<div id=\"mw-subcategories\">\n";
295 $r .= '<h2>' . wfMsg( 'subcategories' ) . "</h2>\n";
296 $r .= $countmsg;
297 $r .= $this->formatList( $this->children, $this->children_start_char );
298 $r .= "\n</div>";
299 }
300 return $r;
301 }
302
303 function getPagesSection() {
304 $ti = htmlspecialchars( $this->title->getText() );
305 # Don't show articles section if there are none.
306 $r = '';
307
308 # FIXME, here and in the other two sections: we don't need to bother
309 # with this rigamarole if the entire category contents fit on one page
310 # and have already been retrieved. We can just use $rescnt in that
311 # case and save a query and some logic.
312 $dbcnt = $this->cat->getPageCount() - $this->cat->getSubcatCount()
313 - $this->cat->getFileCount();
314 $rescnt = count( $this->articles );
315 $countmsg = $this->getCountMessage( $rescnt, $dbcnt, 'article' );
316
317 if( $rescnt > 0 ) {
318 $r = "<div id=\"mw-pages\">\n";
319 $r .= '<h2>' . wfMsg( 'category_header', $ti ) . "</h2>\n";
320 $r .= $countmsg;
321 $r .= $this->formatList( $this->articles, $this->articles_start_char );
322 $r .= "\n</div>";
323 }
324 return $r;
325 }
326
327 function getImageSection() {
328 if( $this->showGallery && ! $this->gallery->isEmpty() ) {
329 $dbcnt = $this->cat->getFileCount();
330 $rescnt = $this->gallery->count();
331 $countmsg = $this->getCountMessage( $rescnt, $dbcnt, 'file' );
332
333 return "<div id=\"mw-category-media\">\n" .
334 '<h2>' . wfMsg( 'category-media-header', htmlspecialchars( $this->title->getText() ) ) . "</h2>\n" .
335 $countmsg . $this->gallery->toHTML() . "\n</div>";
336 } else {
337 return '';
338 }
339 }
340
341 function getCategoryBottom() {
342 if( $this->until != '' ) {
343 return $this->pagingLinks( $this->title, $this->nextPage, $this->until, $this->limit );
344 } elseif( $this->nextPage != '' || $this->from != '' ) {
345 return $this->pagingLinks( $this->title, $this->from, $this->nextPage, $this->limit );
346 } else {
347 return '';
348 }
349 }
350
351 /**
352 * Format a list of articles chunked by letter, either as a
353 * bullet list or a columnar format, depending on the length.
354 *
355 * @param $articles Array
356 * @param $articles_start_char Array
357 * @param $cutoff Int
358 * @return String
359 * @private
360 */
361 function formatList( $articles, $articles_start_char, $cutoff = 6 ) {
362 if ( count ( $articles ) > $cutoff ) {
363 return $this->columnList( $articles, $articles_start_char );
364 } elseif ( count($articles) > 0) {
365 // for short lists of articles in categories.
366 return $this->shortList( $articles, $articles_start_char );
367 }
368 return '';
369 }
370
371 /**
372 * Format a list of articles chunked by letter in a three-column
373 * list, ordered vertically.
374 *
375 * @param $articles Array
376 * @param $articles_start_char Array
377 * @return String
378 * @private
379 */
380 function columnList( $articles, $articles_start_char ) {
381 // divide list into three equal chunks
382 $chunk = (int) ( count( $articles ) / 3 );
383 $remaining = count( $articles ) % 3;
384
385 // get and display header
386 $r = '<table width="100%"><tr valign="top">';
387
388 $prev_start_char = 'none';
389
390 // loop through the chunks
391 for( $startChunk = 0, $endChunk = $chunk, $chunkIndex = 0;
392 $chunkIndex < 3;
393 $chunkIndex++, $startChunk = $endChunk, $endChunk += $remaining == 0 ? $chunk : $chunk + 1 )
394 {
395 $r .= "<td>\n";
396 $atColumnTop = true;
397
398 // output all articles in category
399 for ($index = $startChunk ;
400 $index < $endChunk && $index < count($articles);
401 $index++ )
402 {
403 // check for change of starting letter or begining of chunk
404 if ( ($index == $startChunk) ||
405 ($articles_start_char[$index] != $articles_start_char[$index - 1]) )
406
407 {
408 if( $atColumnTop ) {
409 $atColumnTop = false;
410 } else {
411 $r .= "</ul>\n";
412 }
413 $cont_msg = "";
414 if ( $articles_start_char[$index] == $prev_start_char )
415 $cont_msg = ' ' . wfMsgHtml( 'listingcontinuesabbrev' );
416 $r .= "<h3>" . htmlspecialchars( $articles_start_char[$index] ) . "$cont_msg</h3>\n<ul>";
417 $prev_start_char = $articles_start_char[$index];
418 }
419
420 $r .= "<li>{$articles[$index]}</li>";
421 }
422 if( !$atColumnTop ) {
423 $r .= "</ul>\n";
424 }
425 $r .= "</td>\n";
426
427
428 }
429 $r .= '</tr></table>';
430 return $r;
431 }
432
433 /**
434 * Format a list of articles chunked by letter in a bullet list.
435 * @param $articles Array
436 * @param $articles_start_char Array
437 * @return String
438 * @private
439 */
440 function shortList( $articles, $articles_start_char ) {
441 $r = '<h3>' . htmlspecialchars( $articles_start_char[0] ) . "</h3>\n";
442 $r .= '<ul><li>'.$articles[0].'</li>';
443 for ($index = 1; $index < count($articles); $index++ )
444 {
445 if ($articles_start_char[$index] != $articles_start_char[$index - 1])
446 {
447 $r .= "</ul><h3>" . htmlspecialchars( $articles_start_char[$index] ) . "</h3>\n<ul>";
448 }
449
450 $r .= "<li>{$articles[$index]}</li>";
451 }
452 $r .= '</ul>';
453 return $r;
454 }
455
456 /**
457 * @param $title Title object
458 * @param $first String
459 * @param $last String
460 * @param $limit Int
461 * @param $query Array: additional query options to pass
462 * @return String
463 * @private
464 */
465 function pagingLinks( $title, $first, $last, $limit, $query = array() ) {
466 global $wgLang;
467 $sk = $this->getSkin();
468 $limitText = $wgLang->formatNum( $limit );
469
470 $prevLink = wfMsgExt( 'prevn', array( 'escape', 'parsemag' ), $limitText );
471 if( $first != '' ) {
472 $prevQuery = $query;
473 $prevQuery['until'] = $first;
474 $prevLink = $sk->linkKnown(
475 $title,
476 $prevLink,
477 array(),
478 $prevQuery
479 );
480 }
481 $nextLink = wfMsgExt( 'nextn', array( 'escape', 'parsemag' ), $limitText );
482 if( $last != '' ) {
483 $lastQuery = $query;
484 $lastQuery['from'] = $last;
485 $nextLink = $sk->linkKnown(
486 $title,
487 $nextLink,
488 array(),
489 $lastQuery
490 );
491 }
492
493 return "($prevLink) ($nextLink)";
494 }
495
496 /**
497 * What to do if the category table conflicts with the number of results
498 * returned? This function says what. It works the same whether the
499 * things being counted are articles, subcategories, or files.
500 *
501 * Note for grepping: uses the messages category-article-count,
502 * category-article-count-limited, category-subcat-count,
503 * category-subcat-count-limited, category-file-count,
504 * category-file-count-limited.
505 *
506 * @param $rescnt Int: The number of items returned by our database query.
507 * @param $dbcnt Int: The number of items according to the category table.
508 * @param $type String: 'subcat', 'article', or 'file'
509 * @return String: A message giving the number of items, to output to HTML.
510 */
511 private function getCountMessage( $rescnt, $dbcnt, $type ) {
512 global $wgLang;
513 # There are three cases:
514 # 1) The category table figure seems sane. It might be wrong, but
515 # we can't do anything about it if we don't recalculate it on ev-
516 # ery category view.
517 # 2) The category table figure isn't sane, like it's smaller than the
518 # number of actual results, *but* the number of results is less
519 # than $this->limit and there's no offset. In this case we still
520 # know the right figure.
521 # 3) We have no idea.
522 $totalrescnt = count( $this->articles ) + count( $this->children ) +
523 ($this->showGallery ? $this->gallery->count() : 0);
524 if($dbcnt == $rescnt || (($totalrescnt == $this->limit || $this->from
525 || $this->until) && $dbcnt > $rescnt)){
526 # Case 1: seems sane.
527 $totalcnt = $dbcnt;
528 } elseif($totalrescnt < $this->limit && !$this->from && !$this->until){
529 # Case 2: not sane, but salvageable. Use the number of results.
530 # Since there are fewer than 200, we can also take this opportunity
531 # to refresh the incorrect category table entry -- which should be
532 # quick due to the small number of entries.
533 $totalcnt = $rescnt;
534 $this->cat->refreshCounts();
535 } else {
536 # Case 3: hopeless. Don't give a total count at all.
537 return wfMsgExt("category-$type-count-limited", 'parse',
538 $wgLang->formatNum( $rescnt ) );
539 }
540 return wfMsgExt( "category-$type-count", 'parse', $wgLang->formatNum( $rescnt ),
541 $wgLang->formatNum( $totalcnt ) );
542 }
543 }