4a153ec249b105a0c7f09f797ce4900dbd4c44f9
[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 ) {
141 return false;
142 }
143
144 if( $rights != 1 && $rights != 3 ) {
145 return false;
146 }
147
148 return true;
149 }
150
151 public function getSettingsForm() {
152 if ( $this->canCreateAccounts() ) {
153 $noCreateMsg = false;
154 } else {
155 $noCreateMsg = 'config-db-web-no-create-privs';
156 }
157 $s = $this->getWebUserBox( $noCreateMsg );
158
159 return $s;
160 }
161
162 public function submitSettingsForm() {
163 $status = $this->submitWebUserBox();
164 if ( !$status->isOK() ) {
165 return $status;
166 }
167
168 // Validate the create checkbox
169 $canCreate = $this->canCreateAccounts();
170 if ( !$canCreate ) {
171 $this->setVar( '_CreateDBAccount', false );
172 $create = false;
173 } else {
174 $create = $this->getVar( '_CreateDBAccount' );
175 }
176
177 if ( !$create ) {
178 // Test the web account
179 try {
180 new DatabasePostgres(
181 $this->getVar( 'wgDBserver' ),
182 $this->getVar( 'wgDBuser' ),
183 $this->getVar( 'wgDBpassword' ),
184 false,
185 false,
186 0,
187 $this->getVar( 'wgDBprefix' )
188 );
189 } catch ( DBConnectionError $e ) {
190 return Status::newFatal( 'config-connection-error', $e->getMessage() );
191 }
192 }
193
194 return Status::newGood();
195 }
196
197 public function preInstall() {
198 $commitCB = array(
199 'name' => 'pg-commit',
200 'callback' => array( $this, 'commitChanges' ),
201 );
202 $userCB = array(
203 'name' => 'user',
204 'callback' => array( $this, 'setupUser' ),
205 );
206 $plpgCB = array(
207 'name' => 'pg-plpgsql',
208 'callback' => array( $this, 'setupPLpgSQL' ),
209 );
210 $this->parent->addInstallStep( $commitCB, 'interwiki' );
211 $this->parent->addInstallStep( $userCB );
212 $this->parent->addInstallStep( $plpgCB, 'database' );
213 }
214
215 function setupDatabase() {
216 $this->useAdmin = TRUE;
217 $status = $this->getConnection();
218 if ( !$status->isOK() ) {
219 return $status;
220 }
221 $this->setupSchemaVars();
222 $conn = $status->value;
223
224 $dbName = $this->getVar( 'wgDBname' );
225 $SQL = "SELECT 1 FROM pg_catalog.pg_database WHERE datname = " . $conn->addQuotes( $dbName );
226 $rows = $conn->numRows( $conn->query( $SQL ) );
227 if( !$rows ) {
228 $schema = $this->getVar( 'wgDBmwschema' );
229 $user = $this->getVar( 'wgDBuser' );
230
231 $safeschema = $conn->addIdentifierQuotes( $schema );
232 $safeuser = $conn->addIdentifierQuotes( $user );
233
234 $safedb = $conn->addIdentifierQuotes( $dbName );
235
236 $conn->query( "CREATE DATABASE $safedb OWNER $safeuser", __METHOD__ );
237
238 $conn = new DatabasePostgres(
239 $this->getVar( 'wgDBserver' ),
240 $this->getVar( 'wgDBuser' ),
241 $this->getVar( 'wgDBpassword' ),
242 $dbName,
243 false,
244 0,
245 $this->getVar( 'wgDBprefix' )
246 );
247
248 $result = $conn->schemaExists( $schema );
249 if( !$result ) {
250 $result = $conn->query( "CREATE SCHEMA $safeschema AUTHORIZATION $safeuser" );
251 if( !$result ) {
252 $status->fatal( 'config-install-pg-schema-failed', $user, $schema );
253 }
254 } else {
255 $safeschema2 = $conn->addQuotes( $schema );
256 $SQL = "SELECT 'GRANT ALL ON '||pg_catalog.quote_ident(relname)||' TO $safeuser;'\n".
257 "FROM pg_catalog.pg_class p, pg_catalog.pg_namespace n\n" .
258 "WHERE relnamespace = n.oid AND n.nspname = $safeschema2\n" .
259 "AND p.relkind IN ('r','S','v')\n";
260 $SQL .= "UNION\n";
261 $SQL .= "SELECT 'GRANT ALL ON FUNCTION '||pg_catalog.quote_ident(proname)||'('||\n".
262 "pg_catalog.oidvectortypes(p.proargtypes)||') TO $safeuser;'\n" .
263 "FROM pg_catalog.pg_proc p, pg_catalog.pg_namespace n\n" .
264 "WHERE p.pronamespace = n.oid AND n.nspname = $safeschema2";
265 $res = $conn->query( $SQL );
266 }
267 }
268 return $status;
269 }
270
271 function commitChanges() {
272 $this->db->query( 'COMMIT' );
273 return Status::newGood();
274 }
275
276 function setupUser() {
277 if ( !$this->getVar( '_CreateDBAccount' ) ) {
278 return Status::newGood();
279 }
280
281 $this->useAdmin = TRUE;
282 $status = $this->getConnection();
283
284 if ( !$status->isOK() ) {
285 return $status;
286 }
287
288 $db = $this->getVar( 'wgDBname' );
289 $schema = $this->getVar( 'wgDBmwschema' );
290 $this->db->selectDB( $db );
291 $safeuser = $this->db->addIdentifierQuotes( $this->getVar( 'wgDBuser' ) );
292 $safeusercheck = $this->db->addQuotes( $this->getVar( 'wgDBuser' ) );
293 $safepass = $this->db->addQuotes( $this->getVar( 'wgDBpassword' ) );
294 $safeschema = $this->db->addIdentifierQuotes( $schema );
295
296 $rows = $this->db->numRows(
297 $this->db->query( "SELECT 1 FROM pg_catalog.pg_shadow WHERE usename = $safeusercheck" )
298 );
299 if ( $rows < 1 ) {
300 $res = $this->db->query( "CREATE USER $safeuser NOCREATEDB PASSWORD $safepass", __METHOD__ );
301 if ( $res !== true && !( $res instanceOf ResultWrapper ) ) {
302 $status->fatal( 'config-install-user-failed', $this->getVar( 'wgDBuser' ), $res );
303 }
304 $this->db->query("ALTER USER $safeuser SET search_path = $safeschema");
305 }
306
307 return $status;
308 }
309
310 function getLocalSettings() {
311 $port = $this->getVar( 'wgDBport' );
312 $schema = $this->getVar( 'wgDBmwschema' );
313 return
314 "# Postgres specific settings
315 \$wgDBport = \"{$port}\";
316 \$wgDBmwschema = \"{$schema}\";";
317 }
318
319 public function preUpgrade() {
320 global $wgDBuser, $wgDBpassword;
321
322 # Normal user and password are selected after this step, so for now
323 # just copy these two
324 $wgDBuser = $this->getVar( '_InstallUser' );
325 $wgDBpassword = $this->getVar( '_InstallPassword' );
326 }
327
328 public function createTables() {
329 $this->db = null;
330 $this->useAdmin = false;
331 $status = $this->getConnection();
332 if ( !$status->isOK() ) {
333 return $status;
334 }
335 $this->db->selectDB( $this->getVar( 'wgDBname' ) );
336
337 if( $this->db->tableExists( 'user' ) ) {
338 $status->warning( 'config-install-tables-exist' );
339 return $status;
340 }
341
342 $this->db->setFlag( DBO_DDLMODE ); // For Oracle's handling of schema files
343 $this->db->begin( __METHOD__ );
344
345 $error = $this->db->sourceFile( $this->db->getSchema() );
346 if( $error !== true ) {
347 $this->db->reportQueryError( $error, 0, '', __METHOD__ );
348 $this->db->rollback( __METHOD__ );
349 $status->fatal( 'config-install-tables-failed', $error );
350 } else {
351 $this->db->commit( __METHOD__ );
352 }
353 // Resume normal operations
354 if( $status->isOk() ) {
355 $this->enableLB();
356 }
357 return $status;
358 }
359
360 public function setupPLpgSQL() {
361 $this->useAdmin = TRUE;
362 $status = $this->getConnection();
363 if ( !$status->isOK() ) {
364 return $status;
365 }
366
367 $rows = $this->db->numRows(
368 $this->db->query( "SELECT 1 FROM pg_catalog.pg_language WHERE lanname = 'plpgsql'" )
369 );
370 if ( $rows < 1 ) {
371 // plpgsql is not installed, but if we have a pg_pltemplate table, we should be able to create it
372 $SQL = "SELECT 1 FROM pg_catalog.pg_class c JOIN pg_catalog.pg_namespace n ON (n.oid = c.relnamespace) ".
373 "WHERE relname = 'pg_pltemplate' AND nspname='pg_catalog'";
374 $rows = $this->db->numRows( $this->db->query( $SQL ) );
375 $dbName = $this->getVar( 'wgDBname' );
376 if ( $rows >= 1 ) {
377 $result = $this->db->query( 'CREATE LANGUAGE plpgsql' );
378 if ( !$result ) {
379 return Status::newFatal( 'config-pg-no-plpgsql', $dbName );
380 }
381 } else {
382 return Status::newFatal( 'config-pg-no-plpgsql', $dbName );
383 }
384 }
385 return Status::newGood();
386 }
387 }