protected $connLogger;
/** @var LoggerInterface */
protected $queryLogger;
+ /** @var callback Error logging callback */
+ protected $errorLogger;
/** @var resource Database connection */
protected $mConn = null;
* @param array $p An array of options to pass to the constructor.
* Valid options are: host, user, password, dbname, flags, tablePrefix, schema, driver
* @return IDatabase|null If the database driver or extension cannot be found
- * @throws MWException
+ * @throws InvalidArgumentException If the database driver or extension cannot be found
*/
final public static function factory( $dbType, $p = [] ) {
global $wgCommandLineMode;
$driver = $dbType;
}
if ( $driver === false ) {
- throw new MWException( __METHOD__ .
+ throw new InvalidArgumentException( __METHOD__ .
" no viable database extension found for type '$dbType'" );
}
if ( isset( $p['queryLogger'] ) ) {
$conn->queryLogger = $p['queryLogger'];
}
+ if ( isset( $p['errorLogger'] ) ) {
+ $conn->errorLogger = $p['errorLogger'];
+ } else {
+ $conn->errorLogger = [ MWExceptionHandler::class, 'logException' ];
+ }
} else {
$conn = null;
}
protected function installErrorHandler() {
$this->mPHPError = false;
$this->htmlErrors = ini_set( 'html_errors', '0' );
- set_error_handler( [ $this, 'connectionErrorHandler' ] );
+ set_error_handler( [ $this, 'connectionerrorLogger' ] );
}
/**
* @param int $errno
* @param string $errstr
*/
- public function connectionErrorHandler( $errno, $errstr ) {
+ public function connectionerrorLogger( $errno, $errstr ) {
$this->mPHPError = $errstr;
}
/**
- * Create a log context to pass to wfLogDBError or other logging functions.
+ * Create a log context to pass to PSR logging functions.
*
* @param array $extras Additional data to add to context
* @return array
public function close() {
if ( $this->mConn ) {
if ( $this->trxLevel() ) {
- if ( !$this->mTrxAutomatic ) {
- wfWarn( "Transaction still in progress (from {$this->mTrxFname}), " .
- " performing implicit commit before closing connection!" );
- }
-
$this->commit( __METHOD__, self::FLUSHING_INTERNAL );
}
if ( $this->reconnect() ) {
$msg = __METHOD__ . ": lost connection to {$this->getServer()}; reconnected";
$this->connLogger->warning( $msg );
- $this->queryLogger->warning( "$msg:\n" . wfBacktrace( true ) );
+ $this->queryLogger->warning(
+ "$msg:\n" . ( new RuntimeException() )->getTraceAsString() );
if ( !$recoverable ) {
# Callers may catch the exception and continue to use the DB
$this->queryLogger->debug( "SQL ERROR (ignored): $error\n" );
} else {
$sql1line = mb_substr( str_replace( "\n", "\\n", $sql ), 0, 5 * 1024 );
- wfLogDBError(
+ $this->queryLogger->error(
"{fname}\t{db_server}\t{errno}\t{error}\t{sql1line}",
$this->getLogContext( [
'method' => __METHOD__,
$this->clearFlag( DBO_TRX ); // restore auto-commit
}
} catch ( Exception $ex ) {
- MWExceptionHandler::logException( $ex );
+ call_user_func( $this->errorLogger, $ex );
$e = $e ?: $ex;
// Some callbacks may use startAtomic/endAtomic, so make sure
// their transactions are ended so other callbacks don't fail
list( $phpCallback ) = $callback;
call_user_func( $phpCallback );
} catch ( Exception $ex ) {
- MWExceptionHandler::logException( $ex );
+ call_user_func( $this->errorLogger, $ex );
$e = $e ?: $ex;
}
}
list( $phpCallback ) = $callback;
$phpCallback( $trigger, $this );
} catch ( Exception $ex ) {
- MWExceptionHandler::logException( $ex );
+ call_user_func( $this->errorLogger, $ex );
$e = $e ?: $ex;
}
}
} else {
// @TODO: make this an exception at some point
$msg = "$fname: Implicit transaction already active (from {$this->mTrxFname}).";
- wfLogDBError( $msg );
- wfWarn( $msg );
+ $this->queryLogger->error( $msg );
return; // join the main transaction set
}
} elseif ( $this->getFlag( DBO_TRX ) && $mode !== self::TRANSACTION_INTERNAL ) {
// @TODO: make this an exception at some point
$msg = "$fname: Implicit transaction expected (DBO_TRX set).";
- wfLogDBError( $msg );
- wfWarn( $msg );
+ $this->queryLogger->error( $msg );
return; // let any writes be in the main transaction
}
}
} else {
if ( !$this->mTrxLevel ) {
- wfWarn( "$fname: No transaction to commit, something got out of sync." );
+ $this->queryLogger->error( "$fname: No transaction to commit, something got out of sync." );
return; // nothing to do
} elseif ( $this->mTrxAutomatic ) {
// @TODO: make this an exception at some point
$msg = "$fname: Explicit commit of implicit transaction.";
- wfLogDBError( $msg );
- wfWarn( $msg );
+ $this->queryLogger->error( $msg );
return; // wait for the main transaction set commit round
}
}
}
} else {
if ( !$this->mTrxLevel ) {
- wfWarn( "$fname: No transaction to rollback, something got out of sync." );
+ $this->queryLogger->error(
+ "$fname: No transaction to rollback, something got out of sync." );
return; // nothing to do
} elseif ( $this->getFlag( DBO_TRX ) ) {
throw new DBUnexpectedError(
* @param string $newName Name of table to be created
* @param bool $temporary Whether the new table should be temporary
* @param string $fname Calling function name
- * @throws MWException
+ * @throws RuntimeException
* @return bool True if operation was successful
*/
public function duplicateTableStructure( $oldName, $newName, $temporary = false,
*
* @param string $prefix Only show VIEWs with this prefix, eg. unit_test_
* @param string $fname Name of calling function
- * @throws MWException
+ * @throws RuntimeException
* @return array
* @since 1.22
*/
* Differentiates between a TABLE and a VIEW
*
* @param string $name Name of the database-structure to test.
- * @throws MWException
+ * @throws RuntimeException
* @return bool
* @since 1.22
*/
* generated dynamically using $filename
* @param bool|callable $inputCallback Optional function called for each
* complete line sent
- * @throws Exception|MWException
* @return bool|string
+ * @throws Exception
*/
public function sourceFile(
$filename, $lineCallback = false, $resultCallback = false, $fname = false, $inputCallback = false