Cleanup
[lhc/web/wiklou.git] / includes / Database.php
index 293ed05..f873828 100644 (file)
@@ -35,6 +35,22 @@ class DBObject {
        }
 };
 
+/**
+ * Utility class
+ * @addtogroup Database
+ *
+ * This allows us to distinguish a blob from a normal string and an array of strings
+ */
+class Blob {
+       private $mData;
+       function __construct($data) {
+               $this->mData = $data;
+       }
+       function fetch() {
+               return $this->mData;
+       }
+};
+
 /**
  * Utility class.
  * @addtogroup Database
@@ -214,7 +230,7 @@ border=\"0\" ALT=\"Google\"></A>
 
                        $cache = new HTMLFileCache( $t );
                        if( $cache->isFileCached() ) {
-                               // FIXME: $msg is not defined on the next line.
+                               // @todo, FIXME: $msg is not defined on the next line.
                                $msg = '<p style="color: red"><b>'.$msg."<br />\n" .
                                        $cachederror . "</b></p>\n";
 
@@ -440,6 +456,14 @@ class Database {
                return true;
        }
 
+       /**
+        * Returns true if this database does an implicit order by when the column has an index
+        * For example: SELECT page_title FROM page LIMIT 1
+        */
+       function implicitOrderby() {
+               return true;
+       }
+
        /**
         * Returns true if this database can do a native search on IP columns
         * e.g. this works as expected: .. WHERE rc_ip = '127.42.12.102/32';
@@ -448,6 +472,13 @@ class Database {
                return false;
        }
 
+       /**
+        * Returns true if this database can use functional indexes
+        */
+       function functionalIndexes() {
+               return false;
+       }
+
        /**#@+
         * Get function
         */
