Merge "prop=duplicatefiles does not show duplicates under same name"
[lhc/web/wiklou.git] / includes / Revision.php
index 14bd587..e7ddf3c 100644 (file)
@@ -23,7 +23,7 @@
 /**
  * @todo document
  */
-class Revision {
+class Revision implements IDBAccessObject {
        protected $mId;
        protected $mPage;
        protected $mUserText;
@@ -41,13 +41,14 @@ class Revision {
        protected $mTitle;
        protected $mCurrent;
 
+       // Revision deletion constants
        const DELETED_TEXT = 1;
        const DELETED_COMMENT = 2;
        const DELETED_USER = 4;
        const DELETED_RESTRICTED = 8;
-       // Convenience field
-       const SUPPRESSED_USER = 12;
-       // Audience options for Revision::getText()
+       const SUPPRESSED_USER = 12; // convenience
+
+       // Audience options for accessors
        const FOR_PUBLIC = 1;
        const FOR_THIS_USER = 2;
        const RAW = 3;
@@ -56,11 +57,16 @@ class Revision {
         * Load a page revision from a given revision ID number.
         * Returns null if no such revision can be found.
         *
+        * $flags include:
+        *      Revision::READ_LATEST  : Select the data from the master
+        *      Revision::READ_LOCKING : Select & lock the data from the master
+        *
         * @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,16 @@ class Revision {
         * that's attached to a given title. If not attached
         * to that title, will return null.
         *
+        * $flags include:
+        *      Revision::READ_LATEST  : Select the data from the master
+        *      Revision::READ_LOCKING : Select & lock the data from the master
+        *
         * @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 = null ) {
                $conds = array(
                        'page_namespace' => $title->getNamespace(),
                        'page_title'     => $title->getDBkey()
@@ -80,19 +91,13 @@ class Revision {
                if ( $id ) {
                        // Use the specified ID
                        $conds['rev_id'] = $id;
-               } elseif ( wfGetLB()->getServerCount() > 1 ) {
-                       // Get the latest revision ID from the master
-                       $dbw = wfGetDB( DB_MASTER );
-                       $latest = $dbw->selectField( 'page', 'page_latest', $conds, __METHOD__ );
-                       if ( $latest === false ) {
-                               return null; // page does not exist
-                       }
-                       $conds['rev_id'] = $latest;
                } else {
                        // Use a join to get the latest revision
                        $conds[] = 'rev_id=page_latest';
+                       // Callers assume this will be up-to-date
+                       $flags = is_int( $flags ) ? $flags : self::READ_LATEST; // b/c
                }
-               return Revision::newFromConds( $conds );
+               return self::newFromConds( $conds, (int)$flags );
        }
 
        /**
@@ -100,26 +105,26 @@ class Revision {
         * that's attached to a given page ID.
         * Returns null if no such revision can be found.
         *
+        * $flags include:
+        *      Revision::READ_LATEST  : Select the data from the master
+        *      Revision::READ_LOCKING : Select & lock the data from the master
+        *
         * @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 = null ) {
                $conds = array( 'page_id' => $pageId );
                if ( $revId ) {
                        $conds['rev_id'] = $revId;
-               } elseif ( wfGetLB()->getServerCount() > 1 ) {
-                       // Get the latest revision ID from the master
-                       $dbw = wfGetDB( DB_MASTER );
-                       $latest = $dbw->selectField( 'page', 'page_latest', $conds, __METHOD__ );
-                       if ( $latest === false ) {
-                               return null; // page does not exist
-                       }
-                       $conds['rev_id'] = $latest;
                } else {
+                       // Use a join to get the latest revision
                        $conds[] = 'rev_id = page_latest';
+                       // Callers assume this will be up-to-date
+                       $flags = is_int( $flags ) ? $flags : self::READ_LATEST; // b/c
                }
-               return Revision::newFromConds( $conds );
+               return self::newFromConds( $conds, (int)$flags );
        }
 
        /**
@@ -175,7 +180,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 +200,7 @@ class Revision {
                } else {
                        $conds[] = 'rev_id=page_latest';
                }
-               return Revision::loadFromConds( $db, $conds );
+               return self::loadFromConds( $db, $conds );
        }
 
        /**
@@ -214,7 +219,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 +237,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 +248,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::READ_LATEST ) ? DB_MASTER : DB_SLAVE );
+               $rev = self::loadFromConds( $db, $conditions, $flags );
+               if ( is_null( $rev ) && wfGetLB()->getServerCount() > 1 ) {
+                       if ( !( $flags & self::READ_LATEST ) ) {
+                               $dbw = wfGetDB( DB_MASTER );
+                               $rev = self::loadFromConds( $dbw, $conditions, $flags );
+                       }
                }
                return $rev;
        }
@@ -261,10 +269,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 +294,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 +309,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::READ_LOCKING ) {
+                       $options[] = 'FOR UPDATE';
+               }
                return $db->select(
                        array( 'revision', 'page', 'user' ),
                        $fields,
                        $conditions,
                        __METHOD__,
-                       array( 'LIMIT' => 1 ),
+                       $options,
                        array( 'page' => self::pageJoinCond(), 'user' => self::userJoinCond() )
                );
        }
@@ -834,7 +848,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;
@@ -849,7 +863,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;
@@ -979,7 +993,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 );
@@ -998,7 +1012,7 @@ class Revision {
                wfProfileIn( __METHOD__ );
 
                $data = $this->mText;
-               $flags = Revision::compressRevisionText( $data );
+               $flags = self::compressRevisionText( $data );
 
                # Write to external storage if required
                if( $wgDefaultExternalStore ) {
@@ -1048,7 +1062,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__
                );
@@ -1254,7 +1268,7 @@ class Revision {
         * @return Integer
         */
        static function countByPageId( $db, $id ) {
-               $row = $db->selectRow( 'revision', 'COUNT(*) AS revCount',
+               $row = $db->selectRow( 'revision', array( 'revCount' => 'COUNT(*)' ),
                        array( 'rev_page' => $id ), __METHOD__ );
                if( $row ) {
                        return $row->revCount;
@@ -1272,7 +1286,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;
        }