Changing back to $wgUser in SpecialImport.php. Importing pages returned "call to...
[lhc/web/wiklou.git] / includes / CategoryPage.php
index 86c2717..809466b 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,6 +17,22 @@ 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;
 
@@ -42,58 +58,71 @@ class CategoryPage extends Article {
                }
        }
 
-       /**
-        * Don't return a 404 for categories in use.
-        * In use defined as: either the actual page exists
-        * or the category currently has members.
-        */
-       function hasViewableContent() {
-               if ( parent::hasViewableContent() ) {
-                       return true;
-               } else {
-                       $cat = Category::newFromTitle( $this->mTitle );
-                       // If any of these are not 0, then has members
-                       if ( $cat->getPageCount()
-                               || $cat->getSubcatCount()
-                               || $cat->getFileCount()
-                       ) {
-                               return true;
-                       }
-               }
-               return false;
-       }
-
        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,
-               $imgsNoGalley, $imgsNoGallery_start_char,
-               $skin, $collation;
-       # Category object for this page
+               $showGallery, $imgsNoGalley,
+               $imgsNoGallery_start_char,
+               $skin, $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() ) {
@@ -130,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();
@@ -145,6 +174,15 @@ class CategoryViewer {
                        $r = wfMsgExt( 'category-empty', array( 'parse' ) );
                }
 
+               global $wgBetterDirectionality, $wgTitle;
+               if( $wgBetterDirectionality ) {
+                       $pageLang = $wgTitle->getPageLanguage();
+                       $langAttribs = array( 'lang' => $pageLang->getCode(), 'dir' => $pageLang->getDir() );
+                       # close the previous div, show the headings in user language,
+                       # then open a new div with the page content language again
+                       $r = '</div>' . $r . Html::openElement( 'div', $langAttribs );
+               }
+
                wfProfileOut( __METHOD__ );
                return $wgContLang->convert( $r );
        }
@@ -163,6 +201,9 @@ class CategoryViewer {
                }
        }
 
+       /**
+        * @return Skin
+        */
        function getSkin() {
                if ( !$this->skin ) {
                        global $wgUser;
@@ -193,7 +234,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 );
@@ -205,6 +246,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;
@@ -307,8 +351,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' ),
@@ -324,22 +369,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 );
+                                       $this->addSubcategoryObject( $cat, $humanSortkey, $row->page_len );
                                } elseif ( $title->getNamespace() == NS_FILE ) {
-                                       $this->addImage( $title, $rawSortkey, $row->page_len, $row->page_is_redirect );
+                                       $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 );
                                }
                        }
                }
@@ -377,7 +429,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.
@@ -452,13 +504,22 @@ class CategoryViewer {
         * @private
         */
        function formatList( $articles, $articles_start_char, $cutoff = 6 ) {
+               $list = '';
                if ( count ( $articles ) > $cutoff ) {
-                       return self::columnList( $articles, $articles_start_char );
+                       $list = self::columnList( $articles, $articles_start_char );
                } elseif ( count( $articles ) > 0 ) {
                        // for short lists of articles in categories.
-                       return self::shortList( $articles, $articles_start_char );
+                       $list = self::shortList( $articles, $articles_start_char );
                }
-               return '';
+               global $wgBetterDirectionality, $wgTitle;
+               if( $wgBetterDirectionality ) {
+                       $pageLang = $wgTitle->getPageLanguage();
+                       $attribs = array( 'lang' => $pageLang->getCode(), 'dir' => $pageLang->getDir(),
+                               'class' => 'mw-content-'.$pageLang->getDir() );
+                       $list = Html::rawElement( 'div', $attribs, $list );
+               }
+
+               return $list;
        }
 
        /**
@@ -531,10 +592,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>";
                        }
 
@@ -565,7 +624,7 @@ class CategoryViewer {
                        $prevQuery["{$type}until"] = $first;
                        unset( $prevQuery["{$type}from"] );
                        $prevLink = $sk->linkKnown(
-                               $this->title,
+                               $this->addFragmentToTitle( $this->title, $type ),
                                $prevLink,
                                array(),
                                $prevQuery
@@ -579,7 +638,7 @@ class CategoryViewer {
                        $lastQuery["{$type}from"] = $last;
                        unset( $lastQuery["{$type}until"] );
                        $nextLink = $sk->linkKnown(
-                               $this->title,
+                               $this->addFragmentToTitle( $this->title, $type ),
                                $nextLink,
                                array(),
                                $lastQuery
@@ -589,6 +648,32 @@ 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. Each type is considered independently
@@ -632,8 +717,7 @@ class CategoryViewer {
                }
 
                if ( $dbcnt == $rescnt || ( ( $rescnt == $this->limit || $fromOrUntil )
-                       && $dbcnt > $rescnt ) )
-               {
+                       && $dbcnt > $rescnt ) ) {
                        # Case 1: seems sane.
                        $totalcnt = $dbcnt;
                } elseif ( $rescnt < $this->limit && !$fromOrUntil ) {