Moved constant values from initialiseFromUser() to class definition
[lhc/web/wiklou.git] / includes / installer / MysqlInstaller.php
index f0d6035..67abf43 100644 (file)
@@ -1,6 +1,19 @@
 <?php
+/**
+ * MySQL-specific installer.
+ *
+ * @file
+ * @ingroup Deployment
+ */
+
+/**
+ * Class for setting up the MediaWiki database using MySQL.
+ *
+ * @ingroup Deployment
+ * @since 1.17
+ */
+class MysqlInstaller extends DatabaseInstaller {
 
-class MysqlInstaller extends InstallerDBType {
        protected $globalNames = array(
                'wgDBserver',
                'wgDBname',
@@ -16,11 +29,11 @@ class MysqlInstaller extends InstallerDBType {
                '_MysqlCharset' => 'binary',
        );
 
-       var $supportedEngines = array( 'InnoDB', 'MyISAM' );
+       public $supportedEngines = array( 'InnoDB', 'MyISAM' );
 
-       var $minimumVersion = '4.0.14';
+       public $minimumVersion = '4.0.14';
 
-       var $webUserPrivs = array(
+       public $webUserPrivs = array(
                'DELETE',
                'INSERT',
                'SELECT',
@@ -28,37 +41,38 @@ class MysqlInstaller extends InstallerDBType {
                'CREATE TEMPORARY TABLES',
        );
 
-       function getName() {
+       public function getName() {
                return 'mysql';
        }
-       
-       function isCompiled() {
-               return $this->checkExtension( 'mysql' );
+
+       public function __construct( $parent ) {
+               parent::__construct( $parent );
+       }
+
+       public function isCompiled() {
+               return self::checkExtension( 'mysql' );
        }
 
-       function getGlobalDefaults() {
+       public function getGlobalDefaults() {
                return array();
        }
 
-       function getConnectForm() {
+       public function getConnectForm() {
                return
-                       $this->getTextBox( 'wgDBserver', 'config-db-host' ) .
-                       $this->parent->getHelpBox( 'config-db-host-help' ) . 
-                       Xml::openElement( 'fieldset' ) .
-                       Xml::element( 'legend', array(), wfMsg( 'config-db-wiki-settings' ) ) .
-                       $this->getTextBox( 'wgDBname', 'config-db-name' ) .
-                       $this->parent->getHelpBox( 'config-db-name-help' ) .
-                       $this->getTextBox( 'wgDBprefix', 'config-db-prefix' ) .
-                       $this->parent->getHelpBox( 'config-db-prefix-help' ) .
-                       Xml::closeElement( 'fieldset' ) .
+                       $this->getTextBox( 'wgDBserver', 'config-db-host', array(), $this->parent->getHelpBox( 'config-db-host-help' ) ) .
+                       Html::openElement( 'fieldset' ) .
+                       Html::element( 'legend', array(), wfMsg( 'config-db-wiki-settings' ) ) .
+                       $this->getTextBox( 'wgDBname', 'config-db-name', array(), $this->parent->getHelpBox( 'config-db-name-help' ) ) .
+                       $this->getTextBox( 'wgDBprefix', 'config-db-prefix', array(), $this->parent->getHelpBox( 'config-db-prefix-help' ) ) .
+                       Html::closeElement( 'fieldset' ) .
                        $this->getInstallUserBox();
        }
 
-       function submitConnectForm() {
-               // Get variables from the request
+       public function submitConnectForm() {
+               // Get variables from the request.
                $newValues = $this->setVarsFromRequest( array( 'wgDBserver', 'wgDBname', 'wgDBprefix' ) );
 
-               // Validate them
+               // Validate them.
                $status = Status::newGood();
                if ( !strlen( $newValues['wgDBname'] ) ) {
                        $status->fatal( 'config-missing-db-name' );
@@ -94,7 +108,7 @@ class MysqlInstaller extends InstallerDBType {
                return $status;
        }
 
-       function getConnection() {
+       public function getConnection() {
                $status = Status::newGood();
                try {
                        $this->db = new DatabaseMysql(
@@ -103,29 +117,31 @@ class MysqlInstaller extends InstallerDBType {
                                $this->getVar( '_InstallPassword' ),
                                false,
                                false,
-                               0, 
+                               0,
                                $this->getVar( 'wgDBprefix' )
                        );
                        $status->value = $this->db;
-                       return $status;
                } catch ( DBConnectionError $e ) {
                        $status->fatal( 'config-connection-error', $e->getMessage() );
                }
                return $status;
        }
 
-       function doUpgrade() {
+       public function preUpgrade() {
+               global $wgDBuser, $wgDBpassword;
+
                $status = $this->getConnection();
                if ( !$status->isOK() ) {
                        $this->parent->showStatusError( $status );
                        return;
                }
                $conn = $status->value;
+               $conn->selectDB( $this->getVar( 'wgDBname' ) );
 
                # Determine existing default character set
                if ( $conn->tableExists( "revision" ) ) {
                        $revision = $conn->escapeLike( $this->getVar( 'wgDBprefix' ) . 'revision' );
-                       $res = $conn->query( "SHOW TABLE STATUS LIKE '$revision'" );
+                       $res = $conn->query( "SHOW TABLE STATUS LIKE '$revision'", __METHOD__ );
                        $row = $conn->fetchObject( $res );
                        if ( !$row ) {
                                $this->parent->showMessage( 'config-show-table-status' );
@@ -148,15 +164,30 @@ class MysqlInstaller extends InstallerDBType {
                                        $existingEngine = $row->Type;
                                }
                        }
+               } else {
+                       $existingSchema = false;
+                       $existingEngine = false;
+               }
+
+               if ( $existingSchema && $existingSchema != $this->getVar( '_MysqlCharset' ) ) {
+                       $this->parent->showMessage( 'config-mysql-charset-mismatch', $this->getVar( '_MysqlCharset' ), $existingSchema );
+                       $this->setVar( '_MysqlCharset', $existingSchema );
                }
-               
-               // TODO
+               if ( $existingEngine && $existingEngine != $this->getVar( '_MysqlEngine' ) ) {
+                       $this->parent->showMessage( 'config-mysql-egine-mismatch', $this->getVar( '_MysqlEngine' ), $existingEngine );
+                       $this->setVar( '_MysqlEngine', $existingEngine );
+               }
+
+               # Normal user and password are selected after this step, so for now
+               # just copy these two
+               $wgDBuser = $this->getVar( '_InstallUser' );
+               $wgDBpassword = $this->getVar( '_InstallPassword' );
        }
 
        /**
         * Get a list of storage engines that are available and supported
         */
-       function getEngines() {
+       public function getEngines() {
                $engines = array( 'InnoDB', 'MyISAM' );
                $status = $this->getConnection();
                if ( !$status->isOK() ) {
@@ -171,7 +202,7 @@ class MysqlInstaller extends InstallerDBType {
                }
 
                $engines = array();
-               $res = $conn->query( 'SHOW ENGINES' );
+               $res = $conn->query( 'SHOW ENGINES', __METHOD__ );
                foreach ( $res as $row ) {
                        if ( $row->Support == 'YES' || $row->Support == 'DEFAULT' ) {
                                $engines[] = $row->Engine;
@@ -184,7 +215,7 @@ class MysqlInstaller extends InstallerDBType {
        /**
         * Get a list of character sets that are available and supported
         */
-       function getCharsets() {
+       public function getCharsets() {
                $status = $this->getConnection();
                $mysql5 = array( 'binary', 'utf8' );
                $mysql4 = array( 'mysql4' );
@@ -200,7 +231,7 @@ class MysqlInstaller extends InstallerDBType {
        /**
         * Return true if the install user can create accounts
         */
-       function canCreateAccounts() {
+       public function canCreateAccounts() {
                $status = $this->getConnection();
                if ( !$status->isOK() ) {
                        return false;
@@ -218,12 +249,12 @@ class MysqlInstaller extends InstallerDBType {
                if ( count( $parts ) != 2 ) {
                        return false;
                }
-               $quotedUser = $conn->addQuotes( $parts[0] ) . 
+               $quotedUser = $conn->addQuotes( $parts[0] ) .
                        '@' . $conn->addQuotes( $parts[1] );
 
                // The user needs to have INSERT on mysql.* to be able to CREATE USER
                // The grantee will be double-quoted in this query, as required
-               $res = $conn->select( 'INFORMATION_SCHEMA.USER_PRIVILEGES', '*', 
+               $res = $conn->select( 'INFORMATION_SCHEMA.USER_PRIVILEGES', '*',
                        array( 'GRANTEE' => $quotedUser ), __METHOD__ );
                $insertMysql = false;
                $grantOptions = array_flip( $this->webUserPrivs );
@@ -239,7 +270,7 @@ class MysqlInstaller extends InstallerDBType {
                // Check for DB-specific privs for mysql.*
                if ( !$insertMysql ) {
                        $row = $conn->selectRow( 'INFORMATION_SCHEMA.SCHEMA_PRIVILEGES', '*',
-                               array( 
+                               array(
                                        'GRANTEE' => $quotedUser,
                                        'TABLE_SCHEMA' => 'mysql',
                                        'PRIVILEGE_TYPE' => 'INSERT',
@@ -254,7 +285,7 @@ class MysqlInstaller extends InstallerDBType {
                }
 
                // Check for DB-level grant options
-               $res = $conn->select( 'INFORMATION_SCHEMA.SCHEMA_PRIVILEGES', '*', 
+               $res = $conn->select( 'INFORMATION_SCHEMA.SCHEMA_PRIVILEGES', '*',
                        array(
                                'GRANTEE' => $quotedUser,
                                'IS_GRANTABLE' => 1,
@@ -272,7 +303,7 @@ class MysqlInstaller extends InstallerDBType {
                return true;
        }
 
-       function getSettingsForm() {
+       public function getSettingsForm() {
                if ( $this->canCreateAccounts() ) {
                        $noCreateMsg = false;
                } else {
@@ -288,9 +319,9 @@ class MysqlInstaller extends InstallerDBType {
                }
                if ( count( $engines ) >= 2 ) {
                        $s .= $this->getRadioSet( array(
-                               'var' => '_MysqlEngine', 
-                               'label' => 'config-mysql-engine', 
-                               'itemLabelPrefix' => 'config-mysql-', 
+                               'var' => '_MysqlEngine',
+                               'label' => 'config-mysql-engine',
+                               'itemLabelPrefix' => 'config-mysql-',
                                'values' => $engines
                        ));
                        $s .= $this->parent->getHelpBox( 'config-mysql-engine-help' );
@@ -316,7 +347,7 @@ class MysqlInstaller extends InstallerDBType {
                return $s;
        }
 
-       function submitSettingsForm() {
+       public function submitSettingsForm() {
                $newValues = $this->setVarsFromRequest( array( '_MysqlEngine', '_MysqlCharset' ) );
                $status = $this->submitWebUserBox();
                if ( !$status->isOK() ) {
@@ -335,13 +366,13 @@ class MysqlInstaller extends InstallerDBType {
                if ( !$create ) {
                        // Test the web account
                        try {
-                               $webConn = new Database( 
+                               new Database(
                                        $this->getVar( 'wgDBserver' ),
                                        $this->getVar( 'wgDBuser' ),
                                        $this->getVar( 'wgDBpassword' ),
                                        false,
                                        false,
-                                       0, 
+                                       0,
                                        $this->getVar( 'wgDBprefix' )
                                );
                        } catch ( DBConnectionError $e ) {
@@ -362,7 +393,16 @@ class MysqlInstaller extends InstallerDBType {
                return Status::newGood();
        }
 
-       function setupDatabase() {
+       public function preInstall() {
+               # Add our user callback to installSteps, right before the tables are created.
+               $callback = array(
+                       'name' => 'user',
+                       'callback' => array( $this, 'setupUser' ),
+               );
+               $this->parent->addInstallStepFollowing( "tables", $callback );
+       }
+
+       public function setupDatabase() {
                $status = $this->getConnection();
                if ( !$status->isOK() ) {
                        return $status;
@@ -370,31 +410,40 @@ class MysqlInstaller extends InstallerDBType {
                $conn = $status->value;
                $dbName = $this->getVar( 'wgDBname' );
                if( !$conn->selectDB( $dbName ) ) {
-                       $conn->query( "CREATE DATABASE `$dbName`" );
+                       $conn->query( "CREATE DATABASE `$dbName`", __METHOD__ );
                        $conn->selectDB( $dbName );
                }
                return $status;
        }
 
-       function createTables() {
+       public function setupUser() {
                global $IP;
+
+               if ( !$this->getVar( '_CreateDBAccount' ) ) {
+                       return Status::newGood();
+               }
+
                $status = $this->getConnection();
                if ( !$status->isOK() ) {
                        return $status;
                }
-               $this->db->selectDB( $this->getVar( 'wgDBname' ) );
-               if ( !$this->db->sourceFile( "$IP/maintenance/tables.sql" ) ) {
-                       //@todo
+
+               $db = $this->getVar( 'wgDBname' );
+               $this->db->selectDB( $db );
+               $error = $this->db->sourceFile( "$IP/maintenance/users.sql" );
+               if ( $error !== true ) {
+                       $status->fatal( 'config-install-user-failed', $this->getVar( 'wgDBuser' ), $error );
                }
-               return Status::newGood();
+
+               return $status;
        }
 
-       function getTableOptions() {
+       public function getTableOptions() {
                return array( 'engine' => $this->getVar( '_MysqlEngine' ),
                        'default charset' => $this->getVar( '_MysqlCharset' ) );
        }
 
-       function getLocalSettings() {
+       public function getLocalSettings() {
                $dbmysql5 = wfBoolToStr( $this->getVar( 'wgDBmysql5', true ) );
                $prefix = $this->getVar( 'wgDBprefix' );
                $opts = $this->getTableOptions();