Fix yet more image links bugs
[lhc/web/wiklou.git] / includes / Category.php
index 98c65a1..15e56b1 100644 (file)
@@ -50,7 +50,7 @@ abstract class CategoryListBase {
         * @return mixed Normalized name, or false if the name was invalid.
         */
        private static function setNamesCallback( $name ) {
-               $title = Title::newFromText( $name );
+               $title = Title::newFromText( "Category:$name" );
                if( !is_object( $title ) ) {
                        return false;
                }
@@ -206,6 +206,7 @@ class Category extends CategoryListBase {
        public function getSubcatCount() { return $this->getX( 'mSubcats' ); }
        /** @return mixed Number of member files, or false on failure */
        public function getFileCount() { return $this->getX( 'mFiles' ); }
+
        /**
         * This is not implemented in the base class, because arrays of Titles are
         * evil.
@@ -216,8 +217,7 @@ class Category extends CategoryListBase {
                if( !$this->initialize() ) {
                        return false;
                }
-               # FIXME is there a better way to do this?
-               return Title::newFromText( "Category:{$this->mNames[0]}" );
+               return Title::makeTitleSafe( NS_CATEGORY, $this->mNames[0] );
        }
 
        /** Generic accessor */
@@ -237,6 +237,15 @@ class Category extends CategoryListBase {
         */
        protected function initialize() {
                parent::initialize();
+
+               # (bug 13683) If the count is negative, then 1) it's obviously wrong
+               # and should not be kept, and 2) we *probably* don't have to scan many
+               # rows to obtain the correct figure, so let's risk a one-time recount.
+               if( $this->mPages[0] < 0 || $this->mSubcats[0] < 0 ||
+               $this->mFiles[0] < 0 ) {
+                       $this->refreshCounts();
+               }
+
                if( count( $this->mNames ) != 1 || count( $this->mIDs ) != 1 ) {
                        return false;
                }
@@ -273,11 +282,13 @@ class Category extends CategoryListBase {
                        );
                }
 
+               $cond1 = $dbw->conditional( 'page_namespace='.NS_CATEGORY, 1, 'NULL' );
+               $cond2 = $dbw->conditional( 'page_namespace='.NS_IMAGE, 1, 'NULL' );
                $result = $dbw->selectRow(
                        array( 'categorylinks', 'page' ),
                        array( 'COUNT(*) AS pages',
-                               'COUNT(IF(page_namespace='.NS_CATEGORY.',1,NULL)) AS subcats',
-                               'COUNT(IF(page_namespace='.NS_IMAGE.',1,NULL)) AS files'
+                                  "COUNT($cond1) AS subcats",
+                                  "COUNT($cond2) AS files"
                        ),
                        array( 'cl_to' => $this->mNames[0], 'page_id = cl_from' ),
                        __METHOD__,