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 $page = $dbr->tableName( 'page' );
73 $categorylinks = $dbr->tableName( 'categorylinks' );
75 $t = $dbr->strencode( $this->mTitle
->getDBKey() );
76 $sql = "SELECT DISTINCT page_title, page_namespace FROM $page,$categorylinks " .
77 "WHERE cl_to='$t' AND cl_from=page_id AND page_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->page_namespace
) ;
83 if ( $t != '' ) $t .= ':' ;
84 $t .= $x->page_title
;
86 if ( $x->page_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 ) ;
108 # Showing subcategories
109 if ( count ( $children ) > 0 ) {
110 $r .= '<h2>'.wfMsg('subcategories')."</h2>\n" ;
111 $r .= implode ( ', ' , $children ) ;
114 # Showing pages in this category
115 if ( count ( $articles ) > 0 ) {
116 $ti = $this->mTitle
->getText() ;
117 $h = wfMsg( 'category_header', $ti );
118 $r .= "<h2>$h</h2>\n" ;
119 $r .= implode ( ', ' , $articles ) ;
125 function newCategoryMagic () {
126 global $wgContLang,$wgUser, $wgCategoryMagicGallery;
128 $sk =& $wgUser->getSkin();
130 $r = "<br style=\"clear:both;\"/>\n";
132 $articles = array() ;
133 $articles_start_char = array();
134 $children = array() ;
135 $children_start_char = array();
137 $id = $this->mTitle
->getArticleID() ;
139 if ( $wgCategoryMagicGallery ) {
140 $ig = new ImageGallery();
144 $dbr =& wfGetDB( DB_SLAVE
);
145 $page = $dbr->tableName( 'page' );
146 $categorylinks = $dbr->tableName( 'categorylinks' );
148 $t = $dbr->strencode( $this->mTitle
->getDBKey() );
149 $sql = "SELECT DISTINCT page_title,page_namespace,cl_sortkey FROM " .
150 "$page,$categorylinks WHERE cl_to='$t' AND cl_from=page_id AND page_is_redirect ORDER BY cl_sortkey" ;
151 $res = $dbr->query ( $sql ) ;
152 while ( $x = $dbr->fetchObject ( $res ) )
154 $t = $ns = $wgContLang->getNsText ( $x->page_namespace
) ;
155 if ( $t != '' ) $t .= ':' ;
156 $t .= $x->page_title
;
157 $ctitle = str_replace( '_',' ',$x->page_title
);
159 if ( $x->page_namespace
== NS_CATEGORY
) {
160 array_push ( $children, $sk->makeKnownLink ( $t, $ctitle ) ) ; # Subcategory
162 // If there's a link from Category:A to Category:B, the sortkey of the resulting
163 // entry in the categorylinks table is Category:A, not A, which it SHOULD be.
164 // Workaround: If sortkey == "Category:".$title, than use $title for sorting,
165 // else use sortkey...
166 if ( ($ns.':'.$ctitle) == $x->cl_sortkey
) {
167 array_push ( $children_start_char, $wgContLang->firstChar( $x->page_title
) );
169 array_push ( $children_start_char, $wgContLang->firstChar( $x->cl_sortkey
) ) ;
171 } elseif ( $wgCategoryMagicGallery && $x->page_namespace
== NS_IMAGE
) {
172 $ig->add( new Image( $x->page_title
) );
174 array_push ( $articles , $sk->makeKnownLink ( $t ) ) ; # Page in this category
175 array_push ( $articles_start_char, $wgContLang->firstChar( $x->cl_sortkey
) ) ;
178 $dbr->freeResult ( $res ) ;
180 $ti = $this->mTitle
->getText() ;
182 # Don't show subcategories section if there are none.
183 if ( count ( $children ) > 0 )
185 # Showing subcategories
186 $r .= '<h2>' . wfMsg( 'subcategories' ) . "</h2>\n";
188 $numchild = count( $children );
190 $r .= wfMsg( 'subcategorycount1', $wgContLang->formatNum( 1 ) );
192 $r .= wfMsg( 'subcategorycount' , $wgContLang->formatNum( $numchild ) );
196 if ( count ( $children ) > 6 ) {
198 // divide list into three equal chunks
199 $chunk = (int) (count ( $children ) / 3);
201 // get and display header
202 $r .= '<table width="100%"><tr valign="top">';
207 // loop through the chunks
208 for($startChunk = 0, $endChunk = $chunk, $chunkIndex = 0;
210 $chunkIndex++
, $startChunk = $endChunk, $endChunk +
= $chunk +
1)
214 // output all subcategories to category
215 for ($index = $startChunk ;
216 $index < $endChunk && $index < count($children);
219 // check for change of starting letter or begging of chunk
220 if ( ($index == $startChunk)
221 ||
($children_start_char[$index] != $children_start_char[$index - 1]) )
223 $r .= "</ul><h3>{$children_start_char[$index]}</h3>\n<ul>";
226 $r .= "<li>{$children[$index]}</li>";
232 $r .= '</tr></table>';
234 // for short lists of subcategories to category.
236 $r .= "<h3>{$children_start_char[0]}</h3>\n";
237 $r .= '<ul><li>'.$children[0].'</li>';
238 for ($index = 1; $index < count($children); $index++
)
240 if ($children_start_char[$index] != $children_start_char[$index - 1])
242 $r .= "</ul><h3>{$children_start_char[$index]}</h3>\n<ul>";
244 $r .= "<li>{$children[$index]}</li>";
248 } # END of if ( count($children) > 0 )
250 $r .= '<h2>' . wfMsg( 'category_header', $ti ) . "</h2>\n";
252 $numart = count( $articles );
254 $r .= wfMsg( 'categoryarticlecount1', $wgContLang->formatNum( 1 ) );
256 $r .= wfMsg( 'categoryarticlecount' , $wgContLang->formatNum( $numart ) );
260 # Showing articles in this category
261 if ( count ( $articles ) > 6) {
262 $ti = $this->mTitle
->getText() ;
264 // divide list into three equal chunks
265 $chunk = (int) (count ( $articles ) / 3);
267 // get and display header
268 $r .= '<table width="100%"><tr valign="top">';
270 $prev_start_char = "";
272 // loop through the chunks
273 for($startChunk = 0, $endChunk = $chunk, $chunkIndex = 0;
275 $chunkIndex++
, $startChunk = $endChunk, $endChunk +
= $chunk +
1)
280 // output all articles in category
281 for ($index = $startChunk ;
282 $index < $endChunk && $index < count($articles);
285 // check for change of starting letter or begging of chunk
286 if ( ($index == $startChunk) ||
287 ($articles_start_char[$index] != $articles_start_char[$index - 1]) )
291 if($articles_start_char[$index] == $prev_start_char)
292 $cont_msg = wfMsg('listingcontinuesabbrev');
293 $r .= "</ul><h3>{$articles_start_char[$index]}$cont_msg</h3>\n<ul>";
294 $prev_start_char = $articles_start_char[$index];
297 $r .= "<li>{$articles[$index]}</li>";
303 $r .= '</tr></table>';
304 } elseif ( count($articles) > 0) {
305 // for short lists of articles in categories.
306 $ti = $this->mTitle
->getText() ;
308 $r .= '<h3>'.$articles_start_char[0]."</h3>\n";
309 $r .= '<ul><li>'.$articles[0].'</li>';
310 for ($index = 1; $index < count($articles); $index++
)
312 if ($articles_start_char[$index] != $articles_start_char[$index - 1])
314 $r .= "</ul><h3>{$articles_start_char[$index]}</h3>\n<ul>";
317 $r .= "<li>{$articles[$index]}</li>";
322 if ( $wgCategoryMagicGallery && ! $ig->isEmpty() ) {