a03546dabec04e52a906aeb64d75997b4cc7a9b0
[Packages/TYPO3.CMS.git] / typo3 / sysext / install / Classes / Service / SqlSchemaMigrationService.php
1 <?php
2 namespace TYPO3\CMS\Install\Service;
3
4 /***************************************************************
5 * Copyright notice
6 *
7 * (c) 2011-2013 Christian Kuhn <lolli@schwarzbu.ch>
8 * All rights reserved
9 *
10 * This script is part of the TYPO3 project. The TYPO3 project is
11 * free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * The GNU General Public License can be found at
17 * http://www.gnu.org/copyleft/gpl.html.
18 * A copy is found in the textfile GPL.txt and important notices to the license
19 * from the author is found in LICENSE.txt distributed with these scripts.
20 *
21 *
22 * This script is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
26 *
27 * This copyright notice MUST APPEAR in all copies of the script!
28 ***************************************************************/
29
30 /**
31 * Verify TYPO3 DB table structure. Mainly used in install tool
32 * compare wizard and extension manager.
33 */
34 class SqlSchemaMigrationService {
35
36 /**
37 * @constant Maximum field width of MySQL
38 */
39 const MYSQL_MAXIMUM_FIELD_WIDTH = 64;
40
41 /**
42 * @var string Prefix of deleted tables
43 */
44 protected $deletedPrefixKey = 'zzz_deleted_';
45
46 // Prefix used for tables/fields when deleted/renamed.
47 /**
48 * @var float|int Multiplier of SQL field size (for char, varchar and text fields)
49 */
50 protected $multiplySize = 1;
51
52 /**
53 * @var array Caching output of $GLOBALS['TYPO3_DB']->admin_get_charsets()
54 */
55 protected $character_sets = array();
56
57 /**
58 * Constructor function
59 */
60 public function __construct() {
61 if ($GLOBALS['TYPO3_CONF_VARS']['SYS']['multiplyDBfieldSize'] >= 1 && $GLOBALS['TYPO3_CONF_VARS']['SYS']['multiplyDBfieldSize'] <= 5) {
62 $this->multiplySize = (double) $GLOBALS['TYPO3_CONF_VARS']['SYS']['multiplyDBfieldSize'];
63 }
64 }
65
66 /**
67 * Set prefix of deleted tables
68 *
69 * @param string $prefix Prefix string
70 */
71 public function setDeletedPrefixKey($prefix) {
72 $this->deletedPrefixKey = $prefix;
73 }
74
75 /**
76 * Get prefix of deleted tables
77 *
78 * @return string
79 */
80 public function getDeletedPrefixKey() {
81 return $this->deletedPrefixKey;
82 }
83
84 /**
85 * Reads the field definitions for the input SQL-file string
86 *
87 * @param string $fileContent Should be a string read from an SQL-file made with 'mysqldump [database_name] -d'
88 * @return array Array with information about table.
89 */
90 public function getFieldDefinitions_fileContent($fileContent) {
91 $lines = \TYPO3\CMS\Core\Utility\GeneralUtility::trimExplode(LF, $fileContent, 1);
92 $table = '';
93 $total = array();
94 foreach ($lines as $value) {
95 if (substr($value, 0, 1) == '#') {
96 // Ignore comments
97 continue;
98 }
99 if (!strlen($table)) {
100 $parts = \TYPO3\CMS\Core\Utility\GeneralUtility::trimExplode(' ', $value, TRUE);
101 if (strtoupper($parts[0]) === 'CREATE' && strtoupper($parts[1]) === 'TABLE') {
102 $table = str_replace('`', '', $parts[2]);
103 // tablenames are always lowercase on windows!
104 if (TYPO3_OS == 'WIN') {
105 $table = strtolower($table);
106 }
107 }
108 } else {
109 if (substr($value, 0, 1) == ')' && substr($value, -1) == ';') {
110 $ttype = array();
111 if (preg_match('/(ENGINE|TYPE)[ ]*=[ ]*([a-zA-Z]*)/', $value, $ttype)) {
112 $total[$table]['extra']['ENGINE'] = $ttype[2];
113 }
114 // Otherwise, just do nothing: If table engine is not defined, just accept the system default.
115 // Set the collation, if specified
116 if (preg_match('/(COLLATE)[ ]*=[ ]*([a-zA-z0-9_-]+)/', $value, $tcollation)) {
117 $total[$table]['extra']['COLLATE'] = $tcollation[2];
118 } else {
119 // Otherwise, get the CHARACTER SET and try to find the default collation for it as returned by "SHOW CHARACTER SET" query (for details, see http://dev.mysql.com/doc/refman/5.1/en/charset-table.html)
120 if (preg_match('/(CHARSET|CHARACTER SET)[ ]*=[ ]*([a-zA-z0-9_-]+)/', $value, $tcharset)) {
121 // Note: Keywords "DEFAULT CHARSET" and "CHARSET" are the same, so "DEFAULT" can just be ignored
122 $charset = $tcharset[2];
123 } else {
124 $charset = $GLOBALS['TYPO3_DB']->default_charset;
125 }
126 $total[$table]['extra']['COLLATE'] = $this->getCollationForCharset($charset);
127 }
128 // Remove table marker and start looking for the next "CREATE TABLE" statement
129 $table = '';
130 } else {
131 // Strip trailing commas
132 $lineV = preg_replace('/,$/', '', $value);
133 $lineV = str_replace('`', '', $lineV);
134 // Remove double blanks
135 $lineV = str_replace(' ', ' ', $lineV);
136 $parts = explode(' ', $lineV, 2);
137 // Field definition
138 if (!preg_match('/(PRIMARY|UNIQUE|FULLTEXT|INDEX|KEY)/', $parts[0])) {
139 // Make sure there is no default value when auto_increment is set
140 if (stristr($parts[1], 'auto_increment')) {
141 $parts[1] = preg_replace('/ default \'0\'/i', '', $parts[1]);
142 }
143 // "default" is always lower-case
144 if (stristr($parts[1], ' DEFAULT ')) {
145 $parts[1] = str_ireplace(' DEFAULT ', ' default ', $parts[1]);
146 }
147 // Change order of "default" and "NULL" statements
148 $parts[1] = preg_replace('/(.*) (default .*) (NOT NULL)/', '$1 $3 $2', $parts[1]);
149 $parts[1] = preg_replace('/(.*) (default .*) (NULL)/', '$1 $3 $2', $parts[1]);
150 $key = $parts[0];
151 $total[$table]['fields'][$key] = $parts[1];
152 } else {
153 // Key definition
154 $search = array('/UNIQUE (INDEX|KEY)/', '/FULLTEXT (INDEX|KEY)/', '/INDEX/');
155 $replace = array('UNIQUE', 'FULLTEXT', 'KEY');
156 $lineV = preg_replace($search, $replace, $lineV);
157 if (preg_match('/PRIMARY|UNIQUE|FULLTEXT/', $parts[0])) {
158 $parts[1] = preg_replace('/^(KEY|INDEX) /', '', $parts[1]);
159 }
160 $newParts = explode(' ', $parts[1], 2);
161 $key = $parts[0] == 'PRIMARY' ? $parts[0] : $newParts[0];
162 $total[$table]['keys'][$key] = $lineV;
163 // This is a protection against doing something stupid: Only allow clearing of cache_* and index_* tables.
164 if (preg_match('/^(cache|index)_/', $table)) {
165 // Suggest to truncate (clear) this table
166 $total[$table]['extra']['CLEAR'] = 1;
167 }
168 }
169 }
170 }
171 }
172 $this->getFieldDefinitions_sqlContent_parseTypes($total);
173 return $total;
174 }
175
176 /**
177 * Multiplies varchars/tinytext fields in size according to $this->multiplySize
178 * Useful if you want to use UTF-8 in the database and needs to extend the field sizes in the database so UTF-8 chars are not discarded. For most charsets available as single byte sets, multiplication with 2 should be enough. For chinese, use 3.
179 *
180 * @param array $total Total array (from getFieldDefinitions_fileContent())
181 * @return void
182 * @access private
183 * @see getFieldDefinitions_fileContent()
184 */
185 protected function getFieldDefinitions_sqlContent_parseTypes(&$total) {
186 $mSize = (double) $this->multiplySize;
187 if ($mSize > 1) {
188 /** @var $sqlParser \TYPO3\CMS\Core\Database\SqlParser */
189 $sqlParser = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Database\\SqlParser');
190 foreach ($total as $table => $cfg) {
191 if (is_array($cfg['fields'])) {
192 foreach ($cfg['fields'] as $fN => $fType) {
193 $orig_fType = $fType;
194 $fInfo = $sqlParser->parseFieldDef($fType);
195 switch ($fInfo['fieldType']) {
196 case 'char':
197
198 case 'varchar':
199 $newSize = round($fInfo['value'] * $mSize);
200 if ($newSize <= 255) {
201 $fInfo['value'] = $newSize;
202 } else {
203 $fInfo = array(
204 'fieldType' => 'text',
205 'featureIndex' => array(
206 'NOTNULL' => array(
207 'keyword' => 'NOT NULL'
208 )
209 )
210 );
211 // Change key definition if necessary (must use "prefix" on TEXT columns)
212 if (is_array($cfg['keys'])) {
213 foreach ($cfg['keys'] as $kN => $kType) {
214 $match = array();
215 preg_match('/^([^(]*)\\(([^)]+)\\)(.*)/', $kType, $match);
216 $keys = array();
217 foreach (\TYPO3\CMS\Core\Utility\GeneralUtility::trimExplode(',', $match[2]) as $kfN) {
218 if ($fN == $kfN) {
219 $kfN .= '(' . $newSize . ')';
220 }
221 $keys[] = $kfN;
222 }
223 $total[$table]['keys'][$kN] = $match[1] . '(' . implode(',', $keys) . ')' . $match[3];
224 }
225 }
226 }
227 break;
228 case 'tinytext':
229 $fInfo['fieldType'] = 'text';
230 break;
231 }
232 $total[$table]['fields'][$fN] = $sqlParser->compileFieldCfg($fInfo);
233 if ($sqlParser->parse_error) {
234 throw new \RuntimeException('TYPO3 Fatal Error: ' . $sqlParser->parse_error, 1270853961);
235 }
236 }
237 }
238 }
239 }
240 }
241
242 /**
243 * Look up the default collation for specified character set based on "SHOW CHARACTER SET" output
244 *
245 * @param string $charset Character set
246 * @return string Corresponding default collation
247 */
248 public function getCollationForCharset($charset) {
249 // Load character sets, if not cached already
250 if (!count($this->character_sets)) {
251 if (method_exists($GLOBALS['TYPO3_DB'], 'admin_get_charsets')) {
252 $this->character_sets = $GLOBALS['TYPO3_DB']->admin_get_charsets();
253 } else {
254 // Add empty element to avoid that the check will be repeated
255 $this->character_sets[$charset] = array();
256 }
257 }
258 $collation = '';
259 if (isset($this->character_sets[$charset]['Default collation'])) {
260 $collation = $this->character_sets[$charset]['Default collation'];
261 }
262 return $collation;
263 }
264
265 /**
266 * Reads the field definitions for the current database
267 *
268 * @return array Array with information about table.
269 */
270 public function getFieldDefinitions_database() {
271 $total = array();
272 $tempKeys = array();
273 $tempKeysPrefix = array();
274 $GLOBALS['TYPO3_DB']->sql_select_db();
275 echo $GLOBALS['TYPO3_DB']->sql_error();
276 $tables = $GLOBALS['TYPO3_DB']->admin_get_tables();
277 foreach ($tables as $tableName => $tableStatus) {
278 // Fields
279 $fieldInformation = $GLOBALS['TYPO3_DB']->admin_get_fields($tableName);
280 foreach ($fieldInformation as $fN => $fieldRow) {
281 $total[$tableName]['fields'][$fN] = $this->assembleFieldDefinition($fieldRow);
282 }
283 // Keys
284 $keyInformation = $GLOBALS['TYPO3_DB']->admin_get_keys($tableName);
285 foreach ($keyInformation as $keyRow) {
286 $keyName = $keyRow['Key_name'];
287 $colName = $keyRow['Column_name'];
288 if ($keyRow['Sub_part']) {
289 $colName .= '(' . $keyRow['Sub_part'] . ')';
290 }
291 $tempKeys[$tableName][$keyName][$keyRow['Seq_in_index']] = $colName;
292 if ($keyName == 'PRIMARY') {
293 $prefix = 'PRIMARY KEY';
294 } else {
295 if ($keyRow['Index_type'] == 'FULLTEXT') {
296 $prefix = 'FULLTEXT';
297 } elseif ($keyRow['Non_unique']) {
298 $prefix = 'KEY';
299 } else {
300 $prefix = 'UNIQUE';
301 }
302 $prefix .= ' ' . $keyName;
303 }
304 $tempKeysPrefix[$tableName][$keyName] = $prefix;
305 }
306 // Table status (storage engine, collaction, etc.)
307 if (is_array($tableStatus)) {
308 $tableExtraFields = array(
309 'Engine' => 'ENGINE',
310 'Collation' => 'COLLATE'
311 );
312 foreach ($tableExtraFields as $mysqlKey => $internalKey) {
313 if (isset($tableStatus[$mysqlKey])) {
314 $total[$tableName]['extra'][$internalKey] = $tableStatus[$mysqlKey];
315 }
316 }
317 }
318 }
319 // Compile key information:
320 if (count($tempKeys)) {
321 foreach ($tempKeys as $table => $keyInf) {
322 foreach ($keyInf as $kName => $index) {
323 ksort($index);
324 $total[$table]['keys'][$kName] = $tempKeysPrefix[$table][$kName] . ' (' . implode(',', $index) . ')';
325 }
326 }
327 }
328 return $total;
329 }
330
331 /**
332 * Compares two arrays with field information and returns information about fields that are MISSING and fields that have CHANGED.
333 * FDsrc and FDcomp can be switched if you want the list of stuff to remove rather than update.
334 *
335 * @param array $FDsrc Field definitions, source (from getFieldDefinitions_fileContent())
336 * @param array $FDcomp Field definitions, comparison. (from getFieldDefinitions_database())
337 * @param string $onlyTableList Table names (in list) which is the ONLY one observed.
338 * @param boolean $ignoreNotNullWhenComparing If set, this function ignores NOT NULL statements of the SQL file field definition when comparing current field definition from database with field definition from SQL file. This way, NOT NULL statements will be executed when the field is initially created, but the SQL parser will never complain about missing NOT NULL statements afterwards.
339 * @return array Returns an array with 1) all elements from $FDsrc that is not in $FDcomp (in key 'extra') and 2) all elements from $FDsrc that is different from the ones in $FDcomp
340 */
341 public function getDatabaseExtra($FDsrc, $FDcomp, $onlyTableList = '', $ignoreNotNullWhenComparing = TRUE) {
342 $extraArr = array();
343 $diffArr = array();
344 if (is_array($FDsrc)) {
345 foreach ($FDsrc as $table => $info) {
346 if (!strlen($onlyTableList) || \TYPO3\CMS\Core\Utility\GeneralUtility::inList($onlyTableList, $table)) {
347 if (!isset($FDcomp[$table])) {
348 // If the table was not in the FDcomp-array, the result array is loaded with that table.
349 $extraArr[$table] = $info;
350 $extraArr[$table]['whole_table'] = 1;
351 } else {
352 $keyTypes = explode(',', 'extra,fields,keys');
353 foreach ($keyTypes as $theKey) {
354 if (is_array($info[$theKey])) {
355 foreach ($info[$theKey] as $fieldN => $fieldC) {
356 $fieldN = str_replace('`', '', $fieldN);
357 if ($fieldN == 'COLLATE') {
358 // TODO: collation support is currently disabled (needs more testing)
359 continue;
360 }
361 if (!isset($FDcomp[$table][$theKey][$fieldN])) {
362 $extraArr[$table][$theKey][$fieldN] = $fieldC;
363 } else {
364 $fieldC = trim($fieldC);
365
366 // Lowercase the field type to surround false-positive schema changes to be
367 // reported just because of different caseing of characters
368 // The regex does just trigger for the first word followed by round brackets
369 // that contain a length. It does not trigger for e.g. "PRIMARY KEY" because
370 // "PRIMARY KEY" is being returned from the DB in upper case.
371 $fieldC = preg_replace_callback(
372 '/^([a-zA-Z0-9]+\(.*\))(\s)(.*)/',
373 create_function(
374 '$matches',
375 'return strtolower($matches[1]) . $matches[2] . $matches[3];'
376 ),
377 $fieldC
378 );
379
380 if ($ignoreNotNullWhenComparing) {
381 $fieldC = str_replace(' NOT NULL', '', $fieldC);
382 $FDcomp[$table][$theKey][$fieldN] = str_replace(' NOT NULL', '', $FDcomp[$table][$theKey][$fieldN]);
383 }
384 if ($fieldC !== $FDcomp[$table][$theKey][$fieldN]) {
385 $diffArr[$table][$theKey][$fieldN] = $fieldC;
386 $diffArr_cur[$table][$theKey][$fieldN] = $FDcomp[$table][$theKey][$fieldN];
387 }
388 }
389 }
390 }
391 }
392 }
393 }
394 }
395 }
396 $output = array(
397 'extra' => $extraArr,
398 'diff' => $diffArr,
399 'diff_currentValues' => $diffArr_cur
400 );
401 return $output;
402 }
403
404 /**
405 * Returns an array with SQL-statements that is needed to update according to the diff-array
406 *
407 * @param array $diffArr Array with differences of current and needed DB settings. (from getDatabaseExtra())
408 * @param string $keyList List of fields in diff array to take notice of.
409 * @return array Array of SQL statements (organized in keys depending on type)
410 */
411 public function getUpdateSuggestions($diffArr, $keyList = 'extra,diff') {
412 $statements = array();
413 $deletedPrefixKey = $this->deletedPrefixKey;
414 $deletedPrefixLength = strlen($deletedPrefixKey);
415 $remove = 0;
416 if ($keyList == 'remove') {
417 $remove = 1;
418 $keyList = 'extra';
419 }
420 $keyList = explode(',', $keyList);
421 foreach ($keyList as $theKey) {
422 if (is_array($diffArr[$theKey])) {
423 foreach ($diffArr[$theKey] as $table => $info) {
424 $whole_table = array();
425 if (is_array($info['fields'])) {
426 foreach ($info['fields'] as $fN => $fV) {
427 if ($info['whole_table']) {
428 $whole_table[] = $fN . ' ' . $fV;
429 } else {
430 // Special case to work around MySQL problems when adding auto_increment fields:
431 if (stristr($fV, 'auto_increment')) {
432 // The field can only be set "auto_increment" if there exists a PRIMARY key of that field already.
433 // The check does not look up which field is primary but just assumes it must be the field with the auto_increment value...
434 if (isset($diffArr['extra'][$table]['keys']['PRIMARY'])) {
435 // Remove "auto_increment" from the statement - it will be suggested in a 2nd step after the primary key was created
436 $fV = str_replace(' auto_increment', '', $fV);
437 } else {
438 // In the next step, attempt to clear the table once again (2 = force)
439 $info['extra']['CLEAR'] = 2;
440 }
441 }
442 if ($theKey == 'extra') {
443 if ($remove) {
444 if (substr($fN, 0, $deletedPrefixLength) !== $deletedPrefixKey) {
445 // we've to make sure we don't exceed the maximal length
446 $prefixedFieldName = $deletedPrefixKey . substr($fN, ($deletedPrefixLength - self::MYSQL_MAXIMUM_FIELD_WIDTH));
447 $statement = 'ALTER TABLE ' . $table . ' CHANGE ' . $fN . ' ' . $prefixedFieldName . ' ' . $fV . ';';
448 $statements['change'][md5($statement)] = $statement;
449 } else {
450 $statement = 'ALTER TABLE ' . $table . ' DROP ' . $fN . ';';
451 $statements['drop'][md5($statement)] = $statement;
452 }
453 } else {
454 $statement = 'ALTER TABLE ' . $table . ' ADD ' . $fN . ' ' . $fV . ';';
455 $statements['add'][md5($statement)] = $statement;
456 }
457 } elseif ($theKey == 'diff') {
458 $statement = 'ALTER TABLE ' . $table . ' CHANGE ' . $fN . ' ' . $fN . ' ' . $fV . ';';
459 $statements['change'][md5($statement)] = $statement;
460 $statements['change_currentValue'][md5($statement)] = $diffArr['diff_currentValues'][$table]['fields'][$fN];
461 }
462 }
463 }
464 }
465 if (is_array($info['keys'])) {
466 foreach ($info['keys'] as $fN => $fV) {
467 if ($info['whole_table']) {
468 $whole_table[] = $fV;
469 } else {
470 if ($theKey == 'extra') {
471 if ($remove) {
472 $statement = 'ALTER TABLE ' . $table . ($fN == 'PRIMARY' ? ' DROP PRIMARY KEY' : ' DROP KEY ' . $fN) . ';';
473 $statements['drop'][md5($statement)] = $statement;
474 } else {
475 $statement = 'ALTER TABLE ' . $table . ' ADD ' . $fV . ';';
476 $statements['add'][md5($statement)] = $statement;
477 }
478 } elseif ($theKey == 'diff') {
479 $statement = 'ALTER TABLE ' . $table . ($fN == 'PRIMARY' ? ' DROP PRIMARY KEY' : ' DROP KEY ' . $fN) . ';';
480 $statements['change'][md5($statement)] = $statement;
481 $statement = 'ALTER TABLE ' . $table . ' ADD ' . $fV . ';';
482 $statements['change'][md5($statement)] = $statement;
483 }
484 }
485 }
486 }
487 if (is_array($info['extra'])) {
488 $extras = array();
489 $extras_currentValue = array();
490 $clear_table = FALSE;
491 foreach ($info['extra'] as $fN => $fV) {
492 // Only consider statements which are missing in the database but don't remove existing properties
493 if (!$remove) {
494 if (!$info['whole_table']) {
495 // If the whole table is created at once, we take care of this later by imploding all elements of $info['extra']
496 if ($fN == 'CLEAR') {
497 // Truncate table must happen later, not now
498 // Valid values for CLEAR: 1=only clear if keys are missing, 2=clear anyway (force)
499 if (count($info['keys']) || $fV == 2) {
500 $clear_table = TRUE;
501 }
502 continue;
503 } else {
504 $extras[] = $fN . '=' . $fV;
505 $extras_currentValue[] = $fN . '=' . $diffArr['diff_currentValues'][$table]['extra'][$fN];
506 }
507 }
508 }
509 }
510 if ($clear_table) {
511 $statement = 'TRUNCATE TABLE ' . $table . ';';
512 $statements['clear_table'][md5($statement)] = $statement;
513 }
514 if (count($extras)) {
515 $statement = 'ALTER TABLE ' . $table . ' ' . implode(' ', $extras) . ';';
516 $statements['change'][md5($statement)] = $statement;
517 $statements['change_currentValue'][md5($statement)] = implode(' ', $extras_currentValue);
518 }
519 }
520 if ($info['whole_table']) {
521 if ($remove) {
522 if (substr($table, 0, $deletedPrefixLength) !== $deletedPrefixKey) {
523 // we've to make sure we don't exceed the maximal length
524 $prefixedTableName = $deletedPrefixKey . substr($table, ($deletedPrefixLength - self::MYSQL_MAXIMUM_FIELD_WIDTH));
525 $statement = 'ALTER TABLE ' . $table . ' RENAME ' . $prefixedTableName . ';';
526 $statements['change_table'][md5($statement)] = $statement;
527 } else {
528 $statement = 'DROP TABLE ' . $table . ';';
529 $statements['drop_table'][md5($statement)] = $statement;
530 }
531 // Count
532 $count = $GLOBALS['TYPO3_DB']->exec_SELECTcountRows('*', $table);
533 $statements['tables_count'][md5($statement)] = $count ? 'Records in table: ' . $count : '';
534 } else {
535 $statement = 'CREATE TABLE ' . $table . ' (
536 ' . implode(',
537 ', $whole_table) . '
538 )';
539 if ($info['extra']) {
540 foreach ($info['extra'] as $k => $v) {
541 if ($k == 'COLLATE' || $k == 'CLEAR') {
542 // Skip these special statements. TODO: collation support is currently disabled (needs more testing)
543 continue;
544 }
545 // Add extra attributes like ENGINE, CHARSET, etc.
546 $statement .= ' ' . $k . '=' . $v;
547 }
548 }
549 $statement .= ';';
550 $statements['create_table'][md5($statement)] = $statement;
551 }
552 }
553 }
554 }
555 }
556 return $statements;
557 }
558
559 /**
560 * Converts a result row with field information into the SQL field definition string
561 *
562 * @param array $row MySQL result row
563 * @return string Field definition
564 */
565 public function assembleFieldDefinition($row) {
566 $field = array($row['Type']);
567 if ($row['Null'] == 'NO') {
568 $field[] = 'NOT NULL';
569 }
570 if (!strstr($row['Type'], 'blob') && !strstr($row['Type'], 'text')) {
571 // Add a default value if the field is not auto-incremented (these fields never have a default definition)
572 if (!stristr($row['Extra'], 'auto_increment')) {
573 if ($row['Default'] === NULL) {
574 $field[] = 'default NULL';
575 } else {
576 $field[] = 'default \'' . addslashes($row['Default']) . '\'';
577 }
578 }
579 }
580 if ($row['Extra']) {
581 $field[] = $row['Extra'];
582 }
583 return implode(' ', $field);
584 }
585
586 /**
587 * Returns an array where every entry is a single SQL-statement. Input must be formatted like an ordinary MySQL-dump files.
588 *
589 * @param string $sqlcode The SQL-file content. Provided that 1) every query in the input is ended with ';' and that a line in the file contains only one query or a part of a query.
590 * @param boolean $removeNonSQL If set, non-SQL content (like comments and blank lines) is not included in the final output
591 * @param string $query_regex Regex to filter SQL lines to include
592 * @return array Array of SQL statements
593 */
594 public function getStatementArray($sqlcode, $removeNonSQL = FALSE, $query_regex = '') {
595 $sqlcodeArr = explode(LF, $sqlcode);
596 // Based on the assumption that the sql-dump has
597 $statementArray = array();
598 $statementArrayPointer = 0;
599 foreach ($sqlcodeArr as $line => $lineContent) {
600 $is_set = 0;
601 // Auto_increment fields cannot have a default value!
602 if (stristr($lineContent, 'auto_increment')) {
603 $lineContent = preg_replace('/ default \'0\'/i', '', $lineContent);
604 }
605 if (!$removeNonSQL || strcmp(trim($lineContent), '') && substr(trim($lineContent), 0, 1) != '#' && substr(trim($lineContent), 0, 2) != '--') {
606 // '--' is seen as mysqldump comments from server version 3.23.49
607 $statementArray[$statementArrayPointer] .= $lineContent;
608 $is_set = 1;
609 }
610 if (substr(trim($lineContent), -1) == ';') {
611 if (isset($statementArray[$statementArrayPointer])) {
612 if (!trim($statementArray[$statementArrayPointer]) || $query_regex && !preg_match(('/' . $query_regex . '/i'), trim($statementArray[$statementArrayPointer]))) {
613 unset($statementArray[$statementArrayPointer]);
614 }
615 }
616 $statementArrayPointer++;
617 } elseif ($is_set) {
618 $statementArray[$statementArrayPointer] .= LF;
619 }
620 }
621 return $statementArray;
622 }
623
624 /**
625 * Returns tables to create and how many records in each
626 *
627 * @param array $statements Array of SQL statements to analyse.
628 * @param boolean $insertCountFlag If set, will count number of INSERT INTO statements following that table definition
629 * @return array Array with table definitions in index 0 and count in index 1
630 */
631 public function getCreateTables($statements, $insertCountFlag = FALSE) {
632 $crTables = array();
633 $insertCount = array();
634 foreach ($statements as $line => $lineContent) {
635 $reg = array();
636 if (preg_match('/^create[[:space:]]*table[[:space:]]*[`]?([[:alnum:]_]*)[`]?/i', substr($lineContent, 0, 100), $reg)) {
637 $table = trim($reg[1]);
638 if ($table) {
639 // Table names are always lowercase on Windows!
640 if (TYPO3_OS == 'WIN') {
641 $table = strtolower($table);
642 }
643 $sqlLines = explode(LF, $lineContent);
644 foreach ($sqlLines as $k => $v) {
645 if (stristr($v, 'auto_increment')) {
646 $sqlLines[$k] = preg_replace('/ default \'0\'/i', '', $v);
647 }
648 }
649 $lineContent = implode(LF, $sqlLines);
650 $crTables[$table] = $lineContent;
651 }
652 } elseif ($insertCountFlag && preg_match('/^insert[[:space:]]*into[[:space:]]*[`]?([[:alnum:]_]*)[`]?/i', substr($lineContent, 0, 100), $reg)) {
653 $nTable = trim($reg[1]);
654 $insertCount[$nTable]++;
655 }
656 }
657 return array($crTables, $insertCount);
658 }
659
660 /**
661 * Extracts all insert statements from $statement array where content is inserted into $table
662 *
663 * @param array $statements Array of SQL statements
664 * @param string $table Table name
665 * @return array Array of INSERT INTO statements where table match $table
666 */
667 public function getTableInsertStatements($statements, $table) {
668 $outStatements = array();
669 foreach ($statements as $line => $lineContent) {
670 $reg = array();
671 if (preg_match('/^insert[[:space:]]*into[[:space:]]*[`]?([[:alnum:]_]*)[`]?/i', substr($lineContent, 0, 100), $reg)) {
672 $nTable = trim($reg[1]);
673 if ($nTable && !strcmp($table, $nTable)) {
674 $outStatements[] = $lineContent;
675 }
676 }
677 }
678 return $outStatements;
679 }
680
681 /**
682 * Performs the queries passed from the input array.
683 *
684 * @param array $arr Array of SQL queries to execute.
685 * @param array $keyArr Array with keys that must match keys in $arr. Only where a key in this array is set and TRUE will the query be executed (meant to be passed from a form checkbox)
686 * @return mixed Array with error message from database if any occurred. Otherwise TRUE if everything was executed successfully.
687 */
688 public function performUpdateQueries($arr, $keyArr) {
689 $result = array();
690 if (is_array($arr)) {
691 foreach ($arr as $key => $string) {
692 if (isset($keyArr[$key]) && $keyArr[$key]) {
693 $res = $GLOBALS['TYPO3_DB']->admin_query($string);
694 if ($res === FALSE) {
695 $result[$key] = $GLOBALS['TYPO3_DB']->sql_error();
696 } elseif (is_resource($res) || is_a($res, '\\mysqli_result')) {
697 $GLOBALS['TYPO3_DB']->sql_free_result($res);
698 }
699 }
700 }
701 }
702 if (count($result) > 0) {
703 return $result;
704 } else {
705 return TRUE;
706 }
707 }
708
709 /**
710 * Returns list of tables in the database
711 *
712 * @return array List of tables.
713 * @see \TYPO3\CMS\Core\Database\DatabaseConnection::admin_get_tables()
714 */
715 public function getListOfTables() {
716 $whichTables = $GLOBALS['TYPO3_DB']->admin_get_tables(TYPO3_db);
717 foreach ($whichTables as $key => &$value) {
718 $value = $key;
719 }
720 unset($value);
721 return $whichTables;
722 }
723 }
724 ?>