Fix mysqli support
[lhc/web/wiklou.git] / includes / db / Database.php
index 27e6dad..1f7a830 100644 (file)
@@ -360,6 +360,8 @@ abstract class DatabaseBase implements IDatabase, DatabaseType {
         * code should use lastErrno() and lastError() to handle the
         * situation as appropriate.
         *
+        * Do not use this function outside of the Database classes.
+        *
         * @param $ignoreErrors bool|null
         *
         * @return bool The previous value of the flag.
@@ -582,7 +584,6 @@ abstract class DatabaseBase implements IDatabase, DatabaseType {
         * @param $flag Integer: DBO_* constants from Defines.php:
         *   - DBO_DEBUG: output some debug info (same as debug())
         *   - DBO_NOBUFFER: don't buffer results (inverse of bufferResults())
-        *   - DBO_IGNORE: ignore errors (same as ignoreErrors())
         *   - DBO_TRX: automatically start transactions
         *   - DBO_DEFAULT: automatically sets DBO_TRX if not in command line mode
         *       and removes it in command line mode
@@ -716,7 +717,7 @@ abstract class DatabaseBase implements IDatabase, DatabaseType {
        /**
         * Given a DB type, construct the name of the appropriate child class of
         * DatabaseBase. This is designed to replace all of the manual stuff like:
-        *      $class = 'Database' . ucfirst( strtolower( $type ) );
+        *      $class = 'Database' . ucfirst( strtolower( $dbType ) );
         * as well as validate against the canonical list of DB types we have
         *
         * This factory function is mostly useful for when you need to connect to a
@@ -731,17 +732,47 @@ abstract class DatabaseBase implements IDatabase, DatabaseType {
         *
         * @param string $dbType A possible DB type
         * @param array $p An array of options to pass to the constructor.
-        *    Valid options are: host, user, password, dbname, flags, tablePrefix
+        *    Valid options are: host, user, password, dbname, flags, tablePrefix, driver
         * @return DatabaseBase subclass or null
         */
        final public static function factory( $dbType, $p = array() ) {
                $canonicalDBTypes = array(
-                       'mysql', 'postgres', 'sqlite', 'oracle', 'mssql'
+                       'mysql'    => array( 'mysqli', 'mysql' ),
+                       'postgres' => array(),
+                       'sqlite'   => array(),
+                       'oracle'   => array(),
+                       'mssql'    => array(),
                );
+
+               $driver = false;
                $dbType = strtolower( $dbType );
-               $class = 'Database' . ucfirst( $dbType );
+               if ( isset( $canonicalDBTypes[$dbType] ) && $canonicalDBTypes[$dbType] ) {
+                       $possibleDrivers = $canonicalDBTypes[$dbType];
+                       if ( !empty( $p['driver'] ) ) {
+                               if ( in_array( $p['driver'], $possibleDrivers ) ) {
+                                       $driver = $p['driver'];
+                               } else {
+                                       throw new MWException( __METHOD__ .
+                                               " cannot construct Database with type '$dbType' and driver '{$p['driver']}'" );
+                               }
+                       } else {
+                               foreach ( $possibleDrivers as $posDriver ) {
+                                       if ( extension_loaded( $posDriver ) ) {
+                                               $driver = $posDriver;
+                                               break;
+                                       }
+                               }
+                       }
+               } else {
+                       $driver = $dbType;
+               }
+               if ( $driver === false ) {
+                       throw new MWException( __METHOD__ .
+                               " no viable database extension found for type '$dbType'" );
+               }
 
-               if ( in_array( $dbType, $canonicalDBTypes ) || ( class_exists( $class ) && is_subclass_of( $class, 'DatabaseBase' ) ) ) {
+               $class = 'Database' . ucfirst( $driver );
+               if ( class_exists( $class ) && is_subclass_of( $class, 'DatabaseBase' ) ) {
                        return new $class(
                                isset( $p['host'] ) ? $p['host'] : false,
                                isset( $p['user'] ) ? $p['user'] : false,
@@ -2300,8 +2331,7 @@ abstract class DatabaseBase implements IDatabase, DatabaseType {
        }
 
        /**
-        * If it's a string, adds quotes and backslashes
-        * Otherwise returns as-is
+        * Adds quotes and backslashes.
         *
         * @param $s string
         *