1e55737c651e9c5402d2b7fe9e5bff2764585535
[lhc/web/wiklou.git] / includes / installer / PostgresInstaller.php
1 <?php
2 /**
3 * PostgreSQL-specific installer.
4 *
5 * @file
6 * @ingroup Deployment
7 */
8
9 /**
10 * Class for setting up the MediaWiki database using Postgres.
11 *
12 * @ingroup Deployment
13 * @since 1.17
14 */
15 class PostgresInstaller extends DatabaseInstaller {
16
17 protected $globalNames = array(
18 'wgDBserver',
19 'wgDBport',
20 'wgDBname',
21 'wgDBuser',
22 'wgDBpassword',
23 'wgDBmwschema',
24 );
25
26 var $minimumVersion = '8.3';
27 private $useAdmin = false;
28
29 function getName() {
30 return 'postgres';
31 }
32
33 public function isCompiled() {
34 return self::checkExtension( 'pgsql' );
35 }
36
37 function getConnectForm() {
38 return
39 $this->getTextBox( 'wgDBserver', 'config-db-host', array(), $this->parent->getHelpBox( 'config-db-host-help' ) ) .
40 $this->getTextBox( 'wgDBport', 'config-db-port' ) .
41 Html::openElement( 'fieldset' ) .
42 Html::element( 'legend', array(), wfMsg( 'config-db-wiki-settings' ) ) .
43 $this->getTextBox( 'wgDBname', 'config-db-name', array(), $this->parent->getHelpBox( 'config-db-name-help' ) ) .
44 $this->getTextBox( 'wgDBmwschema', 'config-db-schema', array(), $this->parent->getHelpBox( 'config-db-schema-help' ) ) .
45 Html::closeElement( 'fieldset' ) .
46 $this->getInstallUserBox();
47 }
48
49 function submitConnectForm() {
50 // Get variables from the request
51 $newValues = $this->setVarsFromRequest( array( 'wgDBserver', 'wgDBport',
52 'wgDBname', 'wgDBmwschema' ) );
53
54 // Validate them
55 $status = Status::newGood();
56 if ( !strlen( $newValues['wgDBname'] ) ) {
57 $status->fatal( 'config-missing-db-name' );
58 } elseif ( !preg_match( '/^[a-zA-Z0-9_]+$/', $newValues['wgDBname'] ) ) {
59 $status->fatal( 'config-invalid-db-name', $newValues['wgDBname'] );
60 }
61 if ( !preg_match( '/^[a-zA-Z0-9_]*$/', $newValues['wgDBmwschema'] ) ) {
62 $status->fatal( 'config-invalid-schema', $newValues['wgDBmwschema'] );
63 }
64
65 // Submit user box
66 if ( $status->isOK() ) {
67 $status->merge( $this->submitInstallUserBox() );
68 }
69 if ( !$status->isOK() ) {
70 return $status;
71 }
72
73 $this->useAdmin = true;
74 // Try to connect
75 $status->merge( $this->getConnection() );
76 if ( !$status->isOK() ) {
77 return $status;
78 }
79
80 //Make sure install user can create
81 if( !$this->canCreateAccounts() ) {
82 $status->fatal( 'config-pg-no-create-privs' );
83 }
84 if ( !$status->isOK() ) {
85 return $status;
86 }
87
88 // Check version
89 $version = $this->db->getServerVersion();
90 if ( version_compare( $version, $this->minimumVersion ) < 0 ) {
91 return Status::newFatal( 'config-postgres-old', $this->minimumVersion, $version );
92 }
93
94 $this->setVar( 'wgDBuser', $this->getVar( '_InstallUser' ) );
95 $this->setVar( 'wgDBpassword', $this->getVar( '_InstallPassword' ) );
96 return $status;
97 }
98
99 public function openConnection() {
100 $status = Status::newGood();
101 try {
102 if ( $this->useAdmin ) {
103 $db = new DatabasePostgres(
104 $this->getVar( 'wgDBserver' ),
105 $this->getVar( '_InstallUser' ),
106 $this->getVar( '_InstallPassword' ),
107 'template1' );
108 } else {
109 $db = new DatabasePostgres(
110 $this->getVar( 'wgDBserver' ),
111 $this->getVar( 'wgDBuser' ),
112 $this->getVar( 'wgDBpassword' ),
113 $this->getVar( 'wgDBname' ) );
114 }
115 $status->value = $db;
116 } catch ( DBConnectionError $e ) {
117 $status->fatal( 'config-connection-error', $e->getMessage() );
118 }
119 return $status;
120 }
121
122 protected function canCreateAccounts() {
123 $this->useAdmin = true;
124 $status = $this->getConnection();
125 if ( !$status->isOK() ) {
126 return false;
127 }
128 $conn = $status->value;
129
130 $superuser = $this->getVar( '_InstallUser' );
131
132 $rights = $conn->selectField( 'pg_catalog.pg_user',
133 'CASE WHEN usesuper IS TRUE THEN
134 CASE WHEN usecreatedb IS TRUE THEN 3 ELSE 1 END
135 ELSE CASE WHEN usecreatedb IS TRUE THEN 2 ELSE 0 END
136 END AS rights',
137 array( 'usename' => $superuser ), __METHOD__
138 );
139
140 if( !$rights || ( $rights != 1 && $rights != 3 ) ) {
141 return false;
142 }
143
144 return true;
145 }
146
147 public function getSettingsForm() {
148 if ( $this->canCreateAccounts() ) {
149 $noCreateMsg = false;
150 } else {
151 $noCreateMsg = 'config-db-web-no-create-privs';
152 }
153 $s = $this->getWebUserBox( $noCreateMsg );
154
155 return $s;
156 }
157
158 public function submitSettingsForm() {
159 $status = $this->submitWebUserBox();
160 if ( !$status->isOK() ) {
161 return $status;
162 }
163
164 // Validate the create checkbox
165 $canCreate = $this->canCreateAccounts();
166 if ( $canCreate ) {
167 $this->setVar( '_CreateDBAccount', false );
168 $create = false;
169 } else {
170 $create = $this->getVar( '_CreateDBAccount' );
171 }
172
173 // Don't test the web account if it is the same as the admin.
174 if ( !$create && $this->getVar( 'wgDBuser' ) != $this->getVar( '_InstallUser' ) ) {
175 // Test the web account
176 try {
177 $this->useAdmin = false;
178 return $this->openConnection();
179 } catch ( DBConnectionError $e ) {
180 return Status::newFatal( 'config-connection-error', $e->getMessage() );
181 }
182 }
183
184 return Status::newGood();
185 }
186
187 public function preInstall() {
188 $commitCB = array(
189 'name' => 'pg-commit',
190 'callback' => array( $this, 'commitChanges' ),
191 );
192 $userCB = array(
193 'name' => 'user',
194 'callback' => array( $this, 'setupUser' ),
195 );
196 $plpgCB = array(
197 'name' => 'pg-plpgsql',
198 'callback' => array( $this, 'setupPLpgSQL' ),
199 );
200 $this->parent->addInstallStep( $commitCB, 'interwiki' );
201 $this->parent->addInstallStep( $userCB );
202 $this->parent->addInstallStep( $plpgCB, 'database' );
203 }
204
205 function setupDatabase() {
206 $this->useAdmin = true;
207 $status = $this->getConnection();
208 if ( !$status->isOK() ) {
209 return $status;
210 }
211 $this->setupSchemaVars();
212 $conn = $status->value;
213
214 $dbName = $this->getVar( 'wgDBname' );
215 $SQL = "SELECT 1 FROM pg_catalog.pg_database WHERE datname = " . $conn->addQuotes( $dbName );
216 $rows = $conn->numRows( $conn->query( $SQL ) );
217 if( !$rows ) {
218 $schema = $this->getVar( 'wgDBmwschema' );
219 $user = $this->getVar( 'wgDBuser' );
220
221 $safeschema = $conn->addIdentifierQuotes( $schema );
222 $safeuser = $conn->addIdentifierQuotes( $user );
223
224 $safedb = $conn->addIdentifierQuotes( $dbName );
225
226 $conn->query( "CREATE DATABASE $safedb OWNER $safeuser", __METHOD__ );
227
228 $this->useAdmin = false;
229 $status = $this->getConnection();
230 if ( !$status->isOK() ) {
231 return $status;
232 }
233 $conn = $status->value;
234
235 $result = $conn->schemaExists( $schema );
236 if( !$result ) {
237 $result = $conn->query( "CREATE SCHEMA $safeschema AUTHORIZATION $safeuser" );
238 if( !$result ) {
239 $status->fatal( 'config-install-pg-schema-failed', $user, $schema );
240 }
241 } else {
242 $safeschema2 = $conn->addQuotes( $schema );
243 $SQL = "SELECT 'GRANT ALL ON '||pg_catalog.quote_ident(relname)||' TO $safeuser;'\n".
244 "FROM pg_catalog.pg_class p, pg_catalog.pg_namespace n\n" .
245 "WHERE relnamespace = n.oid AND n.nspname = $safeschema2\n" .
246 "AND p.relkind IN ('r','S','v')\n";
247 $SQL .= "UNION\n";
248 $SQL .= "SELECT 'GRANT ALL ON FUNCTION '||pg_catalog.quote_ident(proname)||'('||\n".
249 "pg_catalog.oidvectortypes(p.proargtypes)||') TO $safeuser;'\n" .
250 "FROM pg_catalog.pg_proc p, pg_catalog.pg_namespace n\n" .
251 "WHERE p.pronamespace = n.oid AND n.nspname = $safeschema2";
252 $conn->query( "SET search_path = $safeschema" );
253 $res = $conn->query( $SQL );
254 }
255 }
256 return $status;
257 }
258
259 function commitChanges() {
260 $this->db->query( 'COMMIT' );
261 return Status::newGood();
262 }
263
264 function setupUser() {
265 if ( !$this->getVar( '_CreateDBAccount' ) ) {
266 return Status::newGood();
267 }
268
269 $this->useAdmin = true;
270 $status = $this->getConnection();
271
272 if ( !$status->isOK() ) {
273 return $status;
274 }
275
276 $schema = $this->getVar( 'wgDBmwschema' );
277 $safeuser = $this->db->addIdentifierQuotes( $this->getVar( 'wgDBuser' ) );
278 $safeusercheck = $this->db->addQuotes( $this->getVar( 'wgDBuser' ) );
279 $safepass = $this->db->addQuotes( $this->getVar( 'wgDBpassword' ) );
280 $safeschema = $this->db->addIdentifierQuotes( $schema );
281
282 $rows = $this->db->numRows(
283 $this->db->query( "SELECT 1 FROM pg_catalog.pg_shadow WHERE usename = $safeusercheck" )
284 );
285 if ( $rows < 1 ) {
286 $res = $this->db->query( "CREATE USER $safeuser NOCREATEDB PASSWORD $safepass", __METHOD__ );
287 if ( $res !== true && !( $res instanceOf ResultWrapper ) ) {
288 $status->fatal( 'config-install-user-failed', $this->getVar( 'wgDBuser' ), $res );
289 }
290 $this->db->query("ALTER USER $safeuser SET search_path = $safeschema");
291 }
292
293 return $status;
294 }
295
296 function getLocalSettings() {
297 $port = $this->getVar( 'wgDBport' );
298 $schema = $this->getVar( 'wgDBmwschema' );
299 return
300 "# Postgres specific settings
301 \$wgDBport = \"{$port}\";
302 \$wgDBmwschema = \"{$schema}\";";
303 }
304
305 public function preUpgrade() {
306 global $wgDBuser, $wgDBpassword;
307
308 # Normal user and password are selected after this step, so for now
309 # just copy these two
310 $wgDBuser = $this->getVar( '_InstallUser' );
311 $wgDBpassword = $this->getVar( '_InstallPassword' );
312 }
313
314 public function createTables() {
315 $schema = $this->getVar( 'wgDBmwschema' );
316
317 $this->db = null;
318 $this->useAdmin = false;
319 $status = $this->getConnection();
320 if ( !$status->isOK() ) {
321 return $status;
322 }
323 $this->db->selectDB( $this->getVar( 'wgDBname' ) );
324
325 if( $this->db->tableExists( 'user' ) ) {
326 $status->warning( 'config-install-tables-exist' );
327 return $status;
328 }
329
330 $this->db->setFlag( DBO_DDLMODE ); // For Oracle's handling of schema files
331 $this->db->begin( __METHOD__ );
332
333
334 if( !$this->db->schemaExists( $schema ) ) {
335 $status->error( 'config-install-pg-schema-not-exist' );
336 return $status;
337 }
338 $safeschema = $this->db->addIdentifierQuotes( $schema );
339 $this->db->query( "SET search_path = $safeschema" );
340 $error = $this->db->sourceFile( $this->db->getSchema() );
341 if( $error !== true ) {
342 $this->db->reportQueryError( $error, 0, '', __METHOD__ );
343 $this->db->rollback( __METHOD__ );
344 $status->fatal( 'config-install-tables-failed', $error );
345 } else {
346 $this->db->commit( __METHOD__ );
347 }
348 // Resume normal operations
349 if( $status->isOk() ) {
350 $this->enableLB();
351 }
352 return $status;
353 }
354
355 public function setupPLpgSQL() {
356 $this->useAdmin = true;
357 $status = $this->getConnection();
358 if ( !$status->isOK() ) {
359 return $status;
360 }
361
362 $rows = $this->db->numRows(
363 $this->db->query( "SELECT 1 FROM pg_catalog.pg_language WHERE lanname = 'plpgsql'" )
364 );
365 if ( $rows < 1 ) {
366 // plpgsql is not installed, but if we have a pg_pltemplate table, we should be able to create it
367 $SQL = "SELECT 1 FROM pg_catalog.pg_class c JOIN pg_catalog.pg_namespace n ON (n.oid = c.relnamespace) ".
368 "WHERE relname = 'pg_pltemplate' AND nspname='pg_catalog'";
369 $rows = $this->db->numRows( $this->db->query( $SQL ) );
370 $dbName = $this->getVar( 'wgDBname' );
371 if ( $rows >= 1 ) {
372 $result = $this->db->query( 'CREATE LANGUAGE plpgsql' );
373 if ( !$result ) {
374 return Status::newFatal( 'config-pg-no-plpgsql', $dbName );
375 }
376 } else {
377 return Status::newFatal( 'config-pg-no-plpgsql', $dbName );
378 }
379 }
380 return Status::newGood();
381 }
382 }