3 * Special handling for category description pages
4 * Modelled after ImagePage.php
9 if( !defined( 'MEDIAWIKI' ) )
12 global $wgCategoryMagicGallery;
13 if( $wgCategoryMagicGallery )
15 require_once('ImageGallery.php');
20 class CategoryPage
extends Article
{
23 if(!wfRunHooks('CategoryPageView', array(&$this))) return;
25 if ( NS_CATEGORY
== $this->mTitle
->getNamespace() ) {
26 $this->openShowCategory();
31 # If the article we've just shown is in the "Image" namespace,
32 # follow it with the history list and link list for the image
35 if ( NS_CATEGORY
== $this->mTitle
->getNamespace() ) {
36 $this->closeShowCategory();
40 function openShowCategory() {
44 # generate a list of subcategories and pages for a category
45 # depending on wfMsg("usenewcategorypage") it either calls the new
46 # or the old code. The new code will not work properly for some
47 # languages due to sorting issues, so they might want to turn it
50 function closeShowCategory() {
51 global $wgOut, $wgRequest;
52 $pageConditions = array();
53 $from = $wgRequest->getVal( 'from' );
54 $until = $wgRequest->getVal( 'until' );
55 $wgOut->addHTML( $this->doCategoryMagic( $from, $until ) );
59 * Format the category data list.
61 * @param string $from -- return only sort keys from this item on
62 * @param string $until -- don't return keys after this point.
63 * @return string HTML output
66 function doCategoryMagic( $from = '', $until = '' ) {
67 global $wgContLang,$wgUser, $wgCategoryMagicGallery;
68 $fname = 'CategoryPage::doCategoryMagic';
69 wfProfileIn( $fname );
72 $articles_start_char = array();
74 $children_start_char = array();
76 if( $wgCategoryMagicGallery ) {
77 $ig = new ImageGallery();
80 $dbr =& wfGetDB( DB_SLAVE
);
82 $pageCondition = 'cl_sortkey >= ' . $dbr->addQuotes( $from );
84 } elseif( $until != '' ) {
85 $pageCondition = 'cl_sortkey < ' . $dbr->addQuotes( $until );
93 array( 'page', 'categorylinks' ),
94 array( 'page_title', 'page_namespace', 'page_len', 'cl_sortkey' ),
95 array( $pageCondition,
97 'cl_to' => $this->mTitle
->getDBKey()),
98 #'page_is_redirect' => 0),
101 array( 'ORDER BY' => $flip ?
'cl_sortkey DESC' : 'cl_sortkey',
102 'LIMIT' => $limit +
1 ) );
104 $sk =& $wgUser->getSkin();
105 $r = "<br style=\"clear:both;\"/>\n";
108 while( $x = $dbr->fetchObject ( $res ) ) {
109 if( ++
$count > $limit ) {
110 // We've reached the one extra which shows that there are
111 // additional pages to be had. Stop here...
112 $nextPage = $x->cl_sortkey
;
116 $title = Title
::makeTitle( $x->page_namespace
, $x->page_title
);
118 if( $title->getNamespace() == NS_CATEGORY
) {
119 // Subcategory; strip the 'Category' namespace from the link text.
120 array_push( $children, $sk->makeKnownLinkObj( $title, $wgContLang->convertHtml( $title->getText() ) ) );
122 // If there's a link from Category:A to Category:B, the sortkey of the resulting
123 // entry in the categorylinks table is Category:A, not A, which it SHOULD be.
124 // Workaround: If sortkey == "Category:".$title, than use $title for sorting,
125 // else use sortkey...
127 if( $title->getPrefixedText() == $x->cl_sortkey
) {
128 $sortkey=$wgContLang->firstChar( $x->page_title
);
130 $sortkey=$wgContLang->firstChar( $x->cl_sortkey
);
132 array_push( $children_start_char, $wgContLang->convert( $sortkey ) ) ;
133 } elseif( $wgCategoryMagicGallery && $title->getNamespace() == NS_IMAGE
) {
134 // Show thumbnails of categorized images, in a separate chunk
136 $ig->insert( Image
::newFromTitle( $title ) );
138 $ig->add( Image
::newFromTitle( $title ) );
141 // Page in this category
142 array_push( $articles, $sk->makeSizeLinkObj( $x->page_len
, $title, $wgContLang->convert( $title->getPrefixedText() ) ) ) ;
143 array_push( $articles_start_char,$wgContLang->convert( $x->cl_sortkey
) );
149 for ($i = 0 ; $i < count($articles) - 1; ++
$i){
150 if ($articles_start_char[$i][$root_length] != $articles_start_char[$i +
1][$root_length] ) {
152 } elseif (count($articles) - 2 == $i) {
153 $root_length = $root_length +
1;
158 for ($i = 0 ; $i < count($articles) ; ++
$i) {
159 $articles_start_char[$i] = $wgContLang->truncate($articles_start_char[$i], $root_length +
1);
162 $dbr->freeResult( $res );
165 $children = array_reverse( $children );
166 $children_start_char = array_reverse( $children_start_char );
167 $articles = array_reverse( $articles );
168 $articles_start_char = array_reverse( $articles_start_char );
172 $r .= $this->pagingLinks( $this->mTitle
, $nextPage, $until, $limit );
173 } elseif( $nextPage != '' ||
$from != '' ) {
174 $r .= $this->pagingLinks( $this->mTitle
, $from, $nextPage, $limit );
177 # Don't show subcategories section if there are none.
178 if( count( $children ) > 0 ) {
179 # Showing subcategories
180 $r .= '<h2>' . wfMsg( 'subcategories' ) . "</h2>\n";
181 $r .= $this->formatCount( $children, 'subcategorycount' );
182 $r .= $this->formatList( $children, $children_start_char );
185 # Showing articles in this category
186 $ti = htmlspecialchars( $this->mTitle
->getText() );
187 $r .= '<h2>' . wfMsg( 'category_header', $ti ) . "</h2>\n";
188 $r .= $this->formatCount( $articles, 'categoryarticlecount' );
189 $r .= $this->formatList( $articles, $articles_start_char );
191 if( $wgCategoryMagicGallery && ! $ig->isEmpty() ) {
195 wfProfileOut( $fname );
200 * @param array $articles
201 * @param string $message
205 function formatCount( $articles, $message ) {
207 $numart = count( $articles );
209 # Slightly different message to avoid silly plural
212 return wfMsg( $message, $wgContLang->formatNum( $numart ) );
215 * Format a list of articles chunked by letter, either as a
216 * bullet list or a columnar format, depending on the length.
218 * @param array $articles
219 * @param array $articles_start_char
224 function formatList( $articles, $articles_start_char, $cutoff = 6 ) {
225 if ( count ( $articles ) > $cutoff ) {
226 return $this->columnList( $articles, $articles_start_char );
227 } elseif ( count($articles) > 0) {
228 // for short lists of articles in categories.
229 return $this->shortList( $articles, $articles_start_char );
235 * Format a list of articles chunked by letter in a three-column
236 * list, ordered vertically.
238 * @param array $articles
239 * @param array $articles_start_char
243 function columnList( $articles, $articles_start_char ) {
244 // divide list into three equal chunks
245 $chunk = (int) (count ( $articles ) / 3);
247 // get and display header
248 $r = '<table width="100%"><tr valign="top">';
250 $prev_start_char = 'none';
252 // loop through the chunks
253 for($startChunk = 0, $endChunk = $chunk, $chunkIndex = 0;
255 $chunkIndex++
, $startChunk = $endChunk, $endChunk +
= $chunk +
1)
260 // output all articles in category
261 for ($index = $startChunk ;
262 $index < $endChunk && $index < count($articles);
265 // check for change of starting letter or begining of chunk
266 if ( ($index == $startChunk) ||
267 ($articles_start_char[$index] != $articles_start_char[$index - 1]) )
271 $atColumnTop = false;
276 if ( $articles_start_char[$index] == $prev_start_char )
277 $cont_msg = wfMsg('listingcontinuesabbrev');
278 $r .= "<h3>{$articles_start_char[$index]}$cont_msg</h3>\n<ul>";
279 $prev_start_char = $articles_start_char[$index];
282 $r .= "<li>{$articles[$index]}</li>";
284 if( !$atColumnTop ) {
291 $r .= '</tr></table>';
296 * Format a list of articles chunked by letter in a bullet list.
297 * @param array $articles
298 * @param array $articles_start_char
302 function shortList( $articles, $articles_start_char ) {
303 $r = '<h3>'.$articles_start_char[0]."</h3>\n";
304 $r .= '<ul><li>'.$articles[0].'</li>';
305 for ($index = 1; $index < count($articles); $index++
)
307 if ($articles_start_char[$index] != $articles_start_char[$index - 1])
309 $r .= "</ul><h3>{$articles_start_char[$index]}</h3>\n<ul>";
312 $r .= "<li>{$articles[$index]}</li>";
319 * @param Title $title
320 * @param string $first
321 * @param string $last
323 * @param array $query - additional query options to pass
327 function pagingLinks( $title, $first, $last, $limit, $query = array() ) {
328 global $wgUser, $wgLang;
329 $sk =& $wgUser->getSkin();
330 $limitText = $wgLang->formatNum( $limit );
332 $prevLink = htmlspecialchars( wfMsg( 'prevn', $limitText ) );
334 $prevLink = $sk->makeLinkObj( $title, $prevLink,
335 wfArrayToCGI( $query +
array( 'until' => $first ) ) );
337 $nextLink = htmlspecialchars( wfMsg( 'nextn', $limitText ) );
339 $nextLink = $sk->makeLinkObj( $title, $nextLink,
340 wfArrayToCGI( $query +
array( 'from' => $last ) ) );
343 return "($prevLink) ($nextLink)";