RC patrol fixlet: include rcid in 'diff' link in enhanced mode, for individually...
[mediawiki.git] / includes / CategoryPage.php
blob42dae594c5f1354b4975f352e388ad512ae088db
1 <?php
2 /**
3 * Special handling for category description pages
4 * Modelled after ImagePage.php
6 * @package MediaWiki
7 */
9 if ( $wgCategoryMagicGallery )
10 require_once('ImageGallery.php');
12 /**
13 * @package MediaWiki
15 class CategoryPage extends Article {
17 function view() {
18 if ( NS_CATEGORY == $this->mTitle->getNamespace() ) {
19 $this->openShowCategory();
22 Article::view();
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.
28 if ( NS_CATEGORY == $this->mTitle->getNamespace() ) {
29 $this->closeShowCategory();
33 function openShowCategory() {
34 # For overloading
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.
43 function closeShowCategory() {
44 global $wgOut;
45 $msg = wfMsg('usenewcategorypage');
46 if ( '0' == @$msg[0] )
48 $wgOut->addHTML( $this->oldCategoryMagic() );
49 } else {
50 $wgOut->addHTML( $this->newCategoryMagic() );
54 # This method generates the list of subcategories and pages for a category
55 function oldCategoryMagic () {
56 global $wgContLang, $wgUser ;
57 $fname = 'CategoryPage::oldCategoryMagic';
60 $sk =& $wgUser->getSkin() ;
62 $articles = array() ;
63 $children = array() ;
64 $r = '';
65 $id = $this->mTitle->getArticleID() ;
67 # FIXME: add limits
68 $dbr =& wfGetDB( DB_SLAVE );
69 $cur = $dbr->tableName( 'cur' );
70 $categorylinks = $dbr->tableName( 'categorylinks' );
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 AND cur_is_redirect=0 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 ) )
79 $t = $wgContLang->getNsText ( $x->cur_namespace ) ;
80 if ( $t != '' ) $t .= ':' ;
81 $t .= $x->cur_title ;
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
89 $dbr->freeResult ( $res ) ;
91 # Showing subcategories
92 if ( count ( $children ) > 0 ) {
93 $r .= '<h2>'.wfMsg('subcategories')."</h2>\n" ;
94 $r .= implode ( ', ' , $children ) ;
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 ) ;
105 return $r ;
108 function newCategoryMagic () {
109 global $wgContLang,$wgUser, $wgCategoryMagicGallery;
111 $sk =& $wgUser->getSkin();
113 $r = "<br style=\"clear:both;\"/>\n";
115 $articles = array() ;
116 $articles_start_char = array();
117 $children = array() ;
118 $children_start_char = array();
119 $data = array () ;
120 $id = $this->mTitle->getArticleID() ;
122 if ( $wgCategoryMagicGallery ) {
123 $ig = new ImageGallery();
126 # FIXME: add limits
127 $dbr =& wfGetDB( DB_SLAVE );
128 $cur = $dbr->tableName( 'cur' );
129 $categorylinks = $dbr->tableName( 'categorylinks' );
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 AND cur_is_redirect=0 ORDER BY cl_sortkey" ;
134 $res = $dbr->query ( $sql ) ;
135 while ( $x = $dbr->fetchObject ( $res ) )
137 $t = $ns = $wgContLang->getNsText ( $x->cur_namespace ) ;
138 if ( $t != '' ) $t .= ':' ;
139 $t .= $x->cur_title ;
140 $ctitle = str_replace( '_',' ',$x->cur_title );
142 if ( $x->cur_namespace == NS_CATEGORY ) {
143 array_push ( $children, $sk->makeKnownLink ( $t, $ctitle ) ) ; # Subcategory
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 ) ) ;
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 ) ) ;
161 $dbr->freeResult ( $res ) ;
163 $ti = $this->mTitle->getText() ;
165 # Don't show subcategories section if there are none.
166 if ( count ( $children ) > 0 )
168 # Showing subcategories
169 $r .= '<h2>' . wfMsg( 'subcategories' ) . "</h2>\n";
171 $numchild = count( $children );
172 if($numchild == 1) {
173 $r .= wfMsg( 'subcategorycount1', $wgContLang->formatNum( 1 ) );
174 } else {
175 $r .= wfMsg( 'subcategorycount' , $wgContLang->formatNum( $numchild ) );
177 unset($numchild);
179 if ( count ( $children ) > 6 ) {
181 // divide list into three equal chunks
182 $chunk = (int) (count ( $children ) / 3);
184 // get and display header
185 $r .= '<table width="100%"><tr valign="top">';
187 $startChunk = 0;
188 $endChunk = $chunk;
190 // loop through the chunks
191 for($startChunk = 0, $endChunk = $chunk, $chunkIndex = 0;
192 $chunkIndex < 3;
193 $chunkIndex++, $startChunk = $endChunk, $endChunk += $chunk + 1)
196 $r .= '<td><ul>';
197 // output all subcategories to category
198 for ($index = $startChunk ;
199 $index < $endChunk && $index < count($children);
200 $index++ )
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]) )
206 $r .= "</ul><h3>{$children_start_char[$index]}</h3>\n<ul>";
209 $r .= "<li>{$children[$index]}</li>";
211 $r .= '</ul></td>';
215 $r .= '</tr></table>';
216 } else {
217 // for short lists of subcategories to category.
219 $r .= "<h3>{$children_start_char[0]}</h3>\n";
220 $r .= '<ul><li>'.$children[0].'</li>';
221 for ($index = 1; $index < count($children); $index++ )
223 if ($children_start_char[$index] != $children_start_char[$index - 1])
225 $r .= "</ul><h3>{$children_start_char[$index]}</h3>\n<ul>";
227 $r .= "<li>{$children[$index]}</li>";
229 $r .= '</ul>';
231 } # END of if ( count($children) > 0 )
233 $r .= '<h2>' . wfMsg( 'category_header', $ti ) . "</h2>\n";
235 $numart = count( $articles );
236 if($numart == 1) {
237 $r .= wfMsg( 'categoryarticlecount1', $wgContLang->formatNum( 1 ) );
238 } else {
239 $r .= wfMsg( 'categoryarticlecount' , $wgContLang->formatNum( $numart ) );
241 unset($numart);
243 # Showing articles in this category
244 if ( count ( $articles ) > 6) {
245 $ti = $this->mTitle->getText() ;
247 // divide list into three equal chunks
248 $chunk = (int) (count ( $articles ) / 3);
250 // get and display header
251 $r .= '<table width="100%"><tr valign="top">';
253 $prev_start_char = "";
255 // loop through the chunks
256 for($startChunk = 0, $endChunk = $chunk, $chunkIndex = 0;
257 $chunkIndex < 3;
258 $chunkIndex++, $startChunk = $endChunk, $endChunk += $chunk + 1)
261 $r .= '<td><ul>';
263 // output all articles in category
264 for ($index = $startChunk ;
265 $index < $endChunk && $index < count($articles);
266 $index++ )
268 // check for change of starting letter or begging of chunk
269 if ( ($index == $startChunk) ||
270 ($articles_start_char[$index] != $articles_start_char[$index - 1]) )
273 $cont_msg = "";
274 if($articles_start_char[$index] == $prev_start_char)
275 $cont_msg = wfMsg('listingcontinuesabbrev');
276 $r .= "</ul><h3>{$articles_start_char[$index]}$cont_msg</h3>\n<ul>";
277 $prev_start_char = $articles_start_char[$index];
280 $r .= "<li>{$articles[$index]}</li>";
282 $r .= '</ul></td>';
286 $r .= '</tr></table>';
287 } elseif ( count($articles) > 0) {
288 // for short lists of articles in categories.
289 $ti = $this->mTitle->getText() ;
291 $r .= '<h3>'.$articles_start_char[0]."</h3>\n";
292 $r .= '<ul><li>'.$articles[0].'</li>';
293 for ($index = 1; $index < count($articles); $index++ )
295 if ($articles_start_char[$index] != $articles_start_char[$index - 1])
297 $r .= "</ul><h3>{$articles_start_char[$index]}</h3>\n<ul>";
300 $r .= "<li>{$articles[$index]}</li>";
302 $r .= '</ul>';
305 if ( $wgCategoryMagicGallery && ! $ig->isEmpty() ) {
306 $r.= $ig->toHTML();
309 return $r ;