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