* (bug 2274) Respect stub threshold in category page list
[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->convert( $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->getText() ) ) ) ;
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 wfProfileOut( $fname );
180 return $r;
181 }
182
183 /**
184 * @param array $articles
185 * @param string $message
186 * @return string
187 * @access private
188 */
189 function formatCount( $articles, $message ) {
190 global $wgContLang;
191 $numart = count( $articles );
192 if( $numart == 1 ) {
193 # Slightly different message to avoid silly plural
194 $message .= '1';
195 }
196 return wfMsg( $message, $wgContLang->formatNum( $numart ) );
197 }
198 /**
199 * Format a list of articles chunked by letter, either as a
200 * bullet list or a columnar format, depending on the length.
201 *
202 * @param array $articles
203 * @param array $articles_start_char
204 * @param int $cutoff
205 * @return string
206 * @access private
207 */
208 function formatList( $articles, $articles_start_char, $cutoff = 6 ) {
209 if ( count ( $articles ) > $cutoff ) {
210 return $this->columnList( $articles, $articles_start_char );
211 } elseif ( count($articles) > 0) {
212 // for short lists of articles in categories.
213 return $this->shortList( $articles, $articles_start_char );
214 }
215 return '';
216 }
217
218 /**
219 * Format a list of articles chunked by letter in a three-column
220 * list, ordered vertically.
221 *
222 * @param array $articles
223 * @param array $articles_start_char
224 * @return string
225 * @access private
226 */
227 function columnList( $articles, $articles_start_char ) {
228 // divide list into three equal chunks
229 $chunk = (int) (count ( $articles ) / 3);
230
231 // get and display header
232 $r = '<table width="100%"><tr valign="top">';
233
234 $prev_start_char = 'none';
235
236 // loop through the chunks
237 for($startChunk = 0, $endChunk = $chunk, $chunkIndex = 0;
238 $chunkIndex < 3;
239 $chunkIndex++, $startChunk = $endChunk, $endChunk += $chunk + 1)
240 {
241 $r .= "<td>\n";
242 $atColumnTop = true;
243
244 // output all articles in category
245 for ($index = $startChunk ;
246 $index < $endChunk && $index < count($articles);
247 $index++ )
248 {
249 // check for change of starting letter or begining of chunk
250 if ( ($index == $startChunk) ||
251 ($articles_start_char[$index] != $articles_start_char[$index - 1]) )
252
253 {
254 if( $atColumnTop ) {
255 $atColumnTop = false;
256 } else {
257 $r .= "</ul>\n";
258 }
259 $cont_msg = "";
260 if ( $articles_start_char[$index] == $prev_start_char )
261 $cont_msg = wfMsg('listingcontinuesabbrev');
262 $r .= "<h3>{$articles_start_char[$index]}$cont_msg</h3>\n<ul>";
263 $prev_start_char = $articles_start_char[$index];
264 }
265
266 $r .= "<li>{$articles[$index]}</li>";
267 }
268 if( !$atColumnTop ) {
269 $r .= "</ul>\n";
270 }
271 $r .= "</td>\n";
272
273
274 }
275 $r .= '</tr></table>';
276 return $r;
277 }
278
279 /**
280 * Format a list of articles chunked by letter in a bullet list.
281 * @param array $articles
282 * @param array $articles_start_char
283 * @return string
284 * @access private
285 */
286 function shortList( $articles, $articles_start_char ) {
287 $r = '<h3>'.$articles_start_char[0]."</h3>\n";
288 $r .= '<ul><li>'.$articles[0].'</li>';
289 for ($index = 1; $index < count($articles); $index++ )
290 {
291 if ($articles_start_char[$index] != $articles_start_char[$index - 1])
292 {
293 $r .= "</ul><h3>{$articles_start_char[$index]}</h3>\n<ul>";
294 }
295
296 $r .= "<li>{$articles[$index]}</li>";
297 }
298 $r .= '</ul>';
299 return $r;
300 }
301
302 /**
303 * @param Title $title
304 * @param string $first
305 * @param string $last
306 * @param int $limit
307 * @param array $query - additional query options to pass
308 * @return string
309 * @access private
310 */
311 function pagingLinks( $title, $first, $last, $limit, $query = array() ) {
312 global $wgUser, $wgLang;
313 $sk =& $wgUser->getSkin();
314 $limitText = $wgLang->formatNum( $limit );
315
316 $prevLink = htmlspecialchars( wfMsg( 'prevn', $limitText ) );
317 if( $first != '' ) {
318 $prevLink = $sk->makeLinkObj( $title, $prevLink,
319 wfArrayToCGI( $query + array( 'until' => $first ) ) );
320 }
321 $nextLink = htmlspecialchars( wfMsg( 'nextn', $limitText ) );
322 if( $last != '' ) {
323 $nextLink = $sk->makeLinkObj( $title, $nextLink,
324 wfArrayToCGI( $query + array( 'from' => $last ) ) );
325 }
326
327 return "($prevLink) ($nextLink)";
328 }
329 }
330
331
332 ?>