Add some conditional die()s to suppress fatal error messages if certain files are...
[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 $cur = $dbr->tableName( 'cur' );
73 $categorylinks = $dbr->tableName( 'categorylinks' );
74
75 $t = $dbr->strencode( $this->mTitle->getDBKey() );
76 $sql = "SELECT DISTINCT cur_title,cur_namespace FROM $cur,$categorylinks " .
77 "WHERE cl_to='$t' AND cl_from=cur_id AND cur_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->cur_namespace ) ;
83 if ( $t != '' ) $t .= ':' ;
84 $t .= $x->cur_title ;
85
86 if ( $x->cur_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 return $r ;
109 }
110
111 function newCategoryMagic () {
112 global $wgContLang,$wgUser, $wgCategoryMagicGallery;
113
114 $sk =& $wgUser->getSkin();
115
116 $r = "<br style=\"clear:both;\"/>\n";
117
118 $articles = array() ;
119 $articles_start_char = array();
120 $children = array() ;
121 $children_start_char = array();
122 $data = array () ;
123 $id = $this->mTitle->getArticleID() ;
124
125 if ( $wgCategoryMagicGallery ) {
126 $ig = new ImageGallery();
127 }
128
129 # FIXME: add limits
130 $dbr =& wfGetDB( DB_SLAVE );
131 $cur = $dbr->tableName( 'cur' );
132 $categorylinks = $dbr->tableName( 'categorylinks' );
133
134 $t = $dbr->strencode( $this->mTitle->getDBKey() );
135 $sql = "SELECT DISTINCT cur_title,cur_namespace,cl_sortkey FROM " .
136 "$cur,$categorylinks WHERE cl_to='$t' AND cl_from=cur_id AND cur_is_redirect=0 ORDER BY cl_sortkey" ;
137 $res = $dbr->query ( $sql ) ;
138 while ( $x = $dbr->fetchObject ( $res ) )
139 {
140 $t = $ns = $wgContLang->getNsText ( $x->cur_namespace ) ;
141 if ( $t != '' ) $t .= ':' ;
142 $t .= $x->cur_title ;
143 $ctitle = str_replace( '_',' ',$x->cur_title );
144
145 if ( $x->cur_namespace == NS_CATEGORY ) {
146 array_push ( $children, $sk->makeKnownLink ( $t, $ctitle ) ) ; # Subcategory
147
148 // If there's a link from Category:A to Category:B, the sortkey of the resulting
149 // entry in the categorylinks table is Category:A, not A, which it SHOULD be.
150 // Workaround: If sortkey == "Category:".$title, than use $title for sorting,
151 // else use sortkey...
152 if ( ($ns.':'.$ctitle) == $x->cl_sortkey ) {
153 array_push ( $children_start_char, $wgContLang->firstChar( $x->cur_title ) );
154 } else {
155 array_push ( $children_start_char, $wgContLang->firstChar( $x->cl_sortkey ) ) ;
156 }
157 } elseif ( $wgCategoryMagicGallery && $x->cur_namespace == NS_IMAGE ) {
158 $ig->add( new Image( $x->cur_title ) );
159 } else {
160 array_push ( $articles , $sk->makeKnownLink ( $t ) ) ; # Page in this category
161 array_push ( $articles_start_char, $wgContLang->firstChar( $x->cl_sortkey ) ) ;
162 }
163 }
164 $dbr->freeResult ( $res ) ;
165
166 $ti = $this->mTitle->getText() ;
167
168 # Don't show subcategories section if there are none.
169 if ( count ( $children ) > 0 )
170 {
171 # Showing subcategories
172 $r .= '<h2>' . wfMsg( 'subcategories' ) . "</h2>\n";
173
174 $numchild = count( $children );
175 if($numchild == 1) {
176 $r .= wfMsg( 'subcategorycount1', $wgContLang->formatNum( 1 ) );
177 } else {
178 $r .= wfMsg( 'subcategorycount' , $wgContLang->formatNum( $numchild ) );
179 }
180 unset($numchild);
181
182 if ( count ( $children ) > 6 ) {
183
184 // divide list into three equal chunks
185 $chunk = (int) (count ( $children ) / 3);
186
187 // get and display header
188 $r .= '<table width="100%"><tr valign="top">';
189
190 $startChunk = 0;
191 $endChunk = $chunk;
192
193 // loop through the chunks
194 for($startChunk = 0, $endChunk = $chunk, $chunkIndex = 0;
195 $chunkIndex < 3;
196 $chunkIndex++, $startChunk = $endChunk, $endChunk += $chunk + 1)
197 {
198
199 $r .= '<td><ul>';
200 // output all subcategories to category
201 for ($index = $startChunk ;
202 $index < $endChunk && $index < count($children);
203 $index++ )
204 {
205 // check for change of starting letter or begging of chunk
206 if ( ($index == $startChunk)
207 || ($children_start_char[$index] != $children_start_char[$index - 1]) )
208 {
209 $r .= "</ul><h3>{$children_start_char[$index]}</h3>\n<ul>";
210 }
211
212 $r .= "<li>{$children[$index]}</li>";
213 }
214 $r .= '</ul></td>';
215
216
217 }
218 $r .= '</tr></table>';
219 } else {
220 // for short lists of subcategories to category.
221
222 $r .= "<h3>{$children_start_char[0]}</h3>\n";
223 $r .= '<ul><li>'.$children[0].'</li>';
224 for ($index = 1; $index < count($children); $index++ )
225 {
226 if ($children_start_char[$index] != $children_start_char[$index - 1])
227 {
228 $r .= "</ul><h3>{$children_start_char[$index]}</h3>\n<ul>";
229 }
230 $r .= "<li>{$children[$index]}</li>";
231 }
232 $r .= '</ul>';
233 }
234 } # END of if ( count($children) > 0 )
235
236 $r .= '<h2>' . wfMsg( 'category_header', $ti ) . "</h2>\n";
237
238 $numart = count( $articles );
239 if($numart == 1) {
240 $r .= wfMsg( 'categoryarticlecount1', $wgContLang->formatNum( 1 ) );
241 } else {
242 $r .= wfMsg( 'categoryarticlecount' , $wgContLang->formatNum( $numart ) );
243 }
244 unset($numart);
245
246 # Showing articles in this category
247 if ( count ( $articles ) > 6) {
248 $ti = $this->mTitle->getText() ;
249
250 // divide list into three equal chunks
251 $chunk = (int) (count ( $articles ) / 3);
252
253 // get and display header
254 $r .= '<table width="100%"><tr valign="top">';
255
256 $prev_start_char = "";
257
258 // loop through the chunks
259 for($startChunk = 0, $endChunk = $chunk, $chunkIndex = 0;
260 $chunkIndex < 3;
261 $chunkIndex++, $startChunk = $endChunk, $endChunk += $chunk + 1)
262 {
263
264 $r .= '<td><ul>';
265
266 // output all articles in category
267 for ($index = $startChunk ;
268 $index < $endChunk && $index < count($articles);
269 $index++ )
270 {
271 // check for change of starting letter or begging of chunk
272 if ( ($index == $startChunk) ||
273 ($articles_start_char[$index] != $articles_start_char[$index - 1]) )
274
275 {
276 $cont_msg = "";
277 if($articles_start_char[$index] == $prev_start_char)
278 $cont_msg = wfMsg('listingcontinuesabbrev');
279 $r .= "</ul><h3>{$articles_start_char[$index]}$cont_msg</h3>\n<ul>";
280 $prev_start_char = $articles_start_char[$index];
281 }
282
283 $r .= "<li>{$articles[$index]}</li>";
284 }
285 $r .= '</ul></td>';
286
287
288 }
289 $r .= '</tr></table>';
290 } elseif ( count($articles) > 0) {
291 // for short lists of articles in categories.
292 $ti = $this->mTitle->getText() ;
293
294 $r .= '<h3>'.$articles_start_char[0]."</h3>\n";
295 $r .= '<ul><li>'.$articles[0].'</li>';
296 for ($index = 1; $index < count($articles); $index++ )
297 {
298 if ($articles_start_char[$index] != $articles_start_char[$index - 1])
299 {
300 $r .= "</ul><h3>{$articles_start_char[$index]}</h3>\n<ul>";
301 }
302
303 $r .= "<li>{$articles[$index]}</li>";
304 }
305 $r .= '</ul>';
306 }
307
308 if ( $wgCategoryMagicGallery && ! $ig->isEmpty() ) {
309 $r.= $ig->toHTML();
310 }
311
312 return $r ;
313 }
314 }
315 ?>