From 9be60e572542e97afc3129df6a4453d22da97336 Mon Sep 17 00:00:00 2001 From: "Mark A. Hershberger" Date: Fri, 3 Jun 2011 03:41:11 +0000 Subject: [PATCH] forward port r88929 --- includes/installer/DatabaseInstaller.php | 11 +++-- includes/installer/Ibm_db2Installer.php | 2 +- includes/installer/Installer.i18n.php | 2 +- includes/installer/MysqlInstaller.php | 2 +- includes/installer/OracleInstaller.php | 2 +- includes/installer/PostgresInstaller.php | 53 +++++++++++++++--------- includes/installer/PostgresUpdater.php | 1 - includes/installer/SqliteInstaller.php | 2 +- 8 files changed, 46 insertions(+), 29 deletions(-) diff --git a/includes/installer/DatabaseInstaller.php b/includes/installer/DatabaseInstaller.php index 7ea5296357..5045351024 100644 --- a/includes/installer/DatabaseInstaller.php +++ b/includes/installer/DatabaseInstaller.php @@ -102,7 +102,7 @@ abstract class DatabaseInstaller { * * @return Status */ - public abstract function openConnection(); + public abstract function openConnection( $dbName = null ); /** * Create the database and return a Status object indicating success or @@ -121,11 +121,14 @@ abstract class DatabaseInstaller { * * @return Status */ - public function getConnection() { - if ( $this->db ) { + public function getConnection( $dbName = null ) { + if ( isset($this->db) && $this->db ) { /* Weirdly get E_STRICT + * errors without the + * isset */ return Status::newGood( $this->db ); } - $status = $this->openConnection(); + + $status = $this->openConnection( $dbName ); if ( $status->isOK() ) { $this->db = $status->value; // Enable autocommit diff --git a/includes/installer/Ibm_db2Installer.php b/includes/installer/Ibm_db2Installer.php index dabbe9105b..c817aa1346 100644 --- a/includes/installer/Ibm_db2Installer.php +++ b/includes/installer/Ibm_db2Installer.php @@ -113,7 +113,7 @@ class Ibm_db2Installer extends DatabaseInstaller { * Open a DB2 database connection * @return Status */ - public function openConnection() { + public function openConnection( $dbName = null ) { $status = Status::newGood(); try { $db = new DatabaseIbm_db2( diff --git a/includes/installer/Installer.i18n.php b/includes/installer/Installer.i18n.php index 586fdea87f..9ad5ec1814 100644 --- a/includes/installer/Installer.i18n.php +++ b/includes/installer/Installer.i18n.php @@ -256,7 +256,7 @@ Use only ASCII letters (a-z, A-Z), numbers (0-9), underscores (_) and hyphens (- Use only ASCII letters (a-z, A-Z), numbers (0-9), underscores (_) and hyphens (-).', 'config-connection-error' => '$1. -Check the host, username and password below and try again.', +Check the host, username and password and try again.', 'config-invalid-schema' => 'Invalid schema for MediaWiki "$1". Use only ASCII letters (a-z, A-Z), numbers (0-9) and underscores (_).', 'config-db-sys-create-oracle' => 'Installer only supports using a SYSDBA account for creating a new account.', diff --git a/includes/installer/MysqlInstaller.php b/includes/installer/MysqlInstaller.php index e049ac245e..6603a0ceb5 100644 --- a/includes/installer/MysqlInstaller.php +++ b/includes/installer/MysqlInstaller.php @@ -126,7 +126,7 @@ class MysqlInstaller extends DatabaseInstaller { /** * @return Status */ - public function openConnection() { + public function openConnection( $dbName = null ) { $status = Status::newGood(); try { $db = new DatabaseMysql( diff --git a/includes/installer/OracleInstaller.php b/includes/installer/OracleInstaller.php index d11841087d..6b3f78eb1e 100644 --- a/includes/installer/OracleInstaller.php +++ b/includes/installer/OracleInstaller.php @@ -127,7 +127,7 @@ class OracleInstaller extends DatabaseInstaller { return $status; } - public function openConnection() { + public function openConnection( $dbName = null ) { $status = Status::newGood(); try { $db = new DatabaseOracle( diff --git a/includes/installer/PostgresInstaller.php b/includes/installer/PostgresInstaller.php index fab680c5a2..f927c01f0c 100644 --- a/includes/installer/PostgresInstaller.php +++ b/includes/installer/PostgresInstaller.php @@ -101,22 +101,31 @@ class PostgresInstaller extends DatabaseInstaller { return $status; } - public function openConnection() { + public function openConnection( $dbName = null ) { $status = Status::newGood(); try { if ( $this->useAdmin ) { + if ( $dbName === null ) $dbName = 'postgres'; + $db = new DatabasePostgres( $this->getVar( 'wgDBserver' ), $this->getVar( '_InstallUser' ), $this->getVar( '_InstallPassword' ), - 'postgres' ); + $dbName ); } else { + if ( $dbName === null ) $dbName = $this->getVar( 'wgDBname' ); + $db = new DatabasePostgres( $this->getVar( 'wgDBserver' ), $this->getVar( 'wgDBuser' ), $this->getVar( 'wgDBpassword' ), - $this->getVar( 'wgDBname' ) ); + $dbName ); } + + 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" ); + $status->value = $db; } catch ( DBConnectionError $e ) { $status->fatal( 'config-connection-error', $e->getMessage() ); @@ -134,15 +143,15 @@ class PostgresInstaller extends DatabaseInstaller { $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__ + $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__ ); - if( !$rights || ( $rights != 1 && $rights != 3 ) ) { + if( !$rights || $rights == 3 ) { return false; } @@ -226,9 +235,10 @@ class PostgresInstaller extends DatabaseInstaller { $rows = $conn->numRows( $conn->query( $SQL ) ); $safedb = $conn->addIdentifierQuotes( $dbName ); if( !$rows ) { - $conn->query( "CREATE DATABASE $safedb OWNER $safeuser", __METHOD__ ); + $conn->query( "CREATE DATABASE $safedb", __METHOD__ ); + $conn->query( "GRANT ALL ON DATABASE $safedb to $safeuser", __METHOD__ ); } else { - $conn->query( "ALTER DATABASE $safedb OWNER TO $safeuser", __METHOD__ ); + $conn->query( "GRANT ALL ON DATABASE $safedb TO $safeuser", __METHOD__ ); } // Now that we've established the real database exists, connect to it @@ -287,17 +297,18 @@ class PostgresInstaller extends DatabaseInstaller { $safeschema = $this->db->addIdentifierQuotes( $schema ); $rows = $this->db->numRows( - $this->db->query( "SELECT 1 FROM pg_catalog.pg_shadow WHERE usename = $safeusercheck" ) + $this->db->query( "SELECT 1 FROM pg_catalog.pg_roles WHERE rolname = $safeusercheck" ) ); if ( $rows < 1 ) { - $res = $this->db->query( "CREATE USER $safeuser NOCREATEDB PASSWORD $safepass", __METHOD__ ); + $res = $this->db->query( "CREATE ROLE $safeuser NOCREATEDB LOGIN PASSWORD $safepass", __METHOD__ ); if ( $res !== true && !( $res instanceOf ResultWrapper ) ) { $status->fatal( 'config-install-user-failed', $this->getVar( 'wgDBuser' ), $res ); } if( $status->isOK() ) { - $this->db->query("ALTER USER $safeuser SET search_path = $safeschema"); + $this->db->query("ALTER ROLE $safeuser LOGIN"); } } + $this->db->query("ALTER ROLE $safeuser SET search_path = $safeschema, public"); return $status; } @@ -337,12 +348,11 @@ class PostgresInstaller extends DatabaseInstaller { $this->db->begin( __METHOD__ ); + // getConnection() should have already selected the schema if it exists if( !$this->db->schemaExists( $schema ) ) { $status->error( 'config-install-pg-schema-not-exist' ); return $status; } - $safeschema = $this->db->addIdentifierQuotes( $schema ); - $this->db->query( "SET search_path = $safeschema" ); $error = $this->db->sourceFile( $this->db->getSchema() ); if( $error !== true ) { $this->db->reportQueryError( $error, 0, '', __METHOD__ ); @@ -359,12 +369,18 @@ class PostgresInstaller extends DatabaseInstaller { } public function setupPLpgSQL() { + $this->db = null; $this->useAdmin = true; - $status = $this->getConnection(); + $dbName = $this->getVar( 'wgDBname' ); + $status = $this->getConnection( $dbName ); if ( !$status->isOK() ) { return $status; } + $this->db = $status->value; + /* Admin user has to be connected to the db it just + created to satisfy ownership requirements for + "CREATE LANGAUGE" */ $rows = $this->db->numRows( $this->db->query( "SELECT 1 FROM pg_catalog.pg_language WHERE lanname = 'plpgsql'" ) ); @@ -373,7 +389,6 @@ class PostgresInstaller extends DatabaseInstaller { $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 ) { diff --git a/includes/installer/PostgresUpdater.php b/includes/installer/PostgresUpdater.php index b40767affd..272638ce75 100644 --- a/includes/installer/PostgresUpdater.php +++ b/includes/installer/PostgresUpdater.php @@ -79,7 +79,6 @@ class PostgresUpdater extends DatabaseUpdater { array( 'addPgField', 'logging', 'log_id', "INTEGER NOT NULL PRIMARY KEY DEFAULT nextval('logging_log_id_seq')" ), array( 'addPgField', 'logging', 'log_params', 'TEXT' ), array( 'addPgField', 'mwuser', 'user_editcount', 'INTEGER' ), - array( 'addPgField', 'mwuser', 'user_hidden', 'SMALLINT NOT NULL DEFAULT 0' ), array( 'addPgField', 'mwuser', 'user_newpass_time', 'TIMESTAMPTZ' ), array( 'addPgField', 'oldimage', 'oi_deleted', 'SMALLINT NOT NULL DEFAULT 0' ), array( 'addPgField', 'oldimage', 'oi_major_mime', "TEXT NOT NULL DEFAULT 'unknown'" ), diff --git a/includes/installer/SqliteInstaller.php b/includes/installer/SqliteInstaller.php index 144e710d0f..c942878d45 100644 --- a/includes/installer/SqliteInstaller.php +++ b/includes/installer/SqliteInstaller.php @@ -122,7 +122,7 @@ class SqliteInstaller extends DatabaseInstaller { /** * @return Status */ - public function openConnection() { + public function openConnection( $dbName = null ) { global $wgSQLiteDataDir; $status = Status::newGood(); -- 2.20.1