wiki.techinc.nl/includes/installer/SqliteInstaller.php

226 lines
5.8 KiB
PHP
Raw Normal View History

<?php
/**
* Sqlite-specific installer.
*
* @file
* @ingroup Deployment
*/
2010-12-16 11:20:39 +00:00
2010-07-29 18:36:39 +00:00
/**
* Class for setting up the MediaWiki database using SQLLite.
*
2010-07-29 18:36:39 +00:00
* @ingroup Deployment
* @since 1.17
*/
class SqliteInstaller extends DatabaseInstaller {
2010-12-16 11:20:39 +00:00
2011-05-28 18:59:42 +00:00
/**
* @var DatabaseSqlite
*/
public $db;
protected $globalNames = array(
'wgDBname',
'wgSQLiteDataDir',
);
2010-07-20 11:25:36 +00:00
public function getName() {
return 'sqlite';
}
public function isCompiled() {
return self::checkExtension( 'pdo_sqlite' );
}
2010-07-20 11:25:36 +00:00
public function getGlobalDefaults() {
if ( isset( $_SERVER['DOCUMENT_ROOT'] ) ) {
$path = str_replace(
array( '/', '\\' ),
DIRECTORY_SEPARATOR,
dirname( $_SERVER['DOCUMENT_ROOT'] ) . '/data'
);
return array( 'wgSQLiteDataDir' => $path );
} else {
return array();
}
}
2010-07-20 11:25:36 +00:00
public function getConnectForm() {
return $this->getTextBox( 'wgSQLiteDataDir', 'config-sqlite-dir', array(), $this->parent->getHelpBox( 'config-sqlite-dir-help' ) ) .
$this->getTextBox( 'wgDBname', 'config-db-name', array(), $this->parent->getHelpBox( 'config-sqlite-name-help' ) );
}
/**
* Safe wrapper for PHP's realpath() that fails gracefully if it's unable to canonicalize the path.
2011-05-28 18:59:42 +00:00
*
* @param $path string
*
* @return string
*/
private static function realpath( $path ) {
$result = realpath( $path );
if ( !$result ) {
return $path;
}
return $result;
}
2011-05-28 18:59:42 +00:00
/**
* @return Status
*/
2010-07-20 11:25:36 +00:00
public function submitConnectForm() {
$this->setVarsFromRequest( array( 'wgSQLiteDataDir', 'wgDBname' ) );
# Try realpath() if the directory already exists
$dir = self::realpath( $this->getVar( 'wgSQLiteDataDir' ) );
$result = self::dataDirOKmaybeCreate( $dir, true /* create? */ );
2011-05-28 18:59:42 +00:00
if ( $result->isOK() ) {
# Try expanding again in case we've just created it
$dir = self::realpath( $dir );
$this->setVar( 'wgSQLiteDataDir', $dir );
}
return $result;
}
2011-05-28 18:59:42 +00:00
/**
* @param $dir
* @param $create bool
* @return Status
*/
private static function dataDirOKmaybeCreate( $dir, $create = false ) {
if ( !is_dir( $dir ) ) {
if ( !is_writable( dirname( $dir ) ) ) {
$webserverGroup = Installer::maybeGetWebserverPrimaryGroup();
if ( $webserverGroup !== null ) {
return Status::newFatal( 'config-sqlite-parent-unwritable-group', $dir, dirname( $dir ), basename( $dir ), $webserverGroup );
} else {
return Status::newFatal( 'config-sqlite-parent-unwritable-nogroup', $dir, dirname( $dir ), basename( $dir ) );
}
}
# Called early on in the installer, later we just want to sanity check
# if it's still writable
if ( $create ) {
wfSuppressWarnings();
$ok = wfMkdirParents( $dir, 0700, __METHOD__ );
wfRestoreWarnings();
if ( !$ok ) {
return Status::newFatal( 'config-sqlite-mkdir-error', $dir );
}
# Put a .htaccess file in in case the user didn't take our advice
file_put_contents( "$dir/.htaccess", "Deny from all\n" );
}
}
if ( !is_writable( $dir ) ) {
return Status::newFatal( 'config-sqlite-dir-unwritable', $dir );
}
# We haven't blown up yet, fall through
return Status::newGood();
}
2011-05-28 18:59:42 +00:00
/**
* @return Status
*/
PostgreSQL install fixes: * Made PG throw a DBQueryError when it gets a query error, instead of DBUnexpectedError. Apparently this mistake goes back to r14625, when exceptions were first introduced. Did it by removing reportQueryError(), the DatabaseBase version works fine. * Fixed several places where there was an attempt to check for a query error by checking if the result of query() was false. This never worked. Used try/catch instead. * Made the DBConnectionError messages go on one line so that they don't mess up the formatting in the installer. * In DatabasePostgres::selectDB(), only disconnect and reconnect if the DB name is actually changing. * Made DatabasePostgres::schemaExists() less weird and scary. * Added DatabasePostgres::roleExists() for use by the installer. * Removed the PostgreSQL-specific hack to make _InstallUser have a default other than "root". Made _InstallUser into a proper DBMS-specific internal variable instead, since every DBMS we support so far needs a different default. * Removed the $dbName parameters from openConnection/getConnection, and got rid of $this->useAdmin. Implemented a more sophisticated caching scheme instead. Partial revert of r89389 and r81440. * When connecting as the install user before DB creation, and when testing the web user's credentials, try a few different database names and use whichever one works. * Instead of connecting as the web user to create tables, I used SET ROLE. It seems cleaner and more like what the other DBMSes do during installation. "SET ROLE wikiuser" requires the same privileges as "CREATE SCHEMA ... AUTHORIZATION wikiuser", so it's unlikely to break anything. * In the area of web account creation, fixed various minor logic errors and introduced more informative error messages at the submit stage, pre-install. Show a helpful error message if the web user exists already and the install user can't do the relevant SET ROLE. * Split schema creation out to a separate install step. * When creating an account as a non-superuser, add the administrative account to the new account's group. This is necessary to avoid a fatal error during installation (bug 28845). * Removed code which alters an existing web user to have appropriate search paths and permissions. This may break other apps and is not necessary. As in other DBMSes, If the web user exists, it is the responsibility of the sysadmin to ensure that it has appropriate permissions. * Rewrote setupPLpgSQL() to use the query builder functions.
2011-06-10 11:32:57 +00:00
public function openConnection() {
global $wgSQLiteDataDir;
$status = Status::newGood();
* Fixed a bug causing the installer to ignore the "engine" and "charset" settings when installing a MySQL database. * Fixed a bug causing the engine and charset settings to not be properly preserved when adding new tables on upgrade. * Fixed total breakage of SQLite upgrade, by reusing the administrative connection to the SQLite database instead of creating a new one when wfGetDB() is called. Added LBFactory_Single to support this. * Introduced a "schema variable" concept to DatabaseBase to avoid the use of globals for communication between the installer and the Database. Removed a lot of old global variable names from Database::replaceVars(), most were only added on a whim and were never used. * Introduced DatabaseInstaller::getSchemaVars(), to allow schema variables to be supplied by the DatabaseInstaller child classes. * Removed messages config-mysql-egine-mismatch [sic] and config-mysql-charset-mismatch. In the old installer it was possible for users to request a certain character set for an upgrade, but in the new installer the question is never asked. So these warnings were shown whenever a non-default character set or engine was used in the old database. * In MysqlInstaller::preUpgrade(), fixed the incorrect strings used to identify the MySQL character sets: mysql5 instead of utf8 and mysql5-binary instead of binary. * On install, initialise the site_stats table, using code copied from the old installer. Unlike the old installer, use SiteStats to increment the user count when the initial user is added. * Fixed several instances of inappropriate call-by-reference. * Replaced call_user_func_array() with call_user_func() where possible, it is shorter and simpler. * Moved the caching boilerplate for DatabaseInstaller::getConnection() to the base class, and have the derived classes override an uncached function openConnection() instead. Updates r80892. * In MysqlInstaller::getLocalSettings(), escape PHP strings correctly with LocalSettingsGenerator::escapePhpString(). * Reduce timeout for checks in dirIsExecutable() to 3 seconds, so that it doesn't take 30s to run when apache is in single-threaded mode for debugging. * MySQL and SQLite have been tested and they appear to work. PostgreSQL upgrade is totally broken, apparently it was like that before I started. The Oracle code is untested.
2011-01-25 07:37:48 +00:00
$dir = $this->getVar( 'wgSQLiteDataDir' );
$dbName = $this->getVar( 'wgDBname' );
try {
# @todo FIXME: Need more sensible constructor parameters, e.g. single associative array
* Fixed a bug causing the installer to ignore the "engine" and "charset" settings when installing a MySQL database. * Fixed a bug causing the engine and charset settings to not be properly preserved when adding new tables on upgrade. * Fixed total breakage of SQLite upgrade, by reusing the administrative connection to the SQLite database instead of creating a new one when wfGetDB() is called. Added LBFactory_Single to support this. * Introduced a "schema variable" concept to DatabaseBase to avoid the use of globals for communication between the installer and the Database. Removed a lot of old global variable names from Database::replaceVars(), most were only added on a whim and were never used. * Introduced DatabaseInstaller::getSchemaVars(), to allow schema variables to be supplied by the DatabaseInstaller child classes. * Removed messages config-mysql-egine-mismatch [sic] and config-mysql-charset-mismatch. In the old installer it was possible for users to request a certain character set for an upgrade, but in the new installer the question is never asked. So these warnings were shown whenever a non-default character set or engine was used in the old database. * In MysqlInstaller::preUpgrade(), fixed the incorrect strings used to identify the MySQL character sets: mysql5 instead of utf8 and mysql5-binary instead of binary. * On install, initialise the site_stats table, using code copied from the old installer. Unlike the old installer, use SiteStats to increment the user count when the initial user is added. * Fixed several instances of inappropriate call-by-reference. * Replaced call_user_func_array() with call_user_func() where possible, it is shorter and simpler. * Moved the caching boilerplate for DatabaseInstaller::getConnection() to the base class, and have the derived classes override an uncached function openConnection() instead. Updates r80892. * In MysqlInstaller::getLocalSettings(), escape PHP strings correctly with LocalSettingsGenerator::escapePhpString(). * Reduce timeout for checks in dirIsExecutable() to 3 seconds, so that it doesn't take 30s to run when apache is in single-threaded mode for debugging. * MySQL and SQLite have been tested and they appear to work. PostgreSQL upgrade is totally broken, apparently it was like that before I started. The Oracle code is untested.
2011-01-25 07:37:48 +00:00
# Setting globals kind of sucks
$wgSQLiteDataDir = $dir;
$db = new DatabaseSqlite( false, false, false, $dbName );
$status->value = $db;
} catch ( DBConnectionError $e ) {
$status->fatal( 'config-sqlite-connection-error', $e->getMessage() );
}
return $status;
}
2011-05-28 18:59:42 +00:00
/**
* @return bool
*/
2010-07-20 11:25:36 +00:00
public function needsUpgrade() {
$dir = $this->getVar( 'wgSQLiteDataDir' );
$dbName = $this->getVar( 'wgDBname' );
// Don't create the data file yet
if ( !file_exists( DatabaseSqlite::generateFileName( $dir, $dbName ) ) ) {
return false;
}
// If the data file exists, look inside it
return parent::needsUpgrade();
}
2011-05-28 18:59:42 +00:00
/**
* @return Status
*/
2010-07-20 11:25:36 +00:00
public function setupDatabase() {
$dir = $this->getVar( 'wgSQLiteDataDir' );
# Sanity check. We checked this before but maybe someone deleted the
# data dir between then and now
$dir_status = self::dataDirOKmaybeCreate( $dir, false /* create? */ );
if ( !$dir_status->isOK() ) {
return $dir_status;
}
$db = $this->getVar( 'wgDBname' );
$file = DatabaseSqlite::generateFileName( $dir, $db );
if ( file_exists( $file ) ) {
if ( !is_writable( $file ) ) {
return Status::newFatal( 'config-sqlite-readonly', $file );
}
} else {
if ( file_put_contents( $file, '' ) === false ) {
return Status::newFatal( 'config-sqlite-cant-create-db', $file );
}
}
// nuke the unused settings for clarity
$this->setVar( 'wgDBserver', '' );
$this->setVar( 'wgDBuser', '' );
$this->setVar( 'wgDBpassword', '' );
$this->setupSchemaVars();
return $this->getConnection();
}
2011-05-28 18:59:42 +00:00
/**
* @return Staus
*/
2010-07-20 11:25:36 +00:00
public function createTables() {
$status = parent::createTables();
2010-09-01 19:05:52 +00:00
return $this->setupSearchIndex( $status );
}
2011-05-28 18:59:42 +00:00
/**
* @param $status Status
* @return Status
*/
public function setupSearchIndex( &$status ) {
global $IP;
$module = DatabaseSqlite::getFulltextSearchModule();
$fts3tTable = $this->db->checkForEnabledSearch();
if ( $fts3tTable && !$module ) {
$status->warning( 'config-sqlite-fts3-downgrade' );
$this->db->sourceFile( "$IP/maintenance/sqlite/archives/searchindex-no-fts.sql" );
} elseif ( !$fts3tTable && $module == 'FTS3' ) {
$this->db->sourceFile( "$IP/maintenance/sqlite/archives/searchindex-fts3.sql" );
}
return $status;
}
2011-05-28 18:59:42 +00:00
/**
* @return string
*/
2010-07-20 11:25:36 +00:00
public function getLocalSettings() {
$dir = LocalSettingsGenerator::escapePhpString( $this->getVar( 'wgSQLiteDataDir' ) );
return
"# SQLite-specific settings
\$wgSQLiteDataDir = \"{$dir}\";";
}
2010-12-16 11:20:39 +00:00
}