forward port r88929
authorMark A. Hershberger <mah@users.mediawiki.org>
Fri, 3 Jun 2011 03:41:11 +0000 (03:41 +0000)
committerMark A. Hershberger <mah@users.mediawiki.org>
Fri, 3 Jun 2011 03:41:11 +0000 (03:41 +0000)
includes/installer/DatabaseInstaller.php
includes/installer/Ibm_db2Installer.php
includes/installer/Installer.i18n.php
includes/installer/MysqlInstaller.php
includes/installer/OracleInstaller.php
includes/installer/PostgresInstaller.php
includes/installer/PostgresUpdater.php
includes/installer/SqliteInstaller.php

index 7ea5296..5045351 100644 (file)
@@ -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
index dabbe91..c817aa1 100644 (file)
@@ -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(
index 586fdea..9ad5ec1 100644 (file)
@@ -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.',
index e049ac2..6603a0c 100644 (file)
@@ -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(
index d118410..6b3f78e 100644 (file)
@@ -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(
index fab680c..f927c01 100644 (file)
@@ -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 ) {
index b40767a..272638c 100644 (file)
@@ -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'" ),
index 144e710..c942878 100644 (file)
@@ -122,7 +122,7 @@ class SqliteInstaller extends DatabaseInstaller {
        /**
         * @return Status
         */
-       public function openConnection() {
+       public function openConnection( $dbName = null ) {
                global $wgSQLiteDataDir;
 
                $status = Status::newGood();