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