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