<?php
-include_once( "FulltextStoplist.php" );
-include_once( "CacheManager.php" );
+require_once( "FulltextStoplist.php" );
+require_once( "CacheManager.php" );
define( "DB_READ", -1 );
define( "DB_WRITE", -2 );
define( "LIST_COMMA", 0 );
define( "LIST_AND", 1 );
+define( "LIST_SET", 2 );
class Database {
function Database()
{
global $wgOut;
- $this->mOut = $wgOut;
+ # Can't get a reference if it hasn't been set yet
+ if ( !isset( $wgOut ) ) {
+ $wgOut = NULL;
+ }
+ $this->mOut =& $wgOut;
}
$db->mFailFunction = $failFunction;
$db->mIgnoreErrors = $ignoreErrors;
$db->mDebug = $debug;
+ $db->mBufferResults = $bufferResults;
$db->open( $server, $user, $password, $dbName );
return $db;
}
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;
return $ret;
}
- function freeResult( $res ) { mysql_free_result( $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
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 lastErrno() { return mysql_errno(); }
+ function lastError() { return mysql_error(); }
function affectedRows() { return mysql_affected_rows( $this->mConn ); }
# Simple UPDATE wrapper
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" )
{
$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;
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;
}
- $nTables = $this->numRows( $res );
- for( $i = 0; $i < $nTables; $i++ ) {
- if( mysql_tablename( $res, $i ) == $table ) return true;
- }
- return false;
}
function fieldInfo( $table, $field )
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 = "";
} 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;
} else {
if($title) {
$t = Title::newFromURL( $title );
- } elseif ($_REQUEST['search']) {
+ } elseif (@$_REQUEST['search']) {
$search = $_REQUEST['search'];
echo wfMsgNoDB( "searchdisabled" );
echo wfMsgNoDB( "googlesearch", htmlspecialchars( $search ), $wgInputEncoding );
$cache = new CacheManager( $t );
if( $cache->isFileCached() ) {
- $msg = "<p style='color: red'><b>$msg<br>\n" .
+ $msg = "<p style='color: red'><b>$msg<br />\n" .
wfMsgNoDB( "cachederror" ) . "</b></p>\n";
$tag = "<div id='article'>";