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