+ $this->useAdmin = true;
+ $status = $this->getConnection();
+ if ( !$status->isOK() ) {
+ return $status;
+ }
+ $this->setupSchemaVars();
+ $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 ) {
+ $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;
+
+ if( !$conn->schemaExists( $schema ) ) {
+ $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";
+ $conn->query( "SET search_path = $safeschema" );
+ $res = $conn->query( $SQL );
+ }
+ return $status;
+ }
+
+ function commitChanges() {
+ $this->db->query( 'COMMIT' );
+ return Status::newGood();
+ }
+
+ function setupUser() {
+ if ( !$this->getVar( '_CreateDBAccount' ) ) {
+ return Status::newGood();
+ }
+
+ $this->useAdmin = true;
+ $status = $this->getConnection();
+
+ if ( !$status->isOK() ) {
+ return $status;
+ }
+
+ $schema = $this->getVar( 'wgDBmwschema' );
+ $safeuser = $this->db->addIdentifierQuotes( $this->getVar( 'wgDBuser' ) );
+ $safeusercheck = $this->db->addQuotes( $this->getVar( 'wgDBuser' ) );
+ $safepass = $this->db->addQuotes( $this->getVar( 'wgDBpassword' ) );
+ $safeschema = $this->db->addIdentifierQuotes( $schema );
+
+ $rows = $this->db->numRows(
+ $this->db->query( "SELECT 1 FROM pg_catalog.pg_shadow WHERE usename = $safeusercheck" )
+ );
+ if ( $rows < 1 ) {
+ $res = $this->db->query( "CREATE USER $safeuser NOCREATEDB 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");
+ }
+ }
+
+ return $status;