Some protection move log cleanup (bug 15754)
[lhc/web/wiklou.git] / includes / Revision.php
index 79aa20f..857c50f 100644 (file)
@@ -13,6 +13,11 @@ class Revision {
        const DELETED_USER = 4;
        const DELETED_RESTRICTED = 8;
 
+       // Audience options for Revision::getText()
+       const FOR_PUBLIC = 1;
+       const FOR_THIS_USER = 2;
+       const RAW = 3;
+
        /**
         * Load a page revision from a given revision ID number.
         * Returns null if no such revision can be found.
@@ -37,16 +42,24 @@ class Revision {
         * @return Revision
         */
        public static function newFromTitle( $title, $id = 0 ) {
-               if( $id ) {
-                       $matchId = intval( $id );
+               $conds = array( 
+                       'page_namespace' => $title->getNamespace(), 
+                       'page_title' => $title->getDBkey()
+               );
+               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__ );
+                       $conds['rev_id'] = $latest;
                } else {
-                       $matchId = 'page_latest';
+                       // Use a join to get the latest revision
+                       $conds[] = 'rev_id=page_latest';
                }
-               return Revision::newFromConds(
-                       array( "rev_id=$matchId",
-                              'page_id=rev_page',
-                              'page_namespace' => $title->getNamespace(),
-                              'page_title'     => $title->getDBkey() ) );
+               $conds[] = 'page_id=rev_page';
+               return Revision::newFromConds( $conds );
        }
 
        /**
@@ -144,7 +157,7 @@ class Revision {
        private static function newFromConds( $conditions ) {
                $db = wfGetDB( DB_SLAVE );
                $row = Revision::loadFromConds( $db, $conditions );
-               if( is_null( $row ) ) {
+               if( is_null( $row ) && wfGetLB()->getServerCount() > 1 ) {
                        $dbw = wfGetDB( DB_MASTER );
                        $row = Revision::loadFromConds( $dbw, $conditions );
                }
@@ -232,7 +245,7 @@ class Revision {
                        array( 'page', 'revision' ),
                        $fields,
                        $conditions,
-                       'Revision::fetchRow',
+                       __METHOD__,
                        array( 'LIMIT' => 1 ) );
                $ret = $db->resultObject( $res );
                return $ret;
@@ -427,11 +440,22 @@ class Revision {
        }
 
        /**
-        * Fetch revision's user id if it's available to all users
+        * Fetch revision's user id if it's available to the specified audience.
+        * If the specified audience does not have access to it, zero will be 
+        * returned.
+        *
+        * @param integer $audience One of:
+        *      Revision::FOR_PUBLIC       to be displayed to all users
+        *      Revision::FOR_THIS_USER    to be displayed to $wgUser
+        *      Revision::RAW              get the ID regardless of permissions
+        *
+        *
         * @return int
         */
