Clarify comment on $wgRateLimits.
[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
74 function __construct( $title, $from = '', $until = '' ) {
75 global $wgCategoryPagingLimit;
76 $this->title = $title;
77 $this->from = $from;
78 $this->until = $until;
79 $this->limit = $wgCategoryPagingLimit;
80 }
81
82 /**
83 * Format the category data list.
84 *
85 * @param string $from -- return only sort keys from this item on
86 * @param string $until -- don't return keys after this point.
87 * @return string HTML output
88 * @private
89 */
90 function getHTML() {
91 global $wgOut, $wgCategoryMagicGallery, $wgCategoryPagingLimit;
92 wfProfileIn( __METHOD__ );
93
94 $this->showGallery = $wgCategoryMagicGallery && !$wgOut->mNoGallery;
95
96 $this->clearCategoryState();
97 $this->doCategoryQuery();
98 $this->finaliseCategoryState();
99
100 $r = $this->getCategoryTop() .
101 $this->getSubcategorySection() .
102 $this->getPagesSection() .
103 $this->getImageSection() .
104 $this->getCategoryBottom();
105
106 // Give a proper message if category is empty
107 if ( $r == '' ) {
108 $r = wfMsgExt( 'category-empty', array( 'parse' ) );
109 }
110
111 wfProfileOut( __METHOD__ );
112 return $r;
113 }
114
115 function clearCategoryState() {
116 $this->articles = array();
117 $this->articles_start_char = array();
118 $this->children = array();
119 $this->children_start_char = array();
120 if( $this->showGallery ) {
121 $this->gallery = new ImageGallery();
122 $this->gallery->setHideBadImages();
123 }
124 }
125
126 function getSkin() {
127 if ( !$this->skin ) {
128 global $wgUser;
129 $this->skin = $wgUser->getSkin();
130 }
131 return $this->skin;
132 }
133
134 /**
135 * Add a subcategory to the internal lists
136 */
137 function addSubcategory( $title, $sortkey, $pageLength ) {
138 global $wgContLang;
139 // Subcategory; strip the 'Category' namespace from the link text.
140 $this->children[] = $this->getSkin()->makeKnownLinkObj(
141 $title, $wgContLang->convertHtml( $title->getText() ) );
142
143 $this->children_start_char[] = $this->getSubcategorySortChar( $title, $sortkey );
144 }
145
146 /**
147 * Get the character to be used for sorting subcategories.
148 * If there's a link from Category:A to Category:B, the sortkey of the resulting
149 * entry in the categorylinks table is Category:A, not A, which it SHOULD be.
150 * Workaround: If sortkey == "Category:".$title, than use $title for sorting,
151 * else use sortkey...
152 */
153 function getSubcategorySortChar( $title, $sortkey ) {
154 global $wgContLang;
155
156 if( $title->getPrefixedText() == $sortkey ) {
157 $firstChar = $wgContLang->firstChar( $title->getDBkey() );
158 } else {
159 $firstChar = $wgContLang->firstChar( $sortkey );
160 }
161
162 return $wgContLang->convert( $firstChar );
163 }
164
165 /**
166 * Add a page in the image namespace
167 */
168 function addImage( Title $title, $sortkey, $pageLength, $isRedirect = false ) {
169 if ( $this->showGallery ) {
170 $image = new Image( $title );
171 if( $this->flip ) {
172 $this->gallery->insert( $image );
173 } else {
174 $this->gallery->add( $image );
175 }
176 } else {
177 $this->addPage( $title, $sortkey, $pageLength, $isRedirect );
178 }
179 }
180
181 /**
182 * Add a miscellaneous page
183 */
184 function addPage( $title, $sortkey, $pageLength, $isRedirect = false ) {
185 global $wgContLang;
186 $this->articles[] = $isRedirect
187 ? '<span class="redirect-in-category">' . $this->getSkin()->makeKnownLinkObj( $title ) . '</span>'
188 : $this->getSkin()->makeSizeLinkObj( $pageLength, $title );
189 $this->articles_start_char[] = $wgContLang->convert( $wgContLang->firstChar( $sortkey ) );
190 }
191
192 function finaliseCategoryState() {
193 if( $this->flip ) {
194 $this->children = array_reverse( $this->children );
195 $this->children_start_char = array_reverse( $this->children_start_char );
196 $this->articles = array_reverse( $this->articles );
197 $this->articles_start_char = array_reverse( $this->articles_start_char );
198 }
199 }
200
201 function doCategoryQuery() {
202 $dbr = wfGetDB( DB_SLAVE );
203 if( $this->from != '' ) {
204 $pageCondition = 'cl_sortkey >= ' . $dbr->addQuotes( $this->from );
205 $this->flip = false;
206 } elseif( $this->until != '' ) {
207 $pageCondition = 'cl_sortkey < ' . $dbr->addQuotes( $this->until );
208 $this->flip = true;
209 } else {
210 $pageCondition = '1 = 1';
211 $this->flip = false;
212 }
213 $res = $dbr->select(
214 array( 'page', 'categorylinks' ),
215 array( 'page_title', 'page_namespace', 'page_len', 'page_is_redirect', 'cl_sortkey' ),
216 array( $pageCondition,
217 'cl_from = page_id',
218 'cl_to' => $this->title->getDBKey()),
219 #'page_is_redirect' => 0),
220 #+ $pageCondition,
221 __METHOD__,
222 array( 'ORDER BY' => $this->flip ? 'cl_sortkey DESC' : 'cl_sortkey',
223 'USE INDEX' => 'cl_sortkey',
224 'LIMIT' => $this->limit + 1 ) );
225
226 $count = 0;
227 $this->nextPage = null;
228 while( $x = $dbr->fetchObject ( $res ) ) {
229 if( ++$count > $this->limit ) {
230 // We've reached the one extra which shows that there are
231 // additional pages to be had. Stop here...
232 $this->nextPage = $x->cl_sortkey;
233 break;
234 }
235
236 $title = Title::makeTitle( $x->page_namespace, $x->page_title );
237
238 if( $title->getNamespace() == NS_CATEGORY ) {
239 $this->addSubcategory( $title, $x->cl_sortkey, $x->page_len );
240 } elseif( $this->showGallery && $title->getNamespace() == NS_IMAGE ) {
241 $this->addImage( $title, $x->cl_sortkey, $x->page_len, $x->page_is_redirect );
242 } else {
243 $this->addPage( $title, $x->cl_sortkey, $x->page_len, $x->page_is_redirect );
244 }
245 }
246 $dbr->freeResult( $res );
247 }
248
249 function getCategoryTop() {
250 $r = '';
251 if( $this->until != '' ) {
252 $r .= $this->pagingLinks( $this->title, $this->nextPage, $this->until, $this->limit );
253 } elseif( $this->nextPage != '' || $this->from != '' ) {
254 $r .= $this->pagingLinks( $this->title, $this->from, $this->nextPage, $this->limit );
255 }
256 return $r == ''
257 ? $r
258 : "<br style=\"clear:both;\"/>\n" . $r;
259 }
260
261 function getSubcategorySection() {
262 # Don't show subcategories section if there are none.
263 $r = '';
264 $c = count( $this->children );
265 if( $c > 0 ) {
266 # Showing subcategories
267 $r .= "<div id=\"mw-subcategories\">\n";
268 $r .= '<h2>' . wfMsg( 'subcategories' ) . "</h2>\n";
269 $r .= wfMsgExt( 'subcategorycount', array( 'parse' ), $c );
270 $r .= $this->formatList( $this->children, $this->children_start_char );
271 $r .= "\n</div>";
272 }
273 return $r;
274 }
275
276 function getPagesSection() {
277 $ti = htmlspecialchars( $this->title->getText() );
278 # Don't show articles section if there are none.
279 $r = '';
280 $c = count( $this->articles );
281 if( $c > 0 ) {
282 $r = "<div id=\"mw-pages\">\n";
283 $r .= '<h2>' . wfMsg( 'category_header', $ti ) . "</h2>\n";
284 $r .= wfMsgExt( 'categoryarticlecount', array( 'parse' ), $c );
285 $r .= $this->formatList( $this->articles, $this->articles_start_char );
286 $r .= "\n</div>";
287 }
288 return $r;
289 }
290
291 function getImageSection() {
292 if( $this->showGallery && ! $this->gallery->isEmpty() ) {
293 return "<div id=\"mw-category-media\">\n" .
294 '<h2>' . wfMsg( 'category-media-header', htmlspecialchars($this->title->getText()) ) . "</h2>\n" .
295 wfMsgExt( 'category-media-count', array( 'parse' ), $this->gallery->count() ) .
296 $this->gallery->toHTML() . "\n</div>";
297 } else {
298 return '';
299 }
300 }
301
302 function getCategoryBottom() {
303 if( $this->until != '' ) {
304 return $this->pagingLinks( $this->title, $this->nextPage, $this->until, $this->limit );
305 } elseif( $this->nextPage != '' || $this->from != '' ) {
306 return $this->pagingLinks( $this->title, $this->from, $this->nextPage, $this->limit );
307 } else {
308 return '';
309 }
310 }
311
312 /**
313 * Format a list of articles chunked by letter, either as a
314 * bullet list or a columnar format, depending on the length.
315 *
316 * @param array $articles
317 * @param array $articles_start_char
318 * @param int $cutoff
319 * @return string
320 * @private
321 */
322 function formatList( $articles, $articles_start_char, $cutoff = 6 ) {
323 if ( count ( $articles ) > $cutoff ) {
324 return $this->columnList( $articles, $articles_start_char );
325 } elseif ( count($articles) > 0) {
326 // for short lists of articles in categories.
327 return $this->shortList( $articles, $articles_start_char );
328 }
329 return '';
330 }
331
332 /**
333 * Format a list of articles chunked by letter in a three-column
334 * list, ordered vertically.
335 *
336 * @param array $articles
337 * @param array $articles_start_char
338 * @return string
339 * @private
340 */
341 function columnList( $articles, $articles_start_char ) {
342 // divide list into three equal chunks
343 $chunk = (int) (count ( $articles ) / 3);
344
345 // get and display header
346 $r = '<table width="100%"><tr valign="top">';
347
348 $prev_start_char = 'none';
349
350 // loop through the chunks
351 for($startChunk = 0, $endChunk = $chunk, $chunkIndex = 0;
352 $chunkIndex < 3;
353 $chunkIndex++, $startChunk = $endChunk, $endChunk += $chunk + 1)
354 {
355 $r .= "<td>\n";
356 $atColumnTop = true;
357
358 // output all articles in category
359 for ($index = $startChunk ;
360 $index < $endChunk && $index < count($articles);
361 $index++ )
362 {
363 // check for change of starting letter or begining of chunk
364 if ( ($index == $startChunk) ||
365 ($articles_start_char[$index] != $articles_start_char[$index - 1]) )
366
367 {
368 if( $atColumnTop ) {
369 $atColumnTop = false;
370 } else {
371 $r .= "</ul>\n";
372 }
373 $cont_msg = "";
374 if ( $articles_start_char[$index] == $prev_start_char )
375 $cont_msg = ' ' . wfMsgHtml( 'listingcontinuesabbrev' );
376 $r .= "<h3>" . htmlspecialchars( $articles_start_char[$index] ) . "$cont_msg</h3>\n<ul>";
377 $prev_start_char = $articles_start_char[$index];
378 }
379
380 $r .= "<li>{$articles[$index]}</li>";
381 }
382 if( !$atColumnTop ) {
383 $r .= "</ul>\n";
384 }
385 $r .= "</td>\n";
386
387
388 }
389 $r .= '</tr></table>';
390 return $r;
391 }
392
393 /**
394 * Format a list of articles chunked by letter in a bullet list.
395 * @param array $articles
396 * @param array $articles_start_char
397 * @return string
398 * @private
399 */
400 function shortList( $articles, $articles_start_char ) {
401 $r = '<h3>' . htmlspecialchars( $articles_start_char[0] ) . "</h3>\n";
402 $r .= '<ul><li>'.$articles[0].'</li>';
403 for ($index = 1; $index < count($articles); $index++ )
404 {
405 if ($articles_start_char[$index] != $articles_start_char[$index - 1])
406 {
407 $r .= "</ul><h3>" . htmlspecialchars( $articles_start_char[$index] ) . "</h3>\n<ul>";
408 }
409
410 $r .= "<li>{$articles[$index]}</li>";
411 }
412 $r .= '</ul>';
413 return $r;
414 }
415
416 /**
417 * @param Title $title
418 * @param string $first
419 * @param string $last
420 * @param int $limit
421 * @param array $query - additional query options to pass
422 * @return string
423 * @private
424 */
425 function pagingLinks( $title, $first, $last, $limit, $query = array() ) {
426 global $wgLang;
427 $sk = $this->getSkin();
428 $limitText = $wgLang->formatNum( $limit );
429
430 $prevLink = htmlspecialchars( wfMsg( 'prevn', $limitText ) );
431 if( $first != '' ) {
432 $prevLink = $sk->makeLinkObj( $title, $prevLink,
433 wfArrayToCGI( $query + array( 'until' => $first ) ) );
434 }
435 $nextLink = htmlspecialchars( wfMsg( 'nextn', $limitText ) );
436 if( $last != '' ) {
437 $nextLink = $sk->makeLinkObj( $title, $nextLink,
438 wfArrayToCGI( $query + array( 'from' => $last ) ) );
439 }
440
441 return "($prevLink) ($nextLink)";
442 }
443 }
444
445
446