Followup r95753 per CR: prevent extensions from making isMovable() return true for...
[lhc/web/wiklou.git] / includes / CategoryPage.php
index 989e9e9..bb08cf5 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,27 +58,6 @@ 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
        }
@@ -91,7 +86,7 @@ class CategoryViewer {
                $children, $children_start_char,
                $showGallery, $imgsNoGalley,
                $imgsNoGallery_start_char,
-               $skin, $imgsNoGallery;
+               $imgsNoGallery;
 
        /**
         * @var 
@@ -148,7 +143,7 @@ class CategoryViewer {
         * @return string HTML output
         */
        public function getHTML() {
-               global $wgOut, $wgCategoryMagicGallery, $wgContLang;
+               global $wgOut, $wgCategoryMagicGallery;
                wfProfileIn( __METHOD__ );
 
                $this->showGallery = $wgCategoryMagicGallery && !$wgOut->mNoGallery;
@@ -177,16 +172,16 @@ class CategoryViewer {
                // Give a proper message if category is empty
                if ( $r == '' ) {
                        $r = wfMsgExt( 'category-empty', array( 'parse' ) );
-               }
-
-               global $wgBetterDirectionality, $wgLang;
-               if( $wgBetterDirectionality ) {
-                       $langAttribs = array( 'lang' => $wgLang->getCode(), 'dir' => $wgLang->getDir() );
-                       $r = Html::rawElement( 'div', $langAttribs, $r );
+               } else {
+                       $pageLang = $this->title->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 );
+               return $r;
        }
 
        function clearCategoryState() {
@@ -203,17 +198,6 @@ class CategoryViewer {
                }
        }
 
-       /**
-        * @return Skin
-        */
-       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
         */
@@ -221,7 +205,7 @@ class CategoryViewer {
                // Subcategory; strip the 'Category' namespace from the link text.
                $title = $cat->getTitle();
 
-               $link = $this->getSkin()->link( $title, $title->getText() );
+               $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
@@ -279,7 +263,7 @@ class CategoryViewer {
                                $this->gallery->add( $title );
                        }
                } else {
-                       $link = $this->getSkin()->link( $title );
+                       $link = Linker::link( $title );
                        if ( $isRedirect ) {
                                // This seems kind of pointless given 'mw-redirect' class,
                                // but keeping for back-compatibility with user css.
@@ -298,7 +282,7 @@ class CategoryViewer {
        function addPage( $title, $sortkey, $pageLength, $isRedirect = false ) {
                global $wgContLang;
 
-               $link = $this->getSkin()->link( $title );
+               $link = Linker::link( $title );
                if ( $isRedirect ) {
                        // This seems kind of pointless given 'mw-redirect' class,
                        // but keeping for back-compatiability with user css.
@@ -513,12 +497,11 @@ class CategoryViewer {
                        // for short lists of articles in categories.
                        $list = self::shortList( $articles, $articles_start_char );
                }
-               global $wgBetterDirectionality, $wgTitle;
-               if( $wgBetterDirectionality ) {
-                       $pageLang = $wgTitle->getPageLanguage();
-                       $attribs = array( 'lang' => $pageLang->getCode(), 'dir' => $pageLang->getDir() );
-                       $list = Html::rawElement( 'div', $attribs, $list );
-               }
+
+               $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;
        }
@@ -615,7 +598,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 );
@@ -624,8 +607,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
@@ -638,8 +621,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
@@ -649,6 +632,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