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 function closeShowCategory() {
45 global $wgOut, $wgRequest;
46 $from = $wgRequest->getVal( 'from' );
47 $until = $wgRequest->getVal( 'until' );
48 $wgOut->addHTML( $this->doCategoryMagic( $from, $until ) );
52 * Format the category data list.
54 * @param string $from -- return only sort keys from this item on
55 * @param string $until -- don't return keys after this point.
56 * @return string HTML output
59 function doCategoryMagic( $from = '', $until = '' ) {
60 global $wgContLang,$wgUser, $wgCategoryMagicGallery, $wgCategoryPagingLimit;
61 $fname = 'CategoryPage::doCategoryMagic';
62 wfProfileIn( $fname );
65 $articles_start_char = array();
67 $children_start_char = array();
68 if( $wgCategoryMagicGallery ) {
69 $ig = new ImageGallery();
72 $dbr =& wfGetDB( DB_SLAVE
);
74 $pageCondition = 'cl_sortkey >= ' . $dbr->addQuotes( $from );
76 } elseif( $until != '' ) {
77 $pageCondition = 'cl_sortkey < ' . $dbr->addQuotes( $until );
80 $pageCondition = '1 = 1';
83 $limit = $wgCategoryPagingLimit;
85 array( 'page', 'categorylinks' ),
86 array( 'page_title', 'page_namespace', 'page_len', 'cl_sortkey' ),
87 array( $pageCondition,
89 'cl_to' => $this->mTitle
->getDBKey()),
90 #'page_is_redirect' => 0),
93 array( 'ORDER BY' => $flip ?
'cl_sortkey DESC' : 'cl_sortkey',
94 'LIMIT' => $limit +
1 ) );
96 $sk =& $wgUser->getSkin();
97 $r = "<br style=\"clear:both;\"/>\n";
100 while( $x = $dbr->fetchObject ( $res ) ) {
101 if( ++
$count > $limit ) {
102 // We've reached the one extra which shows that there are
103 // additional pages to be had. Stop here...
104 $nextPage = $x->cl_sortkey
;
108 $title = Title
::makeTitle( $x->page_namespace
, $x->page_title
);
110 if( $title->getNamespace() == NS_CATEGORY
) {
111 // Subcategory; strip the 'Category' namespace from the link text.
112 array_push( $children, $sk->makeKnownLinkObj( $title, $wgContLang->convertHtml( $title->getText() ) ) );
114 // If there's a link from Category:A to Category:B, the sortkey of the resulting
115 // entry in the categorylinks table is Category:A, not A, which it SHOULD be.
116 // Workaround: If sortkey == "Category:".$title, than use $title for sorting,
117 // else use sortkey...
119 if( $title->getPrefixedText() == $x->cl_sortkey
) {
120 $sortkey=$wgContLang->firstChar( $x->page_title
);
122 $sortkey=$wgContLang->firstChar( $x->cl_sortkey
);
124 array_push( $children_start_char, $wgContLang->convert( $sortkey ) ) ;
125 } elseif( $wgCategoryMagicGallery && $title->getNamespace() == NS_IMAGE
) {
126 // Show thumbnails of categorized images, in a separate chunk
128 $ig->insert( Image
::newFromTitle( $title ) );
130 $ig->add( Image
::newFromTitle( $title ) );
133 // Page in this category
134 array_push( $articles, $sk->makeSizeLinkObj( $x->page_len
, $title, $wgContLang->convert( $title->getPrefixedText() ) ) ) ;
135 array_push( $articles_start_char, $wgContLang->convert( $wgContLang->firstChar( $x->cl_sortkey
) ) );
138 $dbr->freeResult( $res );
141 $children = array_reverse( $children );
142 $children_start_char = array_reverse( $children_start_char );
143 $articles = array_reverse( $articles );
144 $articles_start_char = array_reverse( $articles_start_char );
148 $r .= $this->pagingLinks( $this->mTitle
, $nextPage, $until, $limit );
149 } elseif( $nextPage != '' ||
$from != '' ) {
150 $r .= $this->pagingLinks( $this->mTitle
, $from, $nextPage, $limit );
153 # Don't show subcategories section if there are none.
154 if( count( $children ) > 0 ) {
155 # Showing subcategories
156 $r .= '<h2>' . wfMsg( 'subcategories' ) . "</h2>\n";
157 $r .= $this->formatCount( $children, 'subcategorycount' );
158 $r .= $this->formatList( $children, $children_start_char );
161 # Showing articles in this category
162 $ti = htmlspecialchars( $this->mTitle
->getText() );
163 $r .= '<h2>' . wfMsg( 'category_header', $ti ) . "</h2>\n";
164 $r .= $this->formatCount( $articles, 'categoryarticlecount' );
165 $r .= $this->formatList( $articles, $articles_start_char );
167 if( $wgCategoryMagicGallery && ! $ig->isEmpty() ) {
172 $r .= $this->pagingLinks( $this->mTitle
, $nextPage, $until, $limit );
173 } elseif( $nextPage != '' ||
$from != '' ) {
174 $r .= $this->pagingLinks( $this->mTitle
, $from, $nextPage, $limit );
177 wfProfileOut( $fname );
182 * @param array $articles
183 * @param string $message
187 function formatCount( $articles, $message ) {
189 $numart = count( $articles );
191 # Slightly different message to avoid silly plural
194 return wfMsg( $message, $wgContLang->formatNum( $numart ) );
197 * Format a list of articles chunked by letter, either as a
198 * bullet list or a columnar format, depending on the length.
200 * @param array $articles
201 * @param array $articles_start_char
206 function formatList( $articles, $articles_start_char, $cutoff = 6 ) {
207 if ( count ( $articles ) > $cutoff ) {
208 return $this->columnList( $articles, $articles_start_char );
209 } elseif ( count($articles) > 0) {
210 // for short lists of articles in categories.
211 return $this->shortList( $articles, $articles_start_char );
217 * Format a list of articles chunked by letter in a three-column
218 * list, ordered vertically.
220 * @param array $articles
221 * @param array $articles_start_char
225 function columnList( $articles, $articles_start_char ) {
226 // divide list into three equal chunks
227 $chunk = (int) (count ( $articles ) / 3);
229 // get and display header
230 $r = '<table width="100%"><tr valign="top">';
232 $prev_start_char = 'none';
234 // loop through the chunks
235 for($startChunk = 0, $endChunk = $chunk, $chunkIndex = 0;
237 $chunkIndex++
, $startChunk = $endChunk, $endChunk +
= $chunk +
1)
242 // output all articles in category
243 for ($index = $startChunk ;
244 $index < $endChunk && $index < count($articles);
247 // check for change of starting letter or begining of chunk
248 if ( ($index == $startChunk) ||
249 ($articles_start_char[$index] != $articles_start_char[$index - 1]) )
253 $atColumnTop = false;
258 if ( $articles_start_char[$index] == $prev_start_char )
259 $cont_msg = wfMsgHtml('listingcontinuesabbrev');
260 $r .= "<h3>" . htmlspecialchars( $articles_start_char[$index] ) . "$cont_msg</h3>\n<ul>";
261 $prev_start_char = $articles_start_char[$index];
264 $r .= "<li>{$articles[$index]}</li>";
266 if( !$atColumnTop ) {
273 $r .= '</tr></table>';
278 * Format a list of articles chunked by letter in a bullet list.
279 * @param array $articles
280 * @param array $articles_start_char
284 function shortList( $articles, $articles_start_char ) {
285 $r = '<h3>' . htmlspecialchars( $articles_start_char[0] ) . "</h3>\n";
286 $r .= '<ul><li>'.$articles[0].'</li>';
287 for ($index = 1; $index < count($articles); $index++
)
289 if ($articles_start_char[$index] != $articles_start_char[$index - 1])
291 $r .= "</ul><h3>" . htmlspecialchars( $articles_start_char[$index] ) . "</h3>\n<ul>";
294 $r .= "<li>{$articles[$index]}</li>";
301 * @param Title $title
302 * @param string $first
303 * @param string $last
305 * @param array $query - additional query options to pass
309 function pagingLinks( $title, $first, $last, $limit, $query = array() ) {
310 global $wgUser, $wgLang;
311 $sk =& $wgUser->getSkin();
312 $limitText = $wgLang->formatNum( $limit );
314 $prevLink = htmlspecialchars( wfMsg( 'prevn', $limitText ) );
316 $prevLink = $sk->makeLinkObj( $title, $prevLink,
317 wfArrayToCGI( $query +
array( 'until' => $first ) ) );
319 $nextLink = htmlspecialchars( wfMsg( 'nextn', $limitText ) );
321 $nextLink = $sk->makeLinkObj( $title, $nextLink,
322 wfArrayToCGI( $query +
array( 'from' => $last ) ) );
325 return "($prevLink) ($nextLink)";