c562d17408c0fc263750606a6f27b5139ae473b0
[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 $this->parent->addInstallStep( $commitCB, 'interwiki' );
205 $this->parent->addInstallStep( $userCB );
206 $this->parent->addInstallStep( $ts2CB, 'setupDatabase' );
207 }
208
209 function setupDatabase() {
210 $status = $this->getConnection();
211 if ( !$status->isOK() ) {
212 return $status;
213 }
214 $conn = $status->value;
215
216 $dbName = $this->getVar( 'wgDBname' );
217 $SQL = "SELECT 1 FROM pg_catalog.pg_database WHERE datname = " . $conn->addQuotes( $wgDBname );
218 $rows = $conn->numRows( $conn->doQuery( $SQL ) );
219 if( !$rows ) {
220 $schema = $this->getVar( 'wgDBmwschema' );
221 $user = $this->getVar( 'wgDBuser' );
222
223 $safeschema = $conn->addIdentifierQuotes( $schema );
224 $safeuser = $conn->addIdentifierQuotes( $user );
225
226 $safedb = $conn->addIdentifierQuotes( $dbName );
227
228 $conn->query( "CREATE DATABASE $safedb OWNER $safeuser", __METHOD__ );
229
230 $conn = new DatabasePostgres(
231 $this->getVar( 'wgDBserver' ),
232 $this->getVar( 'wgDBuser' ),
233 $this->getVar( 'wgDBpassword' ),
234 $dbName,
235 false,
236 0,
237 $this->getVar( 'wgDBprefix' )
238 );
239
240 $result = $conn->schemaExists( $schema );
241 if( !$result ) {
242 $result = $conn->doQuery( "CREATE SCHEMA $safeschema AUTHORIZATION $safeuser" );
243 if( !$result ) {
244 $status->fatal( 'config-install-pg-schema-failed', $user, $schema );
245 }
246 } else {
247 $safeschema2 = $conn->addQuotes( $schema );
248 $SQL = "SELECT 'GRANT ALL ON '||pg_catalog.quote_ident(relname)||' TO $safeuser;'\n".
249 "FROM pg_catalog.pg_class p, pg_catalog.pg_namespace n\n".
250 "WHERE relnamespace = n.oid AND n.nspname = $safeschema2\n".
251 "AND p.relkind IN ('r','S','v')\n";
252 $SQL .= "UNION\n";
253 $SQL .= "SELECT 'GRANT ALL ON FUNCTION '||pg_catalog.quote_ident(proname)||'('||\n".
254 "pg_catalog.oidvectortypes(p.proargtypes)||') TO $safeuser;'\n".
255 "FROM pg_catalog.pg_proc p, pg_catalog.pg_namespace n\n".
256 "WHERE p.pronamespace = n.oid AND n.nspname = $safeschema2";
257 $res = $conn->doQuery( $SQL );
258 $conn->doQuery( "SET search_path = $safeschema" );
259 }
260 }
261 return $status;
262 }
263
264 /**
265 * Ts2 isn't needed in newer versions of Postgres, so wrap it in a nice big
266 * version check and skip it if we're new. Maybe we can bump $minimumVersion
267 * one day and render this obsolete :)
268 *
269 * @return Status
270 */
271 function setupTs2() {
272 if( version_compare( $this->db->getServerVersion(), $this->ts2MaxVersion, '<' ) ) {
273 if ( !$this->db->tableExists( 'pg_ts_cfg', $wgDBts2schema ) ) {
274 return Status::newFatal(
275 'config-install-pg-ts2-failed',
276 $this->getVar( 'wgDBname' ),
277 'http://www.devx.com/opensource/Article/21674/0/page/2'
278 );
279 }
280 $safeuser = $this->db->addQuotes( $this->getVar( 'wgDBuser' ) );
281 foreach ( array( 'cfg', 'cfgmap', 'dict', 'parser' ) as $table ) {
282 $sql = "GRANT SELECT ON pg_ts_$table TO $safeuser";
283 $this->db->query( $sql, __METHOD__ );
284 }
285 }
286 return Status::newGood();
287 }
288
289 function commitChanges() {
290 $this->db->query( 'COMMIT' );
291 return Status::newGood();
292 }
293
294 function setupUser() {
295 if ( !$this->getVar( '_CreateDBAccount' ) ) {
296 return Status::newGood();
297 }
298
299 $status = $this->getConnection();
300 if ( !$status->isOK() ) {
301 return $status;
302 }
303
304 $db = $this->getVar( 'wgDBname' );
305 $this->db->selectDB( $db );
306 $safeuser = $this->db->addIdentifierQuotes( $this->getVar( 'wgDBuser' ) );
307 $safepass = $this->db->addQuotes( $this->getVar( 'wgDBpassword' ) );
308 $res = $this->db->query( "CREATE USER $safeuser NOCREATEDB PASSWORD $safepass", __METHOD__ );
309 return $status;
310
311 if ( $res !== true ) {
312 $status->fatal( 'config-install-user-failed', $this->getVar( 'wgDBuser' ) );
313 }
314
315 return $status;
316 }
317
318 function getLocalSettings() {
319 $port = $this->getVar( 'wgDBport' );
320 $schema = $this->getVar( 'wgDBmwschema' );
321 $ts2 = $this->getVar( 'wgDBts2schema' );
322 return
323 "# Postgres specific settings
324 \$wgDBport = \"{$port}\";
325 \$wgDBmwschema = \"{$schema}\";
326 \$wgDBts2schema = \"{$ts2}\";";
327 }
328
329 public function preUpgrade() {
330 global $wgDBuser, $wgDBpassword;
331
332 # Normal user and password are selected after this step, so for now
333 # just copy these two
334 $wgDBuser = $this->getVar( '_InstallUser' );
335 $wgDBpassword = $this->getVar( '_InstallPassword' );
336 }
337
338 private function setupPLpgSQL() {
339 $rows = $this->numRows(
340 $this->db->query( "SELECT 1 FROM pg_catalog.pg_language WHERE lanname = 'plpgsql'" )
341 );
342 if ( $rows < 1 ) {
343 // plpgsql is not installed, but if we have a pg_pltemplate table, we should be able to create it
344 $SQL = "SELECT 1 FROM pg_catalog.pg_class c JOIN pg_catalog.pg_namespace n ON (n.oid = c.relnamespace) ".
345 "WHERE relname = 'pg_pltemplate' AND nspname='pg_catalog'";
346 $rows = $this->numRows( $this->db->query( $SQL ) );
347 global $wgDBname;
348 if ( $rows >= 1 ) {
349 $result = $this->db->query( 'CREATE LANGUAGE plpgsql' );
350 if ( !$result ) {
351 return Status::newFatal( 'config-pg-no-plpgsql', $wgDBname );
352 }
353 } else {
354 return Status::newFatal( 'config-pg-no-plpgsql', $wgDBname );
355 }
356 }
357 return Status::newGood();
358 }
359 }