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