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