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