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