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