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