3 * Special handling for category description pages
4 * Modelled after ImagePage.php
9 if ( $wgCategoryMagicGallery )
10 require_once('ImageGallery.php');
15 class CategoryPage
extends Article
{
18 if ( NS_CATEGORY
== $this->mTitle
->getNamespace() ) {
19 $this->openShowCategory();
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
28 if ( NS_CATEGORY
== $this->mTitle
->getNamespace() ) {
29 $this->closeShowCategory();
33 function openShowCategory() {
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
43 function closeShowCategory() {
45 $msg = wfMsg('usenewcategorypage');
46 if ( '0' == @$msg[0] )
48 $wgOut->addHTML( $this->oldCategoryMagic() );
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() ;
65 $id = $this->mTitle
->getArticleID() ;
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 .= ':' ;
83 if ( $x->cur_namespace
== NS_CATEGORY
) {
84 array_push ( $children , $sk->makeLink ( $t ) ) ; # Subcategory
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 ) ;
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();
120 $id = $this->mTitle
->getArticleID() ;
122 if ( $wgCategoryMagicGallery ) {
123 $ig = new ImageGallery();
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
) );
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
) );
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 );
173 $r .= wfMsg( 'subcategorycount1', 1 );
175 $r .= wfMsg( 'subcategorycount' , $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">';
190 // loop through the chunks
191 for($startChunk = 0, $endChunk = $chunk, $chunkIndex = 0;
193 $chunkIndex++
, $startChunk = $endChunk, $endChunk +
= $chunk +
1)
197 // output all subcategories to category
198 for ($index = $startChunk ;
199 $index < $endChunk && $index < count($children);
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>";
215 $r .= '</tr></table>';
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>";
231 } # END of if ( count($children) > 0 )
233 $r .= '<h2>' . wfMsg( 'category_header', $ti ) . "</h2>\n";
235 $numart = count( $articles );
237 $r .= wfMsg( 'categoryarticlecount1', 1 );
239 $r .= wfMsg( 'categoryarticlecount' , $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 // loop through the chunks
254 for($startChunk = 0, $endChunk = $chunk, $chunkIndex = 0;
256 $chunkIndex++
, $startChunk = $endChunk, $endChunk +
= $chunk +
1)
261 // output all articles in category
262 for ($index = $startChunk ;
263 $index < $endChunk && $index < count($articles);
266 // check for change of starting letter or begging of chunk
267 if ( ($index == $startChunk) ||
268 ($articles_start_char[$index] != $articles_start_char[$index - 1]) )
271 $r .= "</ul><h3>{$articles_start_char[$index]}</h3>\n<ul>";
274 $r .= "<li>{$articles[$index]}</li>";
280 $r .= '</tr></table>';
281 } elseif ( count($articles) > 0) {
282 // for short lists of articles in categories.
283 $ti = $this->mTitle
->getText() ;
285 $r .= '<h3>'.$articles_start_char[0]."</h3>\n";
286 $r .= '<ul><li>'.$articles[0].'</li>';
287 for ($index = 1; $index < count($articles); $index++
)
289 if ($articles_start_char[$index] != $articles_start_char[$index - 1])
291 $r .= "</ul><h3>{$articles_start_char[$index]}</h3>\n<ul>";
294 $r .= "<li>{$articles[$index]}</li>";
299 if ( $wgCategoryMagicGallery && ! $ig->isEmpty() ) {