*/
function open( $server, $user, $password, $dbName ) {
global $wguname;
-
+
# Test for missing mysql.so
# First try to load it
if (!@extension_loaded('mysql')) {
# Otherwise we get a suppressed fatal error, which is very hard to track down
if ( !function_exists( 'mysql_connect' ) ) {
- die( "MySQL functions missing, have you compiled PHP with the --with-mysql option?\n" );
+ wfDie( "MySQL functions missing, have you compiled PHP with the --with-mysql option?\n" );
}
$this->close();
if ( !$success ) {
$this->reportConnectionError();
}
-
+
global $wgDBmysql5;
if( $wgDBmysql5 ) {
// Tell the server we're communicating with it in UTF-8.
// This may engage various charset conversions.
$this->query( 'SET NAMES utf8' );
}
-
+
$this->mOpened = $success;
return $success;
}
if ( $myError ) {
$error = $myError;
}
-
+
if ( $this->mFailFunction ) {
if ( !is_int( $this->mFailFunction ) ) {
$ff = $this->mFailFunction;
* If errors are explicitly ignored, returns success
*/
function query( $sql, $fname = '', $tempIgnore = false ) {
- global $wgProfiling, $wgCommandLineMode;
-
- if ( wfReadOnly() ) {
- # This is a quick check for the most common kinds of write query used
- # in MediaWiki, to provide extra safety in addition to UI-level checks.
- # It is not intended to prevent every conceivable write query, or even
- # to handle such queries gracefully.
- if ( preg_match( '/^(?:update|insert|replace|delete)/i', $sql ) ) {
- wfDebug( "Write query from $fname blocked\n" );
- return false;
- }
- }
+ global $wgProfiling;
if ( $wgProfiling ) {
# generalizeSQL will probably cut down the query to reasonable
$table = $this->tableName( $table );
$sql = "UPDATE $table SET $var = '" .
$this->strencode( $value ) . "' WHERE ($cond)";
- return (bool)$this->query( $sql, DB_MASTER, $fname );
+ return (bool)$this->query( $sql, $fname );
}
/**
*/
function fieldExists( $table, $field, $fname = 'Database::fieldExists' ) {
$table = $this->tableName( $table );
- $res = $this->query( 'DESCRIBE '.$table, DB_SLAVE, $fname );
+ $res = $this->query( 'DESCRIBE '.$table, $fname );
if ( !$res ) {
return NULL;
}
$opts[] = 'IGNORE';
return implode(' ', $opts);
}
-
+
/**
* UPDATE wrapper, takes a condition array and a SET array
*
* @param string $table The table to UPDATE
* @param array $values An array of values to SET
- * @param array $conds An array of conditions (WHERE)
+ * @param array $conds An array of conditions (WHERE). Use '*' to update all rows.
* @param string $fname The Class::Function calling this function
* (for the log)
* @param array $options An array of UPDATE options, can be one or
return mysql_select_db( $db, $this->mConn );
}
- /**
- * Starts a timer which will kill the DB thread after $timeout seconds
- */
- function startTimer( $timeout ) {
- global $IP;
- if( function_exists( 'mysql_thread_id' ) ) {
- # This will kill the query if it's still running after $timeout seconds.
- $tid = mysql_thread_id( $this->mConn );
- exec( "php $IP/includes/killthread.php $timeout $tid &>/dev/null &" );
- }
- }
-
- /**
- * Stop a timer started by startTimer()
- * Currently unimplemented.
- *
- */
- function stopTimer() { }
-
/**
* Format a table name ready for use in constructing an SQL query
*
$s=str_replace(array('%','_'),array('\%','\_'),$s);
return $s;
}
-
+
/**
* Returns an appropriately quoted sequence value for inserting a new row.
* MySQL has autoincrement fields, so this is just NULL. But the PostgreSQL
* $conds may be "*" to copy the whole table
* srcTable may be an array of tables.
*/
- function insertSelect( $destTable, $srcTable, $varMap, $conds, $fname = 'Database::insertSelect' ) {
+ function insertSelect( $destTable, $srcTable, $varMap, $conds, $fname = 'Database::insertSelect',
+ $options = array() )
+ {
$destTable = $this->tableName( $destTable );
- if( is_array( $srcTable ) ) {
- $srcTable = implode( ',', array_map( array( &$this, 'tableName' ), $srcTable ) );
+ if ( is_array( $options ) ) {
+ $options = implode( ' ', $options );
+ }
+ if( is_array( $srcTable ) ) {
+ $srcTable = implode( ',', array_map( array( &$this, 'tableName' ), $srcTable ) );
} else {
$srcTable = $this->tableName( $srcTable );
}
- $sql = "INSERT INTO $destTable (" . implode( ',', array_keys( $varMap ) ) . ')' .
+ $sql = "INSERT $options INTO $destTable (" . implode( ',', array_keys( $varMap ) ) . ')' .
' SELECT ' . implode( ',', $varMap ) .
" FROM $srcTable";
if ( $conds != '*' ) {
/**
* Get status information from SHOW STATUS in an associative array
*/
- function getStatus() {
- $res = $this->query( 'SHOW STATUS' );
+ function getStatus($which="%") {
+ $res = $this->query( "SHOW STATUS LIKE '{$which}'" );
$status = array();
while ( $row = $this->fetchObject( $res ) ) {
$status[$row->Variable_name] = $row->Value;
function encodeBlob($b) {
return $b;
}
+
+ /**
+ * Read and execute SQL commands from a file.
+ * Returns true on success, error string on failure
+ */
+ function sourceFile( $filename ) {
+ $fp = fopen( $filename, 'r' );
+ if ( false === $fp ) {
+ return "Could not open \"{$fname}\".\n";
+ }
+
+ $cmd = "";
+ $done = false;
+
+ while ( ! feof( $fp ) ) {
+ $line = trim( fgets( $fp, 1024 ) );
+ $sl = strlen( $line ) - 1;
+
+ if ( $sl < 0 ) { continue; }
+ if ( '-' == $line{0} && '-' == $line{1} ) { continue; }
+
+ if ( ';' == $line{$sl} && ($sl < 2 || ';' != $line{$sl - 1})) {
+ $done = true;
+ $line = substr( $line, 0, $sl );
+ }
+
+ if ( '' != $cmd ) { $cmd .= ' '; }
+ $cmd .= "$line\n";
+
+ if ( $done ) {
+ $cmd = str_replace(';;', ";", $cmd);
+ $cmd = $this->replaceVars( $cmd );
+ $res = $this->query( $cmd, 'dbsource', true );
+
+ if ( false === $res ) {
+ $err = $this->lastError();
+ return "Query \"{$cmd}\" failed with error code \"$err\".\n";
+ }
+
+ $cmd = '';
+ $done = false;
+ }
+ }
+ fclose( $fp );
+ return true;
+ }
+
+ /**
+ * Replace variables in sourced SQL
+ */
+ function replaceVars( $ins ) {
+ $varnames = array(
+ 'wgDBserver', 'wgDBname', 'wgDBintlname', 'wgDBuser',
+ 'wgDBpassword', 'wgDBsqluser', 'wgDBsqlpassword',
+ 'wgDBadminuser', 'wgDBadminpassword',
+ );
+
+ // Ordinary variables
+ foreach ( $varnames as $var ) {
+ if( isset( $GLOBALS[$var] ) ) {
+ $val = addslashes( $GLOBALS[$var] );
+ $ins = str_replace( '{$' . $var . '}', $val, $ins );
+ $ins = str_replace( '/*$' . $var . '*/`', '`' . $val, $ins );
+ $ins = str_replace( '/*$' . $var . '*/', $val, $ins );
+ }
+ }
+
+ // Table prefixes
+ $ins = preg_replace_callback( '/\/\*(?:\$wgDBprefix|_)\*\/([a-z_]*)/',
+ array( &$this, 'tableNameCallback' ), $ins );
+ return $ins;
+ }
+
+ /**
+ * Table name callback
+ * @access private
+ */
+ function tableNameCallback( $matches ) {
+ return $this->tableName( $matches[1] );
+ }
+
}
/**
function seek( $row ) {
$this->db->dataSeek( $this->result, $row );
}
+
}
+
#------------------------------------------------------------------------------
# Global functions
#------------------------------------------------------------------------------
}
wfLogDBError( "Connection error: $error\n" );
-
+
$text = str_replace( '$1', $error, $noconnect );
$text .= wfGetSiteNotice();