This is a schema change. It's only a table creation, but the table must be created...
[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 $image = new Image( $title );
174 if( $this->flip ) {
175 $this->gallery->insert( $image );
176 } else {
177 $this->gallery->add( $image );
178 }
179 } else {
180 $this->addPage( $title, $sortkey, $pageLength, $isRedirect );
181 }
182 }
183
184 /**
185 * Add a miscellaneous page
186 */
187 function addPage( $title, $sortkey, $pageLength, $isRedirect = false ) {
188 global $wgContLang;
189 $this->articles[] = $isRedirect
190 ? '<span class="redirect-in-category">' . $this->getSkin()->makeKnownLinkObj( $title ) . '</span>'
191 : $this->getSkin()->makeSizeLinkObj( $pageLength, $title );
192 $this->articles_start_char[] = $wgContLang->convert( $wgContLang->firstChar( $sortkey ) );
193 }
194
195 function finaliseCategoryState() {
196 if( $this->flip ) {
197 $this->children = array_reverse( $this->children );
198 $this->children_start_char = array_reverse( $this->children_start_char );
199 $this->articles = array_reverse( $this->articles );
200 $this->articles_start_char = array_reverse( $this->articles_start_char );
201 }
202 }
203
204 function doCategoryQuery() {
205 $dbr = wfGetDB( DB_SLAVE );
206 if( $this->from != '' ) {
207 $pageCondition = 'cl_sortkey >= ' . $dbr->addQuotes( $this->from );
208 $this->flip = false;
209 } elseif( $this->until != '' ) {
210 $pageCondition = 'cl_sortkey < ' . $dbr->addQuotes( $this->until );
211 $this->flip = true;
212 } else {
213 $pageCondition = '1 = 1';
214 $this->flip = false;
215 }
216 $res = $dbr->select(
217 array( 'page', 'categorylinks' ),
218 array( 'page_title', 'page_namespace', 'page_len', 'page_is_redirect', 'cl_sortkey' ),
219 array( $pageCondition,
220 'cl_from = page_id',
221 'cl_to' => $this->title->getDBkey()),
222 #'page_is_redirect' => 0),
223 #+ $pageCondition,
224 __METHOD__,
225 array( 'ORDER BY' => $this->flip ? 'cl_sortkey DESC' : 'cl_sortkey',
226 'USE INDEX' => 'cl_sortkey',
227 'LIMIT' => $this->limit + 1 ) );
228
229 $count = 0;
230 $this->nextPage = null;
231 while( $x = $dbr->fetchObject ( $res ) ) {
232 if( ++$count > $this->limit ) {
233 // We've reached the one extra which shows that there are
234 // additional pages to be had. Stop here...
235 $this->nextPage = $x->cl_sortkey;
236 break;
237 }
238
239 $title = Title::makeTitle( $x->page_namespace, $x->page_title );
240
241 if( $title->getNamespace() == NS_CATEGORY ) {
242 $this->addSubcategory( $title, $x->cl_sortkey, $x->page_len );
243 } elseif( $this->showGallery && $title->getNamespace() == NS_IMAGE ) {
244 $this->addImage( $title, $x->cl_sortkey, $x->page_len, $x->page_is_redirect );
245 } else {
246 $this->addPage( $title, $x->cl_sortkey, $x->page_len, $x->page_is_redirect );
247 }
248 }
249 $dbr->freeResult( $res );
250 }
251
252 function getCategoryTop() {
253 $r = '';
254 if( $this->until != '' ) {
255 $r .= $this->pagingLinks( $this->title, $this->nextPage, $this->until, $this->limit );
256 } elseif( $this->nextPage != '' || $this->from != '' ) {
257 $r .= $this->pagingLinks( $this->title, $this->from, $this->nextPage, $this->limit );
258 }
259 return $r == ''
260 ? $r
261 : "<br style=\"clear:both;\"/>\n" . $r;
262 }
263
264 function getSubcategorySection() {
265 # Don't show subcategories section if there are none.
266 $r = '';
267 $rescnt = count( $this->children );
268 $dbcnt = $this->cat->getSubcatCount();
269 $countmsg = $this->getCountMessage( $rescnt, $dbcnt, 'subcat' );
270 if( $rescnt > 0 ) {
271 # Showing subcategories
272 $r .= "<div id=\"mw-subcategories\">\n";
273 $r .= '<h2>' . wfMsg( 'subcategories' ) . "</h2>\n";
274 $r .= $countmsg;
275 $r .= $this->formatList( $this->children, $this->children_start_char );
276 $r .= "\n</div>";
277 }
278 return $r;
279 }
280
281 function getPagesSection() {
282 $ti = htmlspecialchars( $this->title->getText() );
283 # Don't show articles section if there are none.
284 $r = '';
285
286 # FIXME, here and in the other two sections: we don't need to bother
287 # with this rigamarole if the entire category contents fit on one page
288 # and have already been retrieved. We can just use $rescnt in that
289 # case and save a query and some logic.
290 $dbcnt = $this->cat->getPageCount() - $this->cat->getSubcatCount()
291 - $this->cat->getFileCount();
292 $rescnt = count( $this->articles );
293 $countmsg = $this->getCountMessage( $rescnt, $dbcnt, 'article' );
294
295 if( $rescnt > 0 ) {
296 $r = "<div id=\"mw-pages\">\n";
297 $r .= '<h2>' . wfMsg( 'category_header', $ti ) . "</h2>\n";
298 $r .= $countmsg;
299 $r .= $this->formatList( $this->articles, $this->articles_start_char );
300 $r .= "\n</div>";
301 }
302 return $r;
303 }
304
305 function getImageSection() {
306 if( $this->showGallery && ! $this->gallery->isEmpty() ) {
307 $dbcnt = $this->cat->getFileCount();
308 $rescnt = $this->gallery->count();
309 $countmsg = $this->getCountMessage( $rescnt, $dbcnt, 'file' );
310
311 return "<div id=\"mw-category-media\">\n" .
312 '<h2>' . wfMsg( 'category-media-header', htmlspecialchars($this->title->getText()) ) . "</h2>\n" .
313 $countmsg . $this->gallery->toHTML() . "\n</div>";
314 } else {
315 return '';
316 }
317 }
318
319 function getCategoryBottom() {
320 if( $this->until != '' ) {
321 return $this->pagingLinks( $this->title, $this->nextPage, $this->until, $this->limit );
322 } elseif( $this->nextPage != '' || $this->from != '' ) {
323 return $this->pagingLinks( $this->title, $this->from, $this->nextPage, $this->limit );
324 } else {
325 return '';
326 }
327 }
328
329 /**
330 * Format a list of articles chunked by letter, either as a
331 * bullet list or a columnar format, depending on the length.
332 *
333 * @param array $articles
334 * @param array $articles_start_char
335 * @param int $cutoff
336 * @return string
337 * @private
338 */
339 function formatList( $articles, $articles_start_char, $cutoff = 6 ) {
340 if ( count ( $articles ) > $cutoff ) {
341 return $this->columnList( $articles, $articles_start_char );
342 } elseif ( count($articles) > 0) {
343 // for short lists of articles in categories.
344 return $this->shortList( $articles, $articles_start_char );
345 }
346 return '';
347 }
348
349 /**
350 * Format a list of articles chunked by letter in a three-column
351 * list, ordered vertically.
352 *
353 * @param array $articles
354 * @param array $articles_start_char
355 * @return string
356 * @private
357 */
358 function columnList( $articles, $articles_start_char ) {
359 // divide list into three equal chunks
360 $chunk = (int) (count ( $articles ) / 3);
361
362 // get and display header
363 $r = '<table width="100%"><tr valign="top">';
364
365 $prev_start_char = 'none';
366
367 // loop through the chunks
368 for($startChunk = 0, $endChunk = $chunk, $chunkIndex = 0;
369 $chunkIndex < 3;
370 $chunkIndex++, $startChunk = $endChunk, $endChunk += $chunk + 1)
371 {
372 $r .= "<td>\n";
373 $atColumnTop = true;
374
375 // output all articles in category
376 for ($index = $startChunk ;
377 $index < $endChunk && $index < count($articles);
378 $index++ )
379 {
380 // check for change of starting letter or begining of chunk
381 if ( ($index == $startChunk) ||
382 ($articles_start_char[$index] != $articles_start_char[$index - 1]) )
383
384 {
385 if( $atColumnTop ) {
386 $atColumnTop = false;
387 } else {
388 $r .= "</ul>\n";
389 }
390 $cont_msg = "";
391 if ( $articles_start_char[$index] == $prev_start_char )
392 $cont_msg = ' ' . wfMsgHtml( 'listingcontinuesabbrev' );
393 $r .= "<h3>" . htmlspecialchars( $articles_start_char[$index] ) . "$cont_msg</h3>\n<ul>";
394 $prev_start_char = $articles_start_char[$index];
395 }
396
397 $r .= "<li>{$articles[$index]}</li>";
398 }
399 if( !$atColumnTop ) {
400 $r .= "</ul>\n";
401 }
402 $r .= "</td>\n";
403
404
405 }
406 $r .= '</tr></table>';
407 return $r;
408 }
409
410 /**
411 * Format a list of articles chunked by letter in a bullet list.
412 * @param array $articles
413 * @param array $articles_start_char
414 * @return string
415 * @private
416 */
417 function shortList( $articles, $articles_start_char ) {
418 $r = '<h3>' . htmlspecialchars( $articles_start_char[0] ) . "</h3>\n";
419 $r .= '<ul><li>'.$articles[0].'</li>';
420 for ($index = 1; $index < count($articles); $index++ )
421 {
422 if ($articles_start_char[$index] != $articles_start_char[$index - 1])
423 {
424 $r .= "</ul><h3>" . htmlspecialchars( $articles_start_char[$index] ) . "</h3>\n<ul>";
425 }
426
427 $r .= "<li>{$articles[$index]}</li>";
428 }
429 $r .= '</ul>';
430 return $r;
431 }
432
433 /**
434 * @param Title $title
435 * @param string $first
436 * @param string $last
437 * @param int $limit
438 * @param array $query - additional query options to pass
439 * @return string
440 * @private
441 */
442 function pagingLinks( $title, $first, $last, $limit, $query = array() ) {
443 global $wgLang;
444 $sk = $this->getSkin();
445 $limitText = $wgLang->formatNum( $limit );
446
447 $prevLink = htmlspecialchars( wfMsg( 'prevn', $limitText ) );
448 if( $first != '' ) {
449 $prevLink = $sk->makeLinkObj( $title, $prevLink,
450 wfArrayToCGI( $query + array( 'until' => $first ) ) );
451 }
452 $nextLink = htmlspecialchars( wfMsg( 'nextn', $limitText ) );
453 if( $last != '' ) {
454 $nextLink = $sk->makeLinkObj( $title, $nextLink,
455 wfArrayToCGI( $query + array( 'from' => $last ) ) );
456 }
457
458 return "($prevLink) ($nextLink)";
459 }
460
461 /**
462 * What to do if the category table conflicts with the number of results
463 * returned? This function says what. It works the same whether the
464 * things being counted are articles, subcategories, or files.
465 *
466 * Note for grepping: uses the messages category-article-count,
467 * category-article-count-limited, category-subcat-count,
468 * category-subcat-count-limited, category-file-count,
469 * category-file-count-limited.
470 *
471 * @param int $rescnt The number of items returned by our database query.
472 * @param int $dbcnt The number of items according to the category table.
473 * @param string $type 'subcat', 'article', or 'file'
474 * @return string A message giving the number of items, to output to HTML.
475 */
476 private function getCountMessage( $rescnt, $dbcnt, $type ) {
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->gallery->count();
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', $rescnt);
498 }
499 return wfMsgExt( "category-$type-count", 'parse', $rescnt, $totalcnt );
500 }
501 }
502
503
504