documentation
[lhc/web/wiklou.git] / includes / Database.php
index 326b229..59817b2 100644 (file)
@@ -246,7 +246,7 @@ class Database {
         */
        function open( $server, $user, $password, $dbName ) {
                global $wguname;
-               
+
                # Test for missing mysql.so
                # First try to load it
                if (!@extension_loaded('mysql')) {
@@ -255,7 +255,7 @@ class Database {
 
                # 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();
@@ -302,14 +302,14 @@ class Database {
                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;
        }
@@ -343,7 +343,7 @@ class Database {
                if ( $myError ) {
                        $error = $myError;
                }
-               
+
                if ( $this->mFailFunction ) {
                        if ( !is_int( $this->mFailFunction ) ) {
                                $ff = $this->mFailFunction;
@@ -359,18 +359,7 @@ class Database {
         * 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
@@ -705,7 +694,7 @@ class Database {
                $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 );
        }
 
        /**
@@ -870,7 +859,7 @@ class Database {
         */
        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;
                }
@@ -1042,13 +1031,13 @@ class Database {
                        $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
@@ -1112,25 +1101,6 @@ class Database {
                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
         *
@@ -1226,7 +1196,7 @@ class Database {
                $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
@@ -1357,14 +1327,19 @@ class Database {
         * $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 != '*' ) {
@@ -1653,8 +1628,8 @@ class Database {
        /**
         * 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;
@@ -1672,6 +1647,87 @@ class Database {
        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] );
+       }
+
 }
 
 /**
@@ -1735,8 +1791,10 @@ class ResultWrapper {
        function seek( $row ) {
                $this->db->dataSeek( $this->result, $row );
        }
+
 }
 
+
 #------------------------------------------------------------------------------
 # Global functions
 #------------------------------------------------------------------------------
@@ -1800,7 +1858,7 @@ border=\"0\" ALT=\"Google\"></A>
        }
 
        wfLogDBError( "Connection error: $error\n" );
-       
+
        $text = str_replace( '$1', $error, $noconnect );
        $text .= wfGetSiteNotice();