00001 <?php
00008 if( !defined( 'MEDIAWIKI' ) )
00009 die( 1 );
00010
00013 class CategoryPage extends Article {
00014 function view() {
00015 global $wgRequest, $wgUser;
00016
00017 $diff = $wgRequest->getVal( 'diff' );
00018 $diffOnly = $wgRequest->getBool( 'diffonly', $wgUser->getOption( 'diffonly' ) );
00019
00020 if ( isset( $diff ) && $diffOnly )
00021 return Article::view();
00022
00023 if( !wfRunHooks( 'CategoryPageView', array( &$this ) ) )
00024 return;
00025
00026 if ( NS_CATEGORY == $this->mTitle->getNamespace() ) {
00027 $this->openShowCategory();
00028 }
00029
00030 Article::view();
00031
00032 if ( NS_CATEGORY == $this->mTitle->getNamespace() ) {
00033 $this->closeShowCategory();
00034 }
00035 }
00036
00040 function hasViewableContent() {
00041 if( parent::hasViewableContent() ) {
00042 return true;
00043 } else {
00044 $cat = Category::newFromTitle( $this->mTitle );
00045 return $cat->getId() != 0;
00046 }
00047
00048 }
00049
00050 function openShowCategory() {
00051 # For overloading
00052 }
00053
00054 function closeShowCategory() {
00055 global $wgOut, $wgRequest;
00056 $from = $wgRequest->getVal( 'from' );
00057 $until = $wgRequest->getVal( 'until' );
00058
00059 $viewer = new CategoryViewer( $this->mTitle, $from, $until );
00060 $wgOut->addHTML( $viewer->getHTML() );
00061 }
00062 }
00063
00064 class CategoryViewer {
00065 var $title, $limit, $from, $until,
00066 $articles, $articles_start_char,
00067 $children, $children_start_char,
00068 $showGallery, $gallery,
00069 $skin;
00071 private $cat;
00072
00073 function __construct( $title, $from = '', $until = '' ) {
00074 global $wgCategoryPagingLimit;
00075 $this->title = $title;
00076 $this->from = $from;
00077 $this->until = $until;
00078 $this->limit = $wgCategoryPagingLimit;
00079 $this->cat = Category::newFromTitle( $title );
00080 }
00081
00088 function getHTML() {
00089 global $wgOut, $wgCategoryMagicGallery, $wgCategoryPagingLimit;
00090 wfProfileIn( __METHOD__ );
00091
00092 $this->showGallery = $wgCategoryMagicGallery && !$wgOut->mNoGallery;
00093
00094 $this->clearCategoryState();
00095 $this->doCategoryQuery();
00096 $this->finaliseCategoryState();
00097
00098 $r = $this->getCategoryTop() .
00099 $this->getSubcategorySection() .
00100 $this->getPagesSection() .
00101 $this->getImageSection() .
00102 $this->getCategoryBottom();
00103
00104
00105 if ( $r == '' ) {
00106 $r = wfMsgExt( 'category-empty', array( 'parse' ) );
00107 }
00108
00109 wfProfileOut( __METHOD__ );
00110 return $r;
00111 }
00112
00113 function clearCategoryState() {
00114 $this->articles = array();
00115 $this->articles_start_char = array();
00116 $this->children = array();
00117 $this->children_start_char = array();
00118 if( $this->showGallery ) {
00119 $this->gallery = new ImageGallery();
00120 $this->gallery->setHideBadImages();
00121 }
00122 }
00123
00124 function getSkin() {
00125 if ( !$this->skin ) {
00126 global $wgUser;
00127 $this->skin = $wgUser->getSkin();
00128 }
00129 return $this->skin;
00130 }
00131
00135 function addSubcategoryObject( $cat, $sortkey, $pageLength ) {
00136 $title = $cat->getTitle();
00137 $this->addSubcategory( $title, $sortkey, $pageLength );
00138 }
00139
00144 function addSubcategory( $title, $sortkey, $pageLength ) {
00145 global $wgContLang;
00146
00147 $this->children[] = $this->getSkin()->makeKnownLinkObj(
00148 $title, $wgContLang->convertHtml( $title->getText() ) );
00149
00150 $this->children_start_char[] = $this->getSubcategorySortChar( $title, $sortkey );
00151 }
00152
00160 function getSubcategorySortChar( $title, $sortkey ) {
00161 global $wgContLang;
00162
00163 if( $title->getPrefixedText() == $sortkey ) {
00164 $firstChar = $wgContLang->firstChar( $title->getDBkey() );
00165 } else {
00166 $firstChar = $wgContLang->firstChar( $sortkey );
00167 }
00168
00169 return $wgContLang->convert( $firstChar );
00170 }
00171
00175 function addImage( Title $title, $sortkey, $pageLength, $isRedirect = false ) {
00176 if ( $this->showGallery ) {
00177 if( $this->flip ) {
00178 $this->gallery->insert( $title );
00179 } else {
00180 $this->gallery->add( $title );
00181 }
00182 } else {
00183 $this->addPage( $title, $sortkey, $pageLength, $isRedirect );
00184 }
00185 }
00186
00190 function addPage( $title, $sortkey, $pageLength, $isRedirect = false ) {
00191 global $wgContLang;
00192 $titletext = $wgContLang->convert( $title->getPrefixedText() );
00193 $this->articles[] = $isRedirect
00194 ? '<span class="redirect-in-category">' . $this->getSkin()->makeKnownLinkObj( $title, $titletext ) . '</span>'
00195 : $this->getSkin()->makeSizeLinkObj( $pageLength, $title, $titletext );
00196 $this->articles_start_char[] = $wgContLang->convert( $wgContLang->firstChar( $sortkey ) );
00197 }
00198
00199 function finaliseCategoryState() {
00200 if( $this->flip ) {
00201 $this->children = array_reverse( $this->children );
00202 $this->children_start_char = array_reverse( $this->children_start_char );
00203 $this->articles = array_reverse( $this->articles );
00204 $this->articles_start_char = array_reverse( $this->articles_start_char );
00205 }
00206 }
00207
00208 function doCategoryQuery() {
00209 $dbr = wfGetDB( DB_SLAVE, 'category' );
00210 if( $this->from != '' ) {
00211 $pageCondition = 'cl_sortkey >= ' . $dbr->addQuotes( $this->from );
00212 $this->flip = false;
00213 } elseif( $this->until != '' ) {
00214 $pageCondition = 'cl_sortkey < ' . $dbr->addQuotes( $this->until );
00215 $this->flip = true;
00216 } else {
00217 $pageCondition = '1 = 1';
00218 $this->flip = false;
00219 }
00220 $res = $dbr->select(
00221 array( 'page', 'categorylinks', 'category' ),
00222 array( 'page_title', 'page_namespace', 'page_len', 'page_is_redirect', 'cl_sortkey',
00223 'cat_id', 'cat_title', 'cat_subcats', 'cat_pages', 'cat_files' ),
00224 array( $pageCondition, 'cl_to' => $this->title->getDBkey() ),
00225 __METHOD__,
00226 array( 'ORDER BY' => $this->flip ? 'cl_sortkey DESC' : 'cl_sortkey',
00227 'USE INDEX' => array( 'categorylinks' => 'cl_sortkey' ),
00228 'LIMIT' => $this->limit + 1 ),
00229 array( 'categorylinks' => array( 'INNER JOIN', 'cl_from = page_id' ),
00230 'category' => array( 'LEFT JOIN', 'cat_title = page_title AND page_namespace = ' . NS_CATEGORY ) )
00231 );
00232
00233 $count = 0;
00234 $this->nextPage = null;
00235 while( $x = $dbr->fetchObject ( $res ) ) {
00236 if( ++$count > $this->limit ) {
00237
00238
00239 $this->nextPage = $x->cl_sortkey;
00240 break;
00241 }
00242
00243 $title = Title::makeTitle( $x->page_namespace, $x->page_title );
00244
00245 if( $title->getNamespace() == NS_CATEGORY ) {
00246 $cat = Category::newFromRow( $x, $title );
00247 $this->addSubcategoryObject( $cat, $x->cl_sortkey, $x->page_len );
00248 } elseif( $this->showGallery && $title->getNamespace() == NS_FILE ) {
00249 $this->addImage( $title, $x->cl_sortkey, $x->page_len, $x->page_is_redirect );
00250 } else {
00251 $this->addPage( $title, $x->cl_sortkey, $x->page_len, $x->page_is_redirect );
00252 }
00253 }
00254 $dbr->freeResult( $res );
00255 }
00256
00257 function getCategoryTop() {
00258 $r = '';
00259 if( $this->until != '' ) {
00260 $r .= $this->pagingLinks( $this->title, $this->nextPage, $this->until, $this->limit );
00261 } elseif( $this->nextPage != '' || $this->from != '' ) {
00262 $r .= $this->pagingLinks( $this->title, $this->from, $this->nextPage, $this->limit );
00263 }
00264 return $r == ''
00265 ? $r
00266 : "<br style=\"clear:both;\"/>\n" . $r;
00267 }
00268
00269 function getSubcategorySection() {
00270 # Don't show subcategories section if there are none.
00271 $r = '';
00272 $rescnt = count( $this->children );
00273 $dbcnt = $this->cat->getSubcatCount();
00274 $countmsg = $this->getCountMessage( $rescnt, $dbcnt, 'subcat' );
00275 if( $rescnt > 0 ) {
00276 # Showing subcategories
00277 $r .= "<div id=\"mw-subcategories\">\n";
00278 $r .= '<h2>' . wfMsg( 'subcategories' ) . "</h2>\n";
00279 $r .= $countmsg;
00280 $r .= $this->formatList( $this->children, $this->children_start_char );
00281 $r .= "\n</div>";
00282 }
00283 return $r;
00284 }
00285
00286 function getPagesSection() {
00287 $ti = htmlspecialchars( $this->title->getText() );
00288 # Don't show articles section if there are none.
00289 $r = '';
00290
00291 # FIXME, here and in the other two sections: we don't need to bother
00292 # with this rigamarole if the entire category contents fit on one page
00293 # and have already been retrieved. We can just use $rescnt in that
00294 # case and save a query and some logic.
00295 $dbcnt = $this->cat->getPageCount() - $this->cat->getSubcatCount()
00296 - $this->cat->getFileCount();
00297 $rescnt = count( $this->articles );
00298 $countmsg = $this->getCountMessage( $rescnt, $dbcnt, 'article' );
00299
00300 if( $rescnt > 0 ) {
00301 $r = "<div id=\"mw-pages\">\n";
00302 $r .= '<h2>' . wfMsg( 'category_header', $ti ) . "</h2>\n";
00303 $r .= $countmsg;
00304 $r .= $this->formatList( $this->articles, $this->articles_start_char );
00305 $r .= "\n</div>";
00306 }
00307 return $r;
00308 }
00309
00310 function getImageSection() {
00311 if( $this->showGallery && ! $this->gallery->isEmpty() ) {
00312 $dbcnt = $this->cat->getFileCount();
00313 $rescnt = $this->gallery->count();
00314 $countmsg = $this->getCountMessage( $rescnt, $dbcnt, 'file' );
00315
00316 return "<div id=\"mw-category-media\">\n" .
00317 '<h2>' . wfMsg( 'category-media-header', htmlspecialchars( $this->title->getText() ) ) . "</h2>\n" .
00318 $countmsg . $this->gallery->toHTML() . "\n</div>";
00319 } else {
00320 return '';
00321 }
00322 }
00323
00324 function getCategoryBottom() {
00325 if( $this->until != '' ) {
00326 return $this->pagingLinks( $this->title, $this->nextPage, $this->until, $this->limit );
00327 } elseif( $this->nextPage != '' || $this->from != '' ) {
00328 return $this->pagingLinks( $this->title, $this->from, $this->nextPage, $this->limit );
00329 } else {
00330 return '';
00331 }
00332 }
00333
00344 function formatList( $articles, $articles_start_char, $cutoff = 6 ) {
00345 if ( count ( $articles ) > $cutoff ) {
00346 return $this->columnList( $articles, $articles_start_char );
00347 } elseif ( count($articles) > 0) {
00348
00349 return $this->shortList( $articles, $articles_start_char );
00350 }
00351 return '';
00352 }
00353
00363 function columnList( $articles, $articles_start_char ) {
00364
00365 $chunk = (int) (count ( $articles ) / 3);
00366
00367
00368 $r = '<table width="100%"><tr valign="top">';
00369
00370 $prev_start_char = 'none';
00371
00372
00373 for($startChunk = 0, $endChunk = $chunk, $chunkIndex = 0;
00374 $chunkIndex < 3;
00375 $chunkIndex++, $startChunk = $endChunk, $endChunk += $chunk + 1)
00376 {
00377 $r .= "<td>\n";
00378 $atColumnTop = true;
00379
00380
00381 for ($index = $startChunk ;
00382 $index < $endChunk && $index < count($articles);
00383 $index++ )
00384 {
00385
00386 if ( ($index == $startChunk) ||
00387 ($articles_start_char[$index] != $articles_start_char[$index - 1]) )
00388
00389 {
00390 if( $atColumnTop ) {
00391 $atColumnTop = false;
00392 } else {
00393 $r .= "</ul>\n";
00394 }
00395 $cont_msg = "";
00396 if ( $articles_start_char[$index] == $prev_start_char )
00397 $cont_msg = ' ' . wfMsgHtml( 'listingcontinuesabbrev' );
00398 $r .= "<h3>" . htmlspecialchars( $articles_start_char[$index] ) . "$cont_msg</h3>\n<ul>";
00399 $prev_start_char = $articles_start_char[$index];
00400 }
00401
00402 $r .= "<li>{$articles[$index]}</li>";
00403 }
00404 if( !$atColumnTop ) {
00405 $r .= "</ul>\n";
00406 }
00407 $r .= "</td>\n";
00408
00409
00410 }
00411 $r .= '</tr></table>';
00412 return $r;
00413 }
00414
00422 function shortList( $articles, $articles_start_char ) {
00423 $r = '<h3>' . htmlspecialchars( $articles_start_char[0] ) . "</h3>\n";
00424 $r .= '<ul><li>'.$articles[0].'</li>';
00425 for ($index = 1; $index < count($articles); $index++ )
00426 {
00427 if ($articles_start_char[$index] != $articles_start_char[$index - 1])
00428 {
00429 $r .= "</ul><h3>" . htmlspecialchars( $articles_start_char[$index] ) . "</h3>\n<ul>";
00430 }
00431
00432 $r .= "<li>{$articles[$index]}</li>";
00433 }
00434 $r .= '</ul>';
00435 return $r;
00436 }
00437
00447 function pagingLinks( $title, $first, $last, $limit, $query = array() ) {
00448 global $wgLang;
00449 $sk = $this->getSkin();
00450 $limitText = $wgLang->formatNum( $limit );
00451
00452 $prevLink = wfMsgExt( 'prevn', array( 'escape', 'parsemag' ), $limitText );
00453 if( $first != '' ) {
00454 $prevLink = $sk->makeLinkObj( $title, $prevLink,
00455 wfArrayToCGI( $query + array( 'until' => $first ) ) );
00456 }
00457 $nextLink = wfMsgExt( 'nextn', array( 'escape', 'parsemag' ), $limitText );
00458 if( $last != '' ) {
00459 $nextLink = $sk->makeLinkObj( $title, $nextLink,
00460 wfArrayToCGI( $query + array( 'from' => $last ) ) );
00461 }
00462
00463 return "($prevLink) ($nextLink)";
00464 }
00465
00481 private function getCountMessage( $rescnt, $dbcnt, $type ) {
00482 global $wgLang;
00483 # There are three cases:
00484 # 1) The category table figure seems sane. It might be wrong, but
00485 # we can't do anything about it if we don't recalculate it on ev-
00486 # ery category view.
00487 # 2) The category table figure isn't sane, like it's smaller than the
00488 # number of actual results, *but* the number of results is less
00489 # than $this->limit and there's no offset. In this case we still
00490 # know the right figure.
00491 # 3) We have no idea.
00492 $totalrescnt = count( $this->articles ) + count( $this->children ) +
00493 ($this->showGallery ? $this->gallery->count() : 0);
00494 if($dbcnt == $rescnt || (($totalrescnt == $this->limit || $this->from
00495 || $this->until) && $dbcnt > $rescnt)){
00496 # Case 1: seems sane.
00497 $totalcnt = $dbcnt;
00498 } elseif($totalrescnt < $this->limit && !$this->from && !$this->until){
00499 # Case 2: not sane, but salvageable. Use the number of results.
00500 # Since there are fewer than 200, we can also take this opportunity
00501 # to refresh the incorrect category table entry -- which should be
00502 # quick due to the small number of entries.
00503 $totalcnt = $rescnt;
00504 $this->cat->refreshCounts();
00505 } else {
00506 # Case 3: hopeless. Don't give a total count at all.
00507 return wfMsgExt("category-$type-count-limited", 'parse',
00508 $wgLang->formatNum( $rescnt ) );
00509 }
00510 return wfMsgExt( "category-$type-count", 'parse', $wgLang->formatNum( $rescnt ),
00511 $wgLang->formatNum( $totalcnt ) );
00512 }
00513 }