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