summaryrefslogtreecommitdiff
path: root/includes/installer/Ibm_db2Installer.php
blob: 78e607fb1e44b9903f987571b862de8b16a842dc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
<?php
/**
 * IBM_DB2-specific installer.
 *
 * @file
 * @ingroup Deployment
 */

/**
 * Class for setting up the MediaWiki database using IBM_DB2.
 *
 * @ingroup Deployment
 * @since 1.17
 */
class Ibm_db2Installer extends DatabaseInstaller {


	protected $globalNames = array(
		'wgDBserver',
		'wgDBport',
		'wgDBname',
		'wgDBuser',
		'wgDBpassword',
		'wgDBmwschema',
	);

	protected $internalDefaults = array(
		'_InstallUser' => 'db2admin'
	);

	/**
	 * Get the DB2 database extension name
	 * @return string
	 */
	public function getName(){
		return 'ibm_db2';
	}

	/**
	 * Determine whether the DB2 database extension is currently available in PHP
	 * @return boolean
	 */
	public function isCompiled() {
		return self::checkExtension( 'ibm_db2' );
	}

	/**
	 * Generate a connection form for a DB2 database
	 * @return string
	 */
	public function getConnectForm() {
		return
			$this->getTextBox( 'wgDBserver', 'config-db-host', array(), $this->parent->getHelpBox( 'config-db-host-help' ) ) .
			$this->getTextBox( 'wgDBport', 'config-db-port', array(), $this->parent->getHelpBox( 'config-db-port' ) ) .
			Html::openElement( 'fieldset' ) .
			Html::element( 'legend', array(), wfMsg( 'config-db-wiki-settings' ) ) .
			$this->getTextBox( 'wgDBname', 'config-db-name', array(), $this->parent->getHelpBox( 'config-db-name-help' ) ) .
			$this->getTextBox( 'wgDBmwschema', 'config-db-schema', array(), $this->parent->getHelpBox( 'config-db-schema-help' ) ) .
			Html::closeElement( 'fieldset' ) .
			$this->getInstallUserBox();
	}

	/**
	 * Validate and then execute the connection form for a DB2 database
	 * @return Status
	 */
	public function submitConnectForm() {
		// Get variables from the request
		$newValues = $this->setVarsFromRequest(
			array( 'wgDBserver', 'wgDBport', 'wgDBname',
				'wgDBmwschema', 'wgDBuser', 'wgDBpassword' ) );

		// Validate them
		$status = Status::newGood();
		if ( !strlen( $newValues['wgDBname'] ) ) {
			$status->fatal( 'config-missing-db-name' );
		} elseif ( !preg_match( '/^[a-zA-Z0-9_]+$/', $newValues['wgDBname'] ) ) {
			$status->fatal( 'config-invalid-db-name', $newValues['wgDBname'] );
		}
		if ( !strlen( $newValues['wgDBmwschema'] ) ) {
			$status->fatal( 'config-invalid-schema' );
		}
		elseif ( !preg_match( '/^[a-zA-Z0-9_]*$/', $newValues['wgDBmwschema'] ) ) {
			$status->fatal( 'config-invalid-schema', $newValues['wgDBmwschema'] );
		}
		if ( !strlen( $newValues['wgDBport'] ) ) {
			$status->fatal( 'config-invalid-port' );
		}
		elseif ( !preg_match( '/^[0-9_]*$/', $newValues['wgDBport'] ) ) {
			$status->fatal( 'config-invalid-port', $newValues['wgDBport'] );
		}

		// Submit user box
		if ( $status->isOK() ) {
			$status->merge( $this->submitInstallUserBox() );
		}
		if ( !$status->isOK() ) {
			return $status;
		}

		global $wgDBport;
		$wgDBport = $newValues['wgDBport'];

		// Try to connect
		$status->merge( $this->getConnection() );
		if ( !$status->isOK() ) {
			return $status;
		}

		$this->parent->setVar( 'wgDBuser', $this->getVar( '_InstallUser' ) );
		$this->parent->setVar( 'wgDBpassword', $this->getVar( '_InstallPassword' ) );

		return $status;
	}

