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( $wgContLang->firstChar( $x->cl_sortkey
) ) );
146 $dbr->freeResult( $res );
149 $children = array_reverse( $children );
150 $children_start_char = array_reverse( $children_start_char );
151 $articles = array_reverse( $articles );
152 $articles_start_char = array_reverse( $articles_start_char );
156 $r .= $this->pagingLinks( $this->mTitle
, $nextPage, $until, $limit );
157 } elseif( $nextPage != '' ||
$from != '' ) {
158 $r .= $this->pagingLinks( $this->mTitle
, $from, $nextPage, $limit );
161 # Don't show subcategories section if there are none.
162 if( count( $children ) > 0 ) {
163 # Showing subcategories
164 $r .= '<h2>' . wfMsg( 'subcategories' ) . "</h2>\n";
165 $r .= $this->formatCount( $children, 'subcategorycount' );
166 $r .= $this->formatList( $children, $children_start_char );
169 # Showing articles in this category
170 $ti = htmlspecialchars( $this->mTitle
->getText() );
171 $r .= '<h2>' . wfMsg( 'category_header', $ti ) . "</h2>\n";
172 $r .= $this->formatCount( $articles, 'categoryarticlecount' );
173 $r .= $this->formatList( $articles, $articles_start_char );
175 if( $wgCategoryMagicGallery && ! $ig->isEmpty() ) {
179 wfProfileOut( $fname );
184 * @param array $articles
185 * @param string $message
189 function formatCount( $articles, $message ) {
191 $numart = count( $articles );
193 # Slightly different message to avoid silly plural
196 return wfMsg( $message, $wgContLang->formatNum( $numart ) );
199 * Format a list of articles chunked by letter, either as a
200 * bullet list or a columnar format, depending on the length.
202 * @param array $articles
203 * @param array $articles_start_char
208 function formatList( $articles, $articles_start_char, $cutoff = 6 ) {
209 if ( count ( $articles ) > $cutoff ) {
210 return $this->columnList( $articles, $articles_start_char );
211 } elseif ( count($articles) > 0) {
212 // for short lists of articles in categories.
213 return $this->shortList( $articles, $articles_start_char );
219 * Format a list of articles chunked by letter in a three-column
220 * list, ordered vertically.
222 * @param array $articles
223 * @param array $articles_start_char
227 function columnList( $articles, $articles_start_char ) {
228 // divide list into three equal chunks
229 $chunk = (int) (count ( $articles ) / 3);
231 // get and display header
232 $r = '<table width="100%"><tr valign="top">';
234 $prev_start_char = 'none';
236 // loop through the chunks
237 for($startChunk = 0, $endChunk = $chunk, $chunkIndex = 0;
239 $chunkIndex++
, $startChunk = $endChunk, $endChunk +
= $chunk +
1)
244 // output all articles in category
245 for ($index = $startChunk ;
246 $index < $endChunk && $index < count($articles);
249 // check for change of starting letter or begining of chunk
250 if ( ($index == $startChunk) ||
251 ($articles_start_char[$index] != $articles_start_char[$index - 1]) )
255 $atColumnTop = false;
260 if ( $articles_start_char[$index] == $prev_start_char )
261 $cont_msg = wfMsg('listingcontinuesabbrev');
262 $r .= "<h3>{$articles_start_char[$index]}$cont_msg</h3>\n<ul>";
263 $prev_start_char = $articles_start_char[$index];
266 $r .= "<li>{$articles[$index]}</li>";
268 if( !$atColumnTop ) {
275 $r .= '</tr></table>';
280 * Format a list of articles chunked by letter in a bullet list.
281 * @param array $articles
282 * @param array $articles_start_char
286 function shortList( $articles, $articles_start_char ) {
287 $r = '<h3>'.$articles_start_char[0]."</h3>\n";
288 $r .= '<ul><li>'.$articles[0].'</li>';
289 for ($index = 1; $index < count($articles); $index++
)
291 if ($articles_start_char[$index] != $articles_start_char[$index - 1])
293 $r .= "</ul><h3>{$articles_start_char[$index]}</h3>\n<ul>";
296 $r .= "<li>{$articles[$index]}</li>";
303 * @param Title $title
304 * @param string $first
305 * @param string $last
307 * @param array $query - additional query options to pass
311 function pagingLinks( $title, $first, $last, $limit, $query = array() ) {
312 global $wgUser, $wgLang;
313 $sk =& $wgUser->getSkin();
314 $limitText = $wgLang->formatNum( $limit );
316 $prevLink = htmlspecialchars( wfMsg( 'prevn', $limitText ) );
318 $prevLink = $sk->makeLinkObj( $title, $prevLink,
319 wfArrayToCGI( $query +
array( 'until' => $first ) ) );
321 $nextLink = htmlspecialchars( wfMsg( 'nextn', $limitText ) );
323 $nextLink = $sk->makeLinkObj( $title, $nextLink,
324 wfArrayToCGI( $query +
array( 'from' => $last ) ) );
327 return "($prevLink) ($nextLink)";