summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/SemanticMediaWiki/src/SQLStore/TableBuilder/TableBuilder.php
blob: 9f39a22cbd54f81793d3a1e103448fc057fcc35e (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
<?php

namespace SMW\SQLStore\TableBuilder;

use DatabaseBase;
use Onoi\MessageReporter\MessageReporter;
use Onoi\MessageReporter\MessageReporterAware;
use RuntimeException;
use SMW\SQLStore\TableBuilder as TableBuilderInterface;

/**
 * @license GNU GPL v2+
 * @since 2.5
 *
 * @author mwjames
 */
abstract class TableBuilder implements TableBuilderInterface, MessageReporterAware, MessageReporter {

	/**
	 * @var DatabaseBase
	 */
	protected $connection;

	/**
	 * @var MessageReporter
	 */
	private $messageReporter;

	/**
	 * @var array
	 */
	protected $config = [];

	/**
	 * @var array
	 */
	protected $activityLog = [];

	/**
	 * @since 2.5
	 *
	 * @param DatabaseBase $connection
	 */
	protected function __construct( DatabaseBase $connection ) {
		$this->connection = $connection;
	}

	/**
	 * @since 2.5
	 *
	 * @param DatabaseBase $connection
	 *
	 * @return TableBuilder
	 * @throws RuntimeException
	 */
	public static function factory( DatabaseBase $connection ) {

		$instance = null;

		switch ( $connection->getType() ) {
			case 'mysql':
				$instance = new MySQLTableBuilder( $connection );
				break;
			case 'sqlite':
				$instance = new SQLiteTableBuilder( $connection );
				break;
			case 'postgres':
				$instance = new PostgresTableBuilder( $connection );
				break;
		}

		if ( $instance === null ) {
			throw new RuntimeException( "Unknown or unsupported DB type " . $connection->getType() );
		}

		$instance->addConfig( 'wgDBname', $GLOBALS['wgDBname'] );
		$instance->addConfig( 'wgDBTableOptions', $GLOBALS['wgDBTableOptions'] );

		return $instance;
	}

	/**
	 * @since 2.5
	 *
	 * @param string|integer $key
	 * @param mixed
	 */
	public function addConfig( $key, $value ) {
		$this->config[$key] = $value;
	}

	/**
	 * @see MessageReporterAware::setMessageReporter
	 *
	 * @since 2.5
	 *
	 * @param MessageReporter $messageReporter
	 */
	public function setMessageReporter( MessageReporter $messageReporter ) {
		$this->messageReporter = $messageReporter;
	}

	/**
	 * @see MessageReporter::reportMessage
	 *
	 * @since 2.5
	 *
	 * @param string $message
	 */
	public function reportMessage( $message ) {

		if ( $this->messageReporter === null ) {
			return;
		}

		$this->messageReporter->reportMessage( $message );
	}

	/**
	 * @since 2.5
	 *
	 * {@inheritDoc}
	 */
	public function getStandardFieldType( $fieldType ) {
		return false;
	}

	/**
	 * @since 2.5
	 *
	 * {@inheritDoc}
	 */
	public function create( Table $table ) {

		$attributes = $table->getAttributes();
		$tableName = $table->getName();

		$this->reportMessage( "Checking table $tableName ...\n" );

		if ( $this->connection->tableExists( $tableName ) === false ) { // create new table
			$this->reportMessage( "   Table not found, now creating...\n" );
			$this->doCreateTable( $tableName, $attributes );
		} else {
			$this->reportMessage( "   Table already exists, checking structure ...\n" );
			$this->doUpdateTable( $tableName, $attributes );
		}

		$this->reportMessage( "   ... done.\n" );

		if ( !isset( $attributes['indices'] ) ) {
			return $this->reportMessage( "No index structures for table $tableName ...\n" );
		}

		$this->reportMessage( "Checking index structures for table $tableName ...\n" );
		$this->doCreateIndices( $tableName, $attributes );

		$this->reportMessage( "   ... done.\n" );
	}

	/**
	 * @since 2.5
	 *
	 * {@inheritDoc}
	 */
	public function drop( Table $table ) {

		$tableName = $table->getName();

		if ( $this->connection->tableExists( $tableName ) === false ) { // create new table
			return $this->reportMessage( " ... $tableName not found, skipping removal.\n" );
		}

		$this->doDropTable( $tableName );
		$this->reportMessage( " ... dropped table $tableName.\n" );
	}

	/**
	 * @since 3.0
	 *
	 * {@inheritDoc}
	 */
	public function optimize( Table $table ) {
		$this->doOptimize( $table->getName() );
	}

	/**
	 * @since 2.5
	 *
	 * @param string $event
	 */
	public function checkOn( $event ) {
		return false;
	}

	/**
	 * @since 3.0
	 *
	 * {@inheritDoc}
	 */
	public function getLog() {
		return $this->activityLog;
	}

	/**
	 * @param string $tableName
	 * @param array $tableOptions
	 */
	abstract protected function doCreateTable( $tableName, array $tableOptions = null);

	/**
	 * @param string $tableName
	 * @param array $tableOptions
	 */
	abstract protected function doUpdateTable( $tableName, array $tableOptions = null );

	/**
	 * @param string $tableName
	 * @param array $indexOptions
	 */
	abstract protected function doCreateIndices( $tableName, array $indexOptions = null );

	/**
	 * @param string $tableName
	 */
	abstract protected function doDropTable( $tableName );

	/**
	 * @param string $tableName
	 */
	abstract protected function doOptimize( $tableName );

	// #1978
	// http://php.net/manual/en/function.array-search.php
	protected function recursive_array_search( $needle, $haystack ) {
		foreach( $haystack as $key => $value ) {
			$current_key = $key;

			if ( $needle === $value or ( is_array( $value ) && $this->recursive_array_search( $needle, $value ) !== false ) ) {
				return $current_key;
			}
		}

		return false;
	}

}