* Cleanup massive duplication across Database constructors. Default implementation...
[mediawiki.git] / includes / installer / PostgresInstaller.php
blob3f7d5beea85e36f1a582c319455c5bdc9f0a0195
1 <?php
2 /**
3 * PostgreSQL-specific installer.
5 * @file
6 * @ingroup Deployment
7 */
9 /**
10 * Class for setting up the MediaWiki database using Postgres.
12 * @ingroup Deployment
13 * @since 1.17
15 class PostgresInstaller extends DatabaseInstaller {
17 protected $globalNames = array(
18 'wgDBserver',
19 'wgDBport',
20 'wgDBname',
21 'wgDBuser',
22 'wgDBpassword',
23 'wgDBmwschema',
24 'wgDBts2schema',
27 var $minimumVersion = '8.1';
28 private $ts2MaxVersion = '8.3'; // Doing ts2 is not necessary in PG > 8.3
30 function getName() {
31 return 'postgres';
34 public function isCompiled() {
35 return self::checkExtension( 'pgsql' );
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();
51 function submitConnectForm() {
52 // Get variables from the request
53 $newValues = $this->setVarsFromRequest( array( 'wgDBserver', 'wgDBport',
54 'wgDBname', 'wgDBmwschema', 'wgDBts2schema' ) );
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'] );
63 if ( !preg_match( '/^[a-zA-Z0-9_]*$/', $newValues['wgDBmwschema'] ) ) {
64 $status->fatal( 'config-invalid-schema', $newValues['wgDBmwschema'] );
66 if ( !preg_match( '/^[a-zA-Z0-9_]*$/', $newValues['wgDBts2schema'] ) ) {
67 $status->fatal( 'config-invalid-ts2schema', $newValues['wgDBts2schema'] );
70 // Submit user box
71 if ( $status->isOK() ) {
72 $status->merge( $this->submitInstallUserBox() );
74 if ( !$status->isOK() ) {
75 return $status;
78 // Try to connect
79 $status->merge( $this->getConnection() );
80 if ( !$status->isOK() ) {
81 return $status;
84 /* //Make sure install user can create
85 $status->merge( $this->canCreateAccounts() );
86 if ( !$status->isOK() ) {
87 return $status;
88 } */
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 );
96 $this->setVar( 'wgDBuser', $this->getVar( '_InstallUser' ) );
97 $this->setVar( 'wgDBpassword', $this->getVar( '_InstallPassword' ) );
98 return $status;
101 function getConnection($database = 'template1') {
102 $status = Status::newGood();
103 if( is_null( $this->db ) ) {
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() );
114 } else {
115 $status->value = $this->db;
117 return $status;
120 protected function canCreateAccounts() {
121 $status = $this->getConnection();
122 if ( !$status->isOK() ) {
123 return false;
125 $conn = $status->value;
127 $superuser = $this->getVar( '_InstallUser' );
129 $rights = $conn->selectField( 'pg_catalog.pg_user',
130 'CASE WHEN usesuper IS TRUE THEN
131 CASE WHEN usecreatedb IS TRUE THEN 3 ELSE 1 END
132 ELSE CASE WHEN usecreatedb IS TRUE THEN 2 ELSE 0 END
133 END AS rights',
134 array( 'usename' => $superuser ), __METHOD__
137 if( !$rights ) {
138 return false;
141 if( $rights != 1 && $rights != 3 ) {
142 return false;
145 return true;
148 public function getSettingsForm() {
149 if ( $this->canCreateAccounts() ) {
150 $noCreateMsg = false;
151 } else {
152 $noCreateMsg = 'config-db-web-no-create-privs';
154 $s = $this->getWebUserBox( $noCreateMsg );
156 return $s;
159 public function submitSettingsForm() {
160 $status = $this->submitWebUserBox();
161 if ( !$status->isOK() ) {
162 return $status;
165 // Validate the create checkbox
166 $canCreate = $this->canCreateAccounts();
167 if ( !$canCreate ) {
168 $this->setVar( '_CreateDBAccount', false );
169 $create = false;
170 } else {
171 $create = $this->getVar( '_CreateDBAccount' );
174 if ( !$create ) {
175 // Test the web account
176 try {
177 new DatabasePostgres(
178 $this->getVar( 'wgDBserver' ),
179 $this->getVar( 'wgDBuser' ),
180 $this->getVar( 'wgDBpassword' ),
181 false,
182 false,
184 $this->getVar( 'wgDBprefix' )
186 } catch ( DBConnectionError $e ) {
187 return Status::newFatal( 'config-connection-error', $e->getMessage() );
191 return Status::newGood();
194 public function preInstall() {
195 $commitCB = array(
196 'name' => 'pg-commit',
197 'callback' => array( $this, 'commitChanges' ),
199 $userCB = array(
200 'name' => 'user',
201 'callback' => array( $this, 'setupUser' ),
203 $ts2CB = array(
204 'name' => 'pg-ts2',
205 'callback' => array( $this, 'setupTs2' ),
207 $plpgCB = array(
208 'name' => 'pg-plpgsql',
209 'callback' => array( $this, 'setupPLpgSQL' ),
211 $this->parent->addInstallStep( $commitCB, 'interwiki' );
212 $this->parent->addInstallStep( $userCB );
213 $this->parent->addInstallStep( $ts2CB, 'database' );
214 $this->parent->addInstallStep( $plpgCB, 'database' );
217 function setupDatabase() {
218 $status = $this->getConnection();
219 if ( !$status->isOK() ) {
220 return $status;
222 $conn = $status->value;
224 $dbName = $this->getVar( 'wgDBname' );
225 $SQL = "SELECT 1 FROM pg_catalog.pg_database WHERE datname = " . $conn->addQuotes( $dbName );
226 $rows = $conn->numRows( $conn->query( $SQL ) );
227 if( !$rows ) {
228 $schema = $this->getVar( 'wgDBmwschema' );
229 $user = $this->getVar( 'wgDBuser' );
231 $safeschema = $conn->addIdentifierQuotes( $schema );
232 $safeuser = $conn->addIdentifierQuotes( $user );
234 $safedb = $conn->addIdentifierQuotes( $dbName );
236 $conn->query( "CREATE DATABASE $safedb OWNER $safeuser", __METHOD__ );
238 $conn = new DatabasePostgres(
239 $this->getVar( 'wgDBserver' ),
240 $this->getVar( 'wgDBuser' ),
241 $this->getVar( 'wgDBpassword' ),
242 $dbName,
243 false,
245 $this->getVar( 'wgDBprefix' )
248 $result = $conn->schemaExists( $schema );
249 if( !$result ) {
250 $result = $conn->query( "CREATE SCHEMA $safeschema AUTHORIZATION $safeuser" );
251 if( !$result ) {
252 $status->fatal( 'config-install-pg-schema-failed', $user, $schema );
254 } else {
255 $safeschema2 = $conn->addQuotes( $schema );
256 $SQL = "SELECT 'GRANT ALL ON '||pg_catalog.quote_ident(relname)||' TO $safeuser;'\n".
257 "FROM pg_catalog.pg_class p, pg_catalog.pg_namespace n\n" .
258 "WHERE relnamespace = n.oid AND n.nspname = $safeschema2\n" .
259 "AND p.relkind IN ('r','S','v')\n";
260 $SQL .= "UNION\n";
261 $SQL .= "SELECT 'GRANT ALL ON FUNCTION '||pg_catalog.quote_ident(proname)||'('||\n".
262 "pg_catalog.oidvectortypes(p.proargtypes)||') TO $safeuser;'\n" .
263 "FROM pg_catalog.pg_proc p, pg_catalog.pg_namespace n\n" .
264 "WHERE p.pronamespace = n.oid AND n.nspname = $safeschema2";
265 $res = $conn->query( $SQL );
266 $conn->query( "SET search_path = $safeschema" );
269 return $status;
273 * Ts2 isn't needed in newer versions of Postgres, so wrap it in a nice big
274 * version check and skip it if we're new. Maybe we can bump $minimumVersion
275 * one day and render this obsolete :)
277 * @return Status
279 function setupTs2() {
280 $status = $this->getConnection();
281 if ( !$status->isOK() ) {
282 return $status;
285 if( version_compare( $this->db->getServerVersion(), $this->ts2MaxVersion, '<' ) ) {
286 if ( !$this->db->tableExists( 'pg_ts_cfg', $this->getVar( 'wgDBts2schema' ) ) ) {
287 return Status::newFatal(
288 'config-install-pg-ts2-failed',
289 $this->getVar( 'wgDBname' ),
290 'http://www.devx.com/opensource/Article/21674/0/page/2'
293 $safeuser = $this->db->addQuotes( $this->getVar( 'wgDBuser' ) );
294 foreach ( array( 'cfg', 'cfgmap', 'dict', 'parser' ) as $table ) {
295 $sql = "GRANT SELECT ON pg_ts_$table TO $safeuser";
296 $this->db->query( $sql, __METHOD__ );
299 return Status::newGood();
302 function commitChanges() {
303 $this->db->query( 'COMMIT' );
304 return Status::newGood();
307 function setupUser() {
308 if ( !$this->getVar( '_CreateDBAccount' ) ) {
309 return Status::newGood();
312 $status = $this->getConnection();
313 if ( !$status->isOK() ) {
314 return $status;
317 $db = $this->getVar( 'wgDBname' );
318 $this->db->selectDB( $db );
319 $safeuser = $this->db->addIdentifierQuotes( $this->getVar( 'wgDBuser' ) );
320 $safepass = $this->db->addQuotes( $this->getVar( 'wgDBpassword' ) );
321 $res = $this->db->query( "CREATE USER $safeuser NOCREATEDB PASSWORD $safepass", __METHOD__ );
322 return $status;
324 if ( $res !== true ) {
325 $status->fatal( 'config-install-user-failed', $this->getVar( 'wgDBuser' ) );
328 return $status;
331 function getLocalSettings() {
332 $port = $this->getVar( 'wgDBport' );
333 $schema = $this->getVar( 'wgDBmwschema' );
334 $ts2 = $this->getVar( 'wgDBts2schema' );
335 return
336 "# Postgres specific settings
337 \$wgDBport = \"{$port}\";
338 \$wgDBmwschema = \"{$schema}\";
339 \$wgDBts2schema = \"{$ts2}\";";
342 public function preUpgrade() {
343 global $wgDBuser, $wgDBpassword;
345 # Normal user and password are selected after this step, so for now
346 # just copy these two
347 $wgDBuser = $this->getVar( '_InstallUser' );
348 $wgDBpassword = $this->getVar( '_InstallPassword' );
351 public function setupPLpgSQL() {
352 $status = $this->getConnection();
353 if ( !$status->isOK() ) {
354 return $status;
357 $rows = $this->db->numRows(
358 $this->db->query( "SELECT 1 FROM pg_catalog.pg_language WHERE lanname = 'plpgsql'" )
360 if ( $rows < 1 ) {
361 // plpgsql is not installed, but if we have a pg_pltemplate table, we should be able to create it
362 $SQL = "SELECT 1 FROM pg_catalog.pg_class c JOIN pg_catalog.pg_namespace n ON (n.oid = c.relnamespace) ".
363 "WHERE relname = 'pg_pltemplate' AND nspname='pg_catalog'";
364 $rows = $this->db->numRows( $this->db->query( $SQL ) );
365 $dbName = $this->getVar( 'wgDBname' );
366 if ( $rows >= 1 ) {
367 $result = $this->db->query( 'CREATE LANGUAGE plpgsql' );
368 if ( !$result ) {
369 return Status::newFatal( 'config-pg-no-plpgsql', $dbName );
371 } else {
372 return Status::newFatal( 'config-pg-no-plpgsql', $dbName );
375 return Status::newGood();