3 * Special handling for category description pages
4 * Modelled after ImagePage.php
9 if( !defined( 'MEDIAWIKI' ) )
12 if( $wgCategoryMagicGallery )
13 require_once('ImageGallery.php');
18 class CategoryPage
extends Article
{
21 if ( NS_CATEGORY
== $this->mTitle
->getNamespace() ) {
22 $this->openShowCategory();
27 # If the article we've just shown is in the "Image" namespace,
28 # follow it with the history list and link list for the image
31 if ( NS_CATEGORY
== $this->mTitle
->getNamespace() ) {
32 $this->closeShowCategory();
36 function openShowCategory() {
40 # generate a list of subcategories and pages for a category
41 # depending on wfMsg("usenewcategorypage") it either calls the new
42 # or the old code. The new code will not work properly for some
43 # languages due to sorting issues, so they might want to turn it
46 function closeShowCategory() {
48 $msg = wfMsg('usenewcategorypage');
49 if ( '0' == @$msg[0] )
51 $wgOut->addHTML( $this->oldCategoryMagic() );
53 $wgOut->addHTML( $this->newCategoryMagic() );
57 # This method generates the list of subcategories and pages for a category
58 function oldCategoryMagic () {
59 global $wgContLang, $wgUser ;
60 $fname = 'CategoryPage::oldCategoryMagic';
63 $sk =& $wgUser->getSkin() ;
68 $id = $this->mTitle
->getArticleID() ;
71 $dbr =& wfGetDB( DB_SLAVE
);
72 $cur = $dbr->tableName( 'cur' );
73 $categorylinks = $dbr->tableName( 'categorylinks' );
75 $t = $dbr->strencode( $this->mTitle
->getDBKey() );
76 $sql = "SELECT DISTINCT cur_title,cur_namespace FROM $cur,$categorylinks " .
77 "WHERE cl_to='$t' AND cl_from=cur_id AND cur_is_redirect=0 ORDER BY cl_sortkey" ;
78 $res = $dbr->query( $sql, $fname ) ;
79 # For all pages that link to this category
80 while ( $x = $dbr->fetchObject ( $res ) )
82 $t = $wgContLang->getNsText ( $x->cur_namespace
) ;
83 if ( $t != '' ) $t .= ':' ;
86 if ( $x->cur_namespace
== NS_CATEGORY
) {
87 array_push ( $children , $sk->makeLink ( $t ) ) ; # Subcategory
89 array_push ( $articles , $sk->makeLink ( $t ) ) ; # Page in this category
92 $dbr->freeResult ( $res ) ;
94 # Showing subcategories
95 if ( count ( $children ) > 0 ) {
96 $r .= '<h2>'.wfMsg('subcategories')."</h2>\n" ;
97 $r .= implode ( ', ' , $children ) ;
100 # Showing pages in this category
101 if ( count ( $articles ) > 0 ) {
102 $ti = $this->mTitle
->getText() ;
103 $h = wfMsg( 'category_header', $ti );
104 $r .= "<h2>$h</h2>\n" ;
105 $r .= implode ( ', ' , $articles ) ;
111 function newCategoryMagic () {
112 global $wgContLang,$wgUser, $wgCategoryMagicGallery;
114 $sk =& $wgUser->getSkin();
116 $r = "<br style=\"clear:both;\"/>\n";
118 $articles = array() ;
119 $articles_start_char = array();
120 $children = array() ;
121 $children_start_char = array();
123 $id = $this->mTitle
->getArticleID() ;
125 if ( $wgCategoryMagicGallery ) {
126 $ig = new ImageGallery();
130 $dbr =& wfGetDB( DB_SLAVE
);
131 $cur = $dbr->tableName( 'cur' );
132 $categorylinks = $dbr->tableName( 'categorylinks' );
134 $t = $dbr->strencode( $this->mTitle
->getDBKey() );
135 $sql = "SELECT DISTINCT cur_title,cur_namespace,cl_sortkey FROM " .
136 "$cur,$categorylinks WHERE cl_to='$t' AND cl_from=cur_id AND cur_is_redirect=0 ORDER BY cl_sortkey" ;
137 $res = $dbr->query ( $sql ) ;
138 while ( $x = $dbr->fetchObject ( $res ) )
140 $t = $ns = $wgContLang->getNsText ( $x->cur_namespace
) ;
141 if ( $t != '' ) $t .= ':' ;
142 $t .= $x->cur_title
;
143 $ctitle = str_replace( '_',' ',$x->cur_title
);
145 if ( $x->cur_namespace
== NS_CATEGORY
) {
146 array_push ( $children, $sk->makeKnownLink ( $t, $ctitle ) ) ; # Subcategory
148 // If there's a link from Category:A to Category:B, the sortkey of the resulting
149 // entry in the categorylinks table is Category:A, not A, which it SHOULD be.
150 // Workaround: If sortkey == "Category:".$title, than use $title for sorting,
151 // else use sortkey...
152 if ( ($ns.':'.$ctitle) == $x->cl_sortkey
) {
153 array_push ( $children_start_char, $wgContLang->firstChar( $x->cur_title
) );
155 array_push ( $children_start_char, $wgContLang->firstChar( $x->cl_sortkey
) ) ;
157 } elseif ( $wgCategoryMagicGallery && $x->cur_namespace
== NS_IMAGE
) {
158 $ig->add( new Image( $x->cur_title
) );
160 array_push ( $articles , $sk->makeKnownLink ( $t ) ) ; # Page in this category
161 array_push ( $articles_start_char, $wgContLang->firstChar( $x->cl_sortkey
) ) ;
164 $dbr->freeResult ( $res ) ;
166 $ti = $this->mTitle
->getText() ;
168 # Don't show subcategories section if there are none.
169 if ( count ( $children ) > 0 )
171 # Showing subcategories
172 $r .= '<h2>' . wfMsg( 'subcategories' ) . "</h2>\n";
174 $numchild = count( $children );
176 $r .= wfMsg( 'subcategorycount1', $wgContLang->formatNum( 1 ) );
178 $r .= wfMsg( 'subcategorycount' , $wgContLang->formatNum( $numchild ) );
182 if ( count ( $children ) > 6 ) {
184 // divide list into three equal chunks
185 $chunk = (int) (count ( $children ) / 3);
187 // get and display header
188 $r .= '<table width="100%"><tr valign="top">';
193 // loop through the chunks
194 for($startChunk = 0, $endChunk = $chunk, $chunkIndex = 0;
196 $chunkIndex++
, $startChunk = $endChunk, $endChunk +
= $chunk +
1)
200 // output all subcategories to category
201 for ($index = $startChunk ;
202 $index < $endChunk && $index < count($children);
205 // check for change of starting letter or begging of chunk
206 if ( ($index == $startChunk)
207 ||
($children_start_char[$index] != $children_start_char[$index - 1]) )
209 $r .= "</ul><h3>{$children_start_char[$index]}</h3>\n<ul>";
212 $r .= "<li>{$children[$index]}</li>";
218 $r .= '</tr></table>';
220 // for short lists of subcategories to category.
222 $r .= "<h3>{$children_start_char[0]}</h3>\n";
223 $r .= '<ul><li>'.$children[0].'</li>';
224 for ($index = 1; $index < count($children); $index++
)
226 if ($children_start_char[$index] != $children_start_char[$index - 1])
228 $r .= "</ul><h3>{$children_start_char[$index]}</h3>\n<ul>";
230 $r .= "<li>{$children[$index]}</li>";
234 } # END of if ( count($children) > 0 )
236 $r .= '<h2>' . wfMsg( 'category_header', $ti ) . "</h2>\n";
238 $numart = count( $articles );
240 $r .= wfMsg( 'categoryarticlecount1', $wgContLang->formatNum( 1 ) );
242 $r .= wfMsg( 'categoryarticlecount' , $wgContLang->formatNum( $numart ) );
246 # Showing articles in this category
247 if ( count ( $articles ) > 6) {
248 $ti = $this->mTitle
->getText() ;
250 // divide list into three equal chunks
251 $chunk = (int) (count ( $articles ) / 3);
253 // get and display header
254 $r .= '<table width="100%"><tr valign="top">';
256 $prev_start_char = "";
258 // loop through the chunks
259 for($startChunk = 0, $endChunk = $chunk, $chunkIndex = 0;
261 $chunkIndex++
, $startChunk = $endChunk, $endChunk +
= $chunk +
1)
266 // output all articles in category
267 for ($index = $startChunk ;
268 $index < $endChunk && $index < count($articles);
271 // check for change of starting letter or begging of chunk
272 if ( ($index == $startChunk) ||
273 ($articles_start_char[$index] != $articles_start_char[$index - 1]) )
277 if($articles_start_char[$index] == $prev_start_char)
278 $cont_msg = wfMsg('listingcontinuesabbrev');
279 $r .= "</ul><h3>{$articles_start_char[$index]}$cont_msg</h3>\n<ul>";
280 $prev_start_char = $articles_start_char[$index];
283 $r .= "<li>{$articles[$index]}</li>";
289 $r .= '</tr></table>';
290 } elseif ( count($articles) > 0) {
291 // for short lists of articles in categories.
292 $ti = $this->mTitle
->getText() ;
294 $r .= '<h3>'.$articles_start_char[0]."</h3>\n";
295 $r .= '<ul><li>'.$articles[0].'</li>';
296 for ($index = 1; $index < count($articles); $index++
)
298 if ($articles_start_char[$index] != $articles_start_char[$index - 1])
300 $r .= "</ul><h3>{$articles_start_char[$index]}</h3>\n<ul>";
303 $r .= "<li>{$articles[$index]}</li>";
308 if ( $wgCategoryMagicGallery && ! $ig->isEmpty() ) {