Merge "RCFilters: Only normalize title with 'target' when it is needed"
[lhc/web/wiklou.git] / includes / libs / rdbms / database / Database.php
index 1c9cad5..2eb5c54 100644 (file)
@@ -236,12 +236,6 @@ abstract class Database implements IDatabase, IMaintainableDatabase, LoggerAware
        /** @var TransactionProfiler */
        protected $trxProfiler;
 
-       /**
-        * @var bool Whether writing is allowed on this connection.
-        *      Should be false for connections to replicas.
-        */
-       protected $allowWrite = true;
-
        /**
         * Constructor and database handle and attempt to connect to the DB server
         *
@@ -283,7 +277,6 @@ abstract class Database implements IDatabase, IMaintainableDatabase, LoggerAware
                $this->connLogger = $params['connLogger'];
                $this->queryLogger = $params['queryLogger'];
                $this->errorLogger = $params['errorLogger'];
-               $this->allowWrite = empty( $params['noWrite'] );
 
                // Set initial dummy domain until open() sets the final DB/prefix
                $this->currentDomain = DatabaseDomain::newUnspecified();
@@ -401,7 +394,9 @@ abstract class Database implements IDatabase, IMaintainableDatabase, LoggerAware
                        $p['variables'] = isset( $p['variables'] ) ? $p['variables'] : [];
                        $p['tablePrefix'] = isset( $p['tablePrefix'] ) ? $p['tablePrefix'] : '';
                        $p['schema'] = isset( $p['schema'] ) ? $p['schema'] : '';
-                       $p['cliMode'] = isset( $p['cliMode'] ) ? $p['cliMode'] : ( PHP_SAPI === 'cli' );
+                       $p['cliMode'] = isset( $p['cliMode'] )
+                               ? $p['cliMode']
+                               : ( PHP_SAPI === 'cli' || PHP_SAPI === 'phpdbg' );
                        $p['agent'] = isset( $p['agent'] ) ? $p['agent'] : '';
                        if ( !isset( $p['connLogger'] ) ) {
                                $p['connLogger'] = new \Psr\Log\NullLogger();
@@ -915,13 +910,12 @@ abstract class Database implements IDatabase, IMaintainableDatabase, LoggerAware
                }
 
                if ( $isWrite ) {
-                       if ( !$this->allowWrite ) {
+                       if ( $this->getLBInfo( 'replica' ) === true ) {
                                throw new DBError(
                                        $this,
-                                       'Write operations are not allowed on this database connection!'
+                                       'Write operations are not allowed on replica database connections.'
                                );
                        }
-
                        # In theory, non-persistent writes are allowed in read-only mode, but due to things
                        # like https://bugs.mysql.com/bug.php?id=33669 that might not work anyway...
                        $reason = $this->getReadOnlyReason();
@@ -2965,13 +2959,6 @@ abstract class Database implements IDatabase, IMaintainableDatabase, LoggerAware
        }
 
        final public function begin( $fname = __METHOD__, $mode = self::TRANSACTION_EXPLICIT ) {
-               if ( !$this->allowWrite ) {
-                       throw new DBError(
-                               $this,
-                               'Write operations are not allowed on this database connection!'
-                       );
-               }
-
                // Protect against mismatched atomic section, transaction nesting, and snapshot loss
                if ( $this->mTrxLevel ) {
                        if ( $this->mTrxAtomicLevels ) {
@@ -3033,14 +3020,6 @@ abstract class Database implements IDatabase, IMaintainableDatabase, LoggerAware
        }
 
        final public function commit( $fname = __METHOD__, $flush = '' ) {
-               if ( !$this->allowWrite ) {
-                       // we should never get here, since begin() would already throw
-                       throw new DBError(
-                               $this,
-                               'Write operations are not allowed on this database connection!'
-                       );
-               }
-
                if ( $this->mTrxLevel && $this->mTrxAtomicLevels ) {
                        // There are still atomic sections open. This cannot be ignored
                        $levels = implode( ', ', $this->mTrxAtomicLevels );
@@ -3413,6 +3392,12 @@ abstract class Database implements IDatabase, IMaintainableDatabase, LoggerAware
                $fname = __METHOD__,
                callable $inputCallback = null
        ) {
+               $delimiterReset = new ScopedCallback(
+                       function ( $delimiter ) {
+                               $this->delimiter = $delimiter;
+                       },
+                       [ $this->delimiter ]
+               );
                $cmd = '';
 
                while ( !feof( $fp ) ) {
@@ -3441,7 +3426,15 @@ abstract class Database implements IDatabase, IMaintainableDatabase, LoggerAware
                        if ( $done || feof( $fp ) ) {
                                $cmd = $this->replaceVars( $cmd );
 
-                               if ( !$inputCallback || call_user_func( $inputCallback, $cmd ) ) {
+                               if ( $inputCallback ) {
+                                       $callbackResult = call_user_func( $inputCallback, $cmd );
+
+                                       if ( is_string( $callbackResult ) || !$callbackResult ) {
+                                               $cmd = $callbackResult;
+                                       }
+                               }
+
+                               if ( $cmd ) {
                                        $res = $this->query( $cmd, $fname );
 
                                        if ( $resultCallback ) {
@@ -3458,6 +3451,7 @@ abstract class Database implements IDatabase, IMaintainableDatabase, LoggerAware
                        }
                }
 
+               ScopedCallback::consume( $delimiterReset );
                return true;
        }