Moved constant values from initialiseFromUser() to class definition
[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
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 $this->getTextBox( 'wgDBts2schema', 'config-db-ts2-schema' ) .
46 Html::closeElement( 'fieldset' ) .
47 $this->getInstallUserBox();
48 }
49
50 function submitConnectForm() {
51 // Get variables from the request
52 $newValues = $this->setVarsFromRequest( array( 'wgDBserver', 'wgDBport',
53 'wgDBname', 'wgDBmwschema', 'wgDBts2schema' ) );
54
55 // Validate them
56 $status = Status::newGood();
57 if ( !strlen( $newValues['wgDBname'] ) ) {
58 $status->fatal( 'config-missing-db-name' );
59 } elseif ( !preg_match( '/^[a-zA-Z0-9_]+$/', $newValues['wgDBname'] ) ) {
60 $status->fatal( 'config-invalid-db-name', $newValues['wgDBname'] );
61 }
62 if ( !preg_match( '/^[a-zA-Z0-9_]*$/', $newValues['wgDBmwschema'] ) ) {
63 $status->fatal( 'config-invalid-schema', $newValues['wgDBmwschema'] );
64 }
65 if ( !preg_match( '/^[a-zA-Z0-9_]*$/', $newValues['wgDBts2schema'] ) ) {
66 $status->fatal( 'config-invalid-ts2schema', $newValues['wgDBts2schema'] );
67 }
68
69 // Submit user box
70 if ( $status->isOK() ) {
71 $status->merge( $this->submitInstallUserBox() );
72 }
73 if ( !$status->isOK() ) {
74 return $status;
75 }
76
77 // Try to connect
78 $status->merge( $this->getConnection() );
79 if ( !$status->isOK() ) {
80 return $status;
81 }
82
83 // Check version
84 $version = $this->db->getServerVersion();
85 if ( version_compare( $version, $this->minimumVersion ) < 0 ) {
86 return Status::newFatal( 'config-postgres-old', $this->minimumVersion, $version );
87 }
88
89 $this->setVar( 'wgDBuser', $this->getVar( '_InstallUser' ) );
90 $this->setVar( 'wgDBpassword', $this->getVar( '_InstallPassword' ) );
91 return $status;
92 }
93
94 function getConnection() {
95 $status = Status::newGood();
96
97 try {
98 $this->db = new DatabasePostgres(
99 $this->getVar( 'wgDBserver' ),
100 $this->getVar( '_InstallUser' ),
101 $this->getVar( '_InstallPassword' ),
102 $this->getVar( 'wgDBname' ) );
103 $status->value = $this->db;
104 } catch ( DBConnectionError $e ) {
105 $status->fatal( 'config-connection-error', $e->getMessage() );
106 }
107 return $status;
108 }
109
110 public function preInstall() {
111 # Add our user callback to installSteps, right before the tables are created.
112 $callback = array(
113 'name' => 'pg-commit',
114 'callback' => array( $this, 'commitChanges' ),
115 );
116 $this->parent->addInstallStepFollowing( 'interwiki', $callback );
117 }
118
119 function setupDatabase() {
120 $status = $this->getConnection();
121 if ( !$status->isOK() ) {
122 return $status;
123 }
124 $conn = $status->value;
125
126 // Make sure that we can write to the correct schema
127 // If not, Postgres will happily and silently go to the next search_path item
128 $schema = $this->getVar( 'wgDBmwschema' );
129 $ctest = 'mediawiki_test_table';
130 $safeschema = $conn->addIdentifierQuotes( $schema );
131 if ( $conn->tableExists( $ctest, $schema ) ) {
132 $conn->query( "DROP TABLE $safeschema.$ctest" );
133 }
134 $res = $conn->query( "CREATE TABLE $safeschema.$ctest(a int)" );
135 if ( !$res ) {
136 $status->fatal( 'config-install-pg-schema-failed',
137 $this->getVar( 'wgDBuser'), $schema );
138 }
139 $conn->query( "DROP TABLE $safeschema.$ctest" );
140
141 return $status;
142 }
143
144 function commitChanges() {
145 $this->db->query( 'COMMIT' );
146
147 return Status::newGood();
148 }
149
150 function getLocalSettings() {
151 $port = $this->getVar( 'wgDBport' );
152 $schema = $this->getVar( 'wgDBmwschema' );
153 $ts2 = $this->getVar( 'wgDBts2schema' );
154 return
155 "# Postgres specific settings
156 \$wgDBport = \"{$port}\";
157 \$wgDBmwschema = \"{$schema}\";
158 \$wgDBts2schema = \"{$ts2}\";";
159 }
160
161 public function preUpgrade() {
162 global $wgDBuser, $wgDBpassword;
163
164 # Normal user and password are selected after this step, so for now
165 # just copy these two
166 $wgDBuser = $this->getVar( '_InstallUser' );
167 $wgDBpassword = $this->getVar( '_InstallPassword' );
168 }
169 }