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