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