2010-05-07 12:25:01 +00:00
|
|
|
<?php
|
2010-08-21 18:20:09 +00:00
|
|
|
/**
|
|
|
|
|
* 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-12-06 20:04:28 +00:00
|
|
|
*
|
2010-07-29 18:36:39 +00:00
|
|
|
* @ingroup Deployment
|
|
|
|
|
* @since 1.17
|
|
|
|
|
*/
|
2010-07-20 09:30:33 +00:00
|
|
|
class PostgresInstaller extends DatabaseInstaller {
|
2010-05-07 12:25:01 +00:00
|
|
|
|
|
|
|
|
protected $globalNames = array(
|
|
|
|
|
'wgDBserver',
|
|
|
|
|
'wgDBport',
|
|
|
|
|
'wgDBname',
|
|
|
|
|
'wgDBuser',
|
|
|
|
|
'wgDBpassword',
|
|
|
|
|
'wgDBmwschema',
|
|
|
|
|
);
|
|
|
|
|
|
2011-01-28 14:12:26 +00:00
|
|
|
var $minimumVersion = '8.3';
|
2011-02-03 20:40:20 +00:00
|
|
|
private $useAdmin = false;
|
2010-05-07 12:25:01 +00:00
|
|
|
|
|
|
|
|
function getName() {
|
|
|
|
|
return 'postgres';
|
|
|
|
|
}
|
|
|
|
|
|
2010-07-07 13:52:05 +00:00
|
|
|
public function isCompiled() {
|
2010-07-07 02:53:19 +00:00
|
|
|
return self::checkExtension( 'pgsql' );
|
2010-05-07 12:25:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function getConnectForm() {
|
2011-03-21 14:59:14 +00:00
|
|
|
// If this is our first time here, switch the default user presented in the form
|
|
|
|
|
if ( ! $this->getVar('_switchedInstallUser') ) {
|
|
|
|
|
$this->setVar('_InstallUser', 'postgres');
|
|
|
|
|
$this->setVar('_switchedInstallUser', true);
|
|
|
|
|
}
|
2010-05-07 12:25:01 +00:00
|
|
|
return
|
2010-12-06 20:04:28 +00:00
|
|
|
$this->getTextBox( 'wgDBserver', 'config-db-host', array(), $this->parent->getHelpBox( 'config-db-host-help' ) ) .
|
2010-05-07 12:25:01 +00:00
|
|
|
$this->getTextBox( 'wgDBport', 'config-db-port' ) .
|
2010-11-04 23:21:24 +00:00
|
|
|
Html::openElement( 'fieldset' ) .
|
|
|
|
|
Html::element( 'legend', array(), wfMsg( 'config-db-wiki-settings' ) ) .
|
2010-12-06 20:04:28 +00:00
|
|
|
$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' ) ) .
|
2010-11-04 23:21:24 +00:00
|
|
|
Html::closeElement( 'fieldset' ) .
|
2010-05-07 12:25:01 +00:00
|
|
|
$this->getInstallUserBox();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function submitConnectForm() {
|
|
|
|
|
// Get variables from the request
|
2010-12-06 20:04:28 +00:00
|
|
|
$newValues = $this->setVarsFromRequest( array( 'wgDBserver', 'wgDBport',
|
2011-01-28 14:12:26 +00:00
|
|
|
'wgDBname', 'wgDBmwschema' ) );
|
2010-05-07 12:25:01 +00:00
|
|
|
|
|
|
|
|
// 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;
|
|
|
|
|
}
|
|
|
|
|
|
2011-02-03 20:40:20 +00:00
|
|
|
$this->useAdmin = true;
|
2010-05-07 12:25:01 +00:00
|
|
|
// Try to connect
|
2011-02-03 05:17:18 +00:00
|
|
|
$status->merge( $this->getConnection() );
|
2010-05-07 12:25:01 +00:00
|
|
|
if ( !$status->isOK() ) {
|
|
|
|
|
return $status;
|
|
|
|
|
}
|
|
|
|
|
|
2011-02-03 05:17:18 +00:00
|
|
|
//Make sure install user can create
|
2011-02-03 20:40:20 +00:00
|
|
|
if( !$this->canCreateAccounts() ) {
|
|
|
|
|
$status->fatal( 'config-pg-no-create-privs' );
|
|
|
|
|
}
|
2011-01-11 03:07:45 +00:00
|
|
|
if ( !$status->isOK() ) {
|
|
|
|
|
return $status;
|
2011-02-03 05:17:18 +00:00
|
|
|
}
|
2011-01-11 03:07:45 +00:00
|
|
|
|
2010-05-07 12:25:01 +00:00
|
|
|
// Check version
|
2010-08-17 03:07:27 +00:00
|
|
|
$version = $this->db->getServerVersion();
|
2010-05-07 12:25:01 +00:00
|
|
|
if ( version_compare( $version, $this->minimumVersion ) < 0 ) {
|
|
|
|
|
return Status::newFatal( 'config-postgres-old', $this->minimumVersion, $version );
|
|
|
|
|
}
|
2010-08-17 03:07:27 +00:00
|
|
|
|
|
|
|
|
$this->setVar( 'wgDBuser', $this->getVar( '_InstallUser' ) );
|
|
|
|
|
$this->setVar( 'wgDBpassword', $this->getVar( '_InstallPassword' ) );
|
2010-05-07 12:25:01 +00:00
|
|
|
return $status;
|
|
|
|
|
}
|
|
|
|
|
|
2011-06-03 03:41:11 +00:00
|
|
|
public function openConnection( $dbName = null ) {
|
2010-05-07 12:25:01 +00:00
|
|
|
$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 {
|
2011-02-03 05:17:18 +00:00
|
|
|
if ( $this->useAdmin ) {
|
2011-06-03 03:41:11 +00:00
|
|
|
if ( $dbName === null ) $dbName = 'postgres';
|
|
|
|
|
|
2011-02-03 05:17:18 +00:00
|
|
|
$db = new DatabasePostgres(
|
|
|
|
|
$this->getVar( 'wgDBserver' ),
|
|
|
|
|
$this->getVar( '_InstallUser' ),
|
|
|
|
|
$this->getVar( '_InstallPassword' ),
|
2011-06-03 03:41:11 +00:00
|
|
|
$dbName );
|
2011-02-03 05:17:18 +00:00
|
|
|
} else {
|
2011-06-03 03:41:11 +00:00
|
|
|
if ( $dbName === null ) $dbName = $this->getVar( 'wgDBname' );
|
|
|
|
|
|
2011-02-03 05:17:18 +00:00
|
|
|
$db = new DatabasePostgres(
|
|
|
|
|
$this->getVar( 'wgDBserver' ),
|
|
|
|
|
$this->getVar( 'wgDBuser' ),
|
|
|
|
|
$this->getVar( 'wgDBpassword' ),
|
2011-06-03 03:41:11 +00:00
|
|
|
$dbName );
|
2011-02-03 05:17:18 +00:00
|
|
|
}
|
2011-06-03 03:41:11 +00:00
|
|
|
|
|
|
|
|
if( $db === null ) throw new DBConnectionError("Unknown problem while connecting.");
|
|
|
|
|
$safeschema = $db->addIdentifierQuotes( $this->getVar( 'wgDBmwschema' ) );
|
|
|
|
|
if( $db->schemaExists( $this->getVar( 'wgDBmwschema' ) ) ) $db->query( "SET search_path = $safeschema" );
|
|
|
|
|
|
* 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
|
|
|
$status->value = $db;
|
|
|
|
|
} catch ( DBConnectionError $e ) {
|
|
|
|
|
$status->fatal( 'config-connection-error', $e->getMessage() );
|
2010-05-07 12:25:01 +00:00
|
|
|
}
|
|
|
|
|
return $status;
|
|
|
|
|
}
|
|
|
|
|
|
2011-01-07 18:24:56 +00:00
|
|
|
protected function canCreateAccounts() {
|
2011-02-03 20:40:20 +00:00
|
|
|
$this->useAdmin = true;
|
2011-02-03 05:17:18 +00:00
|
|
|
$status = $this->getConnection();
|
2011-01-07 18:24:56 +00:00
|
|
|
if ( !$status->isOK() ) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
$conn = $status->value;
|
|
|
|
|
|
|
|
|
|
$superuser = $this->getVar( '_InstallUser' );
|
|
|
|
|
|
2011-06-03 03:41:11 +00:00
|
|
|
$rights = $conn->selectField( 'pg_catalog.pg_roles',
|
|
|
|
|
'CASE WHEN rolsuper then 1
|
|
|
|
|
WHEN rolcreatedb then 2
|
|
|
|
|
ELSE 3
|
|
|
|
|
END as rights',
|
|
|
|
|
array( 'rolname' => $superuser ), __METHOD__
|
2011-01-07 18:24:56 +00:00
|
|
|
);
|
|
|
|
|
|
2011-06-03 03:41:11 +00:00
|
|
|
if( !$rights || $rights == 3 ) {
|
2011-02-04 00:21:59 +00:00
|
|
|
return false;
|
2011-01-07 18:24:56 +00:00
|
|
|
}
|
|
|
|
|
|
2011-02-04 00:21:59 +00:00
|
|
|
return true;
|
2011-01-07 18:24:56 +00:00
|
|
|
}
|
|
|
|
|
|
2011-01-10 19:18:28 +00:00
|
|
|
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
|
2011-02-23 14:26:45 +00:00
|
|
|
if ( !$this->canCreateAccounts() ) {
|
2011-01-10 19:18:28 +00:00
|
|
|
$this->setVar( '_CreateDBAccount', false );
|
|
|
|
|
$create = false;
|
|
|
|
|
} else {
|
|
|
|
|
$create = $this->getVar( '_CreateDBAccount' );
|
|
|
|
|
}
|
|
|
|
|
|
2011-02-04 00:15:44 +00:00
|
|
|
// Don't test the web account if it is the same as the admin.
|
|
|
|
|
if ( !$create && $this->getVar( 'wgDBuser' ) != $this->getVar( '_InstallUser' ) ) {
|
2011-01-10 19:18:28 +00:00
|
|
|
// Test the web account
|
|
|
|
|
try {
|
2011-02-04 20:54:43 +00:00
|
|
|
$this->useAdmin = false;
|
2011-02-04 00:15:44 +00:00
|
|
|
return $this->openConnection();
|
2011-01-10 19:18:28 +00:00
|
|
|
} catch ( DBConnectionError $e ) {
|
|
|
|
|
return Status::newFatal( 'config-connection-error', $e->getMessage() );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return Status::newGood();
|
|
|
|
|
}
|
|
|
|
|
|
2010-11-10 16:06:16 +00:00
|
|
|
public function preInstall() {
|
2011-01-07 18:24:56 +00:00
|
|
|
$commitCB = array(
|
2010-11-10 16:06:16 +00:00
|
|
|
'name' => 'pg-commit',
|
|
|
|
|
'callback' => array( $this, 'commitChanges' ),
|
|
|
|
|
);
|
2011-01-11 15:02:59 +00:00
|
|
|
$plpgCB = array(
|
|
|
|
|
'name' => 'pg-plpgsql',
|
|
|
|
|
'callback' => array( $this, 'setupPLpgSQL' ),
|
|
|
|
|
);
|
2011-01-07 18:24:56 +00:00
|
|
|
$this->parent->addInstallStep( $commitCB, 'interwiki' );
|
2011-01-11 15:02:59 +00:00
|
|
|
$this->parent->addInstallStep( $plpgCB, 'database' );
|
2011-02-23 16:01:22 +00:00
|
|
|
if( $this->getVar( '_CreateDBAccount' ) ) {
|
|
|
|
|
$this->parent->addInstallStep( array(
|
|
|
|
|
'name' => 'user',
|
|
|
|
|
'callback' => array( $this, 'setupUser' ),
|
|
|
|
|
) );
|
|
|
|
|
}
|
2010-11-10 16:06:16 +00:00
|
|
|
}
|
|
|
|
|
|
2010-05-07 12:25:01 +00:00
|
|
|
function setupDatabase() {
|
2011-02-04 20:54:43 +00:00
|
|
|
$this->useAdmin = true;
|
2011-02-03 05:17:18 +00:00
|
|
|
$status = $this->getConnection();
|
2010-11-10 16:06:16 +00:00
|
|
|
if ( !$status->isOK() ) {
|
|
|
|
|
return $status;
|
|
|
|
|
}
|
2011-01-27 08:25:48 +00:00
|
|
|
$this->setupSchemaVars();
|
2010-11-10 16:06:16 +00:00
|
|
|
$conn = $status->value;
|
|
|
|
|
|
2011-01-07 18:24:56 +00:00
|
|
|
$dbName = $this->getVar( 'wgDBname' );
|
2011-02-23 16:01:22 +00:00
|
|
|
$schema = $this->getVar( 'wgDBmwschema' );
|
|
|
|
|
$user = $this->getVar( 'wgDBuser' );
|
|
|
|
|
$safeschema = $conn->addIdentifierQuotes( $schema );
|
|
|
|
|
$safeuser = $conn->addIdentifierQuotes( $user );
|
|
|
|
|
|
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 ) );
|
2011-03-21 21:36:21 +00:00
|
|
|
$safedb = $conn->addIdentifierQuotes( $dbName );
|
2011-01-11 03:07:45 +00:00
|
|
|
if( !$rows ) {
|
2011-06-03 03:41:11 +00:00
|
|
|
$conn->query( "CREATE DATABASE $safedb", __METHOD__ );
|
|
|
|
|
$conn->query( "GRANT ALL ON DATABASE $safedb to $safeuser", __METHOD__ );
|
2011-02-23 16:01:22 +00:00
|
|
|
} else {
|
2011-06-03 03:41:11 +00:00
|
|
|
$conn->query( "GRANT ALL ON DATABASE $safedb TO $safeuser", __METHOD__ );
|
2011-02-23 16:01:22 +00:00
|
|
|
}
|
2011-05-20 18:20:16 +00:00
|
|
|
|
2011-03-21 22:09:49 +00:00
|
|
|
// Now that we've established the real database exists, connect to it
|
|
|
|
|
// Because we do not want the same connection, forcibly expire the existing conn
|
|
|
|
|
$this->db = null;
|
2011-02-23 16:01:22 +00:00
|
|
|
$this->useAdmin = false;
|
2011-03-21 22:09:49 +00:00
|
|
|
$status = $this->getConnection();
|
|
|
|
|
if ( !$status->isOK() ) {
|
|
|
|
|
return $status;
|
|
|
|
|
}
|
2011-02-23 16:01:22 +00:00
|
|
|
$conn = $status->value;
|
2011-01-11 03:07:45 +00:00
|
|
|
|
2011-02-23 16:01:22 +00:00
|
|
|
if( !$conn->schemaExists( $schema ) ) {
|
|
|
|
|
$result = $conn->query( "CREATE SCHEMA $safeschema AUTHORIZATION $safeuser" );
|
2011-01-11 03:07:45 +00:00
|
|
|
if( !$result ) {
|
2011-02-23 16:01:22 +00:00
|
|
|
$status->fatal( 'config-install-pg-schema-failed', $user, $schema );
|
2011-01-11 03:07:45 +00:00
|
|
|
}
|
2011-02-23 16:01:22 +00:00
|
|
|
} else {
|
|
|
|
|
$safeschema2 = $conn->addQuotes( $schema );
|
|
|
|
|
$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";
|
|
|
|
|
$conn->query( "SET search_path = $safeschema" );
|
|
|
|
|
$res = $conn->query( $SQL );
|
2011-01-07 18:24:56 +00:00
|
|
|
}
|
2010-11-10 16:06:16 +00:00
|
|
|
return $status;
|
|
|
|
|
}
|
|
|
|
|
|
2010-12-01 20:15:45 +00:00
|
|
|
function commitChanges() {
|
2010-11-10 16:06:16 +00:00
|
|
|
$this->db->query( 'COMMIT' );
|
2010-12-01 20:15:45 +00:00
|
|
|
return Status::newGood();
|
2010-05-07 12:25:01 +00:00
|
|
|
}
|
|
|
|
|
|
2011-01-07 18:24:56 +00:00
|
|
|
function setupUser() {
|
|
|
|
|
if ( !$this->getVar( '_CreateDBAccount' ) ) {
|
|
|
|
|
return Status::newGood();
|
|
|
|
|
}
|
|
|
|
|
|
2011-02-04 21:00:17 +00:00
|
|
|
$this->useAdmin = true;
|
2011-02-03 05:17:18 +00:00
|
|
|
$status = $this->getConnection();
|
|
|
|
|
|
2011-01-07 18:24:56 +00:00
|
|
|
if ( !$status->isOK() ) {
|
|
|
|
|
return $status;
|
|
|
|
|
}
|
|
|
|
|
|
2011-02-03 05:17:18 +00:00
|
|
|
$schema = $this->getVar( 'wgDBmwschema' );
|
2011-01-11 03:07:45 +00:00
|
|
|
$safeuser = $this->db->addIdentifierQuotes( $this->getVar( 'wgDBuser' ) );
|
2011-02-03 04:06:11 +00:00
|
|
|
$safeusercheck = $this->db->addQuotes( $this->getVar( 'wgDBuser' ) );
|
2011-01-07 18:24:56 +00:00
|
|
|
$safepass = $this->db->addQuotes( $this->getVar( 'wgDBpassword' ) );
|
2011-02-03 05:17:18 +00:00
|
|
|
$safeschema = $this->db->addIdentifierQuotes( $schema );
|
2011-02-03 04:06:11 +00:00
|
|
|
|
|
|
|
|
$rows = $this->db->numRows(
|
2011-06-03 03:41:11 +00:00
|
|
|
$this->db->query( "SELECT 1 FROM pg_catalog.pg_roles WHERE rolname = $safeusercheck" )
|
2011-02-03 04:06:11 +00:00
|
|
|
);
|
|
|
|
|
if ( $rows < 1 ) {
|
2011-06-03 03:41:11 +00:00
|
|
|
$res = $this->db->query( "CREATE ROLE $safeuser NOCREATEDB LOGIN PASSWORD $safepass", __METHOD__ );
|
2011-02-03 04:06:11 +00:00
|
|
|
if ( $res !== true && !( $res instanceOf ResultWrapper ) ) {
|
|
|
|
|
$status->fatal( 'config-install-user-failed', $this->getVar( 'wgDBuser' ), $res );
|
2011-02-03 05:17:18 +00:00
|
|
|
}
|
2011-02-23 16:01:22 +00:00
|
|
|
if( $status->isOK() ) {
|
2011-06-03 03:41:11 +00:00
|
|
|
$this->db->query("ALTER ROLE $safeuser LOGIN");
|
2011-02-23 16:01:22 +00:00
|
|
|
}
|
2011-01-07 18:24:56 +00:00
|
|
|
}
|
2011-06-03 03:41:11 +00:00
|
|
|
$this->db->query("ALTER ROLE $safeuser SET search_path = $safeschema, public");
|
2011-01-07 18:24:56 +00:00
|
|
|
|
|
|
|
|
return $status;
|
|
|
|
|
}
|
|
|
|
|
|
2010-05-07 12:25:01 +00:00
|
|
|
function getLocalSettings() {
|
|
|
|
|
$port = $this->getVar( 'wgDBport' );
|
|
|
|
|
$schema = $this->getVar( 'wgDBmwschema' );
|
|
|
|
|
return
|
|
|
|
|
"# Postgres specific settings
|
|
|
|
|
\$wgDBport = \"{$port}\";
|
2011-01-28 15:25:15 +00:00
|
|
|
\$wgDBmwschema = \"{$schema}\";";
|
2010-05-07 12:25:01 +00:00
|
|
|
}
|
2010-08-22 10:37:27 +00:00
|
|
|
|
2010-09-07 20:57:53 +00:00
|
|
|
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' );
|
2010-08-22 10:37:27 +00:00
|
|
|
}
|
2010-12-27 15:41:11 +00:00
|
|
|
|
2011-02-03 20:47:54 +00:00
|
|
|
public function createTables() {
|
2011-02-06 03:06:45 +00:00
|
|
|
$schema = $this->getVar( 'wgDBmwschema' );
|
|
|
|
|
|
2011-02-03 20:40:20 +00:00
|
|
|
$this->db = null;
|
|
|
|
|
$this->useAdmin = false;
|
2011-02-03 20:47:54 +00:00
|
|
|
$status = $this->getConnection();
|
|
|
|
|
if ( !$status->isOK() ) {
|
|
|
|
|
return $status;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if( $this->db->tableExists( 'user' ) ) {
|
|
|
|
|
$status->warning( 'config-install-tables-exist' );
|
|
|
|
|
return $status;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$this->db->begin( __METHOD__ );
|
|
|
|
|
|
2011-06-03 03:41:11 +00:00
|
|
|
// getConnection() should have already selected the schema if it exists
|
2011-02-06 03:06:45 +00:00
|
|
|
if( !$this->db->schemaExists( $schema ) ) {
|
|
|
|
|
$status->error( 'config-install-pg-schema-not-exist' );
|
|
|
|
|
return $status;
|
|
|
|
|
}
|
2011-02-03 20:47:54 +00:00
|
|
|
$error = $this->db->sourceFile( $this->db->getSchema() );
|
|
|
|
|
if( $error !== true ) {
|
|
|
|
|
$this->db->reportQueryError( $error, 0, '', __METHOD__ );
|
|
|
|
|
$this->db->rollback( __METHOD__ );
|
|
|
|
|
$status->fatal( 'config-install-tables-failed', $error );
|
|
|
|
|
} else {
|
|
|
|
|
$this->db->commit( __METHOD__ );
|
|
|
|
|
}
|
|
|
|
|
// Resume normal operations
|
|
|
|
|
if( $status->isOk() ) {
|
|
|
|
|
$this->enableLB();
|
|
|
|
|
}
|
|
|
|
|
return $status;
|
|
|
|
|
}
|
2011-02-03 05:17:18 +00:00
|
|
|
|
2011-01-11 15:02:59 +00:00
|
|
|
public function setupPLpgSQL() {
|
2011-06-03 03:41:11 +00:00
|
|
|
$this->db = null;
|
2011-02-04 21:00:17 +00:00
|
|
|
$this->useAdmin = true;
|
2011-06-03 03:41:11 +00:00
|
|
|
$dbName = $this->getVar( 'wgDBname' );
|
|
|
|
|
$status = $this->getConnection( $dbName );
|
2011-01-11 15:02:59 +00:00
|
|
|
if ( !$status->isOK() ) {
|
|
|
|
|
return $status;
|
|
|
|
|
}
|
2011-06-03 03:41:11 +00:00
|
|
|
$this->db = $status->value;
|
2011-01-11 15:02:59 +00:00
|
|
|
|
2011-06-03 03:41:11 +00:00
|
|
|
/* Admin user has to be connected to the db it just
|
|
|
|
|
created to satisfy ownership requirements for
|
|
|
|
|
"CREATE LANGAUGE" */
|
2011-01-11 15:02:59 +00:00
|
|
|
$rows = $this->db->numRows(
|
2010-12-27 15:41:11 +00:00
|
|
|
$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'";
|
2011-01-11 15:02:59 +00:00
|
|
|
$rows = $this->db->numRows( $this->db->query( $SQL ) );
|
2010-12-27 15:41:11 +00:00
|
|
|
if ( $rows >= 1 ) {
|
|
|
|
|
$result = $this->db->query( 'CREATE LANGUAGE plpgsql' );
|
|
|
|
|
if ( !$result ) {
|
2011-01-11 15:02:59 +00:00
|
|
|
return Status::newFatal( 'config-pg-no-plpgsql', $dbName );
|
2010-12-27 15:41:11 +00:00
|
|
|
}
|
|
|
|
|
} else {
|
2011-01-11 15:02:59 +00:00
|
|
|
return Status::newFatal( 'config-pg-no-plpgsql', $dbName );
|
2010-12-27 15:41:11 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return Status::newGood();
|
|
|
|
|
}
|
2010-05-07 12:25:01 +00:00
|
|
|
}
|