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