The beginnings of HipHop compiled mode support. It works now for parser cache hits.
[lhc/web/wiklou.git] / includes / installer / PostgresInstaller.php
index 4a153ec..c237a11 100644 (file)
@@ -35,6 +35,11 @@ class PostgresInstaller extends DatabaseInstaller {
        }
 
        function getConnectForm() {
+               // 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);
+               }
                return
                        $this->getTextBox( 'wgDBserver', 'config-db-host', array(), $this->parent->getHelpBox( 'config-db-host-help' ) ) .
                        $this->getTextBox( 'wgDBport', 'config-db-port' ) .
@@ -104,7 +109,7 @@ class PostgresInstaller extends DatabaseInstaller {
                                        $this->getVar( 'wgDBserver' ),
                                        $this->getVar( '_InstallUser' ),
                                        $this->getVar( '_InstallPassword' ),
-                                       'template1' );
+                                       'postgres' );
                        } else {
                                $db = new DatabasePostgres(
                                        $this->getVar( 'wgDBserver' ),
@@ -137,11 +142,7 @@ class PostgresInstaller extends DatabaseInstaller {
                        array( 'usename' => $superuser ), __METHOD__
                );
 
-               if( !$rights ) {
-                       return false;
-               }
-
-               if( $rights != 1 && $rights != 3 ) {
+               if( !$rights || ( $rights != 1 && $rights != 3 ) ) {
                        return false;
                }
 
@@ -166,26 +167,19 @@ class PostgresInstaller extends DatabaseInstaller {
                }
 
                // Validate the create checkbox
-               $canCreate = $this->canCreateAccounts();
-               if ( !$canCreate ) {
+               if ( !$this->canCreateAccounts() ) {
                        $this->setVar( '_CreateDBAccount', false );
                        $create = false;
                } else {
                        $create = $this->getVar( '_CreateDBAccount' );
                }
 
-               if ( !$create ) {
+               // Don't test the web account if it is the same as the admin.
+               if ( !$create && $this->getVar( 'wgDBuser' ) != $this->getVar( '_InstallUser' ) ) {
                        // Test the web account
                        try {
-                               new DatabasePostgres(
-                                       $this->getVar( 'wgDBserver' ),
-                                       $this->getVar( 'wgDBuser' ),
-                                       $this->getVar( 'wgDBpassword' ),
-                                       false,
-                                       false,
-                                       0,
-                                       $this->getVar( 'wgDBprefix' )
-                               );
+                               $this->useAdmin = false;
+                               return $this->openConnection();
                        } catch ( DBConnectionError $e ) {
                                return Status::newFatal( 'config-connection-error', $e->getMessage() );
                        }
@@ -199,21 +193,22 @@ class PostgresInstaller extends DatabaseInstaller {
                        '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' );
+               if( $this->getVar( '_CreateDBAccount' ) ) {
+                       $this->parent->addInstallStep( array(
+                               'name' => 'user',
+                               'callback' => array( $this, 'setupUser' ),
+                       ) );
+               }
        }
 
        function setupDatabase() {
-               $this->useAdmin = TRUE;
+               $this->useAdmin = true;
                $status = $this->getConnection();
                if ( !$status->isOK() ) {
                        return $status;
@@ -222,48 +217,48 @@ class PostgresInstaller extends DatabaseInstaller {
                $conn = $status->value;
 
                $dbName = $this->getVar( 'wgDBname' );
+               $schema = $this->getVar( 'wgDBmwschema' );
+               $user = $this->getVar( 'wgDBuser' );
+               $safeschema = $conn->addIdentifierQuotes( $schema );
+               $safeuser = $conn->addIdentifierQuotes( $user );
+
                $SQL = "SELECT 1 FROM pg_catalog.pg_database WHERE datname = " . $conn->addQuotes( $dbName );
                $rows = $conn->numRows( $conn->query( $SQL ) );
+               $safedb = $conn->addIdentifierQuotes( $dbName );
                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__ );
+               } else {
+                       $conn->query( "ALTER DATABASE $safedb OWNER TO $safeuser", __METHOD__ );
+               }
+               
+               // 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;
+               $this->useAdmin = false;
+               $status = $this->getConnection();
+               if ( !$status->isOK() ) {
+                       return $status;
+               }
+               $conn = $status->value;
 
-                       $conn = new DatabasePostgres(
-                               $this->getVar( 'wgDBserver' ),
-                               $this->getVar( 'wgDBuser' ),
-                               $this->getVar( 'wgDBpassword' ),
-                               $dbName,
-                               false,
-                               0,
-                               $this->getVar( 'wgDBprefix' )
-                       );
-
-                       $result = $conn->schemaExists( $schema );
+               if( !$conn->schemaExists( $schema ) ) {
+                       $result = $conn->query( "CREATE SCHEMA $safeschema AUTHORIZATION $safeuser" );
                        if( !$result ) {
-                               $result = $conn->query( "CREATE SCHEMA $safeschema AUTHORIZATION $safeuser" );
-                               if( !$result ) {
-                                       $status->fatal( 'config-install-pg-schema-failed', $user, $schema );
-                               }
-                       } 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";
-                               $res = $conn->query( $SQL );
+                               $status->fatal( 'config-install-pg-schema-failed', $user, $schema );
                        }
+               } 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 );
                }
                return $status;
        }
@@ -278,16 +273,14 @@ class PostgresInstaller extends DatabaseInstaller {
                        return Status::newGood();
                }
 
-               $this->useAdmin = TRUE;
+               $this->useAdmin = true;
                $status = $this->getConnection();
 
                if ( !$status->isOK() ) {
                        return $status;
                }
 
-               $db = $this->getVar( 'wgDBname' );
                $schema = $this->getVar( 'wgDBmwschema' );
-               $this->db->selectDB( $db );
                $safeuser = $this->db->addIdentifierQuotes( $this->getVar( 'wgDBuser' ) );
                $safeusercheck = $this->db->addQuotes( $this->getVar( 'wgDBuser' ) );
                $safepass = $this->db->addQuotes( $this->getVar( 'wgDBpassword' ) );
@@ -301,7 +294,9 @@ class PostgresInstaller extends DatabaseInstaller {
                        if ( $res !== true && !( $res instanceOf ResultWrapper ) ) {
                                $status->fatal( 'config-install-user-failed', $this->getVar( 'wgDBuser' ), $res );
                        }
-                       $this->db->query("ALTER USER $safeuser SET search_path = $safeschema");
+                       if( $status->isOK() ) {
+                               $this->db->query("ALTER USER $safeuser SET search_path = $safeschema");
+                       }
                }
 
                return $status;
@@ -326,22 +321,28 @@ class PostgresInstaller extends DatabaseInstaller {
        }
 
        public function createTables() {
+               $schema = $this->getVar( 'wgDBmwschema' );
+
                $this->db = null;
                $this->useAdmin = false;
                $status = $this->getConnection();
                if ( !$status->isOK() ) {
                        return $status;
                }
-               $this->db->selectDB( $this->getVar( 'wgDBname' ) );
 
                if( $this->db->tableExists( 'user' ) ) {
                        $status->warning( 'config-install-tables-exist' );
                        return $status;
                }
 
-               $this->db->setFlag( DBO_DDLMODE ); // For Oracle's handling of schema files
                $this->db->begin( __METHOD__ );
 
+               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__ );
@@ -358,7 +359,7 @@ class PostgresInstaller extends DatabaseInstaller {
        }
 
        public function setupPLpgSQL() {
-               $this->useAdmin = TRUE;
+               $this->useAdmin = true;
                $status = $this->getConnection();
                if ( !$status->isOK() ) {
                        return $status;