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