Per comment on bug 26612, we should drop the pre-Postgres 8.3 support with the TSearc...
[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.3';
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 // Try to connect
74 $status->merge( $this->getConnection() );
75 if ( !$status->isOK() ) {
76 return $status;
77 }
78
79 /* //Make sure install user can create
80 $status->merge( $this->canCreateAccounts() );
81 if ( !$status->isOK() ) {
82 return $status;
83 } */
84
85 // Check version
86 $version = $this->db->getServerVersion();
87 if ( version_compare( $version, $this->minimumVersion ) < 0 ) {
88 return Status::newFatal( 'config-postgres-old', $this->minimumVersion, $version );
89 }
90
91 $this->setVar( 'wgDBuser', $this->getVar( '_InstallUser' ) );
92 $this->setVar( 'wgDBpassword', $this->getVar( '_InstallPassword' ) );
93 return $status;
94 }
95
96 function openConnection( $database = 'template1' ) {
97 $status = Status::newGood();
98 try {
99 $db = new DatabasePostgres(
100 $this->getVar( 'wgDBserver' ),
101 $this->getVar( '_InstallUser' ),
102 $this->getVar( '_InstallPassword' ),
103 $database );
104 $status->value = $db;
105 } catch ( DBConnectionError $e ) {
106 $status->fatal( 'config-connection-error', $e->getMessage() );
107 }
108 return $status;
109 }
110
111 protected function canCreateAccounts() {
112 $status = $this->getConnection();
113 if ( !$status->isOK() ) {
114 return false;
115 }
116 $conn = $status->value;
117
118 $superuser = $this->getVar( '_InstallUser' );
119
120 $rights = $conn->selectField( 'pg_catalog.pg_user',
121 'CASE WHEN usesuper IS TRUE THEN
122 CASE WHEN usecreatedb IS TRUE THEN 3 ELSE 1 END
123 ELSE CASE WHEN usecreatedb IS TRUE THEN 2 ELSE 0 END
124 END AS rights',
125 array( 'usename' => $superuser ), __METHOD__
126 );
127
128 if( !$rights ) {
129 return false;
130 }
131
132 if( $rights != 1 && $rights != 3 ) {
133 return false;
134 }
135
136 return true;
137 }
138
139 public function getSettingsForm() {
140 if ( $this->canCreateAccounts() ) {
141 $noCreateMsg = false;
142 } else {
143 $noCreateMsg = 'config-db-web-no-create-privs';
144 }
145 $s = $this->getWebUserBox( $noCreateMsg );
146
147 return $s;
148 }
149
150 public function submitSettingsForm() {
151 $status = $this->submitWebUserBox();
152 if ( !$status->isOK() ) {
153 return $status;
154 }
155
156 // Validate the create checkbox
157 $canCreate = $this->canCreateAccounts();
158 if ( !$canCreate ) {
159 $this->setVar( '_CreateDBAccount', false );
160 $create = false;
161 } else {
162 $create = $this->getVar( '_CreateDBAccount' );
163 }
164
165 if ( !$create ) {
166 // Test the web account
167 try {
168 new DatabasePostgres(
169 $this->getVar( 'wgDBserver' ),
170 $this->getVar( 'wgDBuser' ),
171 $this->getVar( 'wgDBpassword' ),
172 false,
173 false,
174 0,
175 $this->getVar( 'wgDBprefix' )
176 );
177 } catch ( DBConnectionError $e ) {
178 return Status::newFatal( 'config-connection-error', $e->getMessage() );
179 }
180 }
181
182 return Status::newGood();
183 }
184
185 public function preInstall() {
186 $commitCB = array(
187 'name' => 'pg-commit',
188 'callback' => array( $this, 'commitChanges' ),
189 );
190 $userCB = array(
191 'name' => 'user',
192 'callback' => array( $this, 'setupUser' ),
193 );
194 $plpgCB = array(
195 'name' => 'pg-plpgsql',
196 'callback' => array( $this, 'setupPLpgSQL' ),
197 );
198 $this->parent->addInstallStep( $commitCB, 'interwiki' );
199 $this->parent->addInstallStep( $userCB );
200 $this->parent->addInstallStep( $plpgCB, 'database' );
201 }
202
203 function setupDatabase() {
204 $status = $this->getConnection();
205 if ( !$status->isOK() ) {
206 return $status;
207 }
208 $this->setupSchemaVars();
209 $conn = $status->value;
210
211 $dbName = $this->getVar( 'wgDBname' );
212 $SQL = "SELECT 1 FROM pg_catalog.pg_database WHERE datname = " . $conn->addQuotes( $dbName );
213 $rows = $conn->numRows( $conn->query( $SQL ) );
214 if( !$rows ) {
215 $schema = $this->getVar( 'wgDBmwschema' );
216 $user = $this->getVar( 'wgDBuser' );
217
218 $safeschema = $conn->addIdentifierQuotes( $schema );
219 $safeuser = $conn->addIdentifierQuotes( $user );
220
221 $safedb = $conn->addIdentifierQuotes( $dbName );
222
223 $conn->query( "CREATE DATABASE $safedb OWNER $safeuser", __METHOD__ );
224
225 $conn = new DatabasePostgres(
226 $this->getVar( 'wgDBserver' ),
227 $this->getVar( 'wgDBuser' ),
228 $this->getVar( 'wgDBpassword' ),
229 $dbName,
230 false,
231 0,
232 $this->getVar( 'wgDBprefix' )
233 );
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 $res = $conn->query( $SQL );
253 $conn->query( "SET search_path = $safeschema" );
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 $status = $this->getConnection();
270 if ( !$status->isOK() ) {
271 return $status;
272 }
273
274 $db = $this->getVar( 'wgDBname' );
275 $this->db->selectDB( $db );
276 $safeuser = $this->db->addIdentifierQuotes( $this->getVar( 'wgDBuser' ) );
277 $safepass = $this->db->addQuotes( $this->getVar( 'wgDBpassword' ) );
278 $res = $this->db->query( "CREATE USER $safeuser NOCREATEDB PASSWORD $safepass", __METHOD__ );
279 return $status;
280
281 if ( $res !== true ) {
282 $status->fatal( 'config-install-user-failed', $this->getVar( 'wgDBuser' ) );
283 }
284
285 return $status;
286 }
287
288 function getLocalSettings() {
289 $port = $this->getVar( 'wgDBport' );
290 $schema = $this->getVar( 'wgDBmwschema' );
291 $ts2 = $this->getVar( 'wgDBts2schema' );
292 return
293 "# Postgres specific settings
294 \$wgDBport = \"{$port}\";
295 \$wgDBmwschema = \"{$schema}\";
296 \$wgDBts2schema = \"{$ts2}\";";
297 }
298
299 public function preUpgrade() {
300 global $wgDBuser, $wgDBpassword;
301
302 # Normal user and password are selected after this step, so for now
303 # just copy these two
304 $wgDBuser = $this->getVar( '_InstallUser' );
305 $wgDBpassword = $this->getVar( '_InstallPassword' );
306 }
307
308 public function setupPLpgSQL() {
309 $status = $this->getConnection();
310 if ( !$status->isOK() ) {
311 return $status;
312 }
313
314 $rows = $this->db->numRows(
315 $this->db->query( "SELECT 1 FROM pg_catalog.pg_language WHERE lanname = 'plpgsql'" )
316 );
317 if ( $rows < 1 ) {
318 // plpgsql is not installed, but if we have a pg_pltemplate table, we should be able to create it
319 $SQL = "SELECT 1 FROM pg_catalog.pg_class c JOIN pg_catalog.pg_namespace n ON (n.oid = c.relnamespace) ".
320 "WHERE relname = 'pg_pltemplate' AND nspname='pg_catalog'";
321 $rows = $this->db->numRows( $this->db->query( $SQL ) );
322 $dbName = $this->getVar( 'wgDBname' );
323 if ( $rows >= 1 ) {
324 $result = $this->db->query( 'CREATE LANGUAGE plpgsql' );
325 if ( !$result ) {
326 return Status::newFatal( 'config-pg-no-plpgsql', $dbName );
327 }
328 } else {
329 return Status::newFatal( 'config-pg-no-plpgsql', $dbName );
330 }
331 }
332 return Status::newGood();
333 }
334 }