Merge "Put callback function within class in SiteConfigurationTest"
[lhc/web/wiklou.git] / includes / Category.php
index b7b12e8..126b8fe 100644 (file)
@@ -40,10 +40,12 @@ class Category {
        /** Counts of membership (cat_pages, cat_subcats, cat_files) */
        private $mPages = null, $mSubcats = null, $mFiles = null;
 
-       private function __construct() { }
+       private function __construct() {
+       }
 
        /**
         * Set up all member variables using a database query.
+        * @throws MWException
         * @return bool True on success, false on failure.
         */
        protected function initialize() {
@@ -57,6 +59,9 @@ class Category {
                        # Already initialized
                        return true;
                }
+
+               wfProfileIn( __METHOD__ );
+
                $dbr = wfGetDB( DB_SLAVE );
                $row = $dbr->selectRow(
                        'category',
@@ -65,15 +70,17 @@ class Category {
                        __METHOD__
                );
 
+               wfProfileOut( __METHOD__ );
+
                if ( !$row ) {
                        # Okay, there were no contents.  Nothing to initialize.
                        if ( $this->mTitle ) {
                                # If there is a title object but no record in the category table, treat this as an empty category
-                               $this->mID      = false;
-                               $this->mName    = $this->mTitle->getDBkey();
-                               $this->mPages   = 0;
+                               $this->mID = false;
+                               $this->mName = $this->mTitle->getDBkey();
+                               $this->mPages = 0;
                                $this->mSubcats = 0;
-                               $this->mFiles   = 0;
+                               $this->mFiles = 0;
 
                                return true;
                        } else {
@@ -81,11 +88,11 @@ class Category {
                        }
                }
 
-               $this->mID      = $row->cat_id;
-               $this->mName    = $row->cat_title;
-               $this->mPages   = $row->cat_pages;
+               $this->mID = $row->cat_id;
+               $this->mName = $row->cat_title;
+               $this->mPages = $row->cat_pages;
                $this->mSubcats = $row->cat_subcats;
-               $this->mFiles   = $row->cat_files;
+               $this->mFiles = $row->cat_files;
 
                # (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
@@ -100,7 +107,7 @@ class Category {
        /**
         * Factory function.
         *
-        * @param $name Array: A category name (no "Category:" prefix).  It need
+        * @param array $name A category name (no "Category:" prefix).  It need
         *   not be normalized, with spaces replaced by underscores.
         * @return mixed Category, or false on a totally invalid name
         */
@@ -161,7 +168,7 @@ class Category {
 
                # NOTE: the row often results from a LEFT JOIN on categorylinks. This may result in
                #       all the cat_xxx fields being null, if the category page exists, but nothing
-               #       was ever added to the category. This case should be treated linke an empty
+               #       was ever added to the category. This case should be treated link an empty
                #       category, if possible.
 
                if ( $row->cat_title === null ) {
@@ -173,41 +180,53 @@ class Category {
                                $cat->mName = $title->getDBkey(); # if we have a title object, fetch the category name from there
                        }
 
-                       $cat->mID =   false;
+                       $cat->mID = false;
                        $cat->mSubcats = 0;
-                       $cat->mPages   = 0;
-                       $cat->mFiles   = 0;
+                       $cat->mPages = 0;
+                       $cat->mFiles = 0;
                } else {
-                       $cat->mName    = $row->cat_title;
-                       $cat->mID      = $row->cat_id;
+                       $cat->mName = $row->cat_title;
+                       $cat->mID = $row->cat_id;
                        $cat->mSubcats = $row->cat_subcats;
-                       $cat->mPages   = $row->cat_pages;
-                       $cat->mFiles   = $row->cat_files;
+                       $cat->mPages = $row->cat_pages;
+                       $cat->mFiles = $row->cat_files;
                }
 
                return $cat;
        }
 
        /** @return mixed DB key name, or false on failure */
-       public function getName() { return $this->getX( 'mName' ); }
+       public function getName() {
+               return $this->getX( 'mName' );
+       }
 
        /** @return mixed Category ID, or false on failure */
-       public function getID() { return $this->getX( 'mID' ); }
+       public function getID() {
+               return $this->getX( 'mID' );
+       }
 
        /** @return mixed Total number of member pages, or false on failure */
-       public function getPageCount() { return $this->getX( 'mPages' ); }
+       public function getPageCount() {
+               return $this->getX( 'mPages' );
+       }
 
        /** @return mixed Number of subcategories, or false on failure */
-       public function getSubcatCount() { return $this->getX( 'mSubcats' ); }
+       public function getSubcatCount() {
+               return $this->getX( 'mSubcats' );
+       }
 
        /** @return mixed Number of member files, or false on failure */
-       public function getFileCount() { return $this->getX( 'mFiles' ); }
+       public function getFileCount() {
+               return $this->getX( 'mFiles' );
+       }
 
        /**
         * @return Title|bool Title for this category, or false on failure.
         */
        public function getTitle() {
-               if ( $this->mTitle ) return $this->mTitle;
+               if ( $this->mTitle ) {
+                       return $this->mTitle;
+               }
 
                if ( !$this->initialize() ) {
                        return false;
@@ -225,20 +244,22 @@ class Category {
         * @return TitleArray object for category members.
         */
        public function getMembers( $limit = false, $offset = '' ) {
+               wfProfileIn( __METHOD__ );
+
                $dbr = wfGetDB( DB_SLAVE );
 
                $conds = array( 'cl_to' => $this->getName(), 'cl_from = page_id' );
                $options = array( 'ORDER BY' => 'cl_sortkey' );
 
                if ( $limit ) {
-                       $options[ 'LIMIT' ] = $limit;
+                       $options['LIMIT'] = $limit;
                }
 
                if ( $offset !== '' ) {
                        $conds[] = 'cl_sortkey > ' . $dbr->addQuotes( $offset );
                }
 
-               return TitleArray::newFromResult(
+               $result = TitleArray::newFromResult(
                        $dbr->select(
                                array( 'page', 'categorylinks' ),
                                array( 'page_id', 'page_namespace', 'page_title', 'page_len',
@@ -248,6 +269,10 @@ class Category {
                                $options
                        )
                );
+
+               wfProfileOut( __METHOD__ );
+
+               return $result;
        }
 
        /**
@@ -258,7 +283,7 @@ class Category {
                if ( !$this->initialize() ) {
                        return false;
                }
-               return $this-> { $key } ;
+               return $this->{$key};
        }
 
        /**
@@ -278,8 +303,10 @@ class Category {
                        }
                }
 
+               wfProfileIn( __METHOD__ );
+
                $dbw = wfGetDB( DB_MASTER );
-               $dbw->begin( __METHOD__  );
+               $dbw->begin( __METHOD__ );
 
                # Insert the row if it doesn't exist yet (e.g., this is being run via
                # update.php from a pre-1.16 schema).  TODO: This will cause lots and
@@ -302,12 +329,12 @@ class Category {
                $result = $dbw->selectRow(
                        array( 'categorylinks', 'page' ),
                        array( 'pages' => 'COUNT(*)',
-                                  'subcats' => "COUNT($cond1)",
-                                  'files' => "COUNT($cond2)"
+                               'subcats' => "COUNT($cond1)",
+                               'files' => "COUNT($cond2)"
                        ),
                        array( 'cl_to' => $this->mName, 'page_id = cl_from' ),
                        __METHOD__,
-                       'LOCK IN SHARE MODE'
+                       array( 'LOCK IN SHARE MODE' )
                );
                $ret = $dbw->update(
                        'category',
@@ -321,10 +348,12 @@ class Category {
                );
                $dbw->commit( __METHOD__ );
 
+               wfProfileOut( __METHOD__ );
+
                # Now we should update our local counts.
-               $this->mPages   = $result->pages;
+               $this->mPages = $result->pages;
                $this->mSubcats = $result->subcats;
-               $this->mFiles   = $result->files;
+               $this->mFiles = $result->files;
 
                return $ret;
        }