Fix http://bugzilla.wikipedia.org/show_bug.cgi?id=459 Sorting tabindex.
[mediawiki.git] / includes / CategoryPage.php
blob5070c1bde7c14ff4fc3aa5735e68bb6c10830ef9
1 <?php
2 /**
3 * Special handling for category description pages
4 * Modelled after ImagePage.php
6 * @package MediaWiki
7 */
9 /**
10 * @package MediaWiki
12 class CategoryPage extends Article {
14 function view() {
15 if ( NS_CATEGORY == $this->mTitle->getNamespace() ) {
16 $this->openShowCategory();
19 Article::view();
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.
25 if ( NS_CATEGORY == $this->mTitle->getNamespace() ) {
26 $this->closeShowCategory();
30 function openShowCategory() {
31 # For overloading
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.
40 function closeShowCategory() {
41 global $wgOut;
42 $msg = wfMsg('usenewcategorypage');
43 if ( '0' == @$msg[0] )
45 $wgOut->addHTML( $this->oldCategoryMagic() );
46 } else {
47 $wgOut->addHTML( $this->newCategoryMagic() );
51 # This method generates the list of subcategories and pages for a category
52 function oldCategoryMagic () {
53 global $wgLang, $wgUser ;
54 $fname = 'CategoryPage::oldCategoryMagic';
57 $sk =& $wgUser->getSkin() ;
59 $articles = array() ;
60 $children = array() ;
61 $r = '';
62 $id = $this->mTitle->getArticleID() ;
64 # FIXME: add limits
65 $dbr =& wfGetDB( DB_SLAVE );
66 $cur = $dbr->tableName( 'cur' );
67 $categorylinks = $dbr->tableName( 'categorylinks' );
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 ) )
76 $t = $wgLang->getNsText ( $x->cur_namespace ) ;
77 if ( $t != '' ) $t .= ':' ;
78 $t .= $x->cur_title ;
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
86 $dbr->freeResult ( $res ) ;
88 # Showing subcategories
89 if ( count ( $children ) > 0 ) {
90 $r .= '<h2>'.wfMsg('subcategories')."</h2>\n" ;
91 $r .= implode ( ', ' , $children ) ;
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 ) ;
102 return $r ;
105 function newCategoryMagic () {
106 global $wgLang,$wgUser;
108 $sk =& $wgUser->getSkin();
110 $r = "<br style=\"clear:both;\"/>\n";
112 $articles = array() ;
113 $articles_start_char = array();
114 $children = array() ;
115 $children_start_char = array();
116 $data = array () ;
117 $id = $this->mTitle->getArticleID() ;
119 # FIXME: add limits
120 $dbr =& wfGetDB( DB_SLAVE );
121 $cur = $dbr->tableName( 'cur' );
122 $categorylinks = $dbr->tableName( 'categorylinks' );
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 ) )
130 $t = $ns = $wgLang->getNsText ( $x->cur_namespace ) ;
131 if ( $t != '' ) $t .= ':' ;
132 $t .= $x->cur_title ;
134 if ( $x->cur_namespace == NS_CATEGORY ) {
135 $ctitle = str_replace( '_',' ',$x->cur_title );
136 array_push ( $children, $sk->makeKnownLink ( $t, $ctitle ) ) ; # Subcategory
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 ) ) ;
147 } else {
148 array_push ( $articles , $sk->makeKnownLink ( $t ) ) ; # Page in this category
149 array_push ( $articles_start_char, $wgLang->firstChar( $x->cl_sortkey ) ) ;
152 $dbr->freeResult ( $res ) ;
154 $ti = $this->mTitle->getText() ;
156 # Don't show subcategories section if there are none.
157 if ( count ( $children ) > 0 )
159 # Showing subcategories
160 $r .= '<h2>' . wfMsg( 'subcategories' ) . "</h2>\n";
162 $numchild = count( $children );
163 if($numchild == 1) {
164 $r .= wfMsg( 'subcategorycount1', 1 );
165 } else {
166 $r .= wfMsg( 'subcategorycount' , $numchild );
168 unset($numchild);
170 if ( count ( $children ) > 6 ) {
172 // divide list into three equal chunks
173 $chunk = (int) (count ( $children ) / 3);
175 // get and display header
176 $r .= '<table width="100%"><tr valign="top">';
178 $startChunk = 0;
179 $endChunk = $chunk;
181 // loop through the chunks
182 for($startChunk = 0, $endChunk = $chunk, $chunkIndex = 0;
183 $chunkIndex < 3;
184 $chunkIndex++, $startChunk = $endChunk, $endChunk += $chunk + 1)
187 $r .= '<td><ul>';
188 // output all subcategories to category
189 for ($index = $startChunk ;
190 $index < $endChunk && $index < count($children);
191 $index++ )
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]) )
197 $r .= "</ul><h3>{$children_start_char[$index]}</h3>\n<ul>";
200 $r .= "<li>{$children[$index]}</li>";
202 $r .= '</ul></td>';
206 $r .= '</tr></table>';
207 } else {
208 // for short lists of subcategories to category.
210 $r .= "<h3>{$children_start_char[0]}</h3>\n";
211 $r .= '<ul><li>'.$children[0].'</li>';
212 for ($index = 1; $index < count($children); $index++ )
214 if ($children_start_char[$index] != $children_start_char[$index - 1])
216 $r .= "</ul><h3>{$children_start_char[$index]}</h3>\n<ul>";
218 $r .= "<li>{$children[$index]}</li>";
220 $r .= '</ul>';
222 } # END of if ( count($children) > 0 )
224 $r .= '<h2>' . wfMsg( 'category_header', $ti ) . "</h2>\n";
226 $numart = count( $articles );
227 if($numart == 1) {
228 $r .= wfMsg( 'categoryarticlecount1', 1 );
229 } else {
230 $r .= wfMsg( 'categoryarticlecount' , $numart );
232 unset($numart);
234 # Showing articles in this category
235 if ( count ( $articles ) > 6) {
236 $ti = $this->mTitle->getText() ;
238 // divide list into three equal chunks
239 $chunk = (int) (count ( $articles ) / 3);
241 // get and display header
242 $r .= '<table width="100%"><tr valign="top">';
244 // loop through the chunks
245 for($startChunk = 0, $endChunk = $chunk, $chunkIndex = 0;
246 $chunkIndex < 3;
247 $chunkIndex++, $startChunk = $endChunk, $endChunk += $chunk + 1)
250 $r .= '<td><ul>';
252 // output all articles in category
253 for ($index = $startChunk ;
254 $index < $endChunk && $index < count($articles);
255 $index++ )
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]) )
262 $r .= "</ul><h3>{$articles_start_char[$index]}</h3>\n<ul>";
265 $r .= "<li>{$articles[$index]}</li>";
267 $r .= '</ul></td>';
271 $r .= '</tr></table>';
272 } elseif ( count($articles) > 0) {
273 // for short lists of articles in categories.
274 $ti = $this->mTitle->getText() ;
276 $r .= '<h3>'.$articles_start_char[0]."</h3>\n";
277 $r .= '<ul><li>'.$articles[0].'</li>';
278 for ($index = 1; $index < count($articles); $index++ )
280 if ($articles_start_char[$index] != $articles_start_char[$index - 1])
282 $r .= "</ul><h3>{$articles_start_char[$index]}</h3>\n<ul>";
285 $r .= "<li>{$articles[$index]}</li>";
287 $r .= '</ul>';
289 return $r ;