9a3d5b737b1a0dfdddecb16a61e7dfc404bb62fc
3 * Special handling for category description pages
4 * Modelled after ImagePage.php
8 if( !defined( 'MEDIAWIKI' ) )
13 class CategoryPage
extends Article
{
15 global $wgRequest, $wgUser;
17 $diff = $wgRequest->getVal( 'diff' );
18 $diffOnly = $wgRequest->getBool( 'diffonly', $wgUser->getOption( 'diffonly' ) );
20 if ( isset( $diff ) && $diffOnly )
21 return Article
::view();
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();
41 * This page should not be cached if 'from' or 'until' has been used
44 function isFileCacheable() {
47 return ( ! Article
::isFileCacheable()
48 ||
$wgRequest->getVal( 'from' )
49 ||
$wgRequest->getVal( 'until' )
53 function openShowCategory() {
57 function closeShowCategory() {
58 global $wgOut, $wgRequest;
59 $from = $wgRequest->getVal( 'from' );
60 $until = $wgRequest->getVal( 'until' );
62 $viewer = new CategoryViewer( $this->mTitle
, $from, $until );
63 $wgOut->addHTML( $viewer->getHTML() );
67 class CategoryViewer
{
68 var $title, $limit, $from, $until,
69 $articles, $articles_start_char,
70 $children, $children_start_char,
71 $showGallery, $gallery,
73 /** Category object for this page */
76 function __construct( $title, $from = '', $until = '' ) {
77 global $wgCategoryPagingLimit;
78 $this->title
= $title;
80 $this->until
= $until;
81 $this->limit
= $wgCategoryPagingLimit;
82 $this->cat
= Category
::newFromName( $title->getDBKey() );
86 * Format the category data list.
88 * @param string $from -- return only sort keys from this item on
89 * @param string $until -- don't return keys after this point.
90 * @return string HTML output
94 global $wgOut, $wgCategoryMagicGallery, $wgCategoryPagingLimit;
95 wfProfileIn( __METHOD__
);
97 $this->showGallery
= $wgCategoryMagicGallery && !$wgOut->mNoGallery
;
99 $this->clearCategoryState();
100 $this->doCategoryQuery();
101 $this->finaliseCategoryState();
103 $r = $this->getCategoryTop() .
104 $this->getSubcategorySection() .
105 $this->getPagesSection() .
106 $this->getImageSection() .
107 $this->getCategoryBottom();
109 // Give a proper message if category is empty
111 $r = wfMsgExt( 'category-empty', array( 'parse' ) );
114 wfProfileOut( __METHOD__
);
118 function clearCategoryState() {
119 $this->articles
= array();
120 $this->articles_start_char
= array();
121 $this->children
= array();
122 $this->children_start_char
= array();
123 if( $this->showGallery
) {
124 $this->gallery
= new ImageGallery();
125 $this->gallery
->setHideBadImages();
130 if ( !$this->skin
) {
132 $this->skin
= $wgUser->getSkin();
138 * Add a subcategory to the internal lists
140 function addSubcategory( $title, $sortkey, $pageLength ) {
142 // Subcategory; strip the 'Category' namespace from the link text.
143 $this->children
[] = $this->getSkin()->makeKnownLinkObj(
144 $title, $wgContLang->convertHtml( $title->getText() ) );
146 $this->children_start_char
[] = $this->getSubcategorySortChar( $title, $sortkey );
150 * Get the character to be used for sorting subcategories.
151 * If there's a link from Category:A to Category:B, the sortkey of the resulting
152 * entry in the categorylinks table is Category:A, not A, which it SHOULD be.
153 * Workaround: If sortkey == "Category:".$title, than use $title for sorting,
154 * else use sortkey...
156 function getSubcategorySortChar( $title, $sortkey ) {
159 if( $title->getPrefixedText() == $sortkey ) {
160 $firstChar = $wgContLang->firstChar( $title->getDBkey() );
162 $firstChar = $wgContLang->firstChar( $sortkey );
165 return $wgContLang->convert( $firstChar );
169 * Add a page in the image namespace
171 function addImage( Title
$title, $sortkey, $pageLength, $isRedirect = false ) {
172 if ( $this->showGallery
) {
174 $this->gallery
->insert( $title );
176 $this->gallery
->add( $title );
179 $this->addPage( $title, $sortkey, $pageLength, $isRedirect );
184 * Add a miscellaneous page
186 function addPage( $title, $sortkey, $pageLength, $isRedirect = false ) {
188 $this->articles
[] = $isRedirect
189 ?
'<span class="redirect-in-category">' . $this->getSkin()->makeKnownLinkObj( $title ) . '</span>'
190 : $this->getSkin()->makeSizeLinkObj( $pageLength, $title );
191 $this->articles_start_char
[] = $wgContLang->convert( $wgContLang->firstChar( $sortkey ) );
194 function finaliseCategoryState() {
196 $this->children
= array_reverse( $this->children
);
197 $this->children_start_char
= array_reverse( $this->children_start_char
);
198 $this->articles
= array_reverse( $this->articles
);
199 $this->articles_start_char
= array_reverse( $this->articles_start_char
);
203 function doCategoryQuery() {
204 $dbr = wfGetDB( DB_SLAVE
);
205 if( $this->from
!= '' ) {
206 $pageCondition = 'cl_sortkey >= ' . $dbr->addQuotes( $this->from
);
208 } elseif( $this->until
!= '' ) {
209 $pageCondition = 'cl_sortkey < ' . $dbr->addQuotes( $this->until
);
212 $pageCondition = '1 = 1';
216 array( 'page', 'categorylinks' ),
217 array( 'page_title', 'page_namespace', 'page_len', 'page_is_redirect', 'cl_sortkey' ),
218 array( $pageCondition,
220 'cl_to' => $this->title
->getDBkey()),
221 #'page_is_redirect' => 0),
224 array( 'ORDER BY' => $this->flip ?
'cl_sortkey DESC' : 'cl_sortkey',
225 'USE INDEX' => 'cl_sortkey',
226 'LIMIT' => $this->limit +
1 ) );
229 $this->nextPage
= null;
230 while( $x = $dbr->fetchObject ( $res ) ) {
231 if( ++
$count > $this->limit
) {
232 // We've reached the one extra which shows that there are
233 // additional pages to be had. Stop here...
234 $this->nextPage
= $x->cl_sortkey
;
238 $title = Title
::makeTitle( $x->page_namespace
, $x->page_title
);
240 if( $title->getNamespace() == NS_CATEGORY
) {
241 $this->addSubcategory( $title, $x->cl_sortkey
, $x->page_len
);
242 } elseif( $this->showGallery
&& $title->getNamespace() == NS_IMAGE
) {
243 $this->addImage( $title, $x->cl_sortkey
, $x->page_len
, $x->page_is_redirect
);
245 $this->addPage( $title, $x->cl_sortkey
, $x->page_len
, $x->page_is_redirect
);
248 $dbr->freeResult( $res );
251 function getCategoryTop() {
253 if( $this->until
!= '' ) {
254 $r .= $this->pagingLinks( $this->title
, $this->nextPage
, $this->until
, $this->limit
);
255 } elseif( $this->nextPage
!= '' ||
$this->from
!= '' ) {
256 $r .= $this->pagingLinks( $this->title
, $this->from
, $this->nextPage
, $this->limit
);
260 : "<br style=\"clear:both;\"/>\n" . $r;
263 function getSubcategorySection() {
264 # Don't show subcategories section if there are none.
266 $rescnt = count( $this->children
);
267 $dbcnt = $this->cat
->getSubcatCount();
268 $countmsg = $this->getCountMessage( $rescnt, $dbcnt, 'subcat' );
270 # Showing subcategories
271 $r .= "<div id=\"mw-subcategories\">\n";
272 $r .= '<h2>' . wfMsg( 'subcategories' ) . "</h2>\n";
274 $r .= $this->formatList( $this->children
, $this->children_start_char
);
280 function getPagesSection() {
281 $ti = htmlspecialchars( $this->title
->getText() );
282 # Don't show articles section if there are none.
285 # FIXME, here and in the other two sections: we don't need to bother
286 # with this rigamarole if the entire category contents fit on one page
287 # and have already been retrieved. We can just use $rescnt in that
288 # case and save a query and some logic.
289 $dbcnt = $this->cat
->getPageCount() - $this->cat
->getSubcatCount()
290 - $this->cat
->getFileCount();
291 $rescnt = count( $this->articles
);
292 $countmsg = $this->getCountMessage( $rescnt, $dbcnt, 'article' );
295 $r = "<div id=\"mw-pages\">\n";
296 $r .= '<h2>' . wfMsg( 'category_header', $ti ) . "</h2>\n";
298 $r .= $this->formatList( $this->articles
, $this->articles_start_char
);
304 function getImageSection() {
305 if( $this->showGallery
&& ! $this->gallery
->isEmpty() ) {
306 $dbcnt = $this->cat
->getFileCount();
307 $rescnt = $this->gallery
->count();
308 $countmsg = $this->getCountMessage( $rescnt, $dbcnt, 'file' );
310 return "<div id=\"mw-category-media\">\n" .
311 '<h2>' . wfMsg( 'category-media-header', htmlspecialchars($this->title
->getText()) ) . "</h2>\n" .
312 $countmsg . $this->gallery
->toHTML() . "\n</div>";
318 function getCategoryBottom() {
319 if( $this->until
!= '' ) {
320 return $this->pagingLinks( $this->title
, $this->nextPage
, $this->until
, $this->limit
);
321 } elseif( $this->nextPage
!= '' ||
$this->from
!= '' ) {
322 return $this->pagingLinks( $this->title
, $this->from
, $this->nextPage
, $this->limit
);
329 * Format a list of articles chunked by letter, either as a
330 * bullet list or a columnar format, depending on the length.
332 * @param array $articles
333 * @param array $articles_start_char
338 function formatList( $articles, $articles_start_char, $cutoff = 6 ) {
339 if ( count ( $articles ) > $cutoff ) {
340 return $this->columnList( $articles, $articles_start_char );
341 } elseif ( count($articles) > 0) {
342 // for short lists of articles in categories.
343 return $this->shortList( $articles, $articles_start_char );
349 * Format a list of articles chunked by letter in a three-column
350 * list, ordered vertically.
352 * @param array $articles
353 * @param array $articles_start_char
357 function columnList( $articles, $articles_start_char ) {
358 // divide list into three equal chunks
359 $chunk = (int) (count ( $articles ) / 3);
361 // get and display header
362 $r = '<table width="100%"><tr valign="top">';
364 $prev_start_char = 'none';
366 // loop through the chunks
367 for($startChunk = 0, $endChunk = $chunk, $chunkIndex = 0;
369 $chunkIndex++
, $startChunk = $endChunk, $endChunk +
= $chunk +
1)
374 // output all articles in category
375 for ($index = $startChunk ;
376 $index < $endChunk && $index < count($articles);
379 // check for change of starting letter or begining of chunk
380 if ( ($index == $startChunk) ||
381 ($articles_start_char[$index] != $articles_start_char[$index - 1]) )
385 $atColumnTop = false;
390 if ( $articles_start_char[$index] == $prev_start_char )
391 $cont_msg = ' ' . wfMsgHtml( 'listingcontinuesabbrev' );
392 $r .= "<h3>" . htmlspecialchars( $articles_start_char[$index] ) . "$cont_msg</h3>\n<ul>";
393 $prev_start_char = $articles_start_char[$index];
396 $r .= "<li>{$articles[$index]}</li>";
398 if( !$atColumnTop ) {
405 $r .= '</tr></table>';
410 * Format a list of articles chunked by letter in a bullet list.
411 * @param array $articles
412 * @param array $articles_start_char
416 function shortList( $articles, $articles_start_char ) {
417 $r = '<h3>' . htmlspecialchars( $articles_start_char[0] ) . "</h3>\n";
418 $r .= '<ul><li>'.$articles[0].'</li>';
419 for ($index = 1; $index < count($articles); $index++
)
421 if ($articles_start_char[$index] != $articles_start_char[$index - 1])
423 $r .= "</ul><h3>" . htmlspecialchars( $articles_start_char[$index] ) . "</h3>\n<ul>";
426 $r .= "<li>{$articles[$index]}</li>";
433 * @param Title $title
434 * @param string $first
435 * @param string $last
437 * @param array $query - additional query options to pass
441 function pagingLinks( $title, $first, $last, $limit, $query = array() ) {
443 $sk = $this->getSkin();
444 $limitText = $wgLang->formatNum( $limit );
446 $prevLink = htmlspecialchars( wfMsg( 'prevn', $limitText ) );
448 $prevLink = $sk->makeLinkObj( $title, $prevLink,
449 wfArrayToCGI( $query +
array( 'until' => $first ) ) );
451 $nextLink = htmlspecialchars( wfMsg( 'nextn', $limitText ) );
453 $nextLink = $sk->makeLinkObj( $title, $nextLink,
454 wfArrayToCGI( $query +
array( 'from' => $last ) ) );
457 return "($prevLink) ($nextLink)";
461 * What to do if the category table conflicts with the number of results
462 * returned? This function says what. It works the same whether the
463 * things being counted are articles, subcategories, or files.
465 * Note for grepping: uses the messages category-article-count,
466 * category-article-count-limited, category-subcat-count,
467 * category-subcat-count-limited, category-file-count,
468 * category-file-count-limited.
470 * @param int $rescnt The number of items returned by our database query.
471 * @param int $dbcnt The number of items according to the category table.
472 * @param string $type 'subcat', 'article', or 'file'
473 * @return string A message giving the number of items, to output to HTML.
475 private function getCountMessage( $rescnt, $dbcnt, $type ) {
477 # There are three cases:
478 # 1) The category table figure seems sane. It might be wrong, but
479 # we can't do anything about it if we don't recalculate it on ev-
481 # 2) The category table figure isn't sane, like it's smaller than the
482 # number of actual results, *but* the number of results is less
483 # than $this->limit and there's no offset. In this case we still
484 # know the right figure.
485 # 3) We have no idea.
486 $totalrescnt = count( $this->articles
) +
count( $this->children
) +
487 ($this->showGallery ?
$this->gallery
->count() : 0);
488 if($dbcnt == $rescnt ||
(($totalrescnt == $this->limit ||
$this->from
489 ||
$this->until
) && $dbcnt > $rescnt)){
490 # Case 1: seems sane.
492 } elseif($totalrescnt < $this->limit
&& !$this->from
&& !$this->until
){
493 # Case 2: not sane, but salvageable.
496 # Case 3: hopeless. Don't give a total count at all.
497 return wfMsgExt("category-$type-count-limited", 'parse',
498 $wgLang->formatNum( $rescnt ) );
500 return wfMsgExt( "category-$type-count", 'parse', $rescnt,
501 $wgLang->formatNum( $totalcnt ) );