Merge "Reduced some master queries by adding flags to Revision functions."
[lhc/web/wiklou.git] / includes / Revision.php
index 6928eb9..13eaae4 100644 (file)
@@ -23,7 +23,7 @@
 /**
  * @todo document
  */
-class Revision {
+class Revision implements IDBAccessObject {
        protected $mId;
        protected $mPage;
        protected $mUserText;
@@ -47,7 +47,7 @@ class Revision {
        const DELETED_RESTRICTED = 8;
        // Convenience field
        const SUPPRESSED_USER = 12;
-       // Audience options for Revision::getText()
+       // Audience options for accessors
        const FOR_PUBLIC = 1;
        const FOR_THIS_USER = 2;
        const RAW = 3;
@@ -56,11 +56,17 @@ class Revision {
         * Load a page revision from a given revision ID number.
         * Returns null if no such revision can be found.
         *
+        * $flags include:
+        *      IDBAccessObject::LATEST_READ  : Select the data from the master
+        *      IDBAccessObject::LOCKING_READ : Select & lock the data from the master
+        *      IDBAccessObject::AVOID_MASTER : Avoid master queries; data may be stale
+        *
         * @param $id Integer
+        * @param $flags Integer (optional)
         * @return Revision or null
         */
-       public static function newFromId( $id ) {
-               return Revision::newFromConds( array( 'rev_id' => intval( $id ) ) );
+       public static function newFromId( $id, $flags = 0 ) {
+               return self::newFromConds( array( 'rev_id' => intval( $id ) ), $flags );
        }
 
        /**
@@ -68,11 +74,17 @@ class Revision {
         * that's attached to a given title. If not attached
         * to that title, will return null.
         *
+        * $flags include:
+        *      IDBAccessObject::LATEST_READ  : Select the data from the master
+        *      IDBAccessObject::LOCKING_READ : Select & lock the data from the master
+        *      IDBAccessObject::AVOID_MASTER : Avoid master queries; data may be stale
+        *
         * @param $title Title
         * @param $id Integer (optional)
+        * @param $flags Integer Bitfield (optional)
         * @return Revision or null
         */
-       public static function newFromTitle( $title, $id = 0 ) {
+       public static function newFromTitle( $title, $id = 0, $flags = 0 ) {
                $conds = array(
                        'page_namespace' => $title->getNamespace(),
                        'page_title'     => $title->getDBkey()
@@ -80,7 +92,7 @@ class Revision {
                if ( $id ) {
                        // Use the specified ID
                        $conds['rev_id'] = $id;
-               } elseif ( wfGetLB()->getServerCount() > 1 ) {
+               } elseif ( !( $flags & self::AVOID_MASTER ) && wfGetLB()->getServerCount() > 1 ) {
                        // Get the latest revision ID from the master
                        $dbw = wfGetDB( DB_MASTER );
                        $latest = $dbw->selectField( 'page', 'page_latest', $conds, __METHOD__ );
@@ -92,7 +104,7 @@ class Revision {
                        // Use a join to get the latest revision
                        $conds[] = 'rev_id=page_latest';
                }
-               return Revision::newFromConds( $conds );
+               return self::newFromConds( $conds, $flags );
        }
 
        /**
@@ -100,15 +112,21 @@ class Revision {
         * that's attached to a given page ID.
         * Returns null if no such revision can be found.
         *
+        * $flags include:
+        *      IDBAccessObject::LATEST_READ  : Select the data from the master
+        *      IDBAccessObject::LOCKING_READ : Select & lock the data from the master
+        *      IDBAccessObject::AVOID_MASTER : Avoid master queries; data may be stale
+        *
         * @param $revId Integer
         * @param $pageId Integer (optional)
+        * @param $flags Integer Bitfield (optional)
         * @return Revision or null
         */
-       public static function newFromPageId( $pageId, $revId = 0 ) {
+       public static function newFromPageId( $pageId, $revId = 0, $flags = 0 ) {
                $conds = array( 'page_id' => $pageId );
                if ( $revId ) {
                        $conds['rev_id'] = $revId;
-               } elseif ( wfGetLB()->getServerCount() > 1 ) {
+               } elseif ( !( $flags & self::AVOID_MASTER ) && wfGetLB()->getServerCount() > 1 ) {
                        // Get the latest revision ID from the master
                        $dbw = wfGetDB( DB_MASTER );
                        $latest = $dbw->selectField( 'page', 'page_latest', $conds, __METHOD__ );
@@ -119,7 +137,7 @@ class Revision {
                } else {
                        $conds[] = 'rev_id = page_latest';
                }
-               return Revision::newFromConds( $conds );
+               return self::newFromConds( $conds, $flags );
        }
 
        /**
@@ -175,7 +193,7 @@ class Revision {
         * @return Revision or null
         */
        public static function loadFromId( $db, $id ) {
-               return Revision::loadFromConds( $db, array( 'rev_id' => intval( $id ) ) );
+               return self::loadFromConds( $db, array( 'rev_id' => intval( $id ) ) );
        }
 
        /**
@@ -195,7 +213,7 @@ class Revision {
                } else {
                        $conds[] = 'rev_id=page_latest';
                }
-               return Revision::loadFromConds( $db, $conds );
+               return self::loadFromConds( $db, $conds );
        }
 
        /**
@@ -214,7 +232,7 @@ class Revision {
                } else {
                        $matchId = 'page_latest';
                }
-               return Revision::loadFromConds( $db,
+               return self::loadFromConds( $db,
                        array( "rev_id=$matchId",
                                   'page_namespace' => $title->getNamespace(),
                                   'page_title'     => $title->getDBkey() )
@@ -232,7 +250,7 @@ class Revision {
         * @return Revision or null
         */
        public static function loadFromTimestamp( $db, $title, $timestamp ) {
-               return Revision::loadFromConds( $db,
+               return self::loadFromConds( $db,
                        array( 'rev_timestamp'  => $db->timestamp( $timestamp ),
                                   'page_namespace' => $title->getNamespace(),
                                   'page_title'     => $title->getDBkey() )
@@ -243,14 +261,17 @@ class Revision {
         * Given a set of conditions, fetch a revision.
         *
         * @param $conditions Array
+        * @param $flags integer (optional)
         * @return Revision or null
         */
-       public static function newFromConds( $conditions ) {
-               $db = wfGetDB( DB_SLAVE );
-               $rev = Revision::loadFromConds( $db, $conditions );
-               if( is_null( $rev ) && wfGetLB()->getServerCount() > 1 ) {
-                       $dbw = wfGetDB( DB_MASTER );
-                       $rev = Revision::loadFromConds( $dbw, $conditions );
+       private static function newFromConds( $conditions, $flags = 0 ) {
+               $db = wfGetDB( ( $flags & self::LATEST_READ ) ? DB_MASTER : DB_SLAVE );
+               $rev = self::loadFromConds( $db, $conditions, $flags );
+               if ( is_null( $rev ) && wfGetLB()->getServerCount() > 1 ) {
+                       if ( !( $flags & self::LATEST_READ ) && !( $flags & self::AVOID_MASTER ) ) {
+                               $dbw = wfGetDB( DB_MASTER );
+                               $rev = self::loadFromConds( $dbw, $conditions, $flags );
+                       }
                }
                return $rev;
        }
@@ -261,10 +282,11 @@ class Revision {
         *
         * @param $db DatabaseBase
         * @param $conditions Array
+        * @param $flags integer (optional)
         * @return Revision or null
         */
-       private static function loadFromConds( $db, $conditions ) {
-               $res = Revision::fetchFromConds( $db, $conditions );
+       private static function loadFromConds( $db, $conditions, $flags = 0 ) {
+               $res = self::fetchFromConds( $db, $conditions, $flags );
                if( $res ) {
                        $row = $res->fetchObject();
                        if( $row ) {
@@ -285,7 +307,7 @@ class Revision {
         * @return ResultWrapper
         */
        public static function fetchRevision( $title ) {
-               return Revision::fetchFromConds(
+               return self::fetchFromConds(
                        wfGetDB( DB_SLAVE ),
                        array( 'rev_id=page_latest',
                                   'page_namespace' => $title->getNamespace(),
@@ -300,20 +322,25 @@ class Revision {
         *
         * @param $db DatabaseBase
         * @param $conditions Array
+        * @param $flags integer (optional)
         * @return ResultWrapper
         */
-       private static function fetchFromConds( $db, $conditions ) {
+       private static function fetchFromConds( $db, $conditions, $flags = 0 ) {
                $fields = array_merge(
                        self::selectFields(),
                        self::selectPageFields(),
                        self::selectUserFields()
                );
+               $options = array( 'LIMIT' => 1 );
+               if ( $flags & self::FOR_UPDATE ) {
+                       $options[] = 'FOR UPDATE';
+               }
                return $db->select(
                        array( 'revision', 'page', 'user' ),
                        $fields,
                        $conditions,
                        __METHOD__,
-                       array( 'LIMIT' => 1 ),
+                       $options,
                        array( 'page' => self::pageJoinCond(), 'user' => self::userJoinCond() )
                );
        }
@@ -381,7 +408,9 @@ class Revision {
                        'page_namespace',
                        'page_title',
                        'page_id',
-                       'page_latest'
+                       'page_latest',
+                       'page_is_redirect',
+                       'page_len',
                );
        }
 
@@ -393,6 +422,29 @@ class Revision {
                return array( 'user_name' );
        }
 
+       /**
+        * Do a batched query to get the parent revision lengths
+        * @param $db DatabaseBase
+        * @param $revIds Array
+        * @return array
+        */
+       public static function getParentLengths( $db, array $revIds ) {
+               $revLens = array();
+               if ( !$revIds ) {
+                       return $revLens; // empty
+               }
+               wfProfileIn( __METHOD__ );
+               $res = $db->select( 'revision',
+                       array( 'rev_id', 'rev_len' ),
+                       array( 'rev_id' => $revIds ),
+                       __METHOD__ );
+               foreach ( $res as $row ) {
+                       $revLens[$row->rev_id] = $row->rev_len;
+               }
+               wfProfileOut( __METHOD__ );
+               return $revLens;
+       }
+
        /**
         * Constructor
         *
@@ -493,7 +545,7 @@ class Revision {
        /**
         * Get revision ID
         *
-        * @return Integer
+        * @return Integer|null
         */
        public function getId() {
                return $this->mId;
@@ -512,7 +564,7 @@ class Revision {
        /**
         * Get text row ID
         *
-        * @return Integer
+        * @return Integer|null
         */
        public function getTextId() {
                return $this->mTextId;
@@ -530,7 +582,7 @@ class Revision {
        /**
         * Returns the length of the text in this revision, or null if unknown.
         *
-        * @return Integer
+        * @return Integer|null
         */
        public function getSize() {
                return $this->mSize;
@@ -539,30 +591,34 @@ class Revision {
        /**
         * Returns the base36 sha1 of the text in this revision, or null if unknown.
         *
-        * @return String
+        * @return String|null
         */
        public function getSha1() {
                return $this->mSha1;
        }
 
        /**
-        * Returns the title of the page associated with this entry.
+        * Returns the title of the page associated with this entry or null.
+        *
+        * Will do a query, when title is not set and id is given.
         *
-        * @return Title
+        * @return Title|null
         */
        public function getTitle() {
                if( isset( $this->mTitle ) ) {
                        return $this->mTitle;
                }
-               $dbr = wfGetDB( DB_SLAVE );
-               $row = $dbr->selectRow(
-                       array( 'page', 'revision' ),
-                       self::selectPageFields(),
-                       array( 'page_id=rev_page',
-                                  'rev_id' => $this->mId ),
-                       __METHOD__ );
-               if ( $row ) {
-                       $this->mTitle = Title::newFromRow( $row );
+               if( !is_null( $this->mId ) ) { //rev_id is defined as NOT NULL
+                       $dbr = wfGetDB( DB_SLAVE );
+                       $row = $dbr->selectRow(
+                               array( 'page', 'revision' ),
+                               self::selectPageFields(),
+                               array( 'page_id=rev_page',
+                                          'rev_id' => $this->mId ),
+                               __METHOD__ );
+                       if ( $row ) {
+                               $this->mTitle = Title::newFromRow( $row );
+                       }
                }
                return $this->mTitle;
        }
@@ -579,7 +635,7 @@ class Revision {
        /**
         * Get the page ID
         *
-        * @return Integer
+        * @return Integer|null
         */
        public function getPage() {
                return $this->mPage;
@@ -592,7 +648,7 @@ class Revision {
         *
         * @param $audience Integer: one of:
         *      Revision::FOR_PUBLIC       to be displayed to all users
-        *      Revision::FOR_THIS_USER    to be displayed to $wgUser
+        *      Revision::FOR_THIS_USER    to be displayed to the given user
         *      Revision::RAW              get the ID regardless of permissions
         * @param $user User object to check for, only if FOR_THIS_USER is passed
         *              to the $audience parameter
@@ -624,7 +680,7 @@ class Revision {
         *
         * @param $audience Integer: one of:
         *      Revision::FOR_PUBLIC       to be displayed to all users
-        *      Revision::FOR_THIS_USER    to be displayed to $wgUser
+        *      Revision::FOR_THIS_USER    to be displayed to the given user
         *      Revision::RAW              get the text regardless of permissions
         * @param $user User object to check for, only if FOR_THIS_USER is passed
         *              to the $audience parameter
@@ -664,7 +720,7 @@ class Revision {
         *
         * @param $audience Integer: one of:
         *      Revision::FOR_PUBLIC       to be displayed to all users
-        *      Revision::FOR_THIS_USER    to be displayed to $wgUser
+        *      Revision::FOR_THIS_USER    to be displayed to the given user
         *      Revision::RAW              get the text regardless of permissions
         * @param $user User object to check for, only if FOR_THIS_USER is passed
         *              to the $audience parameter
@@ -742,7 +798,7 @@ class Revision {
         *
         * @param $audience Integer: one of:
         *      Revision::FOR_PUBLIC       to be displayed to all users
-        *      Revision::FOR_THIS_USER    to be displayed to $wgUser
+        *      Revision::FOR_THIS_USER    to be displayed to the given user
         *      Revision::RAW              get the text regardless of permissions
         * @param $user User object to check for, only if FOR_THIS_USER is passed
         *              to the $audience parameter
@@ -805,7 +861,7 @@ class Revision {
                if( $this->getTitle() ) {
                        $prev = $this->getTitle()->getPreviousRevisionID( $this->getId() );
                        if( $prev ) {
-                               return Revision::newFromTitle( $this->getTitle(), $prev );
+                               return self::newFromTitle( $this->getTitle(), $prev );
                        }
                }
                return null;
@@ -820,7 +876,7 @@ class Revision {
                if( $this->getTitle() ) {
                        $next = $this->getTitle()->getNextRevisionID( $this->getId() );
                        if ( $next ) {
-                               return Revision::newFromTitle( $this->getTitle(), $next );
+                               return self::newFromTitle( $this->getTitle(), $next );
                        }
                }
                return null;
@@ -950,7 +1006,7 @@ class Revision {
                                $text = gzdeflate( $text );
                                $flags[] = 'gzip';
                        } else {
-                               wfDebug( "Revision::compressRevisionText() -- no zlib support, not compressing\n" );
+                               wfDebug( __METHOD__ . " -- no zlib support, not compressing\n" );
                        }
                }
                return implode( ',', $flags );
@@ -969,7 +1025,7 @@ class Revision {
                wfProfileIn( __METHOD__ );
 
                $data = $this->mText;
-               $flags = Revision::compressRevisionText( $data );
+               $flags = self::compressRevisionText( $data );
 
                # Write to external storage if required
                if( $wgDefaultExternalStore ) {
@@ -1019,7 +1075,7 @@ class Revision {
                                        ? $this->getPreviousRevisionId( $dbw )
                                        : $this->mParentId,
                                'rev_sha1'       => is_null( $this->mSha1 )
-                                       ? Revision::base36Sha1( $this->mText )
+                                       ? self::base36Sha1( $this->mText )
                                        : $this->mSha1
                        ), __METHOD__
                );
@@ -1120,7 +1176,8 @@ class Revision {
 
                $current = $dbw->selectRow(
                        array( 'page', 'revision' ),
-                       array( 'page_latest', 'rev_text_id', 'rev_len', 'rev_sha1' ),
+                       array( 'page_latest', 'page_namespace', 'page_title',
+                               'rev_text_id', 'rev_len', 'rev_sha1' ),
                        array(
                                'page_id' => $pageId,
                                'page_latest=rev_id',
@@ -1137,6 +1194,7 @@ class Revision {
                                'len'        => $current->rev_len,
                                'sha1'       => $current->rev_sha1
                                ) );
+                       $revision->setTitle( Title::makeTitle( $current->page_namespace, $current->page_title ) );
                } else {
                        $revision = null;
                }
@@ -1241,7 +1299,7 @@ class Revision {
        static function countByTitle( $db, $title ) {
                $id = $title->getArticleID();
                if( $id ) {
-                       return Revision::countByPageId( $db, $id );
+                       return self::countByPageId( $db, $id );
                }
                return 0;
        }