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