* follow-up r97636: decrease indentation & mention revision per Nikerabbit
[lhc/web/wiklou.git] / includes / CategoryPage.php
index 42e6ff4..8e7ee64 100644 (file)
@@ -1,6 +1,6 @@
 <?php
 /**
- * Special handling for category description pages.
+ * Class for viewing MediaWiki category description pages.
  * Modelled after ImagePage.php.
  *
  * @file
@@ -17,17 +17,35 @@ class CategoryPage extends Article {
        # Subclasses can change this to override the viewer class.
        protected $mCategoryViewerClass = 'CategoryViewer';
 
+       protected function newPage( Title $title ) {
+               // Overload mPage with a category-specific page
+               return new WikiCategoryPage( $title );
+       }
+
+       /**
+        * Constructor from a page id
+        * @param $id Int article ID to load
+        */
+       public static function newFromID( $id ) {
+               $t = Title::newFromID( $id );
+               # @todo FIXME: Doesn't inherit right
+               return $t == null ? null : new self( $t );
+               # return $t == null ? null : new static( $t ); // PHP 5.3
+       }
+
        function view() {
                global $wgRequest, $wgUser;
 
                $diff = $wgRequest->getVal( 'diff' );
                $diffOnly = $wgRequest->getBool( 'diffonly', $wgUser->getOption( 'diffonly' ) );
 
-               if ( isset( $diff ) && $diffOnly )
+               if ( isset( $diff ) && $diffOnly ) {
                        return parent::view();
+               }
 
-               if ( !wfRunHooks( 'CategoryPageView', array( &$this ) ) )
+               if ( !wfRunHooks( 'CategoryPageView', array( &$this ) ) ) {
                        return;
+               }
 
                if ( NS_CATEGORY == $this->mTitle->getNamespace() ) {
                        $this->openShowCategory();
@@ -40,48 +58,71 @@ class CategoryPage extends Article {
                }
        }
 
-       /**
-        * Don't return a 404 for categories in use.
-        */
-       function hasViewableContent() {
-               if ( parent::hasViewableContent() ) {
-                       return true;
-               } else {
-                       $cat = Category::newFromTitle( $this->mTitle );
-                       return $cat->getId() != 0;
-               }
-       }
-
        function openShowCategory() {
                # For overloading
        }
 
        function closeShowCategory() {
-               global $wgOut;
+               global $wgOut, $wgRequest;
 
+               // Use these as defaults for back compat --catrope
+               $oldFrom = $wgRequest->getVal( 'from' );
+               $oldUntil = $wgRequest->getVal( 'until' );
+               
                $from = $until = array();
                foreach ( array( 'page', 'subcat', 'file' ) as $type ) {
-                       # Use $_GET instead of $wgRequest, because the latter helpfully
-                       # normalizes Unicode, which removes nulls.  TODO: do something
-                       # smarter than passing nulls in URLs.  :/
-                       $from[$type] = isset( $_GET["{$type}from"] ) ? $_GET["{$type}from"] : null;
-                       $until[$type] = isset( $_GET["{$type}until"] ) ? $_GET["{$type}until"] : null;
+                       $from[$type] = $wgRequest->getVal( "{$type}from", $oldFrom );
+                       $until[$type] = $wgRequest->getVal( "{$type}until", $oldUntil );
                }
 
-               $viewer = new $this->mCategoryViewerClass( $this->mTitle, $from, $until, $_GET );
+               $viewer = new $this->mCategoryViewerClass( $this->mTitle, $from, $until, $wgRequest->getValues() );
                $wgOut->addHTML( $viewer->getHTML() );
        }
 }
 
 class CategoryViewer {
-       var $title, $limit, $from, $until,
+       var $limit, $from, $until,
                $articles, $articles_start_char,
                $children, $children_start_char,
-               $showGallery, $gallery,
-               $skin;
-       # Category object for this page
+               $showGallery, $imgsNoGalley,
+               $imgsNoGallery_start_char,
+               $imgsNoGallery;
+
+       /**
+        * @var 
+        */
+       var $nextPage;
+
+       /**
+        * @var Array
+        */
+       var $flip;
+
+       /**
+        * @var Title
+        */
+       var $title;
+
+       /**
+        * @var Collation
+        */
+       var $collation;
+
+       /**
+        * @var ImageGallery
+        */
+       var $gallery;
+
+       /**
+        * Category object for this page
+        * @var Category
+        */
        private $cat;
-       # The original query array, to be used in generating paging links.
+
+       /**
+        * The original query array, to be used in generating paging links.
+        * @var array
+        */
        private $query;
 
        function __construct( $title, $from = '', $until = '', $query = array() ) {
@@ -92,6 +133,7 @@ class CategoryViewer {
                $this->limit = $wgCategoryPagingLimit;
                $this->cat = Category::newFromTitle( $title );
                $this->query = $query;
+               $this->collation = Collation::singleton();
                unset( $this->query['title'] );
        }
 
@@ -99,10 +141,9 @@ class CategoryViewer {
         * Format the category data list.
         *
         * @return string HTML output
-        * @private
         */
-       function getHTML() {
-               global $wgOut, $wgCategoryMagicGallery, $wgContLang;
+       public function getHTML() {
+               global $wgOut, $wgCategoryMagicGallery, $wgLang;
                wfProfileIn( __METHOD__ );
 
                $this->showGallery = $wgCategoryMagicGallery && !$wgOut->mNoGallery;
@@ -118,7 +159,7 @@ class CategoryViewer {
                if ( $r == '' ) {
                        // If there is no category content to display, only
                        // show the top part of the navigation links.
-                       // FIXME: cannot be completely suppressed because it
+                       // @todo FIXME: Cannot be completely suppressed because it
                        //        is unknown if 'until' or 'from' makes this
                        //        give 0 results.
                        $r = $r . $this->getCategoryTop();
@@ -133,8 +174,12 @@ class CategoryViewer {
                        $r = wfMsgExt( 'category-empty', array( 'parse' ) );
                }
 
+               $langAttribs = array( 'lang' => $wgLang->getCode(), 'dir' => $wgLang->getDir() );
+               # put a div around the headings which are in the user language
+               $r = Html::openElement( 'div', $langAttribs ) . $r . '</div>';
+
                wfProfileOut( __METHOD__ );
-               return $wgContLang->convert( $r );
+               return $r;
        }
 
        function clearCategoryState() {
@@ -145,29 +190,27 @@ class CategoryViewer {
                if ( $this->showGallery ) {
                        $this->gallery = new ImageGallery();
                        $this->gallery->setHideBadImages();
+               } else {
+                       $this->imgsNoGallery = array();
+                       $this->imgsNoGallery_start_char = array();
                }
        }
 
-       function getSkin() {
-               if ( !$this->skin ) {
-                       global $wgUser;
-                       $this->skin = $wgUser->getSkin();
-               }
-               return $this->skin;
-       }
-
        /**
         * Add a subcategory to the internal lists, using a Category object
         */
        function addSubcategoryObject( Category $cat, $sortkey, $pageLength ) {
                // Subcategory; strip the 'Category' namespace from the link text.
-               $this->children[] = $this->getSkin()->link(
-                       $cat->getTitle(),
-                       null,
-                       array(),
-                       array(),
-                       array( 'known', 'noclasses' )
-               );
+               $title = $cat->getTitle();
+
+               $link = Linker::link( $title, htmlspecialchars( $title->getText() ) );
+               if ( $title->isRedirect() ) {
+                       // This didn't used to add redirect-in-category, but might
+                       // as well be consistent with the rest of the sections
+                       // on a category page.
+                       $link = '<span class="redirect-in-category">' . $link . '</span>';
+               }
+               $this->children[] = $link;
 
                $this->children_start_char[] = 
                        $this->getSubcategorySortChar( $cat->getTitle(), $sortkey );
@@ -175,7 +218,7 @@ class CategoryViewer {
 
        /**
         * Add a subcategory to the internal lists, using a title object
-        * @deprecated kept for compatibility, please use addSubcategoryObject instead
+        * @deprecated since 1.17 kept for compatibility, please use addSubcategoryObject instead
         */
        function addSubcategory( Title $title, $sortkey, $pageLength ) {
                $this->addSubcategoryObject( Category::newFromTitle( $title ), $sortkey, $pageLength );
@@ -187,6 +230,9 @@ class CategoryViewer {
        * entry in the categorylinks table is Category:A, not A, which it SHOULD be.
        * Workaround: If sortkey == "Category:".$title, than use $title for sorting,
        * else use sortkey...
+       *
+       * @param Title $title
+       * @param string $sortkey The human-readable sortkey (before transforming to icu or whatever).
        */
        function getSubcategorySortChar( $title, $sortkey ) {
                global $wgContLang;
@@ -197,7 +243,7 @@ class CategoryViewer {
                        $word = $sortkey;
                }
 
-               $firstChar = $wgContLang->firstLetterForLists( $word );
+               $firstChar = $this->collation->getFirstLetter( $word );
 
                return $wgContLang->convert( $firstChar );
        }
@@ -206,6 +252,7 @@ class CategoryViewer {
         * Add a page in the image namespace
         */
        function addImage( Title $title, $sortkey, $pageLength, $isRedirect = false ) {
+               global $wgContLang;
                if ( $this->showGallery ) {
                        $flip = $this->flip['file'];
                        if ( $flip ) {
@@ -214,7 +261,16 @@ class CategoryViewer {
                                $this->gallery->add( $title );
                        }
                } else {
-                       $this->addPage( $title, $sortkey, $pageLength, $isRedirect );
+                       $link = Linker::link( $title );
+                       if ( $isRedirect ) {
+                               // This seems kind of pointless given 'mw-redirect' class,
+                               // but keeping for back-compatibility with user css.
+                               $link = '<span class="redirect-in-category">' . $link . '</span>';
+                       }
+                       $this->imgsNoGallery[] = $link;
+
+                       $this->imgsNoGallery_start_char[] = $wgContLang->convert( 
+                               $this->collation->getFirstLetter( $sortkey ) );
                }
        }
 
@@ -223,18 +279,17 @@ class CategoryViewer {
         */
        function addPage( $title, $sortkey, $pageLength, $isRedirect = false ) {
                global $wgContLang;
-               $this->articles[] = $isRedirect
-                       ? '<span class="redirect-in-category">' .
-                               $this->getSkin()->link(
-                                       $title,
-                                       null,
-                                       array(),
-                                       array(),
-                                       array( 'known', 'noclasses' )
-                               ) . '</span>'
-                       : $this->getSkin()->link( $title );
-
-               $this->articles_start_char[] = $wgContLang->convert( $wgContLang->firstLetterForLists( $sortkey ) );
+
+               $link = Linker::link( $title );
+               if ( $isRedirect ) {
+                       // This seems kind of pointless given 'mw-redirect' class,
+                       // but keeping for back-compatiability with user css.
+                       $link = '<span class="redirect-in-category">' . $link . '</span>';
+               }
+               $this->articles[] = $link;
+
+               $this->articles_start_char[] = $wgContLang->convert( 
+                       $this->collation->getFirstLetter( $sortkey ) );
        }
 
        function finaliseCategoryState() {
@@ -246,11 +301,13 @@ class CategoryViewer {
                        $this->articles            = array_reverse( $this->articles );
                        $this->articles_start_char = array_reverse( $this->articles_start_char );
                }
+               if ( !$this->showGallery && $this->flip['file'] ) {
+                       $this->imgsNoGallery            = array_reverse( $this->imgsNoGallery );
+                       $this->imgsNoGallery_start_char = array_reverse( $this->imgsNoGallery_start_char );
+               }
        }
 
        function doCategoryQuery() {
-               global $wgContLang;
-
                $dbr = wfGetDB( DB_SLAVE, 'category' );
 
                $this->nextPage = array(
@@ -263,14 +320,14 @@ class CategoryViewer {
                foreach ( array( 'page', 'subcat', 'file' ) as $type ) {
                        # Get the sortkeys for start/end, if applicable.  Note that if
                        # the collation in the database differs from the one
-                       # $wgContLang is using, pagination might go totally haywire.
+                       # set in $wgCategoryCollation, pagination might go totally haywire.
                        $extraConds = array( 'cl_type' => $type );
                        if ( $this->from[$type] !== null ) {
                                $extraConds[] = 'cl_sortkey >= '
-                                       . $dbr->addQuotes( $wgContLang->convertToSortkey( $this->from[$type] ) );
+                                       . $dbr->addQuotes( $this->collation->getSortKey( $this->from[$type] ) );
                        } elseif ( $this->until[$type] !== null ) {
                                $extraConds[] = 'cl_sortkey < '
-                                       . $dbr->addQuotes( $wgContLang->convertToSortkey( $this->until[$type] ) );
+                                       . $dbr->addQuotes( $this->collation->getSortKey( $this->until[$type] ) );
                                $this->flip[$type] = true;
                        }
 
@@ -278,8 +335,9 @@ class CategoryViewer {
                                array( 'page', 'categorylinks', 'category' ),
                                array( 'page_id', 'page_title', 'page_namespace', 'page_len',
                                        'page_is_redirect', 'cl_sortkey', 'cat_id', 'cat_title',
-                                       'cat_subcats', 'cat_pages', 'cat_files', 'cl_sortkey_prefix' ),
-                               array( 'cl_to' => $this->title->getDBkey() ) + $extraConds,
+                                       'cat_subcats', 'cat_pages', 'cat_files',
+                                       'cl_sortkey_prefix', 'cl_collation' ),
+                               array_merge( array( 'cl_to' => $this->title->getDBkey() ),  $extraConds ),
                                __METHOD__,
                                array(
                                        'USE INDEX' => array( 'categorylinks' => 'cl_sortkey' ),
@@ -295,22 +353,29 @@ class CategoryViewer {
                        $count = 0;
                        foreach ( $res as $row ) {
                                $title = Title::newFromRow( $row );
-                               $rawSortkey = $title->getCategorySortkey( $row->cl_sortkey_prefix );
+                               if ( $row->cl_collation === '' ) {
+                                       // Hack to make sure that while updating from 1.16 schema
+                                       // and db is inconsistent, that the sky doesn't fall.
+                                       // See r83544. Could perhaps be removed in a couple decades...
+                                       $humanSortkey = $row->cl_sortkey;
+                               } else {
+                                       $humanSortkey = $title->getCategorySortkey( $row->cl_sortkey_prefix );
+                               }
 
                                if ( ++$count > $this->limit ) {
                                        # We've reached the one extra which shows that there
                                        # are additional pages to be had. Stop here...
-                                       $this->nextPage[$type] = $rawSortkey;
+                                       $this->nextPage[$type] = $humanSortkey;
                                        break;
                                }
 
                                if ( $title->getNamespace() == NS_CATEGORY ) {
                                        $cat = Category::newFromRow( $row, $title );
-                                       $this->addSubcategoryObject( $cat, $rawSortkey, $row->page_len );
-                               } elseif ( $this->showGallery && $title->getNamespace() == NS_FILE ) {
-                                       $this->addImage( $title, $rawSortkey, $row->page_len, $row->page_is_redirect );
+                                       $this->addSubcategoryObject( $cat, $humanSortkey, $row->page_len );
+                               } elseif ( $title->getNamespace() == NS_FILE ) {
+                                       $this->addImage( $title, $humanSortkey, $row->page_len, $row->page_is_redirect );
                                } else {
-                                       $this->addPage( $title, $rawSortkey, $row->page_len, $row->page_is_redirect );
+                                       $this->addPage( $title, $humanSortkey, $row->page_len, $row->page_is_redirect );
                                }
                        }
                }
@@ -348,7 +413,7 @@ class CategoryViewer {
                # Don't show articles section if there are none.
                $r = '';
 
-               # FIXME, here and in the other two sections: we don't need to bother
+               # @todo FIXME: Here and in the other two sections: we don't need to bother
                # with this rigamarole if the entire category contents fit on one page
                # and have already been retrieved.  We can just use $rescnt in that
                # case and save a query and some logic.
@@ -371,16 +436,20 @@ class CategoryViewer {
 
        function getImageSection() {
                $r = '';
-               if ( $this->showGallery && ! $this->gallery->isEmpty() ) {
+               $rescnt = $this->showGallery ? $this->gallery->count() : count( $this->imgsNoGallery );
+               if ( $rescnt > 0 ) {
                        $dbcnt = $this->cat->getFileCount();
-                       $rescnt = $this->gallery->count();
                        $countmsg = $this->getCountMessage( $rescnt, $dbcnt, 'file' );
 
                        $r .= "<div id=\"mw-category-media\">\n";
                        $r .= '<h2>' . wfMsg( 'category-media-header', htmlspecialchars( $this->title->getText() ) ) . "</h2>\n";
                        $r .= $countmsg;
                        $r .= $this->getSectionPagingLinks( 'file' );
-                       $r .= $this->gallery->toHTML();
+                       if ( $this->showGallery ) {
+                               $r .= $this->gallery->toHTML();
+                       } else {
+                               $r .= $this->formatList( $this->imgsNoGallery, $this->imgsNoGallery_start_char );
+                       }
                        $r .= $this->getSectionPagingLinks( 'file' );
                        $r .= "\n</div>";
                }
@@ -419,13 +488,20 @@ class CategoryViewer {
         * @private
         */
        function formatList( $articles, $articles_start_char, $cutoff = 6 ) {
+               $list = '';
                if ( count ( $articles ) > $cutoff ) {
-                       return $this->columnList( $articles, $articles_start_char );
+                       $list = self::columnList( $articles, $articles_start_char );
                } elseif ( count( $articles ) > 0 ) {
                        // for short lists of articles in categories.
-                       return $this->shortList( $articles, $articles_start_char );
+                       $list = self::shortList( $articles, $articles_start_char );
                }
-               return '';
+
+               $pageLang = $this->title->getPageLanguage();
+               $attribs = array( 'lang' => $pageLang->getCode(), 'dir' => $pageLang->getDir(),
+                       'class' => 'mw-content-'.$pageLang->getDir() );
+               $list = Html::rawElement( 'div', $attribs, $list );
+
+               return $list;
        }
 
        /**
@@ -498,10 +574,8 @@ class CategoryViewer {
        static function shortList( $articles, $articles_start_char ) {
                $r = '<h3>' . htmlspecialchars( $articles_start_char[0] ) . "</h3>\n";
                $r .= '<ul><li>' . $articles[0] . '</li>';
-               for ( $index = 1; $index < count( $articles ); $index++ )
-               {
-                       if ( $articles_start_char[$index] != $articles_start_char[$index - 1] )
-                       {
+               for ( $index = 1; $index < count( $articles ); $index++ ) {
+                       if ( $articles_start_char[$index] != $articles_start_char[$index - 1] ) {
                                $r .= "</ul><h3>" . htmlspecialchars( $articles_start_char[$index] ) . "</h3>\n<ul>";
                        }
 
@@ -522,7 +596,7 @@ class CategoryViewer {
         */
        private function pagingLinks( $first, $last, $type = '' ) {
                global $wgLang;
-               $sk = $this->getSkin();
+
                $limitText = $wgLang->formatNum( $this->limit );
 
                $prevLink = wfMsgExt( 'prevn', array( 'escape', 'parsemag' ), $limitText );
@@ -531,8 +605,8 @@ class CategoryViewer {
                        $prevQuery = $this->query;
                        $prevQuery["{$type}until"] = $first;
                        unset( $prevQuery["{$type}from"] );
-                       $prevLink = $sk->linkKnown(
-                               $this->title,
+                       $prevLink = Linker::linkKnown(
+                               $this->addFragmentToTitle( $this->title, $type ),
                                $prevLink,
                                array(),
                                $prevQuery
@@ -545,8 +619,8 @@ class CategoryViewer {
                        $lastQuery = $this->query;
                        $lastQuery["{$type}from"] = $last;
                        unset( $lastQuery["{$type}until"] );
-                       $nextLink = $sk->linkKnown(
-                               $this->title,
+                       $nextLink = Linker::linkKnown(
+                               $this->addFragmentToTitle( $this->title, $type ),
                                $nextLink,
                                array(),
                                $lastQuery
@@ -556,10 +630,36 @@ class CategoryViewer {
                return "($prevLink) ($nextLink)";
        }
 
+       /**
+        * Takes a title, and adds the fragment identifier that
+        * corresponds to the correct segment of the category.
+        *
+        * @param Title $title: The title (usually $this->title)
+        * @param String $section: Which section
+        */
+       private function addFragmentToTitle( $title, $section ) {
+               switch ( $section ) {
+                       case 'page':
+                               $fragment = 'mw-pages';
+                               break;
+                       case 'subcat':
+                               $fragment = 'mw-subcategories';
+                               break;
+                       case 'file':
+                               $fragment = 'mw-category-media';
+                               break;
+                       default:
+                               throw new MWException( __METHOD__ .
+                                       " Invalid section $section." );
+               }
+
+               return Title::makeTitle( $title->getNamespace(),
+                       $title->getDBkey(), $fragment );
+       }
        /**
         * What to do if the category table conflicts with the number of results
-        * returned?  This function says what.  It works the same whether the
-        * things being counted are articles, subcategories, or files.
+        * returned?  This function says what. Each type is considered independently
+        * of the other types.
         *
         * Note for grepping: uses the messages category-article-count,
         * category-article-count-limited, category-subcat-count,
@@ -582,24 +682,27 @@ class CategoryViewer {
                #      than $this->limit and there's no offset.  In this case we still
                #      know the right figure.
                #   3) We have no idea.
-               $totalrescnt = count( $this->articles ) + count( $this->children ) +
-                       ( $this->showGallery ? $this->gallery->count() : 0 );
 
                # Check if there's a "from" or "until" for anything
+
+               // This is a little ugly, but we seem to use different names
+               // for the paging types then for the messages.
+               if ( $type === 'article' ) {
+                       $pagingType = 'page';
+               } else {
+                       $pagingType = $type;
+               }
+
                $fromOrUntil = false;
-               foreach ( array( 'page', 'subcat', 'file' ) as $t ) {
-                       if ( $this->from[$t] !== null || $this->until[$t] !== null ) {
-                               $fromOrUntil = true;
-                               break;
-                       }
+               if ( $this->from[$pagingType] !== null || $this->until[$pagingType] !== null ) {
+                       $fromOrUntil = true;
                }
 
-               if ( $dbcnt == $rescnt || ( ( $totalrescnt == $this->limit || $fromOrUntil )
-                       && $dbcnt > $rescnt ) )
-               {
+               if ( $dbcnt == $rescnt || ( ( $rescnt == $this->limit || $fromOrUntil )
+                       && $dbcnt > $rescnt ) ) {
                        # Case 1: seems sane.
                        $totalcnt = $dbcnt;
-               } elseif ( $totalrescnt < $this->limit && !$fromOrUntil ) {
+               } elseif ( $rescnt < $this->limit && !$fromOrUntil ) {
                        # Case 2: not sane, but salvageable.  Use the number of results.
                        # Since there are fewer than 200, we can also take this opportunity
                        # to refresh the incorrect category table entry -- which should be