* (bug 6072) Add a 'border' keyword to the image syntax
[mediawiki.git] / includes / CategoryPage.php
blob48f545d225d5c9bdda625c9a0620d7e53ce2b918
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 function openShowCategory() {
41 # For overloading
44 function closeShowCategory() {
45 global $wgOut, $wgRequest;
46 $from = $wgRequest->getVal( 'from' );
47 $until = $wgRequest->getVal( 'until' );
49 $viewer = new CategoryViewer( $this->mTitle, $from, $until );
50 $wgOut->addHTML( $viewer->getHTML() );
54 class CategoryViewer {
55 var $title, $limit, $from, $until,
56 $articles, $articles_start_char,
57 $children, $children_start_char,
58 $showGallery, $gallery,
59 $skin;
61 function __construct( $title, $from = '', $until = '' ) {
62 global $wgCategoryPagingLimit;
63 $this->title = $title;
64 $this->from = $from;
65 $this->until = $until;
66 $this->limit = $wgCategoryPagingLimit;
69 /**
70 * Format the category data list.
72 * @param string $from -- return only sort keys from this item on
73 * @param string $until -- don't return keys after this point.
74 * @return string HTML output
75 * @private
77 function getHTML() {
78 global $wgOut, $wgCategoryMagicGallery, $wgCategoryPagingLimit;
79 wfProfileIn( __METHOD__ );
81 $this->showGallery = $wgCategoryMagicGallery && !$wgOut->mNoGallery;
83 $this->clearCategoryState();
84 $this->doCategoryQuery();
85 $this->finaliseCategoryState();
87 $r = $this->getCategoryTop() .
88 $this->getSubcategorySection() .
89 $this->getPagesSection() .
90 $this->getImageSection() .
91 $this->getCategoryBottom();
93 wfProfileOut( __METHOD__ );
94 return $r;
97 function clearCategoryState() {
98 $this->articles = array();
99 $this->articles_start_char = array();
100 $this->children = array();
101 $this->children_start_char = array();
102 if( $this->showGallery ) {
103 $this->gallery = new ImageGallery();
104 $this->gallery->setParsing();
108 function getSkin() {
109 if ( !$this->skin ) {
110 global $wgUser;
111 $this->skin = $wgUser->getSkin();
113 return $this->skin;
117 * Add a subcategory to the internal lists
119 function addSubcategory( $title, $sortkey, $pageLength ) {
120 global $wgContLang;
121 // Subcategory; strip the 'Category' namespace from the link text.
122 $this->children[] = $this->getSkin()->makeKnownLinkObj(
123 $title, $wgContLang->convertHtml( $title->getText() ) );
125 $this->children_start_char[] = $this->getSubcategorySortChar( $title, $sortkey );
129 * Get the character to be used for sorting subcategories.
130 * If there's a link from Category:A to Category:B, the sortkey of the resulting
131 * entry in the categorylinks table is Category:A, not A, which it SHOULD be.
132 * Workaround: If sortkey == "Category:".$title, than use $title for sorting,
133 * else use sortkey...
135 function getSubcategorySortChar( $title, $sortkey ) {
136 global $wgContLang;
138 if( $title->getPrefixedText() == $sortkey ) {
139 $firstChar = $wgContLang->firstChar( $title->getDBkey() );
140 } else {
141 $firstChar = $wgContLang->firstChar( $sortkey );
144 return $wgContLang->convert( $firstChar );
148 * Add a page in the image namespace
150 function addImage( $title, $sortkey, $pageLength, $isRedirect = false ) {
151 if ( $this->showGallery ) {
152 $image = new Image( $title );
153 if( $this->flip ) {
154 $this->gallery->insert( $image );
155 } else {
156 $this->gallery->add( $image );
158 } else {
159 $this->addPage( $title, $sortkey, $pageLength, $isRedirect );
164 * Add a miscellaneous page
166 function addPage( $title, $sortkey, $pageLength, $isRedirect = false ) {
167 global $wgContLang;
168 $link = $this->getSkin()->makeSizeLinkObj(
169 $pageLength, $title, $wgContLang->convert( $title->getPrefixedText() )
171 if ($isRedirect)
172 $link = '<span class="redirect-in-category">'.$link.'</span>';
173 $this->articles[] = $link;
174 $this->articles_start_char[] = $wgContLang->convert( $wgContLang->firstChar( $sortkey ) );
177 function finaliseCategoryState() {
178 if( $this->flip ) {
179 $this->children = array_reverse( $this->children );
180 $this->children_start_char = array_reverse( $this->children_start_char );
181 $this->articles = array_reverse( $this->articles );
182 $this->articles_start_char = array_reverse( $this->articles_start_char );
186 function doCategoryQuery() {
187 $dbr = wfGetDB( DB_SLAVE );
188 if( $this->from != '' ) {
189 $pageCondition = 'cl_sortkey >= ' . $dbr->addQuotes( $this->from );
190 $this->flip = false;
191 } elseif( $this->until != '' ) {
192 $pageCondition = 'cl_sortkey < ' . $dbr->addQuotes( $this->until );
193 $this->flip = true;
194 } else {
195 $pageCondition = '1 = 1';
196 $this->flip = false;
198 $res = $dbr->select(
199 array( 'page', 'categorylinks' ),
200 array( 'page_title', 'page_namespace', 'page_len', 'page_is_redirect', 'cl_sortkey' ),
201 array( $pageCondition,
202 'cl_from = page_id',
203 'cl_to' => $this->title->getDBKey()),
204 #'page_is_redirect' => 0),
205 #+ $pageCondition,
206 __METHOD__,
207 array( 'ORDER BY' => $this->flip ? 'cl_sortkey DESC' : 'cl_sortkey',
208 'USE INDEX' => 'cl_sortkey',
209 'LIMIT' => $this->limit + 1 ) );
211 $count = 0;
212 $this->nextPage = null;
213 while( $x = $dbr->fetchObject ( $res ) ) {
214 if( ++$count > $this->limit ) {
215 // We've reached the one extra which shows that there are
216 // additional pages to be had. Stop here...
217 $this->nextPage = $x->cl_sortkey;
218 break;
221 $title = Title::makeTitle( $x->page_namespace, $x->page_title );
223 if( $title->getNamespace() == NS_CATEGORY ) {
224 $this->addSubcategory( $title, $x->cl_sortkey, $x->page_len );
225 } elseif( $title->getNamespace() == NS_IMAGE ) {
226 $this->addImage( $title, $x->cl_sortkey, $x->page_len, $x->page_is_redirect );
227 } else {
228 $this->addPage( $title, $x->cl_sortkey, $x->page_len, $x->page_is_redirect );
231 $dbr->freeResult( $res );
234 function getCategoryTop() {
235 $r = "<br style=\"clear:both;\"/>\n";
236 if( $this->until != '' ) {
237 $r .= $this->pagingLinks( $this->title, $this->nextPage, $this->until, $this->limit );
238 } elseif( $this->nextPage != '' || $this->from != '' ) {
239 $r .= $this->pagingLinks( $this->title, $this->from, $this->nextPage, $this->limit );
241 return $r;
244 function getSubcategorySection() {
245 # Don't show subcategories section if there are none.
246 $r = '';
247 $c = count( $this->children );
248 if( $c > 0 ) {
249 # Showing subcategories
250 $r .= "<div id=\"mw-subcategories\">\n";
251 $r .= '<h2>' . wfMsg( 'subcategories' ) . "</h2>\n";
252 $r .= wfMsgExt( 'subcategorycount', array( 'parse' ), $c );
253 $r .= $this->formatList( $this->children, $this->children_start_char );
254 $r .= "\n</div>";
256 return $r;
259 function getPagesSection() {
260 $ti = htmlspecialchars( $this->title->getText() );
261 # Don't show articles section if there are none.
262 $r = '';
263 $c = count( $this->articles );
264 if( $c > 0 ) {
265 $r = "<div id=\"mw-pages\">\n";
266 $r .= '<h2>' . wfMsg( 'category_header', $ti ) . "</h2>\n";
267 $r .= wfMsgExt( 'categoryarticlecount', array( 'parse' ), $c );
268 $r .= $this->formatList( $this->articles, $this->articles_start_char );
269 $r .= "\n</div>";
271 return $r;
274 function getImageSection() {
275 if( $this->showGallery && ! $this->gallery->isEmpty() ) {
276 return "<div id=\"mw-category-media\">\n" .
277 '<h2>' . wfMsg( 'category-media-header', htmlspecialchars($this->title->getText()) ) . "</h2>\n" .
278 wfMsgExt( 'category-media-count', array( 'parse' ), $this->gallery->count() ) .
279 $this->gallery->toHTML() . "\n</div>";
280 } else {
281 return '';
285 function getCategoryBottom() {
286 if( $this->until != '' ) {
287 return $this->pagingLinks( $this->title, $this->nextPage, $this->until, $this->limit );
288 } elseif( $this->nextPage != '' || $this->from != '' ) {
289 return $this->pagingLinks( $this->title, $this->from, $this->nextPage, $this->limit );
290 } else {
291 return '';
296 * Format a list of articles chunked by letter, either as a
297 * bullet list or a columnar format, depending on the length.
299 * @param array $articles
300 * @param array $articles_start_char
301 * @param int $cutoff
302 * @return string
303 * @private
305 function formatList( $articles, $articles_start_char, $cutoff = 6 ) {
306 if ( count ( $articles ) > $cutoff ) {
307 return $this->columnList( $articles, $articles_start_char );
308 } elseif ( count($articles) > 0) {
309 // for short lists of articles in categories.
310 return $this->shortList( $articles, $articles_start_char );
312 return '';
316 * Format a list of articles chunked by letter in a three-column
317 * list, ordered vertically.
319 * @param array $articles
320 * @param array $articles_start_char
321 * @return string
322 * @private
324 function columnList( $articles, $articles_start_char ) {
325 // divide list into three equal chunks
326 $chunk = (int) (count ( $articles ) / 3);
328 // get and display header
329 $r = '<table width="100%"><tr valign="top">';
331 $prev_start_char = 'none';
333 // loop through the chunks
334 for($startChunk = 0, $endChunk = $chunk, $chunkIndex = 0;
335 $chunkIndex < 3;
336 $chunkIndex++, $startChunk = $endChunk, $endChunk += $chunk + 1)
338 $r .= "<td>\n";
339 $atColumnTop = true;
341 // output all articles in category
342 for ($index = $startChunk ;
343 $index < $endChunk && $index < count($articles);
344 $index++ )
346 // check for change of starting letter or begining of chunk
347 if ( ($index == $startChunk) ||
348 ($articles_start_char[$index] != $articles_start_char[$index - 1]) )
351 if( $atColumnTop ) {
352 $atColumnTop = false;
353 } else {
354 $r .= "</ul>\n";
356 $cont_msg = "";
357 if ( $articles_start_char[$index] == $prev_start_char )
358 $cont_msg = wfMsgHtml('listingcontinuesabbrev');
359 $r .= "<h3>" . htmlspecialchars( $articles_start_char[$index] ) . "$cont_msg</h3>\n<ul>";
360 $prev_start_char = $articles_start_char[$index];
363 $r .= "<li>{$articles[$index]}</li>";
365 if( !$atColumnTop ) {
366 $r .= "</ul>\n";
368 $r .= "</td>\n";
372 $r .= '</tr></table>';
373 return $r;
377 * Format a list of articles chunked by letter in a bullet list.
378 * @param array $articles
379 * @param array $articles_start_char
380 * @return string
381 * @private
383 function shortList( $articles, $articles_start_char ) {
384 $r = '<h3>' . htmlspecialchars( $articles_start_char[0] ) . "</h3>\n";
385 $r .= '<ul><li>'.$articles[0].'</li>';
386 for ($index = 1; $index < count($articles); $index++ )
388 if ($articles_start_char[$index] != $articles_start_char[$index - 1])
390 $r .= "</ul><h3>" . htmlspecialchars( $articles_start_char[$index] ) . "</h3>\n<ul>";
393 $r .= "<li>{$articles[$index]}</li>";
395 $r .= '</ul>';
396 return $r;
400 * @param Title $title
401 * @param string $first
402 * @param string $last
403 * @param int $limit
404 * @param array $query - additional query options to pass
405 * @return string
406 * @private
408 function pagingLinks( $title, $first, $last, $limit, $query = array() ) {
409 global $wgUser, $wgLang;
410 $sk = $this->getSkin();
411 $limitText = $wgLang->formatNum( $limit );
413 $prevLink = htmlspecialchars( wfMsg( 'prevn', $limitText ) );
414 if( $first != '' ) {
415 $prevLink = $sk->makeLinkObj( $title, $prevLink,
416 wfArrayToCGI( $query + array( 'until' => $first ) ) );
418 $nextLink = htmlspecialchars( wfMsg( 'nextn', $limitText ) );
419 if( $last != '' ) {
420 $nextLink = $sk->makeLinkObj( $title, $nextLink,
421 wfArrayToCGI( $query + array( 'from' => $last ) ) );
424 return "($prevLink) ($nextLink)";