(bug 12846) IE rtl.css issue in RTL wikis special:Preferences when selecting an LTR...
[mediawiki.git] / includes / CategoryPage.php
blob6fbcd3c17cb993302ee37edd5f4105a657f141f2
1 <?php
2 /**
3 * Special handling for category description pages
4 * Modelled after ImagePage.php
6 */
8 if( !defined( 'MEDIAWIKI' ) )
9 die( 1 );
11 /**
13 class CategoryPage extends Article {
14 function view() {
15 global $wgRequest, $wgUser;
17 $diff = $wgRequest->getVal( 'diff' );
18 $diffOnly = $wgRequest->getBool( 'diffonly', $wgUser->getOption( 'diffonly' ) );
20 if ( isset( $diff ) && $diffOnly )
21 return Article::view();
23 if(!wfRunHooks('CategoryPageView', array(&$this))) return;
25 if ( NS_CATEGORY == $this->mTitle->getNamespace() ) {
26 $this->openShowCategory();
29 Article::view();
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.
35 if ( NS_CATEGORY == $this->mTitle->getNamespace() ) {
36 $this->closeShowCategory();
40 /**
41 * This page should not be cached if 'from' or 'until' has been used
42 * @return bool
44 function isFileCacheable() {
45 global $wgRequest;
47 return ( ! Article::isFileCacheable()
48 || $wgRequest->getVal( 'from' )
49 || $wgRequest->getVal( 'until' )
50 ) ? false : true;
53 function openShowCategory() {
54 # For overloading
57 function closeShowCategory() {
58 global $wgOut, $wgRequest;
59 $from = $wgRequest->getVal( 'from' );
60 $until = $wgRequest->getVal( 'until' );
62 $viewer = new CategoryViewer( $this->mTitle, $from, $until );
63 $wgOut->addHTML( $viewer->getHTML() );
67 class CategoryViewer {
68 var $title, $limit, $from, $until,
69 $articles, $articles_start_char,
70 $children, $children_start_char,
71 $showGallery, $gallery,
72 $skin;
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;
82 /**
83 * Format the category data list.
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
90 function getHTML() {
91 global $wgOut, $wgCategoryMagicGallery, $wgCategoryPagingLimit;
92 wfProfileIn( __METHOD__ );
94 $this->showGallery = $wgCategoryMagicGallery && !$wgOut->mNoGallery;
96 $this->clearCategoryState();
97 $this->doCategoryQuery();
98 $this->finaliseCategoryState();
100 $r = $this->getCategoryTop() .
101 $this->getSubcategorySection() .
102 $this->getPagesSection() .
103 $this->getImageSection() .
104 $this->getCategoryBottom();
106 // Give a proper message if category is empty
107 if ( $r == '' ) {
108 $r = wfMsgExt( 'category-empty', array( 'parse' ) );
111 wfProfileOut( __METHOD__ );
112 return $r;
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();
126 function getSkin() {
127 if ( !$this->skin ) {
128 global $wgUser;
129 $this->skin = $wgUser->getSkin();
131 return $this->skin;
135 * Add a subcategory to the internal lists
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() ) );
143 $this->children_start_char[] = $this->getSubcategorySortChar( $title, $sortkey );
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...
153 function getSubcategorySortChar( $title, $sortkey ) {
154 global $wgContLang;
156 if( $title->getPrefixedText() == $sortkey ) {
157 $firstChar = $wgContLang->firstChar( $title->getDBkey() );
158 } else {
159 $firstChar = $wgContLang->firstChar( $sortkey );
162 return $wgContLang->convert( $firstChar );
166 * Add a page in the image namespace
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 );
176 } else {
177 $this->addPage( $title, $sortkey, $pageLength, $isRedirect );
182 * Add a miscellaneous page
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 ) );
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 );
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;
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 ) );
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;
236 $title = Title::makeTitle( $x->page_namespace, $x->page_title );
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 );
246 $dbr->freeResult( $res );
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 );
256 return $r == ''
257 ? $r
258 : "<br style=\"clear:both;\"/>\n" . $r;
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>";
273 return $r;
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>";
288 return $r;
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 '';
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 '';
313 * Format a list of articles chunked by letter, either as a
314 * bullet list or a columnar format, depending on the length.
316 * @param array $articles
317 * @param array $articles_start_char
318 * @param int $cutoff
319 * @return string
320 * @private
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 );
329 return '';
333 * Format a list of articles chunked by letter in a three-column
334 * list, ordered vertically.
336 * @param array $articles
337 * @param array $articles_start_char
338 * @return string
339 * @private
341 function columnList( $articles, $articles_start_char ) {
342 // divide list into three equal chunks
343 $chunk = (int) (count ( $articles ) / 3);
345 // get and display header
346 $r = '<table width="100%"><tr valign="top">';
348 $prev_start_char = 'none';
350 // loop through the chunks
351 for($startChunk = 0, $endChunk = $chunk, $chunkIndex = 0;
352 $chunkIndex < 3;
353 $chunkIndex++, $startChunk = $endChunk, $endChunk += $chunk + 1)
355 $r .= "<td>\n";
356 $atColumnTop = true;
358 // output all articles in category
359 for ($index = $startChunk ;
360 $index < $endChunk && $index < count($articles);
361 $index++ )
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]) )
368 if( $atColumnTop ) {
369 $atColumnTop = false;
370 } else {
371 $r .= "</ul>\n";
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];
380 $r .= "<li>{$articles[$index]}</li>";
382 if( !$atColumnTop ) {
383 $r .= "</ul>\n";
385 $r .= "</td>\n";
389 $r .= '</tr></table>';
390 return $r;
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
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++ )
405 if ($articles_start_char[$index] != $articles_start_char[$index - 1])
407 $r .= "</ul><h3>" . htmlspecialchars( $articles_start_char[$index] ) . "</h3>\n<ul>";
410 $r .= "<li>{$articles[$index]}</li>";
412 $r .= '</ul>';
413 return $r;
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
425 function pagingLinks( $title, $first, $last, $limit, $query = array() ) {
426 global $wgLang;
427 $sk = $this->getSkin();
428 $limitText = $wgLang->formatNum( $limit );
430 $prevLink = htmlspecialchars( wfMsg( 'prevn', $limitText ) );
431 if( $first != '' ) {
432 $prevLink = $sk->makeLinkObj( $title, $prevLink,
433 wfArrayToCGI( $query + array( 'until' => $first ) ) );
435 $nextLink = htmlspecialchars( wfMsg( 'nextn', $limitText ) );
436 if( $last != '' ) {
437 $nextLink = $sk->makeLinkObj( $title, $nextLink,
438 wfArrayToCGI( $query + array( 'from' => $last ) ) );
441 return "($prevLink) ($nextLink)";