* Set default disabled values for DjVu render options
[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 $this->children_start_char[] = $this->getSubcategorySortChar( $title, $sortkey );
120 }
121
122 /**
123 * Get the character to be used for sorting subcategories.
124 * If there's a link from Category:A to Category:B, the sortkey of the resulting
125 * entry in the categorylinks table is Category:A, not A, which it SHOULD be.
126 * Workaround: If sortkey == "Category:".$title, than use $title for sorting,
127 * else use sortkey...
128 */
129 function getSubcategorySortChar( $title, $sortkey ) {
130 global $wgContLang;
131
132 if( $title->getPrefixedText() == $sortkey ) {
133 $firstChar = $wgContLang->firstChar( $title->getDBkey() );
134 } else {
135 $firstChar = $wgContLang->firstChar( $sortkey );
136 }
137
138 return $wgContLang->convert( $firstChar );
139 }
140
141 /**
142 * Add a page in the image namespace
143 */
144 function addImage( $title, $sortkey, $pageLength ) {
145 if ( $this->showGallery ) {
146 $image = new Image( $title );
147 if( $this->flip ) {
148 $this->gallery->insert( $image );
149 } else {
150 $this->gallery->add( $image );
151 }
152 } else {
153 $this->addPage( $title, $sortkey, $pageLength );
154 }
155 }
156
157 /**
158 * Add a miscellaneous page
159 */
160 function addPage( $title, $sortkey, $pageLength ) {
161 global $wgContLang;
162 $this->articles[] = $this->getSkin()->makeSizeLinkObj(
163 $pageLength, $title, $wgContLang->convert( $title->getPrefixedText() )
164 );
165 $this->articles_start_char[] = $wgContLang->convert( $wgContLang->firstChar( $sortkey ) );
166 }
167
168 function finaliseCategoryState() {
169 if( $this->flip ) {
170 $this->children = array_reverse( $this->children );
171 $this->children_start_char = array_reverse( $this->children_start_char );
172 $this->articles = array_reverse( $this->articles );
173 $this->articles_start_char = array_reverse( $this->articles_start_char );
174 }
175 }
176
177 function doCategoryQuery() {
178 $dbr =& wfGetDB( DB_SLAVE );
179 if( $this->from != '' ) {
180 $pageCondition = 'cl_sortkey >= ' . $dbr->addQuotes( $this->from );
181 $this->flip = false;
182 } elseif( $this->until != '' ) {
183 $pageCondition = 'cl_sortkey < ' . $dbr->addQuotes( $this->until );
184 $this->flip = true;
185 } else {
186 $pageCondition = '1 = 1';
187 $this->flip = false;
188 }
189 $res = $dbr->select(
190 array( 'page', 'categorylinks' ),
191 array( 'page_title', 'page_namespace', 'page_len', 'cl_sortkey' ),
192 array( $pageCondition,
193 'cl_from = page_id',
194 'cl_to' => $this->title->getDBKey()),
195 #'page_is_redirect' => 0),
196 #+ $pageCondition,
197 __METHOD__,
198 array( 'ORDER BY' => $this->flip ? 'cl_sortkey DESC' : 'cl_sortkey',
199 'LIMIT' => $this->limit + 1 ) );
200
201 $count = 0;
202 $this->nextPage = null;
203 while( $x = $dbr->fetchObject ( $res ) ) {
204 if( ++$count > $this->limit ) {
205 // We've reached the one extra which shows that there are
206 // additional pages to be had. Stop here...
207 $this->nextPage = $x->cl_sortkey;
208 break;
209 }
210
211 $title = Title::makeTitle( $x->page_namespace, $x->page_title );
212
213 if( $title->getNamespace() == NS_CATEGORY ) {
214 $this->addSubcategory( $title, $x->cl_sortkey, $x->page_len );
215 } elseif( $title->getNamespace() == NS_IMAGE ) {
216 $this->addImage( $title, $x->cl_sortkey, $x->page_len );
217 } else {
218 $this->addPage( $title, $x->cl_sortkey, $x->page_len );
219 }
220 }
221 $dbr->freeResult( $res );
222 }
223
224 function getCategoryTop() {
225 $r = "<br style=\"clear:both;\"/>\n";
226 if( $this->until != '' ) {
227 $r .= $this->pagingLinks( $this->title, $this->nextPage, $this->until, $this->limit );
228 } elseif( $this->nextPage != '' || $this->from != '' ) {
229 $r .= $this->pagingLinks( $this->title, $this->from, $this->nextPage, $this->limit );
230 }
231 return $r;
232 }
233
234 function getSubcategorySection() {
235 # Don't show subcategories section if there are none.
236 $r = '';
237 if( count( $this->children ) > 0 ) {
238 # Showing subcategories
239 $r .= '<h2>' . wfMsg( 'subcategories' ) . "</h2>\n";
240 $r .= wfMsgExt( 'subcategorycount', array( 'parse' ), count( $this->children) );
241 $r .= $this->formatList( $this->children, $this->children_start_char );
242 }
243 return $r;
244 }
245
246 function getPagesSection() {
247 $ti = htmlspecialchars( $this->title->getText() );
248 $r = '<h2>' . wfMsg( 'category_header', $ti ) . "</h2>\n";
249 $r .= wfMsgExt( 'categoryarticlecount', array( 'parse' ), count( $this->articles) );
250 $r .= $this->formatList( $this->articles, $this->articles_start_char );
251 return $r;
252 }
253
254 function getImageSection() {
255 if( $this->showGallery && ! $this->gallery->isEmpty() ) {
256 return $this->gallery->toHTML();
257 } else {
258 return '';
259 }
260 }
261
262 function getCategoryBottom() {
263 if( $this->until != '' ) {
264 return $this->pagingLinks( $this->title, $this->nextPage, $this->until, $this->limit );
265 } elseif( $this->nextPage != '' || $this->from != '' ) {
266 return $this->pagingLinks( $this->title, $this->from, $this->nextPage, $this->limit );
267 } else {
268 return '';
269 }
270 }
271
272 /**
273 * Format a list of articles chunked by letter, either as a
274 * bullet list or a columnar format, depending on the length.
275 *
276 * @param array $articles
277 * @param array $articles_start_char
278 * @param int $cutoff
279 * @return string
280 * @private
281 */
282 function formatList( $articles, $articles_start_char, $cutoff = 6 ) {
283 if ( count ( $articles ) > $cutoff ) {
284 return $this->columnList( $articles, $articles_start_char );
285 } elseif ( count($articles) > 0) {
286 // for short lists of articles in categories.
287 return $this->shortList( $articles, $articles_start_char );
288 }
289 return '';
290 }
291
292 /**
293 * Format a list of articles chunked by letter in a three-column
294 * list, ordered vertically.
295 *
296 * @param array $articles
297 * @param array $articles_start_char
298 * @return string
299 * @private
300 */
301 function columnList( $articles, $articles_start_char ) {
302 // divide list into three equal chunks
303 $chunk = (int) (count ( $articles ) / 3);
304
305 // get and display header
306 $r = '<table width="100%"><tr valign="top">';
307
308 $prev_start_char = 'none';
309
310 // loop through the chunks
311 for($startChunk = 0, $endChunk = $chunk, $chunkIndex = 0;
312 $chunkIndex < 3;
313 $chunkIndex++, $startChunk = $endChunk, $endChunk += $chunk + 1)
314 {
315 $r .= "<td>\n";
316 $atColumnTop = true;
317
318 // output all articles in category
319 for ($index = $startChunk ;
320 $index < $endChunk && $index < count($articles);
321 $index++ )
322 {
323 // check for change of starting letter or begining of chunk
324 if ( ($index == $startChunk) ||
325 ($articles_start_char[$index] != $articles_start_char[$index - 1]) )
326
327 {
328 if( $atColumnTop ) {
329 $atColumnTop = false;
330 } else {
331 $r .= "</ul>\n";
332 }
333 $cont_msg = "";
334 if ( $articles_start_char[$index] == $prev_start_char )
335 $cont_msg = wfMsgHtml('listingcontinuesabbrev');
336 $r .= "<h3>" . htmlspecialchars( $articles_start_char[$index] ) . "$cont_msg</h3>\n<ul>";
337 $prev_start_char = $articles_start_char[$index];
338 }
339
340 $r .= "<li>{$articles[$index]}</li>";
341 }
342 if( !$atColumnTop ) {
343 $r .= "</ul>\n";
344 }
345 $r .= "</td>\n";
346
347
348 }
349 $r .= '</tr></table>';
350 return $r;
351 }
352
353 /**
354 * Format a list of articles chunked by letter in a bullet list.
355 * @param array $articles
356 * @param array $articles_start_char
357 * @return string
358 * @private
359 */
360 function shortList( $articles, $articles_start_char ) {
361 $r = '<h3>' . htmlspecialchars( $articles_start_char[0] ) . "</h3>\n";
362 $r .= '<ul><li>'.$articles[0].'</li>';
363 for ($index = 1; $index < count($articles); $index++ )
364 {
365 if ($articles_start_char[$index] != $articles_start_char[$index - 1])
366 {
367 $r .= "</ul><h3>" . htmlspecialchars( $articles_start_char[$index] ) . "</h3>\n<ul>";
368 }
369
370 $r .= "<li>{$articles[$index]}</li>";
371 }
372 $r .= '</ul>';
373 return $r;
374 }
375
376 /**
377 * @param Title $title
378 * @param string $first
379 * @param string $last
380 * @param int $limit
381 * @param array $query - additional query options to pass
382 * @return string
383 * @private
384 */
385 function pagingLinks( $title, $first, $last, $limit, $query = array() ) {
386 global $wgUser, $wgLang;
387 $sk =& $this->getSkin();
388 $limitText = $wgLang->formatNum( $limit );
389
390 $prevLink = htmlspecialchars( wfMsg( 'prevn', $limitText ) );
391 if( $first != '' ) {
392 $prevLink = $sk->makeLinkObj( $title, $prevLink,
393 wfArrayToCGI( $query + array( 'until' => $first ) ) );
394 }
395 $nextLink = htmlspecialchars( wfMsg( 'nextn', $limitText ) );
396 if( $last != '' ) {
397 $nextLink = $sk->makeLinkObj( $title, $nextLink,
398 wfArrayToCGI( $query + array( 'from' => $last ) ) );
399 }
400
401 return "($prevLink) ($nextLink)";
402 }
403 }
404
405
406 ?>