/**
* MySQL-specific installer.
*
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ * http://www.gnu.org/copyleft/gpl.html
+ *
* @file
* @ingroup Deployment
*/
protected $internalDefaults = array(
'_MysqlEngine' => 'InnoDB',
'_MysqlCharset' => 'binary',
+ '_InstallUser' => 'root',
);
public $supportedEngines = array( 'InnoDB', 'MyISAM' );
- public $minimumVersion = '4.0.14';
+ public $minimumVersion = '5.0.2';
public $webUserPrivs = array(
'DELETE',
'CREATE TEMPORARY TABLES',
);
+ /**
+ * @return string
+ */
public function getName() {
return 'mysql';
}
parent::__construct( $parent );
}
+ /**
+ * @return Bool
+ */
public function isCompiled() {
return self::checkExtension( 'mysql' );
}
+ /**
+ * @return array
+ */
public function getGlobalDefaults() {
return array();
}
+ /**
+ * @return string
+ */
public function getConnectForm() {
return
$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' ) ) .
+ $this->getTextBox( 'wgDBname', 'config-db-name', array( 'dir' => 'ltr' ), $this->parent->getHelpBox( 'config-db-name-help' ) ) .
+ $this->getTextBox( 'wgDBprefix', 'config-db-prefix', array( 'dir' => 'ltr' ), $this->parent->getHelpBox( 'config-db-prefix-help' ) ) .
Html::closeElement( 'fieldset' ) .
$this->getInstallUserBox();
}
if ( !$status->isOK() ) {
return $status;
}
+ /**
+ * @var $conn DatabaseBase
+ */
$conn = $status->value;
// Check version
return $status;
}
+ /**
+ * @return Status
+ */
public function openConnection() {
$status = Status::newGood();
try {
$this->parent->showStatusError( $status );
return;
}
+ /**
+ * @var $conn DatabaseBase
+ */
$conn = $status->value;
$conn->selectDB( $this->getVar( 'wgDBname' ) );
# Determine existing default character set
- if ( $conn->tableExists( "revision" ) ) {
+ if ( $conn->tableExists( "revision", __METHOD__ ) ) {
$revision = $conn->buildLike( $this->getVar( 'wgDBprefix' ) . 'revision' );
$res = $conn->query( "SHOW TABLE STATUS $revision", __METHOD__ );
$row = $conn->fetchObject( $res );
$existingEngine = false;
} else {
if ( preg_match( '/^latin1/', $row->Collation ) ) {
- $existingSchema = 'mysql4';
+ $existingSchema = 'latin1';
} elseif ( preg_match( '/^utf8/', $row->Collation ) ) {
$existingSchema = 'utf8';
} elseif ( preg_match( '/^binary/', $row->Collation ) ) {
/**
* Get a list of storage engines that are available and supported
+ *
+ * @return array
*/
public function getEngines() {
- $engines = array( 'InnoDB', 'MyISAM' );
$status = $this->getConnection();
- if ( !$status->isOK() ) {
- return $engines;
- }
- $conn = $status->value;
- $version = $conn->getServerVersion();
- if ( version_compare( $version, "4.1.2", "<" ) ) {
- // No SHOW ENGINES in this version
- return $engines;
- }
+ /**
+ * @var $conn DatabaseBase
+ */
+ $conn = $status->value;
$engines = array();
$res = $conn->query( 'SHOW ENGINES', __METHOD__ );
/**
* Get a list of character sets that are available and supported
+ *
+ * @return array
*/
public function getCharsets() {
- $status = $this->getConnection();
- $mysql5 = array( 'binary', 'utf8' );
- $mysql4 = array( 'mysql4' );
- if ( !$status->isOK() ) {
- return $mysql5;
- }
- if ( version_compare( $status->value->getServerVersion(), '4.1.0', '>=' ) ) {
- return $mysql5;
- }
- return $mysql4;
+ return array( 'binary', 'utf8' );
}
/**
* Return true if the install user can create accounts
+ *
+ * @return bool
*/
public function canCreateAccounts() {
$status = $this->getConnection();
if ( !$status->isOK() ) {
return false;
}
+ /**
+ * @var $conn DatabaseBase
+ */
$conn = $status->value;
- // Check version, need INFORMATION_SCHEMA and CREATE USER
- if ( version_compare( $conn->getServerVersion(), '5.0.2', '<' ) ) {
- return false;
- }
-
// Get current account name
$currentName = $conn->selectField( '', 'CURRENT_USER()', '', __METHOD__ );
$parts = explode( '@', $currentName );
return true;
}
+ /**
+ * @return string
+ */
public function getSettingsForm() {
if ( $this->canCreateAccounts() ) {
$noCreateMsg = false;
if ( !in_array( $this->getVar( '_MysqlEngine' ), $engines ) ) {
$this->setVar( '_MysqlEngine', reset( $engines ) );
}
+
+ $s .= Xml::openElement( 'div', array(
+ 'id' => 'dbMyisamWarning'
+ ));
+ $s .= $this->parent->getWarningBox( wfMsg( 'config-mysql-myisam-dep' ) );
+ $s .= Xml::closeElement( 'div' );
+
+ if( $this->getVar( '_MysqlEngine' ) != 'MyISAM' ) {
+ $s .= Xml::openElement( 'script', array( 'type' => 'text/javascript' ) );
+ $s .= '$(\'#dbMyisamWarning\').hide();';
+ $s .= Xml::closeElement( 'script' );
+ }
+
if ( count( $engines ) >= 2 ) {
$s .= $this->getRadioSet( array(
'var' => '_MysqlEngine',
'label' => 'config-mysql-engine',
'itemLabelPrefix' => 'config-mysql-',
- 'values' => $engines
- ));
+ 'values' => $engines,
+ 'itemAttribs' => array(
+ 'MyISAM' => array(
+ 'class' => 'showHideRadio',
+ 'rel' => 'dbMyisamWarning'),
+ 'InnoDB' => array(
+ 'class' => 'hideShowRadio',
+ 'rel' => 'dbMyisamWarning')
+ )));
$s .= $this->parent->getHelpBox( 'config-mysql-engine-help' );
}
return $s;
}
+ /**
+ * @return Status
+ */
public function submitSettingsForm() {
$this->setVarsFromRequest( array( '_MysqlEngine', '_MysqlCharset' ) );
$status = $this->submitWebUserBox();
$this->parent->addInstallStep( $callback, 'tables' );
}
+ /**
+ * @return Status
+ */
public function setupDatabase() {
$status = $this->getConnection();
if ( !$status->isOK() ) {
return $status;
}
+ /**
+ * @return Status
+ */
public function setupUser() {
- global $IP;
-
- if ( !$this->getVar( '_CreateDBAccount' ) ) {
+ $dbUser = $this->getVar( 'wgDBuser' );
+ if( $dbUser == $this->getVar( '_InstallUser' ) ) {
return Status::newGood();
}
-
$status = $this->getConnection();
if ( !$status->isOK() ) {
return $status;
}
- $db = $this->getVar( 'wgDBname' );
- $this->db->selectDB( $db );
$this->setupSchemaVars();
- $error = $this->db->sourceFile( "$IP/maintenance/users.sql" );
- if ( $error !== true ) {
- $status->fatal( 'config-install-user-failed', $this->getVar( 'wgDBuser' ), $error );
+ $dbName = $this->getVar( 'wgDBname' );
+ $this->db->selectDB( $dbName );
+ $server = $this->getVar( 'wgDBserver' );
+ $password = $this->getVar( 'wgDBpassword' );
+ $grantableNames = array();
+
+ if ( $this->getVar( '_CreateDBAccount' ) ) {
+ // Before we blindly try to create a user that already has access,
+ try { // first attempt to connect to the database
+ new DatabaseMysql(
+ $server,
+ $dbUser,
+ $password,
+ false,
+ false,
+ 0,
+ $this->getVar( 'wgDBprefix' )
+ );
+ $grantableNames[] = $this->buildFullUserName( $dbUser, $server );
+ $tryToCreate = false;
+ } catch ( DBConnectionError $e ) {
+ $tryToCreate = true;
+ }
+ } else {
+ $grantableNames[] = $this->buildFullUserName( $dbUser, $server );
+ $tryToCreate = false;
+ }
+
+ if( $tryToCreate ) {
+ $createHostList = array($server,
+ 'localhost',
+ 'localhost.localdomain',
+ '%'
+ );
+
+ $createHostList = array_unique( $createHostList );
+ $escPass = $this->db->addQuotes( $password );
+
+ foreach( $createHostList as $host ) {
+ $fullName = $this->buildFullUserName( $dbUser, $host );
+ if( !$this->userDefinitelyExists( $dbUser, $host ) ) {
+ try{
+ $this->db->begin( __METHOD__ );
+ $this->db->query( "CREATE USER $fullName IDENTIFIED BY $escPass", __METHOD__ );
+ $this->db->commit( __METHOD__ );
+ $grantableNames[] = $fullName;
+ } catch( DBQueryError $dqe ) {
+ if( $this->db->lastErrno() == 1396 /* ER_CANNOT_USER */ ) {
+ // User (probably) already exists
+ $this->db->rollback( __METHOD__ );
+ $status->warning( 'config-install-user-alreadyexists', $dbUser );
+ $grantableNames[] = $fullName;
+ break;
+ } else {
+ // If we couldn't create for some bizzare reason and the
+ // user probably doesn't exist, skip the grant
+ $this->db->rollback( __METHOD__ );
+ $status->warning( 'config-install-user-create-failed', $dbUser, $dqe->getText() );
+ }
+ }
+ } else {
+ $status->warning( 'config-install-user-alreadyexists', $dbUser );
+ $grantableNames[] = $fullName;
+ break;
+ }
+ }
+ }
+
+ // Try to grant to all the users we know exist or we were able to create
+ $dbAllTables = $this->db->addIdentifierQuotes( $dbName ) . '.*';
+ foreach( $grantableNames as $name ) {
+ try {
+ $this->db->begin( __METHOD__ );
+ $this->db->query( "GRANT ALL PRIVILEGES ON $dbAllTables TO $name", __METHOD__ );
+ $this->db->commit( __METHOD__ );
+ } catch( DBQueryError $dqe ) {
+ $this->db->rollback( __METHOD__ );
+ $status->fatal( 'config-install-user-grant-failed', $dbUser, $dqe->getText() );
+ }
}
return $status;
}
+ /**
+ * Return a formal 'User'@'Host' username for use in queries
+ * @param $name String Username, quotes will be added
+ * @param $host String Hostname, quotes will be added
+ * @return String
+ */
+ private function buildFullUserName( $name, $host ) {
+ return $this->db->addQuotes( $name ) . '@' . $this->db->addQuotes( $host );
+ }
+
+ /**
+ * Try to see if the user account exists. Our "superuser" may not have
+ * access to mysql.user, so false means "no" or "maybe"
+ * @param $host String Hostname to check
+ * @param $user String Username to check
+ * @return boolean
+ */
+ private function userDefinitelyExists( $host, $user ) {
+ try {
+ $res = $this->db->selectRow( 'mysql.user', array( 'Host', 'User' ),
+ array( 'Host' => $host, 'User' => $user ), __METHOD__ );
+ return (bool)$res;
+ } catch( DBQueryError $dqe ) {
+ return false;
+ }
+
+ }
+
/**
* Return any table options to be applied to all tables that don't
* override them.
/**
* Get variables to substitute into tables.sql and the SQL patch files.
+ *
+ * @return array
*/
public function getSchemaVars() {
return array(
# MySQL table options to use during installation or update
\$wgDBTableOptions = \"{$tblOpts}\";
-# Experimental charset support for MySQL 4.1/5.0.
+# Experimental charset support for MySQL 5.0.
\$wgDBmysql5 = {$dbmysql5};";
}
}