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