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