2bed882d22a89fca74c94e072a1465d7a2132f7a
[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;
48 $msg = wfMsg('usenewcategorypage');
49 if ( '0' == @$msg[0] )
50 {
51 $wgOut->addHTML( $this->oldCategoryMagic() );
52 } else {
53 $wgOut->addHTML( $this->newCategoryMagic() );
54 }
55 }
56
57 # This method generates the list of subcategories and pages for a category
58 function oldCategoryMagic () {
59 global $wgContLang, $wgUser ;
60 $fname = 'CategoryPage::oldCategoryMagic';
61
62
63 $sk =& $wgUser->getSkin() ;
64
65 $articles = array() ;
66 $children = array() ;
67 $r = '';
68 $id = $this->mTitle->getArticleID() ;
69
70 # FIXME: add limits
71 $dbr =& wfGetDB( DB_SLAVE );
72 $page = $dbr->tableName( 'page' );
73 $categorylinks = $dbr->tableName( 'categorylinks' );
74
75 $t = $dbr->strencode( $this->mTitle->getDBKey() );
76 $sql = "SELECT DISTINCT page_title, page_namespace FROM $page,$categorylinks " .
77 "WHERE cl_to='$t' AND cl_from=page_id AND page_is_redirect=0 ORDER BY cl_sortkey" ;
78 $res = $dbr->query( $sql, $fname ) ;
79 # For all pages that link to this category
80 while ( $x = $dbr->fetchObject ( $res ) )
81 {
82 $t = $wgContLang->getNsText ( $x->page_namespace ) ;
83 if ( $t != '' ) $t .= ':' ;
84 $t .= $x->page_title ;
85
86 if ( $x->page_namespace == NS_CATEGORY ) {
87 array_push ( $children , $sk->makeLink ( $t ) ) ; # Subcategory
88 } else {
89 array_push ( $articles , $sk->makeLink ( $t ) ) ; # Page in this category
90 }
91 }
92 $dbr->freeResult ( $res ) ;
93
94 # Showing subcategories
95 if ( count ( $children ) > 0 ) {
96 $r .= '<h2>'.wfMsg('subcategories')."</h2>\n" ;
97 $r .= implode ( ', ' , $children ) ;
98 }
99
100 # Showing pages in this category
101 if ( count ( $articles ) > 0 ) {
102 $ti = $this->mTitle->getText() ;
103 $h = wfMsg( 'category_header', $ti );
104 $r .= "<h2>$h</h2>\n" ;
105 $r .= implode ( ', ' , $articles ) ;
106 }
107
108 # Showing subcategories
109 if ( count ( $children ) > 0 ) {
110 $r .= '<h2>'.wfMsg('subcategories')."</h2>\n" ;
111 $r .= implode ( ', ' , $children ) ;
112 }
113
114 # Showing pages in this category
115 if ( count ( $articles ) > 0 ) {
116 $ti = $this->mTitle->getText() ;
117 $h = wfMsg( 'category_header', $ti );
118 $r .= "<h2>$h</h2>\n" ;
119 $r .= implode ( ', ' , $articles ) ;
120 }
121
122 return $r ;
123 }
124
125 function newCategoryMagic () {
126 global $wgContLang,$wgUser, $wgCategoryMagicGallery;
127
128 $sk =& $wgUser->getSkin();
129
130 $r = "<br style=\"clear:both;\"/>\n";
131
132 $articles = array() ;
133 $articles_start_char = array();
134 $children = array() ;
135 $children_start_char = array();
136 $data = array () ;
137 $id = $this->mTitle->getArticleID() ;
138
139 if ( $wgCategoryMagicGallery ) {
140 $ig = new ImageGallery();
141 }
142
143 # FIXME: add limits
144 $dbr =& wfGetDB( DB_SLAVE );
145 $page = $dbr->tableName( 'page' );
146 $categorylinks = $dbr->tableName( 'categorylinks' );
147
148 $t = $dbr->strencode( $this->mTitle->getDBKey() );
149 $sql = "SELECT DISTINCT page_title,page_namespace,cl_sortkey FROM " .
150 "$page,$categorylinks WHERE cl_to='$t' AND cl_from=page_id AND page_is_redirect ORDER BY cl_sortkey" ;
151 $res = $dbr->query ( $sql ) ;
152 while ( $x = $dbr->fetchObject ( $res ) )
153 {
154 $t = $ns = $wgContLang->getNsText ( $x->page_namespace ) ;
155 if ( $t != '' ) $t .= ':' ;
156 $t .= $x->page_title ;
157 $ctitle = str_replace( '_',' ',$x->page_title );
158
159 if ( $x->page_namespace == NS_CATEGORY ) {
160 array_push ( $children, $sk->makeKnownLink ( $t, $ctitle ) ) ; # Subcategory
161
162 // If there's a link from Category:A to Category:B, the sortkey of the resulting
163 // entry in the categorylinks table is Category:A, not A, which it SHOULD be.
164 // Workaround: If sortkey == "Category:".$title, than use $title for sorting,
165 // else use sortkey...
166 if ( ($ns.':'.$ctitle) == $x->cl_sortkey ) {
167 array_push ( $children_start_char, $wgContLang->firstChar( $x->page_title ) );
168 } else {
169 array_push ( $children_start_char, $wgContLang->firstChar( $x->cl_sortkey ) ) ;
170 }
171 } elseif ( $wgCategoryMagicGallery && $x->page_namespace == NS_IMAGE ) {
172 $ig->add( new Image( $x->page_title ) );
173 } else {
174 array_push ( $articles , $sk->makeKnownLink ( $t ) ) ; # Page in this category
175 array_push ( $articles_start_char, $wgContLang->firstChar( $x->cl_sortkey ) ) ;
176 }
177 }
178 $dbr->freeResult ( $res ) ;
179
180 $ti = $this->mTitle->getText() ;
181
182 # Don't show subcategories section if there are none.
183 if ( count ( $children ) > 0 )
184 {
185 # Showing subcategories
186 $r .= '<h2>' . wfMsg( 'subcategories' ) . "</h2>\n";
187
188 $numchild = count( $children );
189 if($numchild == 1) {
190 $r .= wfMsg( 'subcategorycount1', $wgContLang->formatNum( 1 ) );
191 } else {
192 $r .= wfMsg( 'subcategorycount' , $wgContLang->formatNum( $numchild ) );
193 }
194 unset($numchild);
195
196 if ( count ( $children ) > 6 ) {
197
198 // divide list into three equal chunks
199 $chunk = (int) (count ( $children ) / 3);
200
201 // get and display header
202 $r .= '<table width="100%"><tr valign="top">';
203
204 $startChunk = 0;
205 $endChunk = $chunk;
206
207 // loop through the chunks
208 for($startChunk = 0, $endChunk = $chunk, $chunkIndex = 0;
209 $chunkIndex < 3;
210 $chunkIndex++, $startChunk = $endChunk, $endChunk += $chunk + 1)
211 {
212
213 $r .= '<td><ul>';
214 // output all subcategories to category
215 for ($index = $startChunk ;
216 $index < $endChunk && $index < count($children);
217 $index++ )
218 {
219 // check for change of starting letter or begging of chunk
220 if ( ($index == $startChunk)
221 || ($children_start_char[$index] != $children_start_char[$index - 1]) )
222 {
223 $r .= "</ul><h3>{$children_start_char[$index]}</h3>\n<ul>";
224 }
225
226 $r .= "<li>{$children[$index]}</li>";
227 }
228 $r .= '</ul></td>';
229
230
231 }
232 $r .= '</tr></table>';
233 } else {
234 // for short lists of subcategories to category.
235
236 $r .= "<h3>{$children_start_char[0]}</h3>\n";
237 $r .= '<ul><li>'.$children[0].'</li>';
238 for ($index = 1; $index < count($children); $index++ )
239 {
240 if ($children_start_char[$index] != $children_start_char[$index - 1])
241 {
242 $r .= "</ul><h3>{$children_start_char[$index]}</h3>\n<ul>";
243 }
244 $r .= "<li>{$children[$index]}</li>";
245 }
246 $r .= '</ul>';
247 }
248 } # END of if ( count($children) > 0 )
249
250 $r .= '<h2>' . wfMsg( 'category_header', $ti ) . "</h2>\n";
251
252 $numart = count( $articles );
253 if($numart == 1) {
254 $r .= wfMsg( 'categoryarticlecount1', $wgContLang->formatNum( 1 ) );
255 } else {
256 $r .= wfMsg( 'categoryarticlecount' , $wgContLang->formatNum( $numart ) );
257 }
258 unset($numart);
259
260 # Showing articles in this category
261 if ( count ( $articles ) > 6) {
262 $ti = $this->mTitle->getText() ;
263
264 // divide list into three equal chunks
265 $chunk = (int) (count ( $articles ) / 3);
266
267 // get and display header
268 $r .= '<table width="100%"><tr valign="top">';
269
270 $prev_start_char = "";
271
272 // loop through the chunks
273 for($startChunk = 0, $endChunk = $chunk, $chunkIndex = 0;
274 $chunkIndex < 3;
275 $chunkIndex++, $startChunk = $endChunk, $endChunk += $chunk + 1)
276 {
277
278 $r .= '<td><ul>';
279
280 // output all articles in category
281 for ($index = $startChunk ;
282 $index < $endChunk && $index < count($articles);
283 $index++ )
284 {
285 // check for change of starting letter or begging of chunk
286 if ( ($index == $startChunk) ||
287 ($articles_start_char[$index] != $articles_start_char[$index - 1]) )
288
289 {
290 $cont_msg = "";
291 if($articles_start_char[$index] == $prev_start_char)
292 $cont_msg = wfMsg('listingcontinuesabbrev');
293 $r .= "</ul><h3>{$articles_start_char[$index]}$cont_msg</h3>\n<ul>";
294 $prev_start_char = $articles_start_char[$index];
295 }
296
297 $r .= "<li>{$articles[$index]}</li>";
298 }
299 $r .= '</ul></td>';
300
301
302 }
303 $r .= '</tr></table>';
304 } elseif ( count($articles) > 0) {
305 // for short lists of articles in categories.
306 $ti = $this->mTitle->getText() ;
307
308 $r .= '<h3>'.$articles_start_char[0]."</h3>\n";
309 $r .= '<ul><li>'.$articles[0].'</li>';
310 for ($index = 1; $index < count($articles); $index++ )
311 {
312 if ($articles_start_char[$index] != $articles_start_char[$index - 1])
313 {
314 $r .= "</ul><h3>{$articles_start_char[$index]}</h3>\n<ul>";
315 }
316
317 $r .= "<li>{$articles[$index]}</li>";
318 }
319 $r .= '</ul>';
320 }
321
322 if ( $wgCategoryMagicGallery && ! $ig->isEmpty() ) {
323 $r.= $ig->toHTML();
324 }
325
326 return $r ;
327 }
328 }
329 ?>