Reconcept cl_raw_sortkey as cl_sortkey_prefix
[lhc/web/wiklou.git] / includes / Category.php
index 4386491..bbeb79b 100644 (file)
@@ -1,6 +1,8 @@
 <?php
 /**
- * Category objects are immutable, strictly speaking.  If you call methods that change the database, like to refresh link counts, the objects will be appropriately reinitialized.  Member variables are lazy-initialized.
+ * Category objects are immutable, strictly speaking. If you call methods that change the database,
+ * like to refresh link counts, the objects will be appropriately reinitialized.
+ * Member variables are lazy-initialized.
  *
  * TODO: Move some stuff from CategoryPage.php to here, and use that.
  *
@@ -16,21 +18,22 @@ 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.
         * @return bool True on success, false on failure.
         */
        protected function initialize() {
-               if ( $this->mName === null && $this->mTitle ) 
-                       $this->mName = $title->getDBKey();
+               if ( $this->mName === null && $this->mTitle ) {
+                       $this->mName = $this->mTitle->getDBkey();
+               }
 
-               if( $this->mName === null && $this->mID === null ) {
-                       throw new MWException( __METHOD__.' has both names and IDs null' );
-               } elseif( $this->mID === null ) {
+               if ( $this->mName === null && $this->mID === null ) {
+                       throw new MWException( __METHOD__ . ' has both names and IDs null' );
+               } elseif ( $this->mID === null ) {
                        $where = array( 'cat_title' => $this->mName );
-               } elseif( $this->mName === null ) {
+               } elseif ( $this->mName === null ) {
                        $where = array( 'cat_id' => $this->mID );
                } else {
                        # Already initialized
@@ -39,15 +42,27 @@ class Category {
                $dbr = wfGetDB( DB_SLAVE );
                $row = $dbr->selectRow(
                        'category',
-                       array( 'cat_id', 'cat_title', 'cat_pages', 'cat_subcats',
-                               'cat_files' ),
+                       array( 'cat_id', 'cat_title', 'cat_pages', 'cat_subcats', 'cat_files' ),
                        $where,
                        __METHOD__
                );
-               if( !$row ) {
+
+               if ( !$row ) {
                        # Okay, there were no contents.  Nothing to initialize.
-                       return false;
+                       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->mSubcats = 0;
+                               $this->mFiles   = 0;
+
+                               return true;
+                       } else {
+                               return false; # Fail
+                       }
                }
+
                $this->mID      = $row->cat_id;
                $this->mName    = $row->cat_title;
                $this->mPages   = $row->cat_pages;
@@ -57,8 +72,7 @@ class Category {
                # (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 || $this->mSubcats < 0 ||
-               $this->mFiles < 0 ) {
+               if ( $this->mPages < 0 || $this->mSubcats < 0 || $this->mFiles < 0 ) {
                        $this->refreshCounts();
                }
 
@@ -68,19 +82,20 @@ class Category {
        /**
         * Factory function.
         *
-        * @param array $name A category name (no "Category:" prefix).  It need
+        * @param $name Array: 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
         */
        public static function newFromName( $name ) {
                $cat = new self();
                $title = Title::makeTitleSafe( NS_CATEGORY, $name );
-               if( !is_object( $title ) ) {
+
+               if ( !is_object( $title ) ) {
                        return false;
                }
 
                $cat->mTitle = $title;
-               $cat->mName = $title->getDBKey();
+               $cat->mName = $title->getDBkey();
 
                return $cat;
        }
@@ -88,14 +103,14 @@ class Category {
        /**
         * Factory function.
         *
-        * @param array $title Title for the category page
-        * @return mixed Category, or false on a totally invalid name
+        * @param $title Title for the category page
+        * @return Mixed: category, or false on a totally invalid name
         */
        public static function newFromTitle( $title ) {
                $cat = new self();
 
                $cat->mTitle = $title;
-               $cat->mName = $title->getDBKey();
+               $cat->mName = $title->getDBkey();
 
                return $cat;
        }
@@ -103,7 +118,7 @@ class Category {
        /**
         * Factory function.
         *
-        * @param array $id A category id
+        * @param $id Integer: a category id
         * @return Category
         */
        public static function newFromID( $id ) {
@@ -115,7 +130,7 @@ class Category {
        /**
         * Factory function, for constructing a Category object from a result set
         *
-        * @param $row result set row, must contain the cat_xxx fields. If the fields are null, 
+        * @param $row result set row, must contain the cat_xxx fields. If the fields are null,
         *        the resulting Category object will represent an empty category if a title object
         *        was given. If the fields are null and no title was given, this method fails and returns false.
         * @param $title optional title object for the category represented by the given row.
@@ -123,11 +138,10 @@ class Category {
         * @return Category
         */
        public static function newFromRow( $row, $title = null ) {
-               $cat->mTitle = $title;
-
                $cat = new self();
+               $cat->mTitle = $title;
 
-               # NOTE: the row often results from a LEFT JOIN on categorylinks. This may result in 
+               # 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
                #       category, if possible.
@@ -138,7 +152,7 @@ class Category {
                                # but we can't know that here...
                                return false;
                        } else {
-                               $cat->mName = $title->getDBKey(); # if we have a title object, fetch the category name from there
+                               $cat->mName = $title->getDBkey(); # if we have a title object, fetch the category name from there
                        }
 
                        $cat->mID =   false;
@@ -158,12 +172,16 @@ class Category {
 
        /** @return mixed DB key name, or false on failure */
        public function getName() { return $this->getX( 'mName' ); }
+
        /** @return mixed Category ID, or false on failure */
        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' ); }
+
        /** @return mixed Number of subcategories, or false on failure */
        public function getSubcatCount() { return $this->getX( 'mSubcats' ); }
+
        /** @return mixed Number of member files, or false on failure */
        public function getFileCount() { return $this->getX( 'mFiles' ); }
 
@@ -171,9 +189,9 @@ class Category {
         * @return mixed The Title for this category, or false on failure.
         */
        public function getTitle() {
-               if( $this->mTitle ) return $this->mTitle;
-               
-               if( !$this->initialize() ) {
+               if ( $this->mTitle ) return $this->mTitle;
+
+               if ( !$this->initialize() ) {
                        return false;
                }
 
@@ -181,12 +199,45 @@ class Category {
                return $this->mTitle;
        }
 
+       /**
+        * Fetch a TitleArray of up to $limit category members, beginning after the
+        * category sort key $offset.
+        * @param $limit integer
+        * @param $offset string
+        * @return TitleArray object for category members.
+        */
+       public function getMembers( $limit = false, $offset = '' ) {
+               $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;
+               }
+
+               if ( $offset !== '' ) {
+                       $conds[] = 'cl_sortkey > ' . $dbr->addQuotes( $offset );
+               }
+
+               return TitleArray::newFromResult(
+                       $dbr->select(
+                               array( 'page', 'categorylinks' ),
+                               array( 'page_id', 'page_namespace', 'page_title', 'page_len',
+                                       'page_is_redirect', 'page_latest' ),
+                               $conds,
+                               __METHOD__,
+                               $options
+                       )
+               );
+       }
+
        /** Generic accessor */
        private function getX( $key ) {
-               if( !$this->initialize() ) {
+               if ( !$this->initialize() ) {
                        return false;
                }
-               return $this->{$key};
+               return $this-> { $key } ;
        }
 
        /**
@@ -195,29 +246,33 @@ class Category {
         * @return bool True on success, false on failure
         */
        public function refreshCounts() {
-               if( wfReadOnly() ) {
+               if ( wfReadOnly() ) {
                        return false;
                }
                $dbw = wfGetDB( DB_MASTER );
                $dbw->begin();
                # Note, we must use names for this, since categorylinks does.
-               if( $this->mName === null ) {
-                       if( !$this->initialize() ) {
+               if ( $this->mName === null ) {
+                       if ( !$this->initialize() ) {
                                return false;
                        }
                } else {
                        # Let's be sure that the row exists in the table.  We don't need to
                        # do this if we got the row from the table in initialization!
+                       $seqVal = $dbw->nextSequenceValue( 'category_cat_id_seq' );
                        $dbw->insert(
                                'category',
-                               array( 'cat_title' => $this->mName ),
+                               array(
+                                       'cat_id' => $seqVal,
+                                       'cat_title' => $this->mName
+                               ),
                                __METHOD__,
                                'IGNORE'
                        );
                }
 
-               $cond1 = $dbw->conditional( 'page_namespace='.NS_CATEGORY, 1, 'NULL' );
-               $cond2 = $dbw->conditional( 'page_namespace='.NS_IMAGE, 1, 'NULL' );
+               $cond1 = $dbw->conditional( 'page_namespace=' . NS_CATEGORY, 1, 'NULL' );
+               $cond2 = $dbw->conditional( 'page_namespace=' . NS_FILE, 1, 'NULL' );
                $result = $dbw->selectRow(
                        array( 'categorylinks', 'page' ),
                        array( 'COUNT(*) AS pages',