misc style fix
[lhc/web/wiklou.git] / includes / installer / Ibm_db2Installer.php
1 <?php
2 /**
3 * IBM_DB2-specific installer.
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 * @ingroup Deployment
22 */
23
24 /**
25 * Class for setting up the MediaWiki database using IBM_DB2.
26 *
27 * @ingroup Deployment
28 * @since 1.17
29 */
30 class Ibm_db2Installer extends DatabaseInstaller {
31
32
33 protected $globalNames = array(
34 'wgDBserver',
35 'wgDBport',
36 'wgDBname',
37 'wgDBuser',
38 'wgDBpassword',
39 'wgDBmwschema',
40 );
41
42 protected $internalDefaults = array(
43 '_InstallUser' => 'db2admin'
44 );
45
46 /**
47 * Get the DB2 database extension name
48 * @return string
49 */
50 public function getName() {
51 return 'ibm_db2';
52 }
53
54 /**
55 * Determine whether the DB2 database extension is currently available in PHP
56 * @return boolean
57 */
58 public function isCompiled() {
59 return self::checkExtension( 'ibm_db2' );
60 }
61
62 /**
63 * Generate a connection form for a DB2 database
64 * @return string
65 */
66 public function getConnectForm() {
67 return
68 $this->getTextBox( 'wgDBserver', 'config-db-host', array(), $this->parent->getHelpBox( 'config-db-host-help' ) ) .
69 $this->getTextBox( 'wgDBport', 'config-db-port', array(), $this->parent->getHelpBox( 'config-db-port' ) ) .
70 Html::openElement( 'fieldset' ) .
71 Html::element( 'legend', array(), wfMessage( 'config-db-wiki-settings' )->text() ) .
72 $this->getTextBox( 'wgDBname', 'config-db-name', array(), $this->parent->getHelpBox( 'config-db-name-help' ) ) .
73 $this->getTextBox( 'wgDBmwschema', 'config-db-schema', array(), $this->parent->getHelpBox( 'config-db-schema-help' ) ) .
74 Html::closeElement( 'fieldset' ) .
75 $this->getInstallUserBox();
76 }
77
78 /**
79 * Validate and then execute the connection form for a DB2 database
80 * @return Status
81 */
82 public function submitConnectForm() {
83 // Get variables from the request
84 $newValues = $this->setVarsFromRequest(
85 array( 'wgDBserver', 'wgDBport', 'wgDBname',
86 'wgDBmwschema', 'wgDBuser', 'wgDBpassword' ) );
87
88 // Validate them
89 $status = Status::newGood();
90 if ( !strlen( $newValues['wgDBname'] ) ) {
91 $status->fatal( 'config-missing-db-name' );
92 } elseif ( !preg_match( '/^[a-zA-Z0-9_]+$/', $newValues['wgDBname'] ) ) {
93 $status->fatal( 'config-invalid-db-name', $newValues['wgDBname'] );
94 }
95 if ( !strlen( $newValues['wgDBmwschema'] ) ) {
96 $status->fatal( 'config-invalid-schema' );
97 }
98 elseif ( !preg_match( '/^[a-zA-Z0-9_]*$/', $newValues['wgDBmwschema'] ) ) {
99 $status->fatal( 'config-invalid-schema', $newValues['wgDBmwschema'] );
100 }
101 if ( !strlen( $newValues['wgDBport'] ) ) {
102 $status->fatal( 'config-invalid-port' );
103 }
104 elseif ( !preg_match( '/^[0-9_]*$/', $newValues['wgDBport'] ) ) {
105 $status->fatal( 'config-invalid-port', $newValues['wgDBport'] );
106 }
107
108 // Submit user box
109 if ( $status->isOK() ) {
110 $status->merge( $this->submitInstallUserBox() );
111 }
112 if ( !$status->isOK() ) {
113 return $status;
114 }
115
116 global $wgDBport;
117 $wgDBport = $newValues['wgDBport'];
118
119 // Try to connect
120 $status->merge( $this->getConnection() );
121 if ( !$status->isOK() ) {
122 return $status;
123 }
124
125 $this->parent->setVar( 'wgDBuser', $this->getVar( '_InstallUser' ) );
126 $this->parent->setVar( 'wgDBpassword', $this->getVar( '_InstallPassword' ) );
127
128 return $status;
129 }
130
131 /**
132 * Open a DB2 database connection
133 * @return Status
134 */
135 public function openConnection() {
136 $status = Status::newGood();
137 try {
138 $db = new DatabaseIbm_db2(
139 $this->getVar( 'wgDBserver' ),
140 $this->getVar( '_InstallUser' ),
141 $this->getVar( '_InstallPassword' ),
142 $this->getVar( 'wgDBname' ),
143 0,
144 $this->getVar( 'wgDBmwschema' )
145 );
146 $status->value = $db;
147 } catch ( DBConnectionError $e ) {
148 $status->fatal( 'config-connection-error', $e->getMessage() );
149 }
150 return $status;
151 }
152
153 /**
154 * Create a DB2 database for MediaWiki
155 * @return Status
156 */
157 public function setupDatabase() {
158 $status = $this->getConnection();
159 if ( !$status->isOK() ) {
160 return $status;
161 }
162 /**
163 * @var $conn DatabaseBase
164 */
165 $conn = $status->value;
166 $dbName = $this->getVar( 'wgDBname' );
167 if( !$conn->selectDB( $dbName ) ) {
168 $conn->query( "CREATE DATABASE "
169 . $conn->addIdentifierQuotes( $dbName )
170 . " AUTOMATIC STORAGE YES"
171 . " USING CODESET UTF-8 TERRITORY US COLLATE USING SYSTEM"
172 . " PAGESIZE 32768", __METHOD__ );
173 $conn->selectDB( $dbName );
174 }
175 $this->setupSchemaVars();
176 return $status;
177 }
178
179 /**
180 * Create tables from scratch.
181 * First check if pagesize >= 32k.
182 *
183 * @return Status
184 */
185 public function createTables() {
186 $status = $this->getConnection();
187 if ( !$status->isOK() ) {
188 return $status;
189 }
190 $this->db->selectDB( $this->getVar( 'wgDBname' ) );
191
192 if( $this->db->tableExists( 'user' ) ) {
193 $status->warning( 'config-install-tables-exist' );
194 return $status;
195 }
196
197 /* Check for pagesize */
198 $status = $this->checkPageSize();
199 if ( !$status->isOK() ) {
200 return $status;
201 }
202
203 $this->db->setFlag( DBO_DDLMODE ); // For Oracle's handling of schema files
204 $this->db->begin( __METHOD__ );
205
206 $error = $this->db->sourceFile( $this->db->getSchemaPath() );
207 if( $error !== true ) {
208 $this->db->reportQueryError( $error, 0, '', __METHOD__ );
209 $this->db->rollback( __METHOD__ );
210 $status->fatal( 'config-install-tables-failed', $error );
211 } else {
212 $this->db->commit( __METHOD__ );
213 }
214 // Resume normal operations
215 if( $status->isOk() ) {
216 $this->enableLB();
217 }
218 return $status;
219 }
220
221 /**
222 * Check if database has a tablspace with pagesize >= 32k.
223 *
224 * @return Status
225 */
226 public function checkPageSize() {
227 $status = $this->getConnection();
228 if ( !$status->isOK() ) {
229 return $status;
230 }
231 $this->db->selectDB( $this->getVar( 'wgDBname' ) );
232
233 try {
234 $result = $this->db->query( 'SELECT PAGESIZE FROM SYSCAT.TABLESPACES FOR READ ONLY' );
235 if( $result == false ) {
236 $status->fatal( 'config-connection-error', '' );
237 } else {
238 $row = $this->db->fetchRow( $result );
239 while ( $row ) {
240 if( $row[0] >= 32768 ) {
241 return $status;
242 }
243 $row = $this->db->fetchRow( $result );
244 }
245 $status->fatal( 'config-ibm_db2-low-db-pagesize', '' );
246 }
247 } catch ( DBUnexpectedError $e ) {
248 $status->fatal( 'config-connection-error', $e->getMessage() );
249 }
250
251 return $status;
252 }
253
254 /**
255 * Generate the code to store the DB2-specific settings defined by the configuration form
256 * @return string
257 */
258 public function getLocalSettings() {
259 $schema = LocalSettingsGenerator::escapePhpString( $this->getVar( 'wgDBmwschema' ) );
260 $port = LocalSettingsGenerator::escapePhpString( $this->getVar( 'wgDBport' ) );
261 return
262 "# IBM_DB2 specific settings
263 \$wgDBmwschema = \"{$schema}\";
264 \$wgDBport = \"{$port}\";";
265 }
266
267 public function __construct( $parent ) {
268 parent::__construct( $parent );
269 }
270 }