@@ -714,8 +745,8 @@ class Database {
                        global $wgUser;
                        if ( is_object( $wgUser ) && !($wgUser instanceof StubObject) ) {
                                $userName = $wgUser->getName();
-                               if ( strlen( $userName ) > 15 ) {
-                                       $userName = substr( $userName, 0, 15 ) . '...';
+                               if ( mb_strlen( $userName ) > 15 ) {
+                                       $userName = mb_substr( $userName, 0, 15 ) . '...';
                                }
                                $userName = str_replace( '/', '', $userName );
                        } else {
@@ -728,9 +759,13 @@ class Database {
 
                # If DBO_TRX is set, start a transaction
                if ( ( $this->mFlags & DBO_TRX ) && !$this->trxLevel() && 
-                       $sql != 'BEGIN' && $sql != 'COMMIT' && $sql != 'ROLLBACK' 
-               ) {
-                       $this->begin();
+                       $sql != 'BEGIN' && $sql != 'COMMIT' && $sql != 'ROLLBACK') {
+                       // avoid establishing transactions for SHOW and SET statements too -
+                       // that would delay transaction initializations to once connection 
+                       // is really used by application
+                       $sqlstart = substr($sql,0,10); // very much worth it, benchmark certified(tm)
+                       if (strpos($sqlstart,"SHOW ")!==0 and strpos($sqlstart,"SET ")!==0) 
+                               $this->begin(); 
                }
 
                if ( $this->debug() ) {
@@ -1488,6 +1523,7 @@ class Database {
         *                       (for the log)
         * @param array  $options An array of UPDATE options, can be one or
         *                        more of IGNORE, LOW_PRIORITY
+        * @return bool
         */
        function update( $table, $values, $conds, $fname = 'Database::update', $options = array() ) {
                $table = $this->tableName( $table );
@@ -1496,7 +1532,7 @@ class Database {
                if ( $conds != '*' ) {
                        $sql .= " WHERE " . $this->makeList( $conds, LIST_AND );
                }
-               $this->query( $sql, $fname );
+               return $this->query( $sql, $fname );
        }
 
        /**
@@ -1531,8 +1567,23 @@ class Database {
                                $list .= "($value)";
                        } elseif ( ($mode == LIST_SET) && is_numeric( $field ) ) {
                                $list .= "$value";
-                       } elseif ( ($mode == LIST_AND || $mode == LIST_OR) && is_array ($value) ) {
-                               $list .= $field." IN (".$this->makeList($value).") ";
+                       } elseif ( ($mode == LIST_AND || $mode == LIST_OR) && is_array($value) ) {
+                               if( count( $value ) == 0 ) {
+                                       // Empty input... or should this throw an error?
+                                       $list .= '0';
+                               } elseif( count( $value ) == 1 ) {
+                                       // Special-case single values, as IN isn't terribly efficient
+                                       $list .= $field." = ".$this->addQuotes( $value[0] );
+                               } else {
+                                       $list .= $field." IN (".$this->makeList($value).") ";
+                               }
+                       } elseif( is_null($value) ) {
+                               if ( $mode == LIST_AND || $mode == LIST_OR ) {
+                                       $list .= "$field IS ";
+                               } elseif ( $mode == LIST_SET ) {
+                                       $list .= "$field = ";
+                               }
+                               $list .= 'NULL';
                        } else {
                                if ( $mode == LIST_AND || $mode == LIST_OR || $mode == LIST_SET ) {
                                        $list .= "$field = ";
@@ -1567,7 +1618,7 @@ class Database {
                global $wgSharedDB;
                # Skip quoted literals
                if ( $name{0} != '`' ) {
-                       if ( $this->mTablePrefix !== '' &&  strpos( '.', $name ) === false ) {
+                       if ( $this->mTablePrefix !== '' &&  strpos( $name, '.' ) === false ) {
                                $name = "{$this->mTablePrefix}$name";
                        }
                        if ( isset( $wgSharedDB ) && "{$this->mTablePrefix}user" == $name ) {
@@ -1603,7 +1654,7 @@ class Database {
         * This is handy when you need to construct SQL for joins
         *
         * Example:
-        * list( $user, $watchlist ) = $dbr->tableNames('user','watchlist');
+        * list( $user, $watchlist ) = $dbr->tableNamesN('user','watchlist');
         * $sql = "SELECT wl_namespace,wl_title FROM $watchlist,$user
         *         WHERE wl_user=user_id AND wl_user=$nameWithQuotes";
         */
@@ -1988,10 +2039,11 @@ class Database {
        }
 
        /**
-        * Rollback a transaction
+        * Rollback a transaction.
+        * No-op on non-transactional databases.
         */
        function rollback( $fname = 'Database::rollback' ) {
-               $this->query( 'ROLLBACK', $fname );
+               $this->query( 'ROLLBACK', $fname, true );
                $this->mTrxLevel = 0;
        }
 
@@ -2084,8 +2136,7 @@ class Database {
         */
        function getLag() {
                $res = $this->query( 'SHOW PROCESSLIST' );
-               # Find slave SQL thread. Assumed to be the second one running, which is a bit
-               # dubious, but unfortunately there's no easy rigorous way
+               # Find slave SQL thread
                while ( $row = $this->fetchObject( $res ) ) {
                        /* This should work for most situations - when default db 
                         * for thread is not specified, it had no events executed, 
@@ -2264,6 +2315,13 @@ class Database {
                return $this->tableName( $matches[1] );
        }
 
+       /*
+        * Build a concatenation list to feed into a SQL query
+       */
+       function buildConcat( $stringList ) {
+               return 'CONCAT(' . implode( ',', $stringList ) . ')';
+       }
+
 }
 
 /**
@@ -2282,8 +2340,8 @@ class DatabaseMysql extends Database {
  * Result wrapper for grabbing data queried by someone else
  * @addtogroup Database
  */
-class ResultWrapper {
-       var $db, $result;
+class ResultWrapper implements Iterator {
+       var $db, $result, $pos = 0, $currentRow = null;
 
        /**
         * Create a new result object from a result resource and a Database object
@@ -2345,16 +2403,41 @@ class ResultWrapper {
        function seek( $row ) {
                $this->db->dataSeek( $this->result, $row );
        }
-       
-       /**
-        * Reset the cursor to the start of the result set
+
+       /*********************
+        * Iterator functions
+        * Note that using these in combination with the non-iterator functions
+        * above may cause rows to be skipped or repeated.
         */
+
        function rewind() {
                if ($this->numRows()) {
                        $this->db->dataSeek($this->result, 0);
                }
+               $this->pos = 0;
+               $this->currentRow = null;
+       }
+
+       function current() {
+               if ( is_null( $this->currentRow ) ) {
+                       $this->next();
+               }
+               return $this->currentRow;
        }
 
+       function key() {
+               return $this->pos;
+       }
+
+       function next() {
+               $this->pos++;
+               $this->currentRow = $this->fetchObject();
+               return $this->currentRow;
+       }
+
+       function valid() {
+               return $this->current() !== false;
+       }
 }