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