3 * Special handling for category description pages
4 * Modelled after ImagePage.php
12 class CategoryPage
extends Article
{
15 if ( NS_CATEGORY
== $this->mTitle
->getNamespace() ) {
16 $this->openShowCategory();
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
25 if ( NS_CATEGORY
== $this->mTitle
->getNamespace() ) {
26 $this->closeShowCategory();
30 function openShowCategory() {
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
40 function closeShowCategory() {
42 $msg = wfMsg('usenewcategorypage');
43 if ( '0' == @$msg[0] )
45 $wgOut->addHTML( $this->oldCategoryMagic() );
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() ;
62 $id = $this->mTitle
->getArticleID() ;
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 .= ':' ;
80 if ( $x->cur_namespace
== NS_CATEGORY
) {
81 array_push ( $children , $sk->makeLink ( $t ) ) ; # Subcategory
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 ) ;
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();
117 $id = $this->mTitle
->getArticleID() ;
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
) );
145 array_push ( $children_start_char, $wgLang->firstChar( $x->cl_sortkey
) ) ;
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 );
164 $r .= wfMsg( 'subcategorycount1', 1 );
166 $r .= wfMsg( 'subcategorycount' , $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">';
181 // loop through the chunks
182 for($startChunk = 0, $endChunk = $chunk, $chunkIndex = 0;
184 $chunkIndex++
, $startChunk = $endChunk, $endChunk +
= $chunk +
1)
188 // output all subcategories to category
189 for ($index = $startChunk ;
190 $index < $endChunk && $index < count($children);
193 // check for change of starting letter or begging of chunk
194 if ( ($children_start_char[$index] != $children_start_char[$index - 1])
195 ||
($index == $startChunk) )
197 $r .= "</ul><h3>{$children_start_char[$index]}</h3>\n<ul>";
200 $r .= "<li>{$children[$index]}</li>";
206 $r .= '</tr></table>';
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>";
222 } # END of if ( count($children) > 0 )
224 $r .= '<h2>' . wfMsg( 'category_header', $ti ) . "</h2>\n";
226 $numart = count( $articles );
228 $r .= wfMsg( 'categoryarticlecount1', 1 );
230 $r .= wfMsg( 'categoryarticlecount' , $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;
247 $chunkIndex++
, $startChunk = $endChunk, $endChunk +
= $chunk +
1)
252 // output all articles in category
253 for ($index = $startChunk ;
254 $index < $endChunk && $index < count($articles);
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>";
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>";