-       public function getUser() {
-               if( $this->isDeleted( self::DELETED_USER ) ) {
+       public function getUser( $audience = self::FOR_PUBLIC ) {
+               if( $audience == self::FOR_PUBLIC && $this->isDeleted( self::DELETED_USER ) ) {
+                       return 0;
+               } elseif( $audience == self::FOR_THIS_USER && !$this->userCan( self::DELETED_USER ) ) {
                        return 0;
                } else {
                        return $this->mUser;
@@ -447,11 +471,21 @@ class Revision {
        }
 
        /**
-        * Fetch revision's username if it's available to all users
+        * Fetch revision's username if it's available to the specified audience.
+        * If the specified audience does not have access to the username, an 
+        * empty string will be returned.
+        *
+        * @param integer $audience One of:
+        *      Revision::FOR_PUBLIC       to be displayed to all users
+        *      Revision::FOR_THIS_USER    to be displayed to $wgUser
+        *      Revision::RAW              get the text regardless of permissions
+        *
         * @return string
         */
-       public function getUserText() {
-               if( $this->isDeleted( self::DELETED_USER ) ) {
+       public function getUserText( $audience = self::FOR_PUBLIC ) {
+               if( $audience == self::FOR_PUBLIC && $this->isDeleted( self::DELETED_USER ) ) {
+                       return "";
+               } elseif( $audience == self::FOR_THIS_USER && !$this->userCan( self::DELETED_USER ) ) {
                        return "";
                } else {
                        return $this->mUserText;
@@ -467,11 +501,21 @@ class Revision {
        }
 
        /**
-        * Fetch revision comment if it's available to all users
+        * Fetch revision comment if it's available to the specified audience.
+        * If the specified audience does not have access to the comment, an 
+        * empty string will be returned.
+        *
+        * @param integer $audience One of:
+        *      Revision::FOR_PUBLIC       to be displayed to all users
+        *      Revision::FOR_THIS_USER    to be displayed to $wgUser
+        *      Revision::RAW              get the text regardless of permissions
+        *
         * @return string
         */
-       function getComment() {
-               if( $this->isDeleted( self::DELETED_COMMENT ) ) {
+       function getComment( $audience = self::FOR_PUBLIC ) {
+               if( $audience == self::FOR_PUBLIC && $this->isDeleted( self::DELETED_COMMENT ) ) {
+                       return "";
+               } elseif( $audience == self::FOR_THIS_USER && !$this->userCan( self::DELETED_COMMENT ) ) {
                        return "";
                } else {
                        return $this->mComment;
@@ -502,19 +546,35 @@ class Revision {
        }
 
        /**
-        * Fetch revision text if it's available to all users
+        * Fetch revision text if it's available to the specified audience.
+        * If the specified audience does not have the ability to view this 
+        * revision, an empty string will be returned.
+        *
+        * @param integer $audience One of:
+        *      Revision::FOR_PUBLIC       to be displayed to all users
+        *      Revision::FOR_THIS_USER    to be displayed to $wgUser
+        *      Revision::RAW              get the text regardless of permissions
+        *
+        *
         * @return string
         */
-       public function getText( $isPublic = true ) {
-               if( $isPublic && $this->isDeleted( self::DELETED_TEXT ) ) {
+       public function getText( $audience = self::FOR_PUBLIC ) {
+               if( $audience == self::FOR_PUBLIC && $this->isDeleted( self::DELETED_TEXT ) ) {
                        return "";
-               } else if( !$this->userCan( self::DELETED_TEXT ) ) {
+               } elseif( $audience == self::FOR_THIS_USER && !$this->userCan( self::DELETED_TEXT ) ) {
                        return "";
                } else {
                        return $this->getRawText();
                }
        }
 
+       /**
+        * Alias for getText(Revision::FOR_THIS_USER)
+        */
+       public function revText() {
+               return $this->getText( self::FOR_THIS_USER );
+       }
+
        /**
         * Fetch revision text without regard for view restrictions
         * @return string
@@ -711,7 +771,7 @@ class Revision {
                # Write to external storage if required
                if( $wgDefaultExternalStore ) {
                        // Store and get the URL
-                       $data = ExternalStore::randomInsert( $data );
+                       $data = ExternalStore::insertToDefault( $data );
                        if( !$data ) {
                                throw new MWException( "Unable to store text to external storage" );
                        }
@@ -799,7 +859,7 @@ class Revision {
                                __METHOD__ );
                }
 
-               if( !$row ) {
+               if( !$row && wfGetLB()->getServerCount() > 1 ) {
                        // Possible slave lag!
                        $dbw = wfGetDB( DB_MASTER );
                        $row = $dbw->selectRow( 'text',
@@ -896,7 +956,7 @@ class Revision {
                        $conds['rev_page'] = $pageId;
                }
                $timestamp = $dbr->selectField( 'revision', 'rev_timestamp', $conds, __METHOD__ );
-               if ( $timestamp === false ) {
+               if ( $timestamp === false && wfGetLB()->getServerCount() > 1 ) {
                        # Not in slave, try master
                        $dbw = wfGetDB( DB_MASTER );
                        $timestamp = $dbw->selectField( 'revision', 'rev_timestamp', $conds, __METHOD__ );