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