Fixed a E_DEPRECATED
[lhc/web/wiklou.git] / includes / DatabaseFunctions.php
index 6244ea7..2df5611 100644 (file)
 <?php
-# $Id$
-
-# Backwards compatibility wrapper for Database.php
-
-# I imagine this file will eventually become a backwards
-# compatibility wrapper around a load balancer object, and
-# the load balancer will finally call Database, which will
-# represent a single connection
-
-# NB: This file follows a connect on demand scheme. Do 
-# not access the $wgDatabase variable directly unless
-# you intend to set it. Use wfGetDB().
-
-$wgIsMySQL=false;
-$wgIsPg=false;
-
-if ($wgDBtype=="mysql") {
-    require_once( "Database.php" );
-    $wgIsMySQL=true;
-} elseif ($wgDBtype=="pgsql") {
-    require_once( "DatabasePostgreSQL.php" );
-    $wgIsPg=true;
-} 
-
-# Query the database
-# $db: DB_READ  = -1    read from slave (or only server)
-#      DB_WRITE = -2    write to master (or only server)
-#      0,1,2,...        query a database with a specific index
-# Replication is not actually implemented just yet
-# Usually aborts on failure
-# If errors are explicitly ignored, returns success
-function wfQuery( $sql, $db, $fname = "" )
-{
-       global $wgDatabase, $wgDBserver, $wgDBuser, $wgDBpassword, $wgDBname, 
-               $wgDebugDumpSql, $wgBufferSQLResults, $wgIgnoreSQLErrors;
-       
+/**
+ * Legacy database functions, for compatibility with pre-1.3 code
+ * NOTE: this file is no longer loaded by default.
+ * @file
+ * @ingroup Database
+ */
+
+/**
+ * Usually aborts on failure
+ * If errors are explicitly ignored, returns success
+ * @param $sql String: SQL query
+ * @param $db Mixed: database handler
+ * @param $fname String: name of the php function calling
+ */
+function wfQuery( $sql, $db, $fname = '' ) {
        if ( !is_numeric( $db ) ) {
                # Someone has tried to call this the old way
-               $wgOut->fatalError( wfMsgNoDB( "wrong_wfQuery_params", $db, $sql ) );
+               throw new FatalError( wfMsgNoDB( 'wrong_wfQuery_params', $db, $sql ) );
+       }
+       $c = wfGetDB( $db );
+       if ( $c !== false ) {
+               return $c->query( $sql, $fname );
+       } else {
+               return false;
        }
+}
 
-       $db =&  wfGetDB();
-       return $db->query( $sql, $fname );
+/**
+ *
+ * @param $sql String: SQL query
+ * @param $dbi
+ * @param $fname String: name of the php function calling
+ * @return Array: first row from the database
+ */
+function wfSingleQuery( $sql, $dbi, $fname = '' ) {
+       $db = wfGetDB( $dbi );
+       $res = $db->query($sql, $fname );
+       $row = $db->fetchRow( $res );
+       $ret = $row[0];
+       $db->freeResult( $res );
+       return $ret;
 }
 
-# Connect on demand
-function &wfGetDB()
-{
-       global $wgDatabase, $wgDBserver, $wgDBuser, $wgDBpassword, $wgDBname, 
-               $wgDebugDumpSql, $wgBufferSQLResults, $wgIgnoreSQLErrors;
-       if ( !$wgDatabase ) {
-               $wgDatabase = Database::newFromParams( $wgDBserver, $wgDBuser, $wgDBpassword, 
-                       $wgDBname, false, $wgDebugDumpSql, $wgBufferSQLResults, $wgIgnoreSQLErrors );
+/**
+ * Turns on (false) or off (true) the automatic generation and sending
+ * of a "we're sorry, but there has been a database error" page on
+ * database errors. Default is on (false). When turned off, the
+ * code should use wfLastErrno() and wfLastError() to handle the
+ * situation as appropriate.
+ *
+ * @param $newstate
+ * @param $dbi
+ * @return Returns the previous state.
+ */
+function wfIgnoreSQLErrors( $newstate, $dbi = DB_LAST ) {
+       $db = wfGetDB( $dbi );
+       if ( $db !== false ) {
+               return $db->ignoreErrors( $newstate );
+       } else {
+               return null;
        }
-       return $wgDatabase;
 }
-       
-# Turns buffering of SQL result sets on (true) or off (false). Default is
-# "on" and it should not be changed without good reasons. 
-# Returns the previous state.
 
-function wfBufferSQLResults( $newstate )
+/**#@+
+ * @param $res Database result handler
+ * @param $dbi
+*/
+
+/**
+ * Free a database result
+ * @return Bool: whether result is sucessful or not.
+ */
+function wfFreeResult( $res, $dbi = DB_LAST )
 {
-       $db =& wfGetDB();
-       return $db->setBufferResults( $newstate );
+       $db = wfGetDB( $dbi );
+       if ( $db !== false ) {
+               $db->freeResult( $res );
+               return true;
+       } else {
+               return false;
+       }
 }
 
-# Turns on (false) or off (true) the automatic generation and sending
-# of a "we're sorry, but there has been a database error" page on
-# database errors. Default is on (false). When turned off, the
-# code should use wfLastErrno() and wfLastError() to handle the
-# situation as appropriate.
-# Returns the previous state.
-
-function wfIgnoreSQLErrors( $newstate )
-{
-       $db =& wfGetDB();
-       return $db->setIgnoreErrors( $newstate );
+/**
+ * Get an object from a database result
+ * @return object|false object we requested
+ */
+function wfFetchObject( $res, $dbi = DB_LAST ) {
+       $db = wfGetDB( $dbi );
+       if ( $db !== false ) {
+               return $db->fetchObject( $res, $dbi = DB_LAST );
+       } else {
+               return false;
+       }
 }
 
-function wfFreeResult( $res ) 
-{ 
-       $db =& wfGetDB();
-       $db->freeResult( $res ); 
+/**
+ * Get a row from a database result
+ * @return object|false row we requested
+ */
+function wfFetchRow( $res, $dbi = DB_LAST ) {
+       $db = wfGetDB( $dbi );
+       if ( $db !== false ) {
+               return $db->fetchRow ( $res, $dbi = DB_LAST );
+       } else {
+               return false;
+       }
 }
 
-function wfFetchObject( $res ) 
-{ 
-       $db =& wfGetDB();
-       return $db->fetchObject( $res ); 
+/**
+ * Get a number of rows from a database result
+ * @return integer|false number of rows
+ */
+function wfNumRows( $res, $dbi = DB_LAST ) {
+       $db = wfGetDB( $dbi );
+       if ( $db !== false ) {
+               return $db->numRows( $res, $dbi = DB_LAST );
+       } else {
+               return false;
+       }
 }
 
-function wfNumRows( $res ) 
-{ 
-       $db =& wfGetDB();
-       return $db->numRows( $res ); 
+/**
+ * Get the number of fields from a database result
+ * @return integer|false number of fields
+ */
+function wfNumFields( $res, $dbi = DB_LAST ) {
+       $db = wfGetDB( $dbi );
+       if ( $db !== false ) {
+               return $db->numFields( $res );
+       } else {
+               return false;
+       }
 }
 
-function wfNumFields( $res ) 
-{ 
-       $db =& wfGetDB();
-       return $db->numFields( $res ); 
+/**
+ * Return name of a field in a result
+ * @param $res Mixed: Ressource link see Database::fieldName()
+ * @param $n Integer: id of the field
+ * @param $dbi Default DB_LAST
+ * @return string|false name of field
+ */
+function wfFieldName( $res, $n, $dbi = DB_LAST )
+{
+       $db = wfGetDB( $dbi );
+       if ( $db !== false ) {
+               return $db->fieldName( $res, $n, $dbi = DB_LAST );
+       } else {
+               return false;
+       }
 }
+/**#@-*/
 
-function wfFieldName( $res, $n ) 
-{ 
-       $db =& wfGetDB();
-       return $db->fieldName( $res, $n ); 
+/**
+ * @todo document function
+ * @see Database::insertId()
+ */
+function wfInsertId( $dbi = DB_LAST ) {
+       $db = wfGetDB( $dbi );
+       if ( $db !== false ) {
+               return $db->insertId();
+       } else {
+               return false;
+       }
 }
 
-function wfInsertId() 
-{ 
-       $db =& wfGetDB();
-       return $db->insertId(); 
+/**
+ * @todo document function
+ * @see Database::dataSeek()
+ */
+function wfDataSeek( $res, $row, $dbi = DB_LAST ) {
+       $db = wfGetDB( $dbi );
+       if ( $db !== false ) {
+               return $db->dataSeek( $res, $row );
+       } else {
+               return false;
+       }
 }
-function wfDataSeek( $res, $row ) 
-{ 
-       $db =& wfGetDB();
-       return $db->dataSeek( $res, $row ); 
+
+/**
+ * Get the last error number
+ * @see Database::lastErrno()
+ */
+function wfLastErrno( $dbi = DB_LAST ) {
+       $db = wfGetDB( $dbi );
+       if ( $db !== false ) {
+               return $db->lastErrno();
+       } else {
+               return false;
+       }
 }
 
-function wfLastErrno()  
-{ 
-       $db =& wfGetDB();
-       return $db->lastErrno(); 
+/**
+ * Get the last error
+ * @see Database::lastError()
+ */
+function wfLastError( $dbi = DB_LAST ) {
+       $db = wfGetDB( $dbi );
+       if ( $db !== false ) {
+               return $db->lastError();
+       } else {
+               return false;
+       }
 }
 
-function wfLastError()  
-{ 
-       $db =& wfGetDB();
-       return $db->lastError(); 
+/**
+ * Get the number of affected rows
+ * @see Database::affectedRows()
+ */
+function wfAffectedRows( $dbi = DB_LAST ) {
+       $db = wfGetDB( $dbi );
+       if ( $db !== false ) {
+               return $db->affectedRows();
+       } else {
+               return false;
+       }
 }
 
-function wfAffectedRows()
-{ 
-       $db =& wfGetDB();
-       return $db->affectedRows(); 
+/**
+ * Get the last query ran
+ * @see Database::lastQuery
+ */
+function wfLastDBquery( $dbi = DB_LAST ) {
+       $db = wfGetDB( $dbi );
+       if ( $db !== false ) {
+               return $db->lastQuery();
+       } else {
+               return false;
+       }
 }
 
-function wfLastDBquery()
+/**
+ * @see Database::Set()
+ * @todo document function
+ * @param $table
+ * @param $var
+ * @param $value
+ * @param $cond
+ * @param $dbi Default DB_MASTER
+ */
+function wfSetSQL( $table, $var, $value, $cond, $dbi = DB_MASTER )
 {
-       $db =& wfGetDB();
-       return $db->lastQuery();
+       $db = wfGetDB( $dbi );
+       if ( $db !== false ) {
+               return $db->set( $table, $var, $value, $cond );
+       } else {
+               return false;
+       }
 }
 
-function wfSetSQL( $table, $var, $value, $cond )
+
+/**
+ * Simple select wrapper, return one field
+ * @see Database::selectField()
+ * @param $table
+ * @param $var
+ * @param $cond Default ''
+ * @param $dbi Default DB_LAST
+ */
+function wfGetSQL( $table, $var, $cond='', $dbi = DB_LAST )
 {
-       $db =& wfGetDB();
-       return $db->set( $table, $var, $value, $cond );
+       $db = wfGetDB( $dbi );
+       if ( $db !== false ) {
+               return $db->selectField( $table, $var, $cond );
+       } else {
+               return false;
+       }
 }
 
-function wfGetSQL( $table, $var, $cond )
-{
-       $db =& wfGetDB();
-       return $db->get( $table, $var, $cond );
+/**
+ * Does a given field exist on the specified table?
+ * @see Database::fieldExists()
+ * @param $table
+ * @param $field
+ * @param $dbi Default DB_LAST
+ * @return Result of Database::fieldExists() or false.
+ */
+function wfFieldExists( $table, $field, $dbi = DB_LAST ) {
+       $db = wfGetDB( $dbi );
+       if ( $db !== false ) {
+               return $db->fieldExists( $table, $field );
+       } else {
+               return false;
+       }
 }
 
-function wfFieldExists( $table, $field )
-{
-       $db =& wfGetDB();
-       return $db->fieldExists( $table, $field );
+/**
+ * Does the requested index exist on the specified table?
+ * @see Database::indexExists()
+ * @param $table String
+ * @param $index
+ * @param $dbi Default DB_LAST
+ * @return Result of Database::indexExists() or false.
+ */
+function wfIndexExists( $table, $index, $dbi = DB_LAST ) {
+       $db = wfGetDB( $dbi );
+       if ( $db !== false ) {
+               return $db->indexExists( $table, $index );
+       } else {
+               return false;
+       }
 }
 
-function wfIndexExists( $table, $index ) 
-{
-       $db =& wfGetDB();
-       return $db->indexExists( $table, $index );
+/**
+ * @see Database::insert()
+ * @todo document function
+ * @param $table String
+ * @param $array Array
+ * @param $fname String, default 'wfInsertArray'.
+ * @param $dbi Default DB_MASTER
+ * @return result of Database::insert() or false.
+ */
+function wfInsertArray( $table, $array, $fname = 'wfInsertArray', $dbi = DB_MASTER ) {
+       $db = wfGetDB( $dbi );
+       if ( $db !== false ) {
+               return $db->insert( $table, $array, $fname );
+       } else {
+               return false;
+       }
 }
 
-function wfInsertArray( $table, $array ) 
-{
-       $db =& wfGetDB();
-       return $db->insertArray( $table, $array );
+/**
+ * @see Database::getArray()
+ * @todo document function
+ * @param $table String
+ * @param $vars
+ * @param $conds
+ * @param $fname String, default 'wfGetArray'.
+ * @param $dbi Default DB_LAST
+ * @return result of Database::getArray() or false.
+ */
+function wfGetArray( $table, $vars, $conds, $fname = 'wfGetArray', $dbi = DB_LAST ) {
+       $db = wfGetDB( $dbi );
+       if ( $db !== false ) {
+               return $db->getArray( $table, $vars, $conds, $fname );
+       } else {
+               return false;
+       }
 }
 
-function wfGetArray( $table, $vars, $conds, $fname = "wfGetArray" )
-{
-       $db =& wfGetDB();
-       return $db->getArray( $table, $vars, $conds, $fname );
+/**
+ * @see Database::update()
+ * @param $table String
+ * @param $values
+ * @param $conds
+ * @param $fname String, default 'wfUpdateArray'
+ * @param $dbi Default DB_MASTER
+ * @return Result of Database::update()) or false;
+ * @todo document function
+ */
+function wfUpdateArray( $table, $values, $conds, $fname = 'wfUpdateArray', $dbi = DB_MASTER ) {
+       $db = wfGetDB( $dbi );
+       if ( $db !== false ) {
+               $db->update( $table, $values, $conds, $fname );
+               return true;
+       } else {
+               return false;
+       }
 }
 
-function wfUpdateArray( $table, $values, $conds, $fname = "wfUpdateArray" )
-{
-       $db =& wfGetDB();
-       $db->updateArray( $table, $values, $conds, $fname );
+/**
+ * Get fully usable table name
+ * @see Database::tableName()
+ */
+function wfTableName( $name, $dbi = DB_LAST ) {
+       $db = wfGetDB( $dbi );
+       if ( $db !== false ) {
+               return $db->tableName( $name );
+       } else {
+               return false;
+       }
+}
+
+/**
+ * @todo document function
+ * @see Database::strencode()
+ */
+function wfStrencode( $s, $dbi = DB_LAST ) {
+       $db = wfGetDB( $dbi );
+       if ( $db !== false ) {
+               return $db->strencode( $s );
+       } else {
+               return false;
+       }
 }
 
-?>
+/**
+ * @todo document function
+ * @see Database::nextSequenceValue()
+ */
+function wfNextSequenceValue( $seqName, $dbi = DB_MASTER ) {
+       $db = wfGetDB( $dbi );
+       if ( $db !== false ) {
+               return $db->nextSequenceValue( $seqName );
+       } else {
+               return false;
+       }
+}
+
+/**
+ * @todo document function
+ * @see Database::useIndexClause()
+ */
+function wfUseIndexClause( $index, $dbi = DB_SLAVE ) {
+       $db = wfGetDB( $dbi );
+       if ( $db !== false ) {
+               return $db->useIndexClause( $index );
+       } else {
+               return false;
+       }
+}