be0b4e0c9f384e2f61d4b66476240d0a69ecb1b1
[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 }
267
268 function getCategoryTop() {
269 $r = $this->getCategoryBottom();
270 return $r === ''
271 ? $r
272 : "<br style=\"clear:both;\"/>\n" . $r;
273 }
274
275 function getSubcategorySection() {
276 # Don't show subcategories section if there are none.
277 $r = '';
278 $rescnt = count( $this->children );
279 $dbcnt = $this->cat->getSubcatCount();
280 $countmsg = $this->getCountMessage( $rescnt, $dbcnt, 'subcat' );
281 if( $rescnt > 0 ) {
282 # Showing subcategories
283 $r .= "<div id=\"mw-subcategories\">\n";
284 $r .= '<h2>' . wfMsg( 'subcategories' ) . "</h2>\n";
285 $r .= $countmsg;
286 $r .= $this->formatList( $this->children, $this->children_start_char );
287 $r .= "\n</div>";
288 }
289 return $r;
290 }
291
292 function getPagesSection() {
293 $ti = htmlspecialchars( $this->title->getText() );
294 # Don't show articles section if there are none.
295 $r = '';
296
297 # FIXME, here and in the other two sections: we don't need to bother
298 # with this rigamarole if the entire category contents fit on one page
299 # and have already been retrieved. We can just use $rescnt in that
300 # case and save a query and some logic.
301 $dbcnt = $this->cat->getPageCount() - $this->cat->getSubcatCount()
302 - $this->cat->getFileCount();
303 $rescnt = count( $this->articles );
304 $countmsg = $this->getCountMessage( $rescnt, $dbcnt, 'article' );
305
306 if( $rescnt > 0 ) {
307 $r = "<div id=\"mw-pages\">\n";
308 $r .= '<h2>' . wfMsg( 'category_header', $ti ) . "</h2>\n";
309 $r .= $countmsg;
310 $r .= $this->formatList( $this->articles, $this->articles_start_char );
311 $r .= "\n</div>";
312 }
313 return $r;
314 }
315
316 function getImageSection() {
317 if( $this->showGallery && ! $this->gallery->isEmpty() ) {
318 $dbcnt = $this->cat->getFileCount();
319 $rescnt = $this->gallery->count();
320 $countmsg = $this->getCountMessage( $rescnt, $dbcnt, 'file' );
321
322 return "<div id=\"mw-category-media\">\n" .
323 '<h2>' . wfMsg( 'category-media-header', htmlspecialchars( $this->title->getText() ) ) . "</h2>\n" .
324 $countmsg . $this->gallery->toHTML() . "\n</div>";
325 } else {
326 return '';
327 }
328 }
329
330 function getCategoryBottom() {
331 if( $this->until != '' ) {
332 return $this->pagingLinks( $this->title, $this->nextPage, $this->until, $this->limit );
333 } elseif( $this->nextPage != '' || $this->from != '' ) {
334 return $this->pagingLinks( $this->title, $this->from, $this->nextPage, $this->limit );
335 } else {
336 return '';
337 }
338 }
339
340 /**
341 * Format a list of articles chunked by letter, either as a
342 * bullet list or a columnar format, depending on the length.
343 *
344 * @param $articles Array
345 * @param $articles_start_char Array
346 * @param $cutoff Int
347 * @return String
348 * @private
349 */
350 function formatList( $articles, $articles_start_char, $cutoff = 6 ) {
351 if ( count ( $articles ) > $cutoff ) {
352 return $this->columnList( $articles, $articles_start_char );
353 } elseif ( count($articles) > 0) {
354 // for short lists of articles in categories.
355 return $this->shortList( $articles, $articles_start_char );
356 }
357 return '';
358 }
359
360 /**
361 * Format a list of articles chunked by letter in a three-column
362 * list, ordered vertically.
363 *
364 * @param $articles Array
365 * @param $articles_start_char Array
366 * @return String
367 * @private
368 */
369 function columnList( $articles, $articles_start_char ) {
370 // divide list into three equal chunks
371 $chunk = (int) ( count( $articles ) / 3 );
372 $remaining = count( $articles ) % 3;
373
374 // get and display header
375 $r = '<table width="100%"><tr valign="top">';
376
377 $prev_start_char = 'none';
378
379 // loop through the chunks
380 for( $startChunk = 0, $endChunk = $chunk, $chunkIndex = 0;
381 $chunkIndex < 3;
382 $chunkIndex++, $startChunk = $endChunk, $endChunk += $remaining == 0 ? $chunk : $chunk + 1 )
383 {
384 $r .= "<td>\n";
385 $atColumnTop = true;
386
387 // output all articles in category
388 for ($index = $startChunk ;
389 $index < $endChunk && $index < count($articles);
390 $index++ )
391 {
392 // check for change of starting letter or begining of chunk
393 if ( ($index == $startChunk) ||
394 ($articles_start_char[$index] != $articles_start_char[$index - 1]) )
395
396 {
397 if( $atColumnTop ) {
398 $atColumnTop = false;
399 } else {
400 $r .= "</ul>\n";
401 }
402 $cont_msg = "";
403 if ( $articles_start_char[$index] == $prev_start_char )
404 $cont_msg = ' ' . wfMsgHtml( 'listingcontinuesabbrev' );
405 $r .= "<h3>" . htmlspecialchars( $articles_start_char[$index] ) . "$cont_msg</h3>\n<ul>";
406 $prev_start_char = $articles_start_char[$index];
407 }
408
409 $r .= "<li>{$articles[$index]}</li>";
410 }
411 if( !$atColumnTop ) {
412 $r .= "</ul>\n";
413 }
414 $r .= "</td>\n";
415
416
417 }
418 $r .= '</tr></table>';
419 return $r;
420 }
421
422 /**
423 * Format a list of articles chunked by letter in a bullet list.
424 * @param $articles Array
425 * @param $articles_start_char Array
426 * @return String
427 * @private
428 */
429 function shortList( $articles, $articles_start_char ) {
430 $r = '<h3>' . htmlspecialchars( $articles_start_char[0] ) . "</h3>\n";
431 $r .= '<ul><li>'.$articles[0].'</li>';
432 for ($index = 1; $index < count($articles); $index++ )
433 {
434 if ($articles_start_char[$index] != $articles_start_char[$index - 1])
435 {
436 $r .= "</ul><h3>" . htmlspecialchars( $articles_start_char[$index] ) . "</h3>\n<ul>";
437 }
438
439 $r .= "<li>{$articles[$index]}</li>";
440 }
441 $r .= '</ul>';
442 return $r;
443 }
444
445 /**
446 * @param $title Title object
447 * @param $first String
448 * @param $last String
449 * @param $limit Int
450 * @param $query Array: additional query options to pass
451 * @return String
452 * @private
453 */
454 function pagingLinks( $title, $first, $last, $limit, $query = array() ) {
455 global $wgLang;
456 $sk = $this->getSkin();
457 $limitText = $wgLang->formatNum( $limit );
458
459 $prevLink = wfMsgExt( 'prevn', array( 'escape', 'parsemag' ), $limitText );
460 if( $first != '' ) {
461 $prevQuery = $query;
462 $prevQuery['until'] = $first;
463 $prevLink = $sk->link(
464 $title,
465 $prevLink,
466 array(),
467 $prevQuery
468 );
469 }
470 $nextLink = wfMsgExt( 'nextn', array( 'escape', 'parsemag' ), $limitText );
471 if( $last != '' ) {
472 $lastQuery = $query;
473 $lastQuery['from'] = $last;
474 $nextLink = $sk->link(
475 $title,
476 $nextLink,
477 array(),
478 $lastQuery
479 );
480 }
481
482 return "($prevLink) ($nextLink)";
483 }
484
485 /**
486 * What to do if the category table conflicts with the number of results
487 * returned? This function says what. It works the same whether the
488 * things being counted are articles, subcategories, or files.
489 *
490 * Note for grepping: uses the messages category-article-count,
491 * category-article-count-limited, category-subcat-count,
492 * category-subcat-count-limited, category-file-count,
493 * category-file-count-limited.
494 *
495 * @param $rescnt Int: The number of items returned by our database query.
496 * @param $dbcnt Int: The number of items according to the category table.
497 * @param $type String: 'subcat', 'article', or 'file'
498 * @return String: A message giving the number of items, to output to HTML.
499 */
500 private function getCountMessage( $rescnt, $dbcnt, $type ) {
501 global $wgLang;
502 # There are three cases:
503 # 1) The category table figure seems sane. It might be wrong, but
504 # we can't do anything about it if we don't recalculate it on ev-
505 # ery category view.
506 # 2) The category table figure isn't sane, like it's smaller than the
507 # number of actual results, *but* the number of results is less
508 # than $this->limit and there's no offset. In this case we still
509 # know the right figure.
510 # 3) We have no idea.
511 $totalrescnt = count( $this->articles ) + count( $this->children ) +
512 ($this->showGallery ? $this->gallery->count() : 0);
513 if($dbcnt == $rescnt || (($totalrescnt == $this->limit || $this->from
514 || $this->until) && $dbcnt > $rescnt)){
515 # Case 1: seems sane.
516 $totalcnt = $dbcnt;
517 } elseif($totalrescnt < $this->limit && !$this->from && !$this->until){
518 # Case 2: not sane, but salvageable. Use the number of results.
519 # Since there are fewer than 200, we can also take this opportunity
520 # to refresh the incorrect category table entry -- which should be
521 # quick due to the small number of entries.
522 $totalcnt = $rescnt;
523 $this->cat->refreshCounts();
524 } else {
525 # Case 3: hopeless. Don't give a total count at all.
526 return wfMsgExt("category-$type-count-limited", 'parse',
527 $wgLang->formatNum( $rescnt ) );
528 }
529 return wfMsgExt( "category-$type-count", 'parse', $wgLang->formatNum( $rescnt ),
530 $wgLang->formatNum( $totalcnt ) );
531 }
532 }