Forgot param 1, make URL param 2 per Nikerabbit
[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 // 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 getConnection() {
96 $status = Status::newGood();
97
98 try {
99 $this->db = new DatabasePostgres(
100 $this->getVar( 'wgDBserver' ),
101 $this->getVar( '_InstallUser' ),
102 $this->getVar( '_InstallPassword' ),
103 false );
104 $status->value = $this->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->query( 'pg_catalog.pg_user',
121 'SELECT
122 CASE WHEN usesuper IS TRUE THEN
123 CASE WHEN usecreatedb IS TRUE THEN 3 ELSE 1 END
124 ELSE CASE WHEN usecreatedb IS TRUE THEN 2 ELSE 0 END
125 END AS rights',
126 array( 'usename' => $superuser ), __METHOD__
127 );
128
129 if( !$rights ) {
130 return false;
131 }
132
133 if( $rights != 1 && $rights != 3 ) {
134 return false;
135 }
136
137 return true;
138 }
139
140 public function preInstall() {
141 $commitCB = array(
142 'name' => 'pg-commit',
143 'callback' => array( $this, 'commitChanges' ),
144 );
145 $userCB = array(
146 'name' => 'user',
147 'callback' => array( $this, 'setupUser' ),
148 );
149 $ts2CB = array(
150 'name' => 'pg-ts2',
151 'callback' => array( $this, 'setupTs2' ),
152 );
153 $this->parent->addInstallStep( $commitCB, 'interwiki' );
154 $this->parent->addInstallStep( $userCB );
155 }
156
157 function setupDatabase() {
158 $status = $this->getConnection();
159 if ( !$status->isOK() ) {
160 return $status;
161 }
162 $conn = $status->value;
163
164 // Make sure that we can write to the correct schema
165 // If not, Postgres will happily and silently go to the next search_path item
166 $schema = $this->getVar( 'wgDBmwschema' );
167 $ctest = 'mediawiki_test_table';
168 $safeschema = $conn->addIdentifierQuotes( $schema );
169 if ( $conn->tableExists( $ctest, $schema ) ) {
170 $conn->query( "DROP TABLE $safeschema.$ctest" );
171 }
172 $res = $conn->query( "CREATE TABLE $safeschema.$ctest(a int)" );
173 if ( !$res ) {
174 $status->fatal( 'config-install-pg-schema-failed',
175 $this->getVar( 'wgDBuser'), $schema );
176 }
177 $conn->query( "DROP TABLE $safeschema.$ctest" );
178
179 $dbName = $this->getVar( 'wgDBname' );
180 if( !$conn->selectDB( $dbName ) ) {
181 $safedb = $conn->addIdentifierQuotes( $dbName );
182 $safeuser = $conn->addQuotes( $this->getVar( 'wgDBuser' ) );
183 $conn->query( "CREATE DATABASE $safedb OWNER $safeuser", __METHOD__ );
184 $conn->selectDB( $dbName );
185 }
186 return $status;
187 }
188
189 /**
190 * Ts2 isn't needed in newer versions of Postgres, so wrap it in a nice big
191 * version check and skip it if we're new. Maybe we can bump $minimumVersion
192 * one day and render this obsolete :)
193 *
194 * @return Status
195 */
196 function setupTs2() {
197 if( version_compare( $this->db->getServerVersion(), $this->ts2MaxVersion, '<' ) ) {
198 if ( !$this->db->tableExists( 'pg_ts_cfg', $wgDBts2schema ) ) {
199 return Status::newFatal(
200 'config-install-pg-ts2-failed',
201 $this->getVar( 'wgDBname' ),
202 'http://www.devx.com/opensource/Article/21674/0/page/2'
203 );
204 }
205 $safeuser = $this->db->addQuotes( $this->getVar( 'wgDBuser' ) );
206 foreach ( array( 'cfg', 'cfgmap', 'dict', 'parser' ) as $table ) {
207 $sql = "GRANT SELECT ON pg_ts_$table TO $safeuser";
208 $this->db->query( $sql, __METHOD__ );
209 }
210 }
211 return Status::newGood();
212 }
213
214 function commitChanges() {
215 $this->db->query( 'COMMIT' );
216 return Status::newGood();
217 }
218
219 function setupUser() {
220 if ( !$this->getVar( '_CreateDBAccount' ) ) {
221 return Status::newGood();
222 }
223
224 $status = $this->getConnection();
225 if ( !$status->isOK() ) {
226 return $status;
227 }
228
229 $db = $this->getVar( 'wgDBname' );
230 $this->db->selectDB( $db );
231 $safeuser = $this->db->addQuotes( $this->getVar( 'wgDBuser' ) );
232 $safepass = $this->db->addQuotes( $this->getVar( 'wgDBpassword' ) );
233 $res = $this->db->query( "CREATE USER $safeuser NOCREATEDB PASSWORD $safepass", __METHOD__ );
234 if ( $res !== true ) {
235 $status->fatal( 'config-install-user-failed', $this->getVar( 'wgDBuser' ) );
236 }
237
238 return $status;
239 }
240
241 function getLocalSettings() {
242 $port = $this->getVar( 'wgDBport' );
243 $schema = $this->getVar( 'wgDBmwschema' );
244 $ts2 = $this->getVar( 'wgDBts2schema' );
245 return
246 "# Postgres specific settings
247 \$wgDBport = \"{$port}\";
248 \$wgDBmwschema = \"{$schema}\";
249 \$wgDBts2schema = \"{$ts2}\";";
250 }
251
252 public function preUpgrade() {
253 global $wgDBuser, $wgDBpassword;
254
255 # Normal user and password are selected after this step, so for now
256 # just copy these two
257 $wgDBuser = $this->getVar( '_InstallUser' );
258 $wgDBpassword = $this->getVar( '_InstallPassword' );
259 }
260
261 private function setupPLpgSQL() {
262 $rows = $this->numRows(
263 $this->db->query( "SELECT 1 FROM pg_catalog.pg_language WHERE lanname = 'plpgsql'" )
264 );
265 if ( $rows < 1 ) {
266 // plpgsql is not installed, but if we have a pg_pltemplate table, we should be able to create it
267 $SQL = "SELECT 1 FROM pg_catalog.pg_class c JOIN pg_catalog.pg_namespace n ON (n.oid = c.relnamespace) ".
268 "WHERE relname = 'pg_pltemplate' AND nspname='pg_catalog'";
269 $rows = $this->numRows( $this->db->query( $SQL ) );
270 global $wgDBname;
271 if ( $rows >= 1 ) {
272 $result = $this->db->query( 'CREATE LANGUAGE plpgsql' );
273 if ( !$result ) {
274 return Status::newFatal( 'config-pg-no-plpgsql', $wgDBname );
275 }
276 } else {
277 return Status::newFatal( 'config-pg-no-plpgsql', $wgDBname );
278 }
279 }
280 return Status::newGood();
281 }
282 }