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

332 lines
9 KiB
PHP
Raw Normal View History

<?php
/**
* PostgreSQL-specific installer.
*
* @file
* @ingroup Deployment
*/
2010-12-01 20:22:45 +00:00
2010-07-29 18:36:39 +00:00
/**
* Class for setting up the MediaWiki database using Postgres.
*
2010-07-29 18:36:39 +00:00
* @ingroup Deployment
* @since 1.17
*/
class PostgresInstaller extends DatabaseInstaller {
protected $globalNames = array(
'wgDBserver',
'wgDBport',
'wgDBname',
'wgDBuser',
'wgDBpassword',
'wgDBmwschema',
);
var $minimumVersion = '8.3';
function getName() {
return 'postgres';
}
public function isCompiled() {
return self::checkExtension( 'pgsql' );
}
function getConnectForm() {
return
$this->getTextBox( 'wgDBserver', 'config-db-host', array(), $this->parent->getHelpBox( 'config-db-host-help' ) ) .
$this->getTextBox( 'wgDBport', 'config-db-port' ) .
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( 'wgDBmwschema', 'config-db-schema', array(), $this->parent->getHelpBox( 'config-db-schema-help' ) ) .
Html::closeElement( 'fieldset' ) .
$this->getInstallUserBox();
}
function submitConnectForm() {
// Get variables from the request
$newValues = $this->setVarsFromRequest( array( 'wgDBserver', 'wgDBport',
'wgDBname', 'wgDBmwschema' ) );
// Validate them
$status = Status::newGood();
if ( !strlen( $newValues['wgDBname'] ) ) {
$status->fatal( 'config-missing-db-name' );
} elseif ( !preg_match( '/^[a-zA-Z0-9_]+$/', $newValues['wgDBname'] ) ) {
$status->fatal( 'config-invalid-db-name', $newValues['wgDBname'] );
}
if ( !preg_match( '/^[a-zA-Z0-9_]*$/', $newValues['wgDBmwschema'] ) ) {
$status->fatal( 'config-invalid-schema', $newValues['wgDBmwschema'] );
}
// Submit user box
if ( $status->isOK() ) {
$status->merge( $this->submitInstallUserBox() );
}
if ( !$status->isOK() ) {
return $status;
}
// Try to connect
2010-10-17 19:35:13 +00:00
$status->merge( $this->getConnection() );
if ( !$status->isOK() ) {
return $status;
}
/* //Make sure install user can create
$status->merge( $this->canCreateAccounts() );
if ( !$status->isOK() ) {
return $status;
} */
// Check version
$version = $this->db->getServerVersion();
if ( version_compare( $version, $this->minimumVersion ) < 0 ) {
return Status::newFatal( 'config-postgres-old', $this->minimumVersion, $version );
}
$this->setVar( 'wgDBuser', $this->getVar( '_InstallUser' ) );
$this->setVar( 'wgDBpassword', $this->getVar( '_InstallPassword' ) );
return $status;
}
* 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
function openConnection( $database = 'template1' ) {
$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
try {
$db = new DatabasePostgres(
$this->getVar( 'wgDBserver' ),
$this->getVar( '_InstallUser' ),
$this->getVar( '_InstallPassword' ),
$database );
$status->value = $db;
} catch ( DBConnectionError $e ) {
$status->fatal( 'config-connection-error', $e->getMessage() );
}
return $status;
}
protected function canCreateAccounts() {
$status = $this->getConnection();
if ( !$status->isOK() ) {
return false;
}
$conn = $status->value;
$superuser = $this->getVar( '_InstallUser' );
$rights = $conn->selectField( 'pg_catalog.pg_user',
'CASE WHEN usesuper IS TRUE THEN
CASE WHEN usecreatedb IS TRUE THEN 3 ELSE 1 END
ELSE CASE WHEN usecreatedb IS TRUE THEN 2 ELSE 0 END
END AS rights',
array( 'usename' => $superuser ), __METHOD__
);
if( !$rights ) {
return false;
}
if( $rights != 1 && $rights != 3 ) {
return false;
}
return true;
}
public function getSettingsForm() {
if ( $this->canCreateAccounts() ) {
$noCreateMsg = false;
} else {
$noCreateMsg = 'config-db-web-no-create-privs';
}
$s = $this->getWebUserBox( $noCreateMsg );
return $s;
}
public function submitSettingsForm() {
$status = $this->submitWebUserBox();
if ( !$status->isOK() ) {
return $status;
}
// Validate the create checkbox
$canCreate = $this->canCreateAccounts();
if ( !$canCreate ) {
$this->setVar( '_CreateDBAccount', false );
$create = false;
} else {
$create = $this->getVar( '_CreateDBAccount' );
}
if ( !$create ) {
// Test the web account
try {
new DatabasePostgres(
$this->getVar( 'wgDBserver' ),
$this->getVar( 'wgDBuser' ),
$this->getVar( 'wgDBpassword' ),
false,
false,
0,
$this->getVar( 'wgDBprefix' )
);
} catch ( DBConnectionError $e ) {
return Status::newFatal( 'config-connection-error', $e->getMessage() );
}
}
return Status::newGood();
}
public function preInstall() {
$commitCB = array(
'name' => 'pg-commit',
'callback' => array( $this, 'commitChanges' ),
);
$userCB = array(
'name' => 'user',
'callback' => array( $this, 'setupUser' ),
);
$plpgCB = array(
'name' => 'pg-plpgsql',
'callback' => array( $this, 'setupPLpgSQL' ),
);
$this->parent->addInstallStep( $commitCB, 'interwiki' );
$this->parent->addInstallStep( $userCB );
$this->parent->addInstallStep( $plpgCB, 'database' );
}
function setupDatabase() {
$status = $this->getConnection();
if ( !$status->isOK() ) {
return $status;
}
$this->setupSchemaVars();
$conn = $status->value;
$dbName = $this->getVar( 'wgDBname' );
2011-01-12 23:01:47 +00:00
$SQL = "SELECT 1 FROM pg_catalog.pg_database WHERE datname = " . $conn->addQuotes( $dbName );
$rows = $conn->numRows( $conn->query( $SQL ) );
if( !$rows ) {
$schema = $this->getVar( 'wgDBmwschema' );
$user = $this->getVar( 'wgDBuser' );
$safeschema = $conn->addIdentifierQuotes( $schema );
$safeuser = $conn->addIdentifierQuotes( $user );
$safedb = $conn->addIdentifierQuotes( $dbName );
$conn->query( "CREATE DATABASE $safedb OWNER $safeuser", __METHOD__ );
$conn = new DatabasePostgres(
2011-01-12 23:01:47 +00:00
$this->getVar( 'wgDBserver' ),
$this->getVar( 'wgDBuser' ),
$this->getVar( 'wgDBpassword' ),
$dbName,
false,
0,
$this->getVar( 'wgDBprefix' )
);
$result = $conn->schemaExists( $schema );
if( !$result ) {
2011-01-11 16:02:56 +00:00
$result = $conn->query( "CREATE SCHEMA $safeschema AUTHORIZATION $safeuser" );
if( !$result ) {
$status->fatal( 'config-install-pg-schema-failed', $user, $schema );
}
} else {
$safeschema2 = $conn->addQuotes( $schema );
2011-01-12 23:01:47 +00:00
$SQL = "SELECT 'GRANT ALL ON '||pg_catalog.quote_ident(relname)||' TO $safeuser;'\n".
"FROM pg_catalog.pg_class p, pg_catalog.pg_namespace n\n" .
"WHERE relnamespace = n.oid AND n.nspname = $safeschema2\n" .
"AND p.relkind IN ('r','S','v')\n";
$SQL .= "UNION\n";
$SQL .= "SELECT 'GRANT ALL ON FUNCTION '||pg_catalog.quote_ident(proname)||'('||\n".
"pg_catalog.oidvectortypes(p.proargtypes)||') TO $safeuser;'\n" .
"FROM pg_catalog.pg_proc p, pg_catalog.pg_namespace n\n" .
"WHERE p.pronamespace = n.oid AND n.nspname = $safeschema2";
$res = $conn->query( $SQL );
2011-01-11 16:02:56 +00:00
$conn->query( "SET search_path = $safeschema" );
}
}
return $status;
}
function commitChanges() {
$this->db->query( 'COMMIT' );
return Status::newGood();
}
function setupUser() {
if ( !$this->getVar( '_CreateDBAccount' ) ) {
return Status::newGood();
}
$status = $this->getConnection();
if ( !$status->isOK() ) {
return $status;
}
$db = $this->getVar( 'wgDBname' );
$this->db->selectDB( $db );
$safeuser = $this->db->addIdentifierQuotes( $this->getVar( 'wgDBuser' ) );
$safepass = $this->db->addQuotes( $this->getVar( 'wgDBpassword' ) );
$res = $this->db->query( "CREATE USER $safeuser NOCREATEDB PASSWORD $safepass", __METHOD__ );
return $status;
if ( $res !== true ) {
$status->fatal( 'config-install-user-failed', $this->getVar( 'wgDBuser' ) );
}
return $status;
}
function getLocalSettings() {
$port = $this->getVar( 'wgDBport' );
$schema = $this->getVar( 'wgDBmwschema' );
return
"# Postgres specific settings
\$wgDBport = \"{$port}\";
\$wgDBmwschema = \"{$schema}\";";
}
public function preUpgrade() {
global $wgDBuser, $wgDBpassword;
# Normal user and password are selected after this step, so for now
# just copy these two
$wgDBuser = $this->getVar( '_InstallUser' );
$wgDBpassword = $this->getVar( '_InstallPassword' );
}
public function setupPLpgSQL() {
$status = $this->getConnection();
if ( !$status->isOK() ) {
return $status;
}
$rows = $this->db->numRows(
$this->db->query( "SELECT 1 FROM pg_catalog.pg_language WHERE lanname = 'plpgsql'" )
);
if ( $rows < 1 ) {
// plpgsql is not installed, but if we have a pg_pltemplate table, we should be able to create it
$SQL = "SELECT 1 FROM pg_catalog.pg_class c JOIN pg_catalog.pg_namespace n ON (n.oid = c.relnamespace) ".
"WHERE relname = 'pg_pltemplate' AND nspname='pg_catalog'";
$rows = $this->db->numRows( $this->db->query( $SQL ) );
$dbName = $this->getVar( 'wgDBname' );
if ( $rows >= 1 ) {
$result = $this->db->query( 'CREATE LANGUAGE plpgsql' );
if ( !$result ) {
return Status::newFatal( 'config-pg-no-plpgsql', $dbName );
}
} else {
return Status::newFatal( 'config-pg-no-plpgsql', $dbName );
}
}
return Status::newGood();
}
}