84bf394144bfc30735bc1c911b21ce6b409ae938
3 * Special handling for category description pages
4 * Modelled after ImagePage.php
11 class CategoryPage
extends Article
{
14 if ( NS_CATEGORY
== $this->mTitle
->getNamespace() ) {
15 $this->openShowCategory();
20 # If the article we've just shown is in the "Image" namespace,
21 # follow it with the history list and link list for the image
24 if ( NS_CATEGORY
== $this->mTitle
->getNamespace() ) {
25 $this->closeShowCategory();
29 function openShowCategory() {
33 # generate a list of subcategories and pages for a category
34 # depending on wfMsg("usenewcategorypage") it either calls the new
35 # or the old code. The new code will not work properly for some
36 # languages due to sorting issues, so they might want to turn it
39 function closeShowCategory() {
41 $msg = wfMsg('usenewcategorypage');
42 if ( '0' == @$msg[0] )
44 $wgOut->addHTML( $this->oldCategoryMagic() );
46 $wgOut->addHTML( $this->newCategoryMagic() );
50 # This method generates the list of subcategories and pages for a category
51 function oldCategoryMagic () {
52 global $wgLang, $wgUser ;
53 $fname = 'CategoryPage::oldCategoryMagic';
56 $sk =& $wgUser->getSkin() ;
61 $id = $this->mTitle
->getArticleID() ;
64 $dbr =& wfGetDB( DB_SLAVE
);
65 $cur = $dbr->tableName( 'cur' );
66 $categorylinks = $dbr->tableName( 'categorylinks' );
68 $t = $dbr->strencode( $this->mTitle
->getDBKey() );
69 $sql = "SELECT DISTINCT cur_title,cur_namespace FROM $cur,$categorylinks " .
70 "WHERE cl_to='$t' AND cl_from=cur_id ORDER BY cl_sortkey" ;
71 $res = $dbr->query( $sql, $fname ) ;
72 # For all pages that link to this category
73 while ( $x = $dbr->fetchObject ( $res ) )
75 $t = $wgLang->getNsText ( $x->cur_namespace
) ;
76 if ( $t != '' ) $t .= ':' ;
79 if ( $x->cur_namespace
== NS_CATEGORY
) {
80 array_push ( $children , $sk->makeLink ( $t ) ) ; # Subcategory
82 array_push ( $articles , $sk->makeLink ( $t ) ) ; # Page in this category
85 $dbr->freeResult ( $res ) ;
87 # Showing subcategories
88 if ( count ( $children ) > 0 ) {
89 $r .= '<h2>'.wfMsg('subcategories')."</h2>\n" ;
90 $r .= implode ( ', ' , $children ) ;
93 # Showing pages in this category
94 if ( count ( $articles ) > 0 ) {
95 $ti = $this->mTitle
->getText() ;
96 $h = wfMsg( 'category_header', $ti );
97 $r .= "<h2>$h</h2>\n" ;
98 $r .= implode ( ', ' , $articles ) ;
104 function newCategoryMagic () {
105 global $wgLang,$wgUser;
107 $sk =& $wgUser->getSkin();
109 $r = "<br style=\"clear:both;\"/>\n";
111 $articles = array() ;
112 $articles_start_char = array();
113 $children = array() ;
114 $children_start_char = array();
116 $id = $this->mTitle
->getArticleID() ;
119 $dbr =& wfGetDB( DB_SLAVE
);
120 $cur = $dbr->tableName( 'cur' );
121 $categorylinks = $dbr->tableName( 'categorylinks' );
123 $t = $dbr->strencode( $this->mTitle
->getDBKey() );
124 $sql = "SELECT DISTINCT cur_title,cur_namespace,cl_sortkey FROM " .
125 "$cur,$categorylinks WHERE cl_to='$t' AND cl_from=cur_id ORDER BY cl_sortkey" ;
126 $res = $dbr->query ( $sql ) ;
127 while ( $x = $dbr->fetchObject ( $res ) )
129 $t = $ns = $wgLang->getNsText ( $x->cur_namespace
) ;
130 if ( $t != '' ) $t .= ':' ;
131 $t .= $x->cur_title
;
133 if ( $x->cur_namespace
== NS_CATEGORY
) {
134 $ctitle = str_replace( '_',' ',$x->cur_title
);
135 array_push ( $children, $sk->makeKnownLink ( $t, $ctitle ) ) ; # Subcategory
137 // If there's a link from Category:A to Category:B, the sortkey of the resulting
138 // entry in the categorylinks table is Category:A, not A, which it SHOULD be.
139 // Workaround: If sortkey == "Category:".$title, than use $title for sorting,
140 // else use sortkey...
141 if ( ($ns.':'.$ctitle) == $x->cl_sortkey
) {
142 array_push ( $children_start_char, $wgLang->firstChar( $x->cur_title
) );
144 array_push ( $children_start_char, $wgLang->firstChar( $x->cl_sortkey
) ) ;
147 array_push ( $articles , $sk->makeKnownLink ( $t ) ) ; # Page in this category
148 array_push ( $articles_start_char, $wgLang->firstChar( $x->cl_sortkey
) ) ;
151 $dbr->freeResult ( $res ) ;
153 $ti = $this->mTitle
->getText() ;
155 # Don't show subcategories section if there are none.
156 if ( count ( $children ) > 0 )
158 # Showing subcategories
159 $r .= '<h2>' . wfMsg( 'subcategories' ) . "</h2>\n";
161 $numchild = count( $children );
163 $r .= wfMsg( 'subcategorycount1', 1 );
165 $r .= wfMsg( 'subcategorycount' , $numchild );
169 if ( count ( $children ) > 6 ) {
171 // divide list into three equal chunks
172 $chunk = (int) (count ( $children ) / 3);
174 // get and display header
175 $r .= '<table width="100%"><tr valign="top">';
180 // loop through the chunks
181 for($startChunk = 0, $endChunk = $chunk, $chunkIndex = 0;
183 $chunkIndex++
, $startChunk = $endChunk, $endChunk +
= $chunk +
1)
187 // output all subcategories to category
188 for ($index = $startChunk ;
189 $index < $endChunk && $index < count($children);
192 // check for change of starting letter or begging of chunk
193 if ( ($children_start_char[$index] != $children_start_char[$index - 1])
194 ||
($index == $startChunk) )
196 $r .= "</ul><h3>{$children_start_char[$index]}</h3>\n<ul>";
199 $r .= "<li>{$children[$index]}</li>";
205 $r .= '</tr></table>';
207 // for short lists of subcategories to category.
209 $r .= "<h3>{$children_start_char[0]}</h3>\n";
210 $r .= '<ul><li>'.$children[0].'</li>';
211 for ($index = 1; $index < count($children); $index++
)
213 if ($children_start_char[$index] != $children_start_char[$index - 1])
215 $r .= "</ul><h3>{$children_start_char[$index]}</h3>\n<ul>";
217 $r .= "<li>{$children[$index]}</li>";
221 } # END of if ( count($children) > 0 )
223 $r .= '<h2>' . wfMsg( 'category_header', $ti ) . "</h2>\n";
225 $numart = count( $articles );
227 $r .= wfMsg( 'categoryarticlecount1', 1 );
229 $r .= wfMsg( 'categoryarticlecount' , $numart );
233 # Showing articles in this category
234 if ( count ( $articles ) > 6) {
235 $ti = $this->mTitle
->getText() ;
237 // divide list into three equal chunks
238 $chunk = (int) (count ( $articles ) / 3);
240 // get and display header
241 $r .= '<table width="100%"><tr valign="top">';
243 // loop through the chunks
244 for($startChunk = 0, $endChunk = $chunk, $chunkIndex = 0;
246 $chunkIndex++
, $startChunk = $endChunk, $endChunk +
= $chunk +
1)
251 // output all articles in category
252 for ($index = $startChunk ;
253 $index < $endChunk && $index < count($articles);
256 // check for change of starting letter or begging of chunk
257 if ( ($index == $startChunk) ||
258 ($articles_start_char[$index] != $articles_start_char[$index - 1]) )
261 $r .= "</ul><h3>{$articles_start_char[$index]}</h3>\n<ul>";
264 $r .= "<li>{$articles[$index]}</li>";
270 $r .= '</tr></table>';
271 } elseif ( count($articles) > 0) {
272 // for short lists of articles in categories.
273 $ti = $this->mTitle
->getText() ;
275 $r .= '<h3>'.$articles_start_char[0]."</h3>\n";
276 $r .= '<ul><li>'.$articles[0].'</li>';
277 for ($index = 1; $index < count($articles); $index++
)
279 if ($articles_start_char[$index] != $articles_start_char[$index - 1])
281 $r .= "</ul><h3>{$articles_start_char[$index]}</h3>\n<ul>";
284 $r .= "<li>{$articles[$index]}</li>";