Index: trunk/phase3/includes/installer/PostgresInstaller.php |
— | — | @@ -81,6 +81,12 @@ |
82 | 82 | return $status; |
83 | 83 | } |
84 | 84 | |
| 85 | +/* //Make sure install user can create |
| 86 | + $status->merge( $this->canCreateAccounts() ); |
| 87 | + if ( !$status->isOK() ) { |
| 88 | + return $status; |
| 89 | + } */ |
| 90 | + |
85 | 91 | // Check version |
86 | 92 | $version = $this->db->getServerVersion(); |
87 | 93 | if ( version_compare( $version, $this->minimumVersion ) < 0 ) { |
— | — | @@ -92,7 +98,7 @@ |
93 | 99 | return $status; |
94 | 100 | } |
95 | 101 | |
96 | | - function getConnection() { |
| 102 | + function getConnection($database = 'template1') { |
97 | 103 | $status = Status::newGood(); |
98 | 104 | |
99 | 105 | try { |
— | — | @@ -100,7 +106,7 @@ |
101 | 107 | $this->getVar( 'wgDBserver' ), |
102 | 108 | $this->getVar( '_InstallUser' ), |
103 | 109 | $this->getVar( '_InstallPassword' ), |
104 | | - false ); |
| 110 | + $database ); |
105 | 111 | $status->value = $this->db; |
106 | 112 | } catch ( DBConnectionError $e ) { |
107 | 113 | $status->fatal( 'config-connection-error', $e->getMessage() ); |
— | — | @@ -187,20 +193,25 @@ |
188 | 194 | 'name' => 'pg-commit', |
189 | 195 | 'callback' => array( $this, 'commitChanges' ), |
190 | 196 | ); |
191 | | - $userCB = array( |
| 197 | +/* $userCB = array( |
192 | 198 | 'name' => 'user', |
193 | 199 | 'callback' => array( $this, 'setupUser' ), |
194 | | - ); |
| 200 | + ); */ |
195 | 201 | $ts2CB = array( |
196 | 202 | 'name' => 'pg-ts2', |
197 | 203 | 'callback' => array( $this, 'setupTs2' ), |
198 | 204 | ); |
199 | 205 | $this->parent->addInstallStep( $commitCB, 'interwiki' ); |
200 | | - $this->parent->addInstallStep( $userCB ); |
| 206 | +// $this->parent->addInstallStep( $userCB ); |
201 | 207 | $this->parent->addInstallStep( $ts2CB, 'setupDatabase' ); |
202 | 208 | } |
203 | 209 | |
204 | 210 | function setupDatabase() { |
| 211 | + $status = $this->setupUser(); |
| 212 | + if (!$status->isOK() ) { |
| 213 | + return $status; |
| 214 | + } |
| 215 | + |
205 | 216 | $status = $this->getConnection(); |
206 | 217 | if ( !$status->isOK() ) { |
207 | 218 | return $status; |
— | — | @@ -208,26 +219,49 @@ |
209 | 220 | $conn = $status->value; |
210 | 221 | |
211 | 222 | $dbName = $this->getVar( 'wgDBname' ); |
212 | | - if( !$conn->selectDB( $dbName ) ) { |
213 | | - // Make sure that we can write to the correct schema |
214 | | - // If not, Postgres will happily and silently go to the next search_path item |
| 223 | + $SQL = "SELECT 1 FROM pg_catalog.pg_database WHERE datname = " . $conn->addQuotes( $wgDBname ); |
| 224 | + $rows = $conn->numRows( $conn->doQuery( $SQL ) ); |
| 225 | + if( !$rows ) { |
215 | 226 | $schema = $this->getVar( 'wgDBmwschema' ); |
216 | | - $ctest = 'mediawiki_test_table'; |
| 227 | + $user = $this->getVar( 'wgDBuser' ); |
| 228 | + |
217 | 229 | $safeschema = $conn->addIdentifierQuotes( $schema ); |
218 | | - if ( $conn->tableExists( $ctest, $schema ) ) { |
219 | | - $conn->query( "DROP TABLE $safeschema.$ctest" ); |
220 | | - } |
221 | | - $res = $conn->query( "CREATE TABLE $safeschema.$ctest(a int)" ); |
222 | | - if ( !$res ) { |
223 | | - $status->fatal( 'config-install-pg-schema-failed', |
224 | | - $this->getVar( 'wgDBuser'), $schema ); |
225 | | - } |
226 | | - $conn->query( "DROP TABLE $safeschema.$ctest" ); |
| 230 | + $safeuser = $conn->addIdentifierQuotes( $user ); |
227 | 231 | |
228 | 232 | $safedb = $conn->addIdentifierQuotes( $dbName ); |
229 | | - $safeuser = $conn->addQuotes( $this->getVar( 'wgDBuser' ) ); |
| 233 | + |
230 | 234 | $conn->query( "CREATE DATABASE $safedb OWNER $safeuser", __METHOD__ ); |
231 | | - $conn->selectDB( $dbName ); |
| 235 | + |
| 236 | + $conn = new DatabasePostgres( |
| 237 | + $this->getVar( 'wgDBserver' ), |
| 238 | + $this->getVar( 'wgDBuser' ), |
| 239 | + $this->getVar( 'wgDBpassword' ), |
| 240 | + $dbName, |
| 241 | + false, |
| 242 | + 0, |
| 243 | + $this->getVar( 'wgDBprefix' ) |
| 244 | + ); |
| 245 | + |
| 246 | + $result = $conn->schemaExists( $schema ); |
| 247 | + if( !$result ) { |
| 248 | + $result = $conn->doQuery( "CREATE SCHEMA $safeschema AUTHORIZATION $safeuser" ); |
| 249 | + if( !$result ) { |
| 250 | + $status->fatal( 'config-install-pg-schema-failed', $user, $schema ); |
| 251 | + } |
| 252 | + } else { |
| 253 | + $safeschema2 = $conn->addQuotes( $schema ); |
| 254 | + $SQL = "SELECT 'GRANT ALL ON '||pg_catalog.quote_ident(relname)||' TO $safeuser;'\n". |
| 255 | + "FROM pg_catalog.pg_class p, pg_catalog.pg_namespace n\n". |
| 256 | + "WHERE relnamespace = n.oid AND n.nspname = $safeschema2\n". |
| 257 | + "AND p.relkind IN ('r','S','v')\n"; |
| 258 | + $SQL .= "UNION\n"; |
| 259 | + $SQL .= "SELECT 'GRANT ALL ON FUNCTION '||pg_catalog.quote_ident(proname)||'('||\n". |
| 260 | + "pg_catalog.oidvectortypes(p.proargtypes)||') TO $safeuser;'\n". |
| 261 | + "FROM pg_catalog.pg_proc p, pg_catalog.pg_namespace n\n". |
| 262 | + "WHERE p.pronamespace = n.oid AND n.nspname = $safeschema2"; |
| 263 | + $res = $conn->doQuery( $SQL ); |
| 264 | + $conn->doQuery( "SET search_path = $safeschema" ); |
| 265 | + } |
232 | 266 | } |
233 | 267 | return $status; |
234 | 268 | } |
— | — | @@ -274,9 +308,11 @@ |
275 | 309 | |
276 | 310 | $db = $this->getVar( 'wgDBname' ); |
277 | 311 | $this->db->selectDB( $db ); |
278 | | - $safeuser = $this->db->addQuotes( $this->getVar( 'wgDBuser' ) ); |
| 312 | + $safeuser = $this->db->addIdentifierQuotes( $this->getVar( 'wgDBuser' ) ); |
279 | 313 | $safepass = $this->db->addQuotes( $this->getVar( 'wgDBpassword' ) ); |
280 | 314 | $res = $this->db->query( "CREATE USER $safeuser NOCREATEDB PASSWORD $safepass", __METHOD__ ); |
| 315 | + return $status; |
| 316 | + |
281 | 317 | if ( $res !== true ) { |
282 | 318 | $status->fatal( 'config-install-user-failed', $this->getVar( 'wgDBuser' ) ); |
283 | 319 | } |