* Fixed unclosed <p> tag
[lhc/web/wiklou.git] / includes / DatabaseFunctions.php
index ebd9b79..772ca43 100644 (file)
-<?
-include_once( "FulltextStoplist.php" );
-include_once( "CacheManager.php" );
+<?php
+/**
+ * Backwards compatibility wrapper for Database.php
+ * 
+ * Note: $wgDatabase has ceased to exist. Destroy all references.
+ *
+ * @package MediaWiki
+ */
 
-define( "DB_READ", -1 );
-define( "DB_WRITE", -2 );
-define( "DB_LAST", -3 );
+/**
+ * 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 ) );
+       }
+       $c =& wfGetDB( $db );
+       if ( $c !== false ) {
+               return $c->query( $sql, $fname );
+       } else {        
+               return false;
+       }
+}
 
-$wgLastDatabaseQuery = "";
+/**
+ *
+ * @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;
+}
 
-function wfGetDB( $altuser = "", $altpassword = "", $altserver = "", $altdb = "" )
-{
-       global $wgDBserver, $wgDBuser, $wgDBpassword;
-       global $wgDBname, $wgDBconnection, $wgEmergencyContact;
-       
-       $noconn = wfMsgNoDB( "noconnect", $wgDBserver );
-       $nodb = wfMsgNoDB( "nodb", $wgDBname );
-
-       $helpme = "\n<p>If this error persists after reloading and clearing " .
-         "your browser cache, please notify the <a href=\"mailto:" .
-         $wgEmergencyContact . "\">Wikipedia developers</a>.</p>";
-
-       if ( $altuser != "" ) {
-               $serve = ($altserver ? $altserver : $wgDBserver );
-               $db = ($altdb ? $altdb : $wgDBname );
-               $wgDBconnection = mysql_connect( $serve, $altuser, $altpassword )
-                       or die( "bad sql user" );
-               mysql_select_db( $db, $wgDBconnection ) or die(
-                 htmlspecialchars(mysql_error()) );
-       }
-
-       if ( ! $wgDBconnection ) {
-               @$wgDBconnection = mysql_pconnect( $wgDBserver, $wgDBuser, $wgDBpassword )
-                       or wfEmergencyAbort();
-               
-               if( !mysql_select_db( $wgDBname, $wgDBconnection ) ) {
-                       /* Persistent connections may become stuck in an unusable state */
-                       wfDebug( "Persistent connection is broken?\n", true );
-                       
-                       @$wgDBconnection = mysql_connect( $wgDBserver, $wgDBuser, $wgDBpassword )
-                               or wfEmergencyAbort();
-                       
-                       @mysql_select_db( $wgDBname, $wgDBconnection )
-                               or wfEmergencyAbort();
-               }
-       }
-       # mysql_ping( $wgDBconnection );
-       return $wgDBconnection;
-}
-
-/* Call this function if we couldn't contact the database...
-   We'll try to use the cache to display something in the meantime */
-function wfEmergencyAbort( $msg = "" ) {
-       global $wgTitle, $wgUseFileCache, $title, $wgOutputEncoding;
+/*
+ * @todo document function
+ */
+function &wfGetDB( $db = DB_LAST, $groups = array() ) {
+       global $wgLoadBalancer;
+       return $wgLoadBalancer->getConnection( $db, true, $groups );
+}
        
-       header( "Content-type: text/html; charset=$wgOutputEncoding" );
-       if($msg == "") $msg = wfMsgNoDB( "noconnect" );
-       $text = $msg;
-
-       if($wgUseFileCache) {
-               if($wgTitle) {
-                       $t =& $wgTitle;
-               } else {
-                       if($title) {
-                               $t = Title::newFromURL( $title );
-                       } else {
-                               $t = Title::newFromText( wfMsgNoDB( "mainpage" ) );
-                       }
-               }
-
-               $cache = new CacheManager( $t );
-               if( $cache->isFileCached() ) {
-                       $msg = "<p style='color: red'><b>$msg<br>\n" .
-                               wfMsgNoDB( "cachederror" ) . "</b></p>\n";
-                       
-                       $tag = "<div id='article'>";
-                       $text = str_replace(
-                               $tag,
-                               $tag . $msg,
-                               $cache->fetchPageText() );
-               }
+/**
+ * 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;
        }
-       
-       /* Don't cache error pages!  They cause no end of trouble... */
-       header( "Cache-control: none" );
-       header( "Pragma: nocache" );
-       echo $text;
-       exit;
-}
-
-# $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
-function wfQuery( $sql, $db, $fname = "" )
-{
-       global $wgLastDatabaseQuery, $wgOut, $wgDebugDumpSql;
+}
 
-        # wfGeneralizeSQL will probably cut down the query to reasonable
-        # logging size most of the time. The substr is really just a sanity check.
-        $profName = "wfQuery: " . substr( wfGeneralizeSQL( $sql ), 0, 255 ); 
-       
-       wfProfileIn( $profName );
+/**#@+
+ * @param $res database result handler
+ * @param $dbi
+*/
 
-       if ( !is_numeric( $db ) ) {
-               # Someone has tried to call this the old way
-               $wgOut->fatalError( wfMsgNoDB( "wrong_wfQuery_params", $db, $sql ) );
+/**
+ * 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;
        }
-       
-       $wgLastDatabaseQuery = $sql;
-       
-       if( $wgDebugDumpSql ) {
-               $sqlx = substr( $sql, 0, 500 );
-               $sqlx = wordwrap(strtr($sqlx,"\t\n","  "));
-               wfDebug( "SQL: $sqlx\n" );
+}
+
+/**
+ * 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;
        }
+}
 
-       $conn = wfGetDB();
-       $ret = mysql_query( $sql, $conn );
+/**
+ * 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;
+       }
+}
 
-       if ( false === $ret ) {
-               $wgOut->databaseError( $fname );
-               exit;
+/**
+ * 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;
        }
-       wfProfileOut( $profName );
-       return $ret;
 }
 
-function wfFreeResult( $res ) { mysql_free_result( $res ); }
-function wfFetchObject( $res ) { return mysql_fetch_object( $res ); }
-function wfNumRows( $res ) { return mysql_num_rows( $res ); }
-function wfNumFields( $res ) { return mysql_num_fields( $res ); }
-function wfFieldName( $res, $n ) { return mysql_field_name( $res, $n ); }
-function wfInsertId() { return mysql_insert_id( wfGetDB() ); }
-function wfDataSeek( $res, $row ) { return mysql_data_seek( $res, $row ); }
-function wfLastErrno() { return mysql_errno(); }
-function wfLastError() { return mysql_error(); }
-function wfAffectedRows() { return mysql_affected_rows( wfGetDB() ); }
+/**
+ * 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 wfLastDBquery()
-{
-       global $wgLastDatabaseQuery;
-       return $wgLastDatabaseQuery;
+/**
+ * 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( $dbi );
+       if ( $db !== false ) {
+               return $db->fieldName( $res, $n, $dbi = DB_LAST ); 
+       } else {        
+               return false;
+       }
 }
+/**#@-*/
 
-function wfSetSQL( $table, $var, $value, $cond )
+/**
+ * @todo document function
+ */
+function wfInsertId( $dbi = DB_LAST ) { 
+       $db =& wfGetDB( $dbi );
+       if ( $db !== false ) {
+               return $db->insertId(); 
+       } else {        
+               return false;
+       }
+}
+
+/**
+ * @todo document function
+ */
+function wfDataSeek( $res, $row, $dbi = DB_LAST ) { 
+       $db =& wfGetDB( $dbi );
+       if ( $db !== false ) {
+               return $db->dataSeek( $res, $row ); 
+       } else {        
+               return false;
+       }
+}
+
+/**
+ * @todo document function
+ */
+function wfLastErrno( $dbi = DB_LAST ) { 
+       $db =& wfGetDB( $dbi );
+       if ( $db !== false ) {
+               return $db->lastErrno(); 
+       } else {        
+               return false;
+       }
+}
+
+/**
+ * @todo document function
+ */
+function wfLastError( $dbi = DB_LAST ) { 
+       $db =& wfGetDB( $dbi );
+       if ( $db !== false ) {
+               return $db->lastError(); 
+       } else {        
+               return false;
+       }
+}
+
+/**
+ * @todo document function
+ */
+function wfAffectedRows( $dbi = DB_LAST ) { 
+       $db =& wfGetDB( $dbi );
+       if ( $db !== false ) {
+               return $db->affectedRows(); 
+       } else {        
+               return false;
+       }
+}
+
+/**
+ * @todo document function
+ */
+function wfLastDBquery( $dbi = DB_LAST ) {
+       $db =& wfGetDB( $dbi );
+       if ( $db !== false ) {
+               return $db->lastQuery();
+       } else {        
+               return false;
+       }
+}
+
+/**
+ * @todo document function
+ */
+function wfSetSQL( $table, $var, $value, $cond, $dbi = DB_MASTER )
 {
-       $sql = "UPDATE $table SET $var = '" .
-         wfStrencode( $value ) . "' WHERE ($cond)";
-       wfQuery( $sql, DB_WRITE, "wfSetSQL" );
+       $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 )
 {
-       $sql = "SELECT $var FROM $table WHERE ($cond)";
-       $result = wfQuery( $sql, DB_READ, "wfGetSQL" );
+       $db =& wfGetDB( $dbi );
+       if ( $db !== false ) {
+               return $db->selectField( $table, $var, $cond );
+       } else {        
+               return false;
+       }
+}
 
-       $ret = "";
-       if ( mysql_num_rows( $result ) > 0 ) {
-               $s = mysql_fetch_object( $result );
-               $ret = $s->$var;
-               mysql_free_result( $result );
+/**
+ * @todo document function
+ */
+function wfFieldExists( $table, $field, $dbi = DB_LAST ) {
+       $db =& wfGetDB( $dbi );
+       if ( $db !== false ) {
+               return $db->fieldExists( $table, $field );
+       } else {        
+               return false;
        }
-       return $ret;
 }
 
-function wfStrencode( $s )
-{
-       return addslashes( $s );
+/**
+ * @todo document function
+ */
+function wfIndexExists( $table, $index, $dbi = DB_LAST ) {
+       $db =& wfGetDB( $dbi );
+       if ( $db !== false ) {
+               return $db->indexExists( $table, $index );
+       } else {        
+               return false;
+       }
 }
 
-# Ideally we'd be using actual time fields in the db
-function wfTimestamp2Unix( $ts ) {
-       return gmmktime( ( (int)substr( $ts, 8, 2) ),
-                 (int)substr( $ts, 10, 2 ), (int)substr( $ts, 12, 2 ),
-                 (int)substr( $ts, 4, 2 ), (int)substr( $ts, 6, 2 ),
-                 (int)substr( $ts, 0, 4 ) );
+/**
+ * @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;
+       }
 }
 
-function wfUnix2Timestamp( $unixtime ) {
-       return gmdate( "YmdHis", $unixtime );
+/**
+ * @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;
+       }
 }
 
-function wfTimestampNow() {
-       # return NOW
-       return gmdate( "YmdHis" );
+/**
+ * @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;
+       }
 }
 
-# Sorting hack for MySQL 3, which doesn't use index sorts for DESC
-function wfInvertTimestamp( $ts ) {
-       return strtr(
-               $ts,
-               "0123456789",
-               "9876543210"
-       );
+/**
+ * @todo document function
+ */
+function wfTableName( $name, $dbi = DB_LAST ) {
+       $db =& wfGetDB( $dbi );
+       if ( $db !== false ) {
+               return $db->tableName( $name );
+       } else {
+               return false;
+       }
 }
 
-# Removes most variables from an SQL query and replaces them with X or N for numbers.
-# It's only slightly flawed. Don't use for anything important.
-function wfGeneralizeSQL( $sql )
-{
-        # This could be done faster with some arrays and a single preg_replace,
-        # but this show more clearly what's going on. Which may be a good thing.
-        $sql = preg_replace ( "/([\'\"])([^\\\\]|\\\\\\\\)*?\\1/", "\\1X\\1", $sql);
-        $sql = preg_replace ( "/-?\d+/" , "N", $sql);
-        $sql = preg_replace ( "/\s+/", " ", $sql);
-        return $sql;
+/**
+ * @todo document function
+ */
+function wfStrencode( $s, $dbi = DB_LAST ) {
+       $db =& wfGetDB( $dbi );
+       if ( $db !== false ) {
+               return $db->strencode( $s );
+       } else {
+               return false;
+       }
 }
 
-function wfFieldExists( $table, $field )
-{
-       $fname = "wfFieldExists";
-       $res = wfQuery( "DESCRIBE $table", DB_READ, $fname );
-       $found = false;
-       
-       while ( $row = wfFetchObject( $res ) ) {
-               if ( $row->Field == $field ) {
-                       $found = true;
-                       break;
-               }
+/**
+ * @todo document function
+ */
+function wfNextSequenceValue( $seqName, $dbi = DB_MASTER ) {
+       $db =& wfGetDB( $dbi );
+       if ( $db !== false ) {
+               return $db->nextSequenceValue( $seqName );
+       } else {
+               return false;
        }
-       return $found;
 }
 
-function wfIndexExists( $table, $index ) 
-{
-       global $wgDBname;
-       $fname = "wfIndexExists";
-       $sql = "SHOW INDEXES FROM $table";
-       $res = wfQuery( $sql, DB_READ, $fname );
-       $found = false;
-       while ( $row = wfFetchObject( $res ) ) {
-               if ( $row->Key_name == $index ) {
-                       $found = true;
-                       break;
-               }
-       }
-       return $found;
+/**
+ * @todo document function
+ */
+function wfUseIndexClause( $index, $dbi = DB_SLAVE ) {
+       $db =& wfGetDB( $dbi );
+       if ( $db !== false ) {
+               return $db->useIndexClause( $index );
+       } else {
+               return false;
+       }
 }
 ?>