3e89a5fec20d828b8588e0c224740f4dd7fc13a3
[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 ( $wgCategoryMagicGallery )
10 require_once('ImageGallery.php');
11
12 /**
13 * @package MediaWiki
14 */
15 class CategoryPage extends Article {
16
17 function view() {
18 if ( NS_CATEGORY == $this->mTitle->getNamespace() ) {
19 $this->openShowCategory();
20 }
21
22 Article::view();
23
24 # If the article we've just shown is in the "Image" namespace,
25 # follow it with the history list and link list for the image
26 # it describes.
27
28 if ( NS_CATEGORY == $this->mTitle->getNamespace() ) {
29 $this->closeShowCategory();
30 }
31 }
32
33 function openShowCategory() {
34 # For overloading
35 }
36
37 # generate a list of subcategories and pages for a category
38 # depending on wfMsg("usenewcategorypage") it either calls the new
39 # or the old code. The new code will not work properly for some
40 # languages due to sorting issues, so they might want to turn it
41 # off.
42
43 function closeShowCategory() {
44 global $wgOut;
45 $msg = wfMsg('usenewcategorypage');
46 if ( '0' == @$msg[0] )
47 {
48 $wgOut->addHTML( $this->oldCategoryMagic() );
49 } else {
50 $wgOut->addHTML( $this->newCategoryMagic() );
51 }
52 }
53
54 # This method generates the list of subcategories and pages for a category
55 function oldCategoryMagic () {
56 global $wgContLang, $wgUser ;
57 $fname = 'CategoryPage::oldCategoryMagic';
58
59
60 $sk =& $wgUser->getSkin() ;
61
62 $articles = array() ;
63 $children = array() ;
64 $r = '';
65 $id = $this->mTitle->getArticleID() ;
66
67 # FIXME: add limits
68 $dbr =& wfGetDB( DB_SLAVE );
69 $cur = $dbr->tableName( 'cur' );
70 $categorylinks = $dbr->tableName( 'categorylinks' );
71
72 $t = $dbr->strencode( $this->mTitle->getDBKey() );
73 $sql = "SELECT DISTINCT cur_title,cur_namespace FROM $cur,$categorylinks " .
74 "WHERE cl_to='$t' AND cl_from=cur_id ORDER BY cl_sortkey" ;
75 $res = $dbr->query( $sql, $fname ) ;
76 # For all pages that link to this category
77 while ( $x = $dbr->fetchObject ( $res ) )
78 {
79 $t = $wgContLang->getNsText ( $x->cur_namespace ) ;
80 if ( $t != '' ) $t .= ':' ;
81 $t .= $x->cur_title ;
82
83 if ( $x->cur_namespace == NS_CATEGORY ) {
84 array_push ( $children , $sk->makeLink ( $t ) ) ; # Subcategory
85 } else {
86 array_push ( $articles , $sk->makeLink ( $t ) ) ; # Page in this category
87 }
88 }
89 $dbr->freeResult ( $res ) ;
90
91 # Showing subcategories
92 if ( count ( $children ) > 0 ) {
93 $r .= '<h2>'.wfMsg('subcategories')."</h2>\n" ;
94 $r .= implode ( ', ' , $children ) ;
95 }
96
97 # Showing pages in this category
98 if ( count ( $articles ) > 0 ) {
99 $ti = $this->mTitle->getText() ;
100 $h = wfMsg( 'category_header', $ti );
101 $r .= "<h2>$h</h2>\n" ;
102 $r .= implode ( ', ' , $articles ) ;
103 }
104
105 return $r ;
106 }
107
108 function newCategoryMagic () {
109 global $wgContLang,$wgUser, $wgCategoryMagicGallery;
110
111 $sk =& $wgUser->getSkin();
112
113 $r = "<br style=\"clear:both;\"/>\n";
114
115 $articles = array() ;
116 $articles_start_char = array();
117 $children = array() ;
118 $children_start_char = array();
119 $data = array () ;
120 $id = $this->mTitle->getArticleID() ;
121
122 if ( $wgCategoryMagicGallery ) {
123 $ig = new ImageGallery();
124 }
125
126 # FIXME: add limits
127 $dbr =& wfGetDB( DB_SLAVE );
128 $cur = $dbr->tableName( 'cur' );
129 $categorylinks = $dbr->tableName( 'categorylinks' );
130
131 $t = $dbr->strencode( $this->mTitle->getDBKey() );
132 $sql = "SELECT DISTINCT cur_title,cur_namespace,cl_sortkey FROM " .
133 "$cur,$categorylinks WHERE cl_to='$t' AND cl_from=cur_id ORDER BY cl_sortkey" ;
134 $res = $dbr->query ( $sql ) ;
135 while ( $x = $dbr->fetchObject ( $res ) )
136 {
137 $t = $ns = $wgContLang->getNsText ( $x->cur_namespace ) ;
138 if ( $t != '' ) $t .= ':' ;
139 $t .= $x->cur_title ;
140 $ctitle = str_replace( '_',' ',$x->cur_title );
141
142 if ( $x->cur_namespace == NS_CATEGORY ) {
143 array_push ( $children, $sk->makeKnownLink ( $t, $ctitle ) ) ; # Subcategory
144
145 // If there's a link from Category:A to Category:B, the sortkey of the resulting
146 // entry in the categorylinks table is Category:A, not A, which it SHOULD be.
147 // Workaround: If sortkey == "Category:".$title, than use $title for sorting,
148 // else use sortkey...
149 if ( ($ns.':'.$ctitle) == $x->cl_sortkey ) {
150 array_push ( $children_start_char, $wgContLang->firstChar( $x->cur_title ) );
151 } else {
152 array_push ( $children_start_char, $wgContLang->firstChar( $x->cl_sortkey ) ) ;
153 }
154 } elseif ( $wgCategoryMagicGallery && $x->cur_namespace == NS_IMAGE ) {
155 $ig->add( new Image( $x->cur_title ) );
156 } else {
157 array_push ( $articles , $sk->makeKnownLink ( $t ) ) ; # Page in this category
158 array_push ( $articles_start_char, $wgContLang->firstChar( $x->cl_sortkey ) ) ;
159 }
160 }
161 $dbr->freeResult ( $res ) ;
162
163 $ti = $this->mTitle->getText() ;
164
165 # Don't show subcategories section if there are none.
166 if ( count ( $children ) > 0 )
167 {
168 # Showing subcategories
169 $r .= '<h2>' . wfMsg( 'subcategories' ) . "</h2>\n";
170
171 $numchild = count( $children );
172 if($numchild == 1) {
173 $r .= wfMsg( 'subcategorycount1', 1 );
174 } else {
175 $r .= wfMsg( 'subcategorycount' , $numchild );
176 }
177 unset($numchild);
178
179 if ( count ( $children ) > 6 ) {
180
181 // divide list into three equal chunks
182 $chunk = (int) (count ( $children ) / 3);
183
184 // get and display header
185 $r .= '<table width="100%"><tr valign="top">';
186
187 $startChunk = 0;
188 $endChunk = $chunk;
189
190 // loop through the chunks
191 for($startChunk = 0, $endChunk = $chunk, $chunkIndex = 0;
192 $chunkIndex < 3;
193 $chunkIndex++, $startChunk = $endChunk, $endChunk += $chunk + 1)
194 {
195
196 $r .= '<td><ul>';
197 // output all subcategories to category
198 for ($index = $startChunk ;
199 $index < $endChunk && $index < count($children);
200 $index++ )
201 {
202 // check for change of starting letter or begging of chunk
203 if ( ($index == $startChunk)
204 || ($children_start_char[$index] != $children_start_char[$index - 1]) )
205 {
206 $r .= "</ul><h3>{$children_start_char[$index]}</h3>\n<ul>";
207 }
208
209 $r .= "<li>{$children[$index]}</li>";
210 }
211 $r .= '</ul></td>';
212
213
214 }
215 $r .= '</tr></table>';
216 } else {
217 // for short lists of subcategories to category.
218
219 $r .= "<h3>{$children_start_char[0]}</h3>\n";
220 $r .= '<ul><li>'.$children[0].'</li>';
221 for ($index = 1; $index < count($children); $index++ )
222 {
223 if ($children_start_char[$index] != $children_start_char[$index - 1])
224 {
225 $r .= "</ul><h3>{$children_start_char[$index]}</h3>\n<ul>";
226 }
227 $r .= "<li>{$children[$index]}</li>";
228 }
229 $r .= '</ul>';
230 }
231 } # END of if ( count($children) > 0 )
232
233 $r .= '<h2>' . wfMsg( 'category_header', $ti ) . "</h2>\n";
234
235 $numart = count( $articles );
236 if($numart == 1) {
237 $r .= wfMsg( 'categoryarticlecount1', 1 );
238 } else {
239 $r .= wfMsg( 'categoryarticlecount' , $numart );
240 }
241 unset($numart);
242
243 # Showing articles in this category
244 if ( count ( $articles ) > 6) {
245 $ti = $this->mTitle->getText() ;
246
247 // divide list into three equal chunks
248 $chunk = (int) (count ( $articles ) / 3);
249
250 // get and display header
251 $r .= '<table width="100%"><tr valign="top">';
252
253 // loop through the chunks
254 for($startChunk = 0, $endChunk = $chunk, $chunkIndex = 0;
255 $chunkIndex < 3;
256 $chunkIndex++, $startChunk = $endChunk, $endChunk += $chunk + 1)
257 {
258
259 $r .= '<td><ul>';
260
261 // output all articles in category
262 for ($index = $startChunk ;
263 $index < $endChunk && $index < count($articles);
264 $index++ )
265 {
266 // check for change of starting letter or begging of chunk
267 if ( ($index == $startChunk) ||
268 ($articles_start_char[$index] != $articles_start_char[$index - 1]) )
269
270 {
271 $r .= "</ul><h3>{$articles_start_char[$index]}</h3>\n<ul>";
272 }
273
274 $r .= "<li>{$articles[$index]}</li>";
275 }
276 $r .= '</ul></td>';
277
278
279 }
280 $r .= '</tr></table>';
281 } elseif ( count($articles) > 0) {
282 // for short lists of articles in categories.
283 $ti = $this->mTitle->getText() ;
284
285 $r .= '<h3>'.$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>{$articles_start_char[$index]}</h3>\n<ul>";
292 }
293
294 $r .= "<li>{$articles[$index]}</li>";
295 }
296 $r .= '</ul>';
297 }
298
299 if ( $wgCategoryMagicGallery && ! $ig->isEmpty() ) {
300 $r.= $ig->toHTML();
301 }
302
303 return $r ;
304 }
305 }
306 ?>