* (bug 2802) Display more than one character of the sort key.
[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();
11
12 global $wgCategoryMagicGallery;
13 if( $wgCategoryMagicGallery )
14 /** */
15 require_once('ImageGallery.php');
16
17 /**
18 * @package MediaWiki
19 */
20 class CategoryPage extends Article {
21
22 function view() {
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 # generate a list of subcategories and pages for a category
45 # depending on wfMsg("usenewcategorypage") it either calls the new
46 # or the old code. The new code will not work properly for some
47 # languages due to sorting issues, so they might want to turn it
48 # off.
49
50 function closeShowCategory() {
51 global $wgOut, $wgRequest;
52 $pageConditions = array();
53 $from = $wgRequest->getVal( 'from' );
54 $until = $wgRequest->getVal( 'until' );
55 $wgOut->addHTML( $this->doCategoryMagic( $from, $until ) );
56 }
57
58 /**
59 * Format the category data list.
60 *
61 * @param string $from -- return only sort keys from this item on
62 * @param string $until -- don't return keys after this point.
63 * @return string HTML output
64 * @access private
65 */
66 function doCategoryMagic( $from = '', $until = '' ) {
67 global $wgContLang,$wgUser, $wgCategoryMagicGallery;
68 $fname = 'CategoryPage::doCategoryMagic';
69 wfProfileIn( $fname );
70
71 $articles = array();
72 $articles_start_char = array();
73 $children = array();
74 $children_start_char = array();
75 $data = array();
76 if( $wgCategoryMagicGallery ) {
77 $ig = new ImageGallery();
78 }
79
80 $dbr =& wfGetDB( DB_SLAVE );
81 if( $from != '' ) {
82 $pageCondition = 'cl_sortkey >= ' . $dbr->addQuotes( $from );
83 $flip = false;
84 } elseif( $until != '' ) {
85 $pageCondition = 'cl_sortkey < ' . $dbr->addQuotes( $until );
86 $flip = true;
87 } else {
88 $pageCondition = '1';
89 $flip = false;
90 }
91 $limit = 200;
92 $res = $dbr->select(
93 array( 'page', 'categorylinks' ),
94 array( 'page_title', 'page_namespace', 'page_len', 'cl_sortkey' ),
95 array( $pageCondition,
96 'cl_from = page_id',
97 'cl_to' => $this->mTitle->getDBKey()),
98 #'page_is_redirect' => 0),
99 #+ $pageCondition,
100 $fname,
101 array( 'ORDER BY' => $flip ? 'cl_sortkey DESC' : 'cl_sortkey',
102 'LIMIT' => $limit + 1 ) );
103
104 $sk =& $wgUser->getSkin();
105 $r = "<br style=\"clear:both;\"/>\n";
106 $count = 0;
107 $nextPage = null;
108 while( $x = $dbr->fetchObject ( $res ) ) {
109 if( ++$count > $limit ) {
110 // We've reached the one extra which shows that there are
111 // additional pages to be had. Stop here...
112 $nextPage = $x->cl_sortkey;
113 break;
114 }
115
116 $title = Title::makeTitle( $x->page_namespace, $x->page_title );
117
118 if( $title->getNamespace() == NS_CATEGORY ) {
119 // Subcategory; strip the 'Category' namespace from the link text.
120 array_push( $children, $sk->makeKnownLinkObj( $title, $wgContLang->convertHtml( $title->getText() ) ) );
121
122 // If there's a link from Category:A to Category:B, the sortkey of the resulting
123 // entry in the categorylinks table is Category:A, not A, which it SHOULD be.
124 // Workaround: If sortkey == "Category:".$title, than use $title for sorting,
125 // else use sortkey...
126 $sortkey='';
127 if( $title->getPrefixedText() == $x->cl_sortkey ) {
128 $sortkey=$wgContLang->firstChar( $x->page_title );
129 } else {
130 $sortkey=$wgContLang->firstChar( $x->cl_sortkey );
131 }
132 array_push( $children_start_char, $wgContLang->convert( $sortkey ) ) ;
133 } elseif( $wgCategoryMagicGallery && $title->getNamespace() == NS_IMAGE ) {
134 // Show thumbnails of categorized images, in a separate chunk
135 if( $flip ) {
136 $ig->insert( Image::newFromTitle( $title ) );
137 } else {
138 $ig->add( Image::newFromTitle( $title ) );
139 }
140 } else {
141 // Page in this category
142 array_push( $articles, $sk->makeSizeLinkObj( $x->page_len, $title, $wgContLang->convert( $title->getPrefixedText() ) ) ) ;
143 array_push( $articles_start_char,$wgContLang->convert( $x->cl_sortkey) );
144 }
145 }
146
147 $root_length = 0;
148
149 for ($i = 0 ; $i < count($articles) - 1; ++$i){
150 if ($articles_start_char[$i][$root_length] != $articles_start_char[$i + 1][$root_length] ) {
151 break;
152 } elseif (count($articles) - 2 == $i) {
153 $root_length = $root_length + 1;
154 $i = -1;
155 }
156 }
157
158 for ($i = 0 ; $i < count($articles) ; ++$i) {
159 $articles_start_char[$i] = $wgContLang->truncate($articles_start_char[$i], $root_length + 1);
160 }
161
162 $dbr->freeResult( $res );
163
164 if( $flip ) {
165 $children = array_reverse( $children );
166 $children_start_char = array_reverse( $children_start_char );
167 $articles = array_reverse( $articles );
168 $articles_start_char = array_reverse( $articles_start_char );
169 }
170
171 if( $until != '' ) {
172 $r .= $this->pagingLinks( $this->mTitle, $nextPage, $until, $limit );
173 } elseif( $nextPage != '' || $from != '' ) {
174 $r .= $this->pagingLinks( $this->mTitle, $from, $nextPage, $limit );
175 }
176
177 # Don't show subcategories section if there are none.
178 if( count( $children ) > 0 ) {
179 # Showing subcategories
180 $r .= '<h2>' . wfMsg( 'subcategories' ) . "</h2>\n";
181 $r .= $this->formatCount( $children, 'subcategorycount' );
182 $r .= $this->formatList( $children, $children_start_char );
183 }
184
185 # Showing articles in this category
186 $ti = htmlspecialchars( $this->mTitle->getText() );
187 $r .= '<h2>' . wfMsg( 'category_header', $ti ) . "</h2>\n";
188 $r .= $this->formatCount( $articles, 'categoryarticlecount' );
189 $r .= $this->formatList( $articles, $articles_start_char );
190
191 if( $wgCategoryMagicGallery && ! $ig->isEmpty() ) {
192 $r.= $ig->toHTML();
193 }
194
195 wfProfileOut( $fname );
196 return $r;
197 }
198
199 /**
200 * @param array $articles
201 * @param string $message
202 * @return string
203 * @access private
204 */
205 function formatCount( $articles, $message ) {
206 global $wgContLang;
207 $numart = count( $articles );
208 if( $numart == 1 ) {
209 # Slightly different message to avoid silly plural
210 $message .= '1';
211 }
212 return wfMsg( $message, $wgContLang->formatNum( $numart ) );
213 }
214 /**
215 * Format a list of articles chunked by letter, either as a
216 * bullet list or a columnar format, depending on the length.
217 *
218 * @param array $articles
219 * @param array $articles_start_char
220 * @param int $cutoff
221 * @return string
222 * @access private
223 */
224 function formatList( $articles, $articles_start_char, $cutoff = 6 ) {
225 if ( count ( $articles ) > $cutoff ) {
226 return $this->columnList( $articles, $articles_start_char );
227 } elseif ( count($articles) > 0) {
228 // for short lists of articles in categories.
229 return $this->shortList( $articles, $articles_start_char );
230 }
231 return '';
232 }
233
234 /**
235 * Format a list of articles chunked by letter in a three-column
236 * list, ordered vertically.
237 *
238 * @param array $articles
239 * @param array $articles_start_char
240 * @return string
241 * @access private
242 */
243 function columnList( $articles, $articles_start_char ) {
244 // divide list into three equal chunks
245 $chunk = (int) (count ( $articles ) / 3);
246
247 // get and display header
248 $r = '<table width="100%"><tr valign="top">';
249
250 $prev_start_char = 'none';
251
252 // loop through the chunks
253 for($startChunk = 0, $endChunk = $chunk, $chunkIndex = 0;
254 $chunkIndex < 3;
255 $chunkIndex++, $startChunk = $endChunk, $endChunk += $chunk + 1)
256 {
257 $r .= "<td>\n";
258 $atColumnTop = true;
259
260 // output all articles in category
261 for ($index = $startChunk ;
262 $index < $endChunk && $index < count($articles);
263 $index++ )
264 {
265 // check for change of starting letter or begining of chunk
266 if ( ($index == $startChunk) ||
267 ($articles_start_char[$index] != $articles_start_char[$index - 1]) )
268
269 {
270 if( $atColumnTop ) {
271 $atColumnTop = false;
272 } else {
273 $r .= "</ul>\n";
274 }
275 $cont_msg = "";
276 if ( $articles_start_char[$index] == $prev_start_char )
277 $cont_msg = wfMsg('listingcontinuesabbrev');
278 $r .= "<h3>{$articles_start_char[$index]}$cont_msg</h3>\n<ul>";
279 $prev_start_char = $articles_start_char[$index];
280 }
281
282 $r .= "<li>{$articles[$index]}</li>";
283 }
284 if( !$atColumnTop ) {
285 $r .= "</ul>\n";
286 }
287 $r .= "</td>\n";
288
289
290 }
291 $r .= '</tr></table>';
292 return $r;
293 }
294
295 /**
296 * Format a list of articles chunked by letter in a bullet list.
297 * @param array $articles
298 * @param array $articles_start_char
299 * @return string
300 * @access private
301 */
302 function shortList( $articles, $articles_start_char ) {
303 $r = '<h3>'.$articles_start_char[0]."</h3>\n";
304 $r .= '<ul><li>'.$articles[0].'</li>';
305 for ($index = 1; $index < count($articles); $index++ )
306 {
307 if ($articles_start_char[$index] != $articles_start_char[$index - 1])
308 {
309 $r .= "</ul><h3>{$articles_start_char[$index]}</h3>\n<ul>";
310 }
311
312 $r .= "<li>{$articles[$index]}</li>";
313 }
314 $r .= '</ul>';
315 return $r;
316 }
317
318 /**
319 * @param Title $title
320 * @param string $first
321 * @param string $last
322 * @param int $limit
323 * @param array $query - additional query options to pass
324 * @return string
325 * @access private
326 */
327 function pagingLinks( $title, $first, $last, $limit, $query = array() ) {
328 global $wgUser, $wgLang;
329 $sk =& $wgUser->getSkin();
330 $limitText = $wgLang->formatNum( $limit );
331
332 $prevLink = htmlspecialchars( wfMsg( 'prevn', $limitText ) );
333 if( $first != '' ) {
334 $prevLink = $sk->makeLinkObj( $title, $prevLink,
335 wfArrayToCGI( $query + array( 'until' => $first ) ) );
336 }
337 $nextLink = htmlspecialchars( wfMsg( 'nextn', $limitText ) );
338 if( $last != '' ) {
339 $nextLink = $sk->makeLinkObj( $title, $nextLink,
340 wfArrayToCGI( $query + array( 'from' => $last ) ) );
341 }
342
343 return "($prevLink) ($nextLink)";
344 }
345 }
346
347
348 ?>