	/**
	 * Open a DB2 database connection
	 * @return Status
	 */
	public function openConnection() {
		$status = Status::newGood();
		try {
			$db = new DatabaseIbm_db2(
				$this->getVar( 'wgDBserver' ),
				$this->getVar( '_InstallUser' ),
				$this->getVar( '_InstallPassword' ),
				$this->getVar( 'wgDBname' ),
				0,
				$this->getVar( 'wgDBmwschema' )
			);
			$status->value = $db;
		} catch ( DBConnectionError $e ) {
			$status->fatal( 'config-connection-error', $e->getMessage() );
		}
		return $status;
	}

	/**
	 * Create a DB2 database for MediaWiki
	 * @return Status
	 */
	public function setupDatabase() {
		$status = $this->getConnection();
		if ( !$status->isOK() ) {
			return $status;
		}
		$conn = $status->value;
		$dbName = $this->getVar( 'wgDBname' );
		if( !$conn->selectDB( $dbName ) ) {
			$conn->query( "CREATE DATABASE "
				. $conn->addIdentifierQuotes( $dbName )
				. " AUTOMATIC STORAGE YES"
				. " USING CODESET UTF-8 TERRITORY US COLLATE USING SYSTEM"
				. " PAGESIZE 32768", __METHOD__ );
			$conn->selectDB( $dbName );
		}
		$this->setupSchemaVars();
		return $status;
	}

	/**
	 * Create tables from scratch.
	 * First check if pagesize >= 32k.
	 *
	 * @return Status
	 */
	public function createTables() {
		$status = $this->getConnection();
		if ( !$status->isOK() ) {
			return $status;
		}
		$this->db->selectDB( $this->getVar( 'wgDBname' ) );

		if( $this->db->tableExists( 'user' ) ) {
			$status->warning( 'config-install-tables-exist' );
			return $status;
		}

		/* Check for pagesize */
		$status = $this->checkPageSize();
		if ( !$status->isOK() ) {
			return $status;
		}

		$this->db->setFlag( DBO_DDLMODE ); // For Oracle's handling of schema files
		$this->db->begin( __METHOD__ );

		$error = $this->db->sourceFile( $this->db->getSchemaPath() );
		if( $error !== true ) {
			$this->db->reportQueryError( $error, 0, '', __METHOD__ );
			$this->db->rollback( __METHOD__ );
			$status->fatal( 'config-install-tables-failed', $error );
		} else {
			$this->db->commit( __METHOD__ );
		}
		// Resume normal operations
		if( $status->isOk() ) {
			$this->enableLB();
		}
		return $status;
	}

	/**
	 * Check if database has a tablspace with pagesize >= 32k.
	 *
	 * @return Status
	 */
	public function checkPageSize() {
		$status = $this->getConnection();
		if ( !$status->isOK() ) {
			return $status;
		}
		$this->db->selectDB( $this->getVar( 'wgDBname' ) );

		try {
			$result = $this->db->query( 'SELECT PAGESIZE FROM SYSCAT.TABLESPACES' );
			if( $result == false ) {
				$status->fatal( 'config-connection-error', '' );
			}
			else {
				while ( $row = $this->db->fetchRow( $result ) ) {
					if( $row[0] >= 32768 ) {
						return $status;
					}
				}
				$status->fatal( 'config-ibm_db2-low-db-pagesize', '' );
			}
		} catch ( DBUnexpectedError $e ) {
			$status->fatal( 'config-connection-error', $e->getMessage() );
		}

		return $status;
	}

	/**
	 * Generate the code to store the DB2-specific settings defined by the configuration form
	 * @return string
	 */
	public function getLocalSettings() {
		$schema = LocalSettingsGenerator::escapePhpString( $this->getVar( 'wgDBmwschema' ) );
		$port = LocalSettingsGenerator::escapePhpString( $this->getVar( 'wgDBport' ) );
		return
"# IBM_DB2 specific settings
\$wgDBmwschema         = \"{$schema}\";
\$wgDBport             = \"{$port}\";";
	}

	public function __construct($parent) {
		parent::__construct($parent);
	}
}