Merge "Improve docs for Title::getInternalURL/getCanonicalURL"
[lhc/web/wiklou.git] / includes / libs / rdbms / database / Database.php
index 18961bd..ba97887 100644 (file)
@@ -104,7 +104,7 @@ abstract class Database implements IDatabase, IMaintainableDatabase, LoggerAware
        /** @var callable Deprecation logging callback */
        protected $deprecationLogger;
 
-       /** @var resource|null Database connection */
+       /** @var object|resource|null Database connection */
        protected $conn = null;
        /** @var bool */
        protected $opened = false;
@@ -524,20 +524,18 @@ abstract class Database implements IDatabase, IMaintainableDatabase, LoggerAware
                        $possibleDrivers = $builtinTypes[$dbType];
                        if ( is_string( $possibleDrivers ) ) {
                                $class = $possibleDrivers;
+                       } elseif ( (string)$driver !== '' ) {
+                               if ( !isset( $possibleDrivers[$driver] ) ) {
+                                       throw new InvalidArgumentException( __METHOD__ .
+                                               " type '$dbType' does not support driver '{$driver}'" );
+                               }
+
+                               $class = $possibleDrivers[$driver];
                        } else {
-                               if ( (string)$driver !== '' ) {
-                                       if ( !isset( $possibleDrivers[$driver] ) ) {
-                                               throw new InvalidArgumentException( __METHOD__ .
-                                                       " type '$dbType' does not support driver '{$driver}'" );
-                                       } else {
-                                               $class = $possibleDrivers[$driver];
-                                       }
-                               } else {
-                                       foreach ( $possibleDrivers as $posDriver => $possibleClass ) {
-                                               if ( extension_loaded( $posDriver ) ) {
-                                                       $class = $possibleClass;
-                                                       break;
-                                               }
+                               foreach ( $possibleDrivers as $posDriver => $possibleClass ) {
+                                       if ( extension_loaded( $posDriver ) ) {
+                                               $class = $possibleClass;
+                                               break;
                                        }
                                }
                        }
@@ -644,13 +642,13 @@ abstract class Database implements IDatabase, IMaintainableDatabase, LoggerAware
        public function getLBInfo( $name = null ) {
                if ( is_null( $name ) ) {
                        return $this->lbInfo;
-               } else {
-                       if ( array_key_exists( $name, $this->lbInfo ) ) {
-                               return $this->lbInfo[$name];
-                       } else {
-                               return null;
-                       }
                }
+
+               if ( array_key_exists( $name, $this->lbInfo ) ) {
+                       return $this->lbInfo[$name];
+               }
+
+               return null;
        }
 
        public function setLBInfo( $name, $value = null ) {
@@ -1034,7 +1032,7 @@ abstract class Database implements IDatabase, IMaintainableDatabase, LoggerAware
         */
        protected function assertIsWritableMaster() {
                if ( $this->getLBInfo( 'replica' ) === true ) {
-                       throw new DBUnexpectedError(
+                       throw new DBReadOnlyRoleError(
                                $this,
                                'Write operations are not allowed on replica database connections.'
                        );
@@ -1196,7 +1194,6 @@ abstract class Database implements IDatabase, IMaintainableDatabase, LoggerAware
 
                $flags = (int)$flags; // b/c; this field used to be a bool
                $ignoreErrors = $this->hasFlags( $flags, self::QUERY_SILENCE_ERRORS );
-               $pseudoPermanent = $this->hasFlags( $flags, self::QUERY_PSEUDO_PERMANENT );
 
                $priorTransaction = $this->trxLevel;
                $priorWritesPending = $this->writesOrCallbacksPending();
@@ -1208,8 +1205,13 @@ abstract class Database implements IDatabase, IMaintainableDatabase, LoggerAware
                        $this->assertIsWritableMaster();
                        # Do not treat temporary table writes as "meaningful writes" that need committing.
                        # Profile them as reads. Integration tests can override this behavior via $flags.
+                       $pseudoPermanent = $this->hasFlags( $flags, self::QUERY_PSEUDO_PERMANENT );
                        $tableType = $this->registerTempTableWrite( $sql, $pseudoPermanent );
                        $isEffectiveWrite = ( $tableType !== self::TEMP_NORMAL );
+                       # DBConnRef uses QUERY_REPLICA_ROLE to enforce the replica role for raw SQL queries
+                       if ( $isEffectiveWrite && $this->hasFlags( $flags, self::QUERY_REPLICA_ROLE ) ) {
+                               throw new DBReadOnlyRoleError( $this, "Cannot write; target role is DB_REPLICA" );
+                       }
                } else {
                        $isEffectiveWrite = false;
                }
@@ -3582,6 +3584,7 @@ abstract class Database implements IDatabase, IMaintainableDatabase, LoggerAware
                                list( $phpCallback ) = $callback;
                                $this->clearFlag( self::DBO_TRX ); // make each query its own transaction
                                try {
+                                       // @phan-suppress-next-line PhanParamTooManyCallable
                                        call_user_func( $phpCallback, $trigger, $this );
                                } catch ( Exception $ex ) {
                                        call_user_func( $this->errorLogger, $ex );
@@ -3975,17 +3978,15 @@ abstract class Database implements IDatabase, IMaintainableDatabase, LoggerAware
                                        "$fname: Flushing an explicit transaction, getting out of sync."
                                );
                        }
-               } else {
-                       if ( !$this->trxLevel ) {
-                               $this->queryLogger->error(
-                                       "$fname: No transaction to commit, something got out of sync." );
-                               return; // nothing to do
-                       } elseif ( $this->trxAutomatic ) {
-                               throw new DBUnexpectedError(
-                                       $this,
-                                       "$fname: Expected mass commit of all peer transactions (DBO_TRX set)."
-                               );
-                       }
+               } elseif ( !$this->trxLevel ) {
+                       $this->queryLogger->error(
+                               "$fname: No transaction to commit, something got out of sync." );
+                       return; // nothing to do
+               } elseif ( $this->trxAutomatic ) {
+                       throw new DBUnexpectedError(
+                               $this,
+                               "$fname: Expected mass commit of all peer transactions (DBO_TRX set)."
+                       );
                }
 
                $this->assertHasConnectionHandle();
@@ -4030,13 +4031,14 @@ abstract class Database implements IDatabase, IMaintainableDatabase, LoggerAware
        final public function rollback( $fname = __METHOD__, $flush = '' ) {
                $trxActive = $this->trxLevel;
 
-               if ( $flush !== self::FLUSHING_INTERNAL && $flush !== self::FLUSHING_ALL_PEERS ) {
-                       if ( $this->getFlag( self::DBO_TRX ) ) {
-                               throw new DBUnexpectedError(
-                                       $this,
-                                       "$fname: Expected mass rollback of all peer transactions (DBO_TRX set)."
-                               );
-                       }
+               if ( $flush !== self::FLUSHING_INTERNAL
+                       && $flush !== self::FLUSHING_ALL_PEERS
+                       && $this->getFlag( self::DBO_TRX )
+               ) {
+                       throw new DBUnexpectedError(
+                               $this,
+                               "$fname: Expected mass rollback of all peer transactions (DBO_TRX set)."
+                       );
                }
 
                if ( $trxActive ) {