Followup r81132, missed some more ts2 removal
[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() );
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 protected function canCreateAccounts() {
111 $status = $this->getConnection();
112 if ( !$status->isOK() ) {
113 return false;
114 }
115 $conn = $status->value;
116
117 $superuser = $this->getVar( '_InstallUser' );
118
119 $rights = $conn->selectField( 'pg_catalog.pg_user',
120 'CASE WHEN usesuper IS TRUE THEN
121 CASE WHEN usecreatedb IS TRUE THEN 3 ELSE 1 END
122 ELSE CASE WHEN usecreatedb IS TRUE THEN 2 ELSE 0 END
123 END AS rights',
124 array( 'usename' => $superuser ), __METHOD__
125 );
126
127 if( !$rights ) {
128 return false;
129 }
130
131 if( $rights != 1 && $rights != 3 ) {
132 return false;
133 }
134
135 return true;
136 }
137
138 public function getSettingsForm() {
139 if ( $this->canCreateAccounts() ) {
140 $noCreateMsg = false;
141 } else {
142 $noCreateMsg = 'config-db-web-no-create-privs';
143 }
144 $s = $this->getWebUserBox( $noCreateMsg );
145
146 return $s;
147 }
148
149 public function submitSettingsForm() {
150 $status = $this->submitWebUserBox();
151 if ( !$status->isOK() ) {
152 return $status;
153 }
154
155 // Validate the create checkbox
156 $canCreate = $this->canCreateAccounts();
157 if ( !$canCreate ) {
158 $this->setVar( '_CreateDBAccount', false );
159 $create = false;
160 } else {
161 $create = $this->getVar( '_CreateDBAccount' );
162 }
163
164 if ( !$create ) {
165 // Test the web account
166 try {
167 new DatabasePostgres(
168 $this->getVar( 'wgDBserver' ),
169 $this->getVar( 'wgDBuser' ),
170 $this->getVar( 'wgDBpassword' ),
171 false,
172 false,
173 0,
174 $this->getVar( 'wgDBprefix' )
175 );
176 } catch ( DBConnectionError $e ) {
177 return Status::newFatal( 'config-connection-error', $e->getMessage() );
178 }
179 }
180
181 return Status::newGood();
182 }
183
184 public function preInstall() {
185 $commitCB = array(
186 'name' => 'pg-commit',
187 'callback' => array( $this, 'commitChanges' ),
188 );
189 $userCB = array(
190 'name' => 'user',
191 'callback' => array( $this, 'setupUser' ),
192 );
193 $plpgCB = array(
194 'name' => 'pg-plpgsql',
195 'callback' => array( $this, 'setupPLpgSQL' ),
196 );
197 $this->parent->addInstallStep( $commitCB, 'interwiki' );
198 $this->parent->addInstallStep( $userCB );
199 $this->parent->addInstallStep( $plpgCB, 'database' );
200 }
201
202 function setupDatabase() {
203 $status = $this->getConnection();
204 if ( !$status->isOK() ) {
205 return $status;
206 }
207 $this->setupSchemaVars();
208 $conn = $status->value;
209
210 $dbName = $this->getVar( 'wgDBname' );
211 $SQL = "SELECT 1 FROM pg_catalog.pg_database WHERE datname = " . $conn->addQuotes( $dbName );
212 $rows = $conn->numRows( $conn->query( $SQL ) );
213 if( !$rows ) {
214 $schema = $this->getVar( 'wgDBmwschema' );
215 $user = $this->getVar( 'wgDBuser' );
216
217 $safeschema = $conn->addIdentifierQuotes( $schema );
218 $safeuser = $conn->addIdentifierQuotes( $user );
219
220 $safedb = $conn->addIdentifierQuotes( $dbName );
221
222 $conn->query( "CREATE DATABASE $safedb OWNER $safeuser", __METHOD__ );
223
224 $conn = new DatabasePostgres(
225 $this->getVar( 'wgDBserver' ),
226 $this->getVar( 'wgDBuser' ),
227 $this->getVar( 'wgDBpassword' ),
228 $dbName,
229 false,
230 0,
231 $this->getVar( 'wgDBprefix' )
232 );
233
234 $result = $conn->schemaExists( $schema );
235 if( !$result ) {
236 $result = $conn->query( "CREATE SCHEMA $safeschema AUTHORIZATION $safeuser" );
237 if( !$result ) {
238 $status->fatal( 'config-install-pg-schema-failed', $user, $schema );
239 }
240 } else {
241 $safeschema2 = $conn->addQuotes( $schema );
242 $SQL = "SELECT 'GRANT ALL ON '||pg_catalog.quote_ident(relname)||' TO $safeuser;'\n".
243 "FROM pg_catalog.pg_class p, pg_catalog.pg_namespace n\n" .
244 "WHERE relnamespace = n.oid AND n.nspname = $safeschema2\n" .
245 "AND p.relkind IN ('r','S','v')\n";
246 $SQL .= "UNION\n";
247 $SQL .= "SELECT 'GRANT ALL ON FUNCTION '||pg_catalog.quote_ident(proname)||'('||\n".
248 "pg_catalog.oidvectortypes(p.proargtypes)||') TO $safeuser;'\n" .
249 "FROM pg_catalog.pg_proc p, pg_catalog.pg_namespace n\n" .
250 "WHERE p.pronamespace = n.oid AND n.nspname = $safeschema2";
251 $res = $conn->query( $SQL );
252 $conn->query( "SET search_path = $safeschema" );
253 }
254 }
255 return $status;
256 }
257
258 function commitChanges() {
259 $this->db->query( 'COMMIT' );
260 return Status::newGood();
261 }
262
263 function setupUser() {
264 if ( !$this->getVar( '_CreateDBAccount' ) ) {
265 return Status::newGood();
266 }
267
268 $status = $this->getConnection();
269 if ( !$status->isOK() ) {
270 return $status;
271 }
272
273 $db = $this->getVar( 'wgDBname' );
274 $this->db->selectDB( $db );
275 $safeuser = $this->db->addIdentifierQuotes( $this->getVar( 'wgDBuser' ) );
276 $safepass = $this->db->addQuotes( $this->getVar( 'wgDBpassword' ) );
277 $res = $this->db->query( "CREATE USER $safeuser NOCREATEDB PASSWORD $safepass", __METHOD__ );
278 return $status;
279
280 if ( $res !== true ) {
281 $status->fatal( 'config-install-user-failed', $this->getVar( 'wgDBuser' ) );
282 }
283
284 return $status;
285 }
286
287 function getLocalSettings() {
288 $port = $this->getVar( 'wgDBport' );
289 $schema = $this->getVar( 'wgDBmwschema' );
290 return
291 "# Postgres specific settings
292 \$wgDBport = \"{$port}\";
293 \$wgDBmwschema = \"{$schema}\";";
294 }
295
296 public function preUpgrade() {
297 global $wgDBuser, $wgDBpassword;
298
299 # Normal user and password are selected after this step, so for now
300 # just copy these two
301 $wgDBuser = $this->getVar( '_InstallUser' );
302 $wgDBpassword = $this->getVar( '_InstallPassword' );
303 }
304
305 public function setupPLpgSQL() {
306 $status = $this->getConnection();
307 if ( !$status->isOK() ) {
308 return $status;
309 }
310
311 $rows = $this->db->numRows(
312 $this->db->query( "SELECT 1 FROM pg_catalog.pg_language WHERE lanname = 'plpgsql'" )
313 );
314 if ( $rows < 1 ) {
315 // plpgsql is not installed, but if we have a pg_pltemplate table, we should be able to create it
316 $SQL = "SELECT 1 FROM pg_catalog.pg_class c JOIN pg_catalog.pg_namespace n ON (n.oid = c.relnamespace) ".
317 "WHERE relname = 'pg_pltemplate' AND nspname='pg_catalog'";
318 $rows = $this->db->numRows( $this->db->query( $SQL ) );
319 $dbName = $this->getVar( 'wgDBname' );
320 if ( $rows >= 1 ) {
321 $result = $this->db->query( 'CREATE LANGUAGE plpgsql' );
322 if ( !$result ) {
323 return Status::newFatal( 'config-pg-no-plpgsql', $dbName );
324 }
325 } else {
326 return Status::newFatal( 'config-pg-no-plpgsql', $dbName );
327 }
328 }
329 return Status::newGood();
330 }
331 }