(bug 4970) Make category paging limits configurable
[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 $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, $wgCategoryPagingLimit;
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 = 1';
89 $flip = false;
90 }
91 $limit = $wgCategoryPagingLimit;
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( $wgContLang->firstChar( $x->cl_sortkey ) ) );
144 }
145 }
146 $dbr->freeResult( $res );
147
148 if( $flip ) {
149 $children = array_reverse( $children );
150 $children_start_char = array_reverse( $children_start_char );
151 $articles = array_reverse( $articles );
152 $articles_start_char = array_reverse( $articles_start_char );
153 }
154
155 if( $until != '' ) {
156 $r .= $this->pagingLinks( $this->mTitle, $nextPage, $until, $limit );
157 } elseif( $nextPage != '' || $from != '' ) {
158 $r .= $this->pagingLinks( $this->mTitle, $from, $nextPage, $limit );
159 }
160
161 # Don't show subcategories section if there are none.
162 if( count( $children ) > 0 ) {
163 # Showing subcategories
164 $r .= '<h2>' . wfMsg( 'subcategories' ) . "</h2>\n";
165 $r .= $this->formatCount( $children, 'subcategorycount' );
166 $r .= $this->formatList( $children, $children_start_char );
167 }
168
169 # Showing articles in this category
170 $ti = htmlspecialchars( $this->mTitle->getText() );
171 $r .= '<h2>' . wfMsg( 'category_header', $ti ) . "</h2>\n";
172 $r .= $this->formatCount( $articles, 'categoryarticlecount' );
173 $r .= $this->formatList( $articles, $articles_start_char );
174
175 if( $wgCategoryMagicGallery && ! $ig->isEmpty() ) {
176 $r.= $ig->toHTML();
177 }
178
179 if( $until != '' ) {
180 $r .= $this->pagingLinks( $this->mTitle, $nextPage, $until, $limit );
181 } elseif( $nextPage != '' || $from != '' ) {
182 $r .= $this->pagingLinks( $this->mTitle, $from, $nextPage, $limit );
183 }
184
185 wfProfileOut( $fname );
186 return $r;
187 }
188
189 /**
190 * @param array $articles
191 * @param string $message
192 * @return string
193 * @access private
194 */
195 function formatCount( $articles, $message ) {
196 global $wgContLang;
197 $numart = count( $articles );
198 if( $numart == 1 ) {
199 # Slightly different message to avoid silly plural
200 $message .= '1';
201 }
202 return wfMsg( $message, $wgContLang->formatNum( $numart ) );
203 }
204 /**
205 * Format a list of articles chunked by letter, either as a
206 * bullet list or a columnar format, depending on the length.
207 *
208 * @param array $articles
209 * @param array $articles_start_char
210 * @param int $cutoff
211 * @return string
212 * @access private
213 */
214 function formatList( $articles, $articles_start_char, $cutoff = 6 ) {
215 if ( count ( $articles ) > $cutoff ) {
216 return $this->columnList( $articles, $articles_start_char );
217 } elseif ( count($articles) > 0) {
218 // for short lists of articles in categories.
219 return $this->shortList( $articles, $articles_start_char );
220 }
221 return '';
222 }
223
224 /**
225 * Format a list of articles chunked by letter in a three-column
226 * list, ordered vertically.
227 *
228 * @param array $articles
229 * @param array $articles_start_char
230 * @return string
231 * @access private
232 */
233 function columnList( $articles, $articles_start_char ) {
234 // divide list into three equal chunks
235 $chunk = (int) (count ( $articles ) / 3);
236
237 // get and display header
238 $r = '<table width="100%"><tr valign="top">';
239
240 $prev_start_char = 'none';
241
242 // loop through the chunks
243 for($startChunk = 0, $endChunk = $chunk, $chunkIndex = 0;
244 $chunkIndex < 3;
245 $chunkIndex++, $startChunk = $endChunk, $endChunk += $chunk + 1)
246 {
247 $r .= "<td>\n";
248 $atColumnTop = true;
249
250 // output all articles in category
251 for ($index = $startChunk ;
252 $index < $endChunk && $index < count($articles);
253 $index++ )
254 {
255 // check for change of starting letter or begining of chunk
256 if ( ($index == $startChunk) ||
257 ($articles_start_char[$index] != $articles_start_char[$index - 1]) )
258
259 {
260 if( $atColumnTop ) {
261 $atColumnTop = false;
262 } else {
263 $r .= "</ul>\n";
264 }
265 $cont_msg = "";
266 if ( $articles_start_char[$index] == $prev_start_char )
267 $cont_msg = wfMsgHtml('listingcontinuesabbrev');
268 $r .= "<h3>" . htmlspecialchars( $articles_start_char[$index] ) . "$cont_msg</h3>\n<ul>";
269 $prev_start_char = $articles_start_char[$index];
270 }
271
272 $r .= "<li>{$articles[$index]}</li>";
273 }
274 if( !$atColumnTop ) {
275 $r .= "</ul>\n";
276 }
277 $r .= "</td>\n";
278
279
280 }
281 $r .= '</tr></table>';
282 return $r;
283 }
284
285 /**
286 * Format a list of articles chunked by letter in a bullet list.
287 * @param array $articles
288 * @param array $articles_start_char
289 * @return string
290 * @access private
291 */
292 function shortList( $articles, $articles_start_char ) {
293 $r = '<h3>' . htmlspecialchars( $articles_start_char[0] ) . "</h3>\n";
294 $r .= '<ul><li>'.$articles[0].'</li>';
295 for ($index = 1; $index < count($articles); $index++ )
296 {
297 if ($articles_start_char[$index] != $articles_start_char[$index - 1])
298 {
299 $r .= "</ul><h3>" . htmlspecialchars( $articles_start_char[$index] ) . "</h3>\n<ul>";
300 }
301
302 $r .= "<li>{$articles[$index]}</li>";
303 }
304 $r .= '</ul>';
305 return $r;
306 }
307
308 /**
309 * @param Title $title
310 * @param string $first
311 * @param string $last
312 * @param int $limit
313 * @param array $query - additional query options to pass
314 * @return string
315 * @access private
316 */
317 function pagingLinks( $title, $first, $last, $limit, $query = array() ) {
318 global $wgUser, $wgLang;
319 $sk =& $wgUser->getSkin();
320 $limitText = $wgLang->formatNum( $limit );
321
322 $prevLink = htmlspecialchars( wfMsg( 'prevn', $limitText ) );
323 if( $first != '' ) {
324 $prevLink = $sk->makeLinkObj( $title, $prevLink,
325 wfArrayToCGI( $query + array( 'until' => $first ) ) );
326 }
327 $nextLink = htmlspecialchars( wfMsg( 'nextn', $limitText ) );
328 if( $last != '' ) {
329 $nextLink = $sk->makeLinkObj( $title, $nextLink,
330 wfArrayToCGI( $query + array( 'from' => $last ) ) );
331 }
332
333 return "($prevLink) ($nextLink)";
334 }
335 }
336
337
338 ?>