* Fix nohistory message on empty page history
[lhc/web/wiklou.git] / includes / DatabaseFunctions.php
index b3208e1..772ca43 100644 (file)
-<?
+<?php
+/**
+ * Backwards compatibility wrapper for Database.php
+ * 
+ * Note: $wgDatabase has ceased to exist. Destroy all references.
+ *
+ * @package MediaWiki
+ */
 
-# Backwards compatibility wrapper for Database.php
-
-# NB: This file follows a connect on demand scheme. Do 
-# not access the $wgDatabase variable directly, use wfGetDB()
-
-# 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
-
-include_once( "Database.php" );
-
-function wfQuery( $sql, $db, $fname = "" )
-{
-       global $wgDatabase, $wgDBserver, $wgDBuser, $wgDBpassword, $wgDBname, 
-               $wgDebugDumpSql, $wgBufferSQLResults, $wgIgnoreSQLErrors;
-       
+/**
+ * Usually aborts on failure
+ * If errors are explicitly ignored, returns success
+ * @param string $sql SQL query
+ * @param mixed $db database handler
+ * @param string $fname name of the php function calling
+ */
+function wfQuery( $sql, $db, $fname = '' ) {
+       global $wgOut;
        if ( !is_numeric( $db ) ) {
                # Someone has tried to call this the old way
-               $wgOut->fatalError( wfMsgNoDB( "wrong_wfQuery_params", $db, $sql ) );
+               $wgOut->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, $db, $fname );
+/**
+ *
+ * @param string $sql SQL query
+ * @param $dbi
+ * @param string $fname 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 );
-       }
-       return $wgDatabase;
+/*
+ * @todo document function
+ */
+function &wfGetDB( $db = DB_LAST, $groups = array() ) {
+       global $wgLoadBalancer;
+       return $wgLoadBalancer->getConnection( $db, true, $groups );
 }
        
-# 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 )
-{
-       $db = wfGetDB();
-       return $db->setBufferResults( $newstate );
+/**
+ * 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;
+       }
 }
 
-# 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.
+/**#@+
+ * @param $res database result handler
+ * @param $dbi
+*/
 
-function wfIgnoreSQLErrors( $newstate )
-{
-       $db = wfGetDB();
-       return $db->setIgnoreErrors( $newstate );
+/**
+ * Free a database result
+ * @return bool whether result is sucessful or not
+ */
+function wfFreeResult( $res, $dbi = DB_LAST ) 
+{ 
+       $db =& wfGetDB( $dbi );
+       if ( $db !== false ) {
+               $db->freeResult( $res ); 
+               return true;
+       } else {        
+               return false;
+       }
 }
 
-function wfFreeResult( $res ) 
-{ 
-       $db = wfGetDB();
-       $db->freeResult( $res ); 
+/**
+ * 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 wfFetchObject( $res ) 
-{ 
-       $db = wfGetDB();
-       return $db->fetchObject( $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 wfNumRows( $res ) 
-{ 
-       $db = wfGetDB();
-       return $db->numRows( $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 wfNumFields( $res ) 
-{ 
-       $db = wfGetDB();
-       return $db->numFields( $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 wfFieldName( $res, $n ) 
+/**
+ * Return name of a field in a result
+ * @param integer $n id of the field
+ * @return string|false name of field
+ */
+function wfFieldName( $res, $n, $dbi = DB_LAST ) 
 { 
-       $db = wfGetDB();
-       return $db->fieldName( $res, $n ); 
+       $db =& wfGetDB( $dbi );
+       if ( $db !== false ) {
+               return $db->fieldName( $res, $n, $dbi = DB_LAST ); 
+       } else {        
+               return false;
+       }
 }
+/**#@-*/
 
-function wfInsertId() 
-{ 
-       $db = wfGetDB();
-       return $db->insertId(); 
+/**
+ * @todo document function
+ */
+function wfInsertId( $dbi = DB_LAST ) { 
+       $db =& wfGetDB( $dbi );
+       if ( $db !== false ) {
+               return $db->insertId(); 
+       } else {        
+               return false;
+       }
 }
-function wfDataSeek( $res, $row ) 
-{ 
-       $db = wfGetDB();
-       return $db->dataSeek( $res, $row ); 
+
+/**
+ * @todo document function
+ */
+function wfDataSeek( $res, $row, $dbi = DB_LAST ) { 
+       $db =& wfGetDB( $dbi );
+       if ( $db !== false ) {
+               return $db->dataSeek( $res, $row ); 
+       } else {        
+               return false;
+       }
 }
 
-function wfLastErrno()  
-{ 
-       $db = wfGetDB();
-       return $db->lastErrno(); 
+/**
+ * @todo document function
+ */
+function wfLastErrno( $dbi = DB_LAST ) { 
+       $db =& wfGetDB( $dbi );
+       if ( $db !== false ) {
+               return $db->lastErrno(); 
+       } else {        
+               return false;
+       }
 }
 
-function wfLastError()  
-{ 
-       $db = wfGetDB();
-       return $db->lastError(); 
+/**
+ * @todo document function
+ */
+function wfLastError( $dbi = DB_LAST ) { 
+       $db =& wfGetDB( $dbi );
+       if ( $db !== false ) {
+               return $db->lastError(); 
+       } else {        
+               return false;
+       }
 }
 
-function wfAffectedRows()
-{ 
-       $db = wfGetDB();
-       return $db->affectedRows(); 
+/**
+ * @todo document function
+ */
+function wfAffectedRows( $dbi = DB_LAST ) { 
+       $db =& wfGetDB( $dbi );
+       if ( $db !== false ) {
+               return $db->affectedRows(); 
+       } else {        
+               return false;
+       }
 }
 
-function wfLastDBquery()
-{
-       $db = wfGetDB();
-       return $db->lastQuery();
+/**
+ * @todo document function
+ */
+function wfLastDBquery( $dbi = DB_LAST ) {
+       $db =& wfGetDB( $dbi );
+       if ( $db !== false ) {
+               return $db->lastQuery();
+       } else {        
+               return false;
+       }
 }
 
-function wfSetSQL( $table, $var, $value, $cond )
+/**
+ * @todo document function
+ */
+function wfSetSQL( $table, $var, $value, $cond, $dbi = DB_MASTER )
 {
-       $db = wfGetDB();
-       return $db->set( $table, $var, $value, $cond );
+       $db =& wfGetDB( $dbi );
+       if ( $db !== false ) {
+               return $db->set( $table, $var, $value, $cond );
+       } else {        
+               return false;
+       }
 }
 
-function wfGetSQL( $table, $var, $cond )
+
+/**
+ * @todo document function
+ */
+function wfGetSQL( $table, $var, $cond='', $dbi = DB_LAST )
 {
-       $db = wfGetDB();
-       return $db->get( $table, $var, $cond );
+       $db =& wfGetDB( $dbi );
+       if ( $db !== false ) {
+               return $db->selectField( $table, $var, $cond );
+       } else {        
+               return false;
+       }
 }
 
-function wfFieldExists( $table, $field )
-{
-       $db = wfGetDB();
-       return $db->fieldExists( $table, $field );
+/**
+ * @todo document function
+ */
+function wfFieldExists( $table, $field, $dbi = DB_LAST ) {
+       $db =& wfGetDB( $dbi );
+       if ( $db !== false ) {
+               return $db->fieldExists( $table, $field );
+       } else {        
+               return false;
+       }
 }
 
-function wfIndexExists( $table, $index ) 
-{
-       $db = wfGetDB();
-       return $wgDatabase->indexExists( $table, $index );
+/**
+ * @todo document function
+ */
+function wfIndexExists( $table, $index, $dbi = DB_LAST ) {
+       $db =& wfGetDB( $dbi );
+       if ( $db !== false ) {
+               return $db->indexExists( $table, $index );
+       } else {        
+               return false;
+       }
+}
+
+/**
+ * @todo document function
+ */
+function wfInsertArray( $table, $array, $fname = 'wfInsertArray', $dbi = DB_MASTER ) {
+       $db =& wfGetDB( $dbi );
+       if ( $db !== false ) {
+               return $db->insert( $table, $array, $fname );
+       } else {        
+               return false;
+       }
+}
+
+/**
+ * @todo document function
+ */
+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;
+       }
+}
+
+/**
+ * @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;
+       }
+}
+
+/**
+ * @todo document function
+ */
+function wfTableName( $name, $dbi = DB_LAST ) {
+       $db =& wfGetDB( $dbi );
+       if ( $db !== false ) {
+               return $db->tableName( $name );
+       } else {
+               return false;
+       }
 }
 
+/**
+ * @todo document function
+ */
+function wfStrencode( $s, $dbi = DB_LAST ) {
+       $db =& wfGetDB( $dbi );
+       if ( $db !== false ) {
+               return $db->strencode( $s );
+       } else {
+               return false;
+       }
+}
+
+/**
+ * @todo document function
+ */
+function wfNextSequenceValue( $seqName, $dbi = DB_MASTER ) {
+       $db =& wfGetDB( $dbi );
+       if ( $db !== false ) {
+               return $db->nextSequenceValue( $seqName );
+       } else {
+               return false;
+       }
+}
+
+/**
+ * @todo document function
+ */
+function wfUseIndexClause( $index, $dbi = DB_SLAVE ) {
+       $db =& wfGetDB( $dbi );
+       if ( $db !== false ) {
+               return $db->useIndexClause( $index );
+       } else {
+               return false;
+       }
+}
 ?>