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