X-Git-Url: http://git.cyclocoop.org/?a=blobdiff_plain;f=includes%2FDatabase.php;h=64cfbf11fa614abd73fa1031fb43fba91f2cd2ed;hb=d6425d7fc9dd1ba118654b91af59e589637301ce;hp=60069171affca518372caf9fd50ab07780fe948d;hpb=4c7ff09b2898c29fea868bc27c6f225577bc87cf;p=lhc%2Fweb%2Fwiklou.git diff --git a/includes/Database.php b/includes/Database.php index 60069171af..64cfbf11fa 100644 --- a/includes/Database.php +++ b/includes/Database.php @@ -1,4 +1,4 @@ -mOut = $wgOut; + # Can't get a reference if it hasn't been set yet + if ( !isset( $wgOut ) ) { + $wgOut = NULL; + } + $this->mOut =& $wgOut; } @@ -100,14 +105,14 @@ class Database { wfDebug( "Error selecting database \"$dbName\": " . $this->lastError() . "\n" ); } } else { - wfDebug( "DB connect error: " . $this->lastError() . "\n" ); + wfDebug( "DB connection error\n" ); wfDebug( "Server: $server, User: $user, Password: " . substr( $password, 0, 3 ) . "...\n" ); $success = false; } } else { - # Delay USE - $success = true; + # Delay USE query + $success = !!$this->mConn; } if ( !$success ) { @@ -173,7 +178,8 @@ class Database { } else { wfDebug("SQL ERROR: " . mysql_error( $this->mConn ) . "\n"); if ( $this->mOut ) { - $this->mOut->databaseError( $fname ); // calls wfAbruptExit() + // this calls wfAbruptExit() + $this->mOut->databaseError( $fname, $this ); } } } @@ -184,15 +190,32 @@ class Database { return $ret; } - function freeResult( $res ) { mysql_free_result( $res ); } - function fetchObject( $res ) { return mysql_fetch_object( $res ); } - function numRows( $res ) { return mysql_num_rows( $res ); } + function freeResult( $res ) { + if ( !@mysql_free_result( $res ) ) { + wfDebugDieBacktrace( "Unable to free MySQL result\n" ); + } + } + function fetchObject( $res ) { + @$row = mysql_fetch_object( $res ); + # FIXME: HACK HACK HACK HACK debug + if( mysql_errno() ) { + wfDebugDieBacktrace( "SQL error: " . htmlspecialchars( mysql_error() ) ); + } + return $row; + } + function numRows( $res ) { + @$n = mysql_num_rows( $res ); + if( mysql_errno() ) { + wfDebugDieBacktrace( "SQL error: " . htmlspecialchars( mysql_error() ) ); + } + return $n; + } function numFields( $res ) { return mysql_num_fields( $res ); } function fieldName( $res, $n ) { return mysql_field_name( $res, $n ); } function insertId() { return mysql_insert_id( $this->mConn ); } function dataSeek( $res, $row ) { return mysql_data_seek( $res, $row ); } function lastErrno() { return mysql_errno( $this->mConn ); } - function lastError() { return mysql_error( $this->mConn ); } + function lastError() { return mysql_error( $this->mConn ); } function affectedRows() { return mysql_affected_rows( $this->mConn ); } # Simple UPDATE wrapper @@ -205,7 +228,7 @@ class Database { return !!$this->query( $sql, DB_WRITE, $fname ); } - # Simple SELECT wrapper + # Simple SELECT wrapper, returns a single field, input must be encoded # Usually aborts on failure # If errors are explicitly ignored, returns FALSE on failure function get( $table, $var, $cond, $fname = "Database::get" ) @@ -231,12 +254,14 @@ class Database { { $vars = implode( ",", $vars ); $where = Database::makeList( $conds, LIST_AND ); - $sql = "SELECT $vars FROM $table WHERE $where"; + $sql = "SELECT $vars FROM $table WHERE $where LIMIT 1"; $res = $this->query( $sql, $fname ); if ( $res === false || !$this->numRows( $res ) ) { return false; } - return $this->fetchObject( $res ); + $obj = $this->fetchObject( $res ); + $this->freeResult( $res ); + return $obj; } # Removes most variables from an SQL query and replaces them with X or N for numbers. @@ -307,15 +332,15 @@ class Database { function tableExists( $table ) { - $res = mysql_list_tables( $this->mDBname ); - if( !$res ) { - echo "** " . $this->lastError() . "\n"; + $old = $this->mIgnoreErrors; + $res = $this->query( "SELECT 1 FROM $table LIMIT 1" ); + $this->mIgnoreErrors = $old; + if( $res ) { + $this->freeResult( $res ); + return true; + } else { return false; } - for( $i = $this->numRows( $res ) - 1; $i--; $i > 0 ) { - if( mysql_tablename( $res, $i ) == $table ) return true; - } - return false; } function fieldInfo( $table, $field ) @@ -351,10 +376,19 @@ class Database { return !!$this->query( $sql, $fname ); } + # A cross between insertArray and getArray, takes a condition array and a SET array + function updateArray( $table, $values, $conds, $fname = "Database::updateArray" ) + { + $sql = "UPDATE $table SET " . $this->makeList( $values, LIST_SET ); + $sql .= " WHERE " . $this->makeList( $conds, LIST_AND ); + $this->query( $sql, $fname ); + } + # Makes a wfStrencoded list from an array - # $mode: LIST_COMMA - comma separated + # $mode: LIST_COMMA - comma separated, no field names # LIST_AND - ANDed WHERE clause (without the WHERE) - /* static */ function makeList( $a, $mode = LIST_COMMA) + # LIST_SET - comma separated with field names, like a SET clause + /* static */ function makeList( $a, $mode = LIST_COMMA ) { $first = true; $list = ""; @@ -368,10 +402,10 @@ class Database { } else { $first = false; } - if ( $mode == LIST_AND ) { + if ( $mode == LIST_AND || $mode == LIST_SET ) { $list .= "$field="; } - if ( is_string( $value ) ) { + if ( !is_numeric( $value ) ) { $list .= "'" . wfStrencode( $value ) . "'"; } else { $list .= $value; @@ -382,9 +416,22 @@ class Database { function selectDB( $db ) { - $this->mDatabase = $db; + $this->mDBname = $db; mysql_select_db( $db, $this->mConn ); } + + function startTimer( $timeout ) + { + global $IP; + + $tid = mysql_thread_id( $this->mConn ); + exec( "php $IP/killthread.php $timeout $tid &>/dev/null &" ); + } + + function stopTimer() + { + } + } #------------------------------------------------------------------------------ @@ -407,9 +454,10 @@ function wfEmergencyAbort( &$conn ) { } else { if($title) { $t = Title::newFromURL( $title ); - } elseif ($_REQUEST['search']) { + } elseif (@$_REQUEST['search']) { $search = $_REQUEST['search']; - echo wfMsgNoDB( "searchdisabled", htmlspecialchars( $search ), $wgInputEncoding ); + echo wfMsgNoDB( "searchdisabled" ); + echo wfMsgNoDB( "googlesearch", htmlspecialchars( $search ), $wgInputEncoding ); wfAbruptExit(); } else { $t = Title::newFromText( wfMsgNoDB( "mainpage" ) );