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