[TASK] Fix broken references to exceptions in global namespace
[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 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 * Verify TYPO3 DB table structure. Mainly used in install tool
31 * compare wizard and extension manager.
32 *
33 * @author Christian Kuhn <lolli@schwarzbu.ch>
34 * @package TYPO3
35 * @subpackage t3lib
36 */
37 class SchemaMigrator {
38
39 /**
40 * @var string Prefix of deleted tables
41 */
42 protected $deletedPrefixKey = 'zzz_deleted_';
43
44 // Prefix used for tables/fields when deleted/renamed.
45 /**
46 * @var float|int Multiplier of SQL field size (for char, varchar and text fields)
47 */
48 protected $multiplySize = 1;
49
50 /**
51 * @var array Caching output of $GLOBALS['TYPO3_DB']->admin_get_charsets()
52 */
53 protected $character_sets = array();
54
55 // Maximum field width of MYSQL
56 const MYSQL_MAXIMUM_FIELD_WIDTH = 64;
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(TYPO3_db);
275 echo $GLOBALS['TYPO3_DB']->sql_error();
276 $tables = $GLOBALS['TYPO3_DB']->admin_get_tables(TYPO3_db);
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 if ($ignoreNotNullWhenComparing) {
366 $fieldC = str_replace(' NOT NULL', '', $fieldC);
367 $FDcomp[$table][$theKey][$fieldN] = str_replace(' NOT NULL', '', $FDcomp[$table][$theKey][$fieldN]);
368 }
369 if ($fieldC !== $FDcomp[$table][$theKey][$fieldN]) {
370 $diffArr[$table][$theKey][$fieldN] = $fieldC;
371 $diffArr_cur[$table][$theKey][$fieldN] = $FDcomp[$table][$theKey][$fieldN];
372 }
373 }
374 }
375 }
376 }
377 }
378 }
379 }
380 }
381 $output = array(
382 'extra' => $extraArr,
383 'diff' => $diffArr,
384 'diff_currentValues' => $diffArr_cur
385 );
386 return $output;
387 }
388
389 /**
390 * Returns an array with SQL-statements that is needed to update according to the diff-array
391 *
392 * @param array $diffArr Array with differences of current and needed DB settings. (from getDatabaseExtra())
393 * @param string $keyList List of fields in diff array to take notice of.
394 * @return array Array of SQL statements (organized in keys depending on type)
395 */
396 public function getUpdateSuggestions($diffArr, $keyList = 'extra,diff') {
397 $statements = array();
398 $deletedPrefixKey = $this->deletedPrefixKey;
399 $deletedPrefixLength = strlen($deletedPrefixKey);
400 $remove = 0;
401 if ($keyList == 'remove') {
402 $remove = 1;
403 $keyList = 'extra';
404 }
405 $keyList = explode(',', $keyList);
406 foreach ($keyList as $theKey) {
407 if (is_array($diffArr[$theKey])) {
408 foreach ($diffArr[$theKey] as $table => $info) {
409 $whole_table = array();
410 if (is_array($info['fields'])) {
411 foreach ($info['fields'] as $fN => $fV) {
412 if ($info['whole_table']) {
413 $whole_table[] = ($fN . ' ') . $fV;
414 } else {
415 // Special case to work around MySQL problems when adding auto_increment fields:
416 if (stristr($fV, 'auto_increment')) {
417 // The field can only be set "auto_increment" if there exists a PRIMARY key of that field already.
418 // The check does not look up which field is primary but just assumes it must be the field with the auto_increment value...
419 if (isset($diffArr['extra'][$table]['keys']['PRIMARY'])) {
420 // Remove "auto_increment" from the statement - it will be suggested in a 2nd step after the primary key was created
421 $fV = str_replace(' auto_increment', '', $fV);
422 } else {
423 // In the next step, attempt to clear the table once again (2 = force)
424 $info['extra']['CLEAR'] = 2;
425 }
426 }
427 if ($theKey == 'extra') {
428 if ($remove) {
429 if (substr($fN, 0, $deletedPrefixLength) !== $deletedPrefixKey) {
430 // we've to make sure we don't exceed the maximal length
431 $prefixedFieldName = $deletedPrefixKey . substr($fN, ($deletedPrefixLength - self::MYSQL_MAXIMUM_FIELD_WIDTH));
432 $statement = ((((((('ALTER TABLE ' . $table) . ' CHANGE ') . $fN) . ' ') . $prefixedFieldName) . ' ') . $fV) . ';';
433 $statements['change'][md5($statement)] = $statement;
434 } else {
435 $statement = ((('ALTER TABLE ' . $table) . ' DROP ') . $fN) . ';';
436 $statements['drop'][md5($statement)] = $statement;
437 }
438 } else {
439 $statement = ((((('ALTER TABLE ' . $table) . ' ADD ') . $fN) . ' ') . $fV) . ';';
440 $statements['add'][md5($statement)] = $statement;
441 }
442 } elseif ($theKey == 'diff') {
443 $statement = ((((((('ALTER TABLE ' . $table) . ' CHANGE ') . $fN) . ' ') . $fN) . ' ') . $fV) . ';';
444 $statements['change'][md5($statement)] = $statement;
445 $statements['change_currentValue'][md5($statement)] = $diffArr['diff_currentValues'][$table]['fields'][$fN];
446 }
447 }
448 }
449 }
450 if (is_array($info['keys'])) {
451 foreach ($info['keys'] as $fN => $fV) {
452 if ($info['whole_table']) {
453 $whole_table[] = $fV;
454 } else {
455 if ($theKey == 'extra') {
456 if ($remove) {
457 $statement = (('ALTER TABLE ' . $table) . ($fN == 'PRIMARY' ? ' DROP PRIMARY KEY' : ' DROP KEY ' . $fN)) . ';';
458 $statements['drop'][md5($statement)] = $statement;
459 } else {
460 $statement = ((('ALTER TABLE ' . $table) . ' ADD ') . $fV) . ';';
461 $statements['add'][md5($statement)] = $statement;
462 }
463 } elseif ($theKey == 'diff') {
464 $statement = (('ALTER TABLE ' . $table) . ($fN == 'PRIMARY' ? ' DROP PRIMARY KEY' : ' DROP KEY ' . $fN)) . ';';
465 $statements['change'][md5($statement)] = $statement;
466 $statement = ((('ALTER TABLE ' . $table) . ' ADD ') . $fV) . ';';
467 $statements['change'][md5($statement)] = $statement;
468 }
469 }
470 }
471 }
472 if (is_array($info['extra'])) {
473 $extras = array();
474 $extras_currentValue = array();
475 $clear_table = FALSE;
476 foreach ($info['extra'] as $fN => $fV) {
477 // Only consider statements which are missing in the database but don't remove existing properties
478 if (!$remove) {
479 if (!$info['whole_table']) {
480 // If the whole table is created at once, we take care of this later by imploding all elements of $info['extra']
481 if ($fN == 'CLEAR') {
482 // Truncate table must happen later, not now
483 // Valid values for CLEAR: 1=only clear if keys are missing, 2=clear anyway (force)
484 if (count($info['keys']) || $fV == 2) {
485 $clear_table = TRUE;
486 }
487 continue;
488 } else {
489 $extras[] = ($fN . '=') . $fV;
490 $extras_currentValue[] = ($fN . '=') . $diffArr['diff_currentValues'][$table]['extra'][$fN];
491 }
492 }
493 }
494 }
495 if ($clear_table) {
496 $statement = ('TRUNCATE TABLE ' . $table) . ';';
497 $statements['clear_table'][md5($statement)] = $statement;
498 }
499 if (count($extras)) {
500 $statement = ((('ALTER TABLE ' . $table) . ' ') . implode(' ', $extras)) . ';';
501 $statements['change'][md5($statement)] = $statement;
502 $statements['change_currentValue'][md5($statement)] = implode(' ', $extras_currentValue);
503 }
504 }
505 if ($info['whole_table']) {
506 if ($remove) {
507 if (substr($table, 0, $deletedPrefixLength) !== $deletedPrefixKey) {
508 // we've to make sure we don't exceed the maximal length
509 $prefixedTableName = $deletedPrefixKey . substr($table, ($deletedPrefixLength - self::MYSQL_MAXIMUM_FIELD_WIDTH));
510 $statement = ((('ALTER TABLE ' . $table) . ' RENAME ') . $prefixedTableName) . ';';
511 $statements['change_table'][md5($statement)] = $statement;
512 } else {
513 $statement = ('DROP TABLE ' . $table) . ';';
514 $statements['drop_table'][md5($statement)] = $statement;
515 }
516 // Count
517 $count = $GLOBALS['TYPO3_DB']->exec_SELECTcountRows('*', $table);
518 $statements['tables_count'][md5($statement)] = $count ? 'Records in table: ' . $count : '';
519 } else {
520 $statement = ((('CREATE TABLE ' . $table) . ' (
521 ') . implode(',
522 ', $whole_table)) . '
523 )';
524 if ($info['extra']) {
525 foreach ($info['extra'] as $k => $v) {
526 if ($k == 'COLLATE' || $k == 'CLEAR') {
527 // Skip these special statements. TODO: collation support is currently disabled (needs more testing)
528 continue;
529 }
530 // Add extra attributes like ENGINE, CHARSET, etc.
531 $statement .= ((' ' . $k) . '=') . $v;
532 }
533 }
534 $statement .= ';';
535 $statements['create_table'][md5($statement)] = $statement;
536 }
537 }
538 }
539 }
540 }
541 return $statements;
542 }
543
544 /**
545 * Converts a result row with field information into the SQL field definition string
546 *
547 * @param array $row MySQL result row
548 * @return string Field definition
549 */
550 public function assembleFieldDefinition($row) {
551 $field = array($row['Type']);
552 if ($row['Null'] == 'NO') {
553 $field[] = 'NOT NULL';
554 }
555 if (!strstr($row['Type'], 'blob') && !strstr($row['Type'], 'text')) {
556 // Add a default value if the field is not auto-incremented (these fields never have a default definition)
557 if (!stristr($row['Extra'], 'auto_increment')) {
558 if ($row['Default'] === NULL) {
559 $field[] = 'default NULL';
560 } else {
561 $field[] = ('default \'' . addslashes($row['Default'])) . '\'';
562 }
563 }
564 }
565 if ($row['Extra']) {
566 $field[] = $row['Extra'];
567 }
568 return implode(' ', $field);
569 }
570
571 /**
572 * Returns an array where every entry is a single SQL-statement. Input must be formatted like an ordinary MySQL-dump files.
573 *
574 * @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.
575 * @param boolean $removeNonSQL If set, non-SQL content (like comments and blank lines) is not included in the final output
576 * @param string $query_regex Regex to filter SQL lines to include
577 * @return array Array of SQL statements
578 */
579 public function getStatementArray($sqlcode, $removeNonSQL = FALSE, $query_regex = '') {
580 $sqlcodeArr = explode(LF, $sqlcode);
581 // Based on the assumption that the sql-dump has
582 $statementArray = array();
583 $statementArrayPointer = 0;
584 foreach ($sqlcodeArr as $line => $lineContent) {
585 $is_set = 0;
586 // Auto_increment fields cannot have a default value!
587 if (stristr($lineContent, 'auto_increment')) {
588 $lineContent = preg_replace('/ default \'0\'/i', '', $lineContent);
589 }
590 if (!$removeNonSQL || (strcmp(trim($lineContent), '') && substr(trim($lineContent), 0, 1) != '#') && substr(trim($lineContent), 0, 2) != '--') {
591 // '--' is seen as mysqldump comments from server version 3.23.49
592 $statementArray[$statementArrayPointer] .= $lineContent;
593 $is_set = 1;
594 }
595 if (substr(trim($lineContent), -1) == ';') {
596 if (isset($statementArray[$statementArrayPointer])) {
597 if (!trim($statementArray[$statementArrayPointer]) || $query_regex && !preg_match((('/' . $query_regex) . '/i'), trim($statementArray[$statementArrayPointer]))) {
598 unset($statementArray[$statementArrayPointer]);
599 }
600 }
601 $statementArrayPointer++;
602 } elseif ($is_set) {
603 $statementArray[$statementArrayPointer] .= LF;
604 }
605 }
606 return $statementArray;
607 }
608
609 /**
610 * Returns tables to create and how many records in each
611 *
612 * @param array $statements Array of SQL statements to analyse.
613 * @param boolean $insertCountFlag If set, will count number of INSERT INTO statements following that table definition
614 * @return array Array with table definitions in index 0 and count in index 1
615 */
616 public function getCreateTables($statements, $insertCountFlag = FALSE) {
617 $crTables = array();
618 $insertCount = array();
619 foreach ($statements as $line => $lineContent) {
620 $reg = array();
621 if (preg_match('/^create[[:space:]]*table[[:space:]]*[`]?([[:alnum:]_]*)[`]?/i', substr($lineContent, 0, 100), $reg)) {
622 $table = trim($reg[1]);
623 if ($table) {
624 // Table names are always lowercase on Windows!
625 if (TYPO3_OS == 'WIN') {
626 $table = strtolower($table);
627 }
628 $sqlLines = explode(LF, $lineContent);
629 foreach ($sqlLines as $k => $v) {
630 if (stristr($v, 'auto_increment')) {
631 $sqlLines[$k] = preg_replace('/ default \'0\'/i', '', $v);
632 }
633 }
634 $lineContent = implode(LF, $sqlLines);
635 $crTables[$table] = $lineContent;
636 }
637 } elseif ($insertCountFlag && preg_match('/^insert[[:space:]]*into[[:space:]]*[`]?([[:alnum:]_]*)[`]?/i', substr($lineContent, 0, 100), $reg)) {
638 $nTable = trim($reg[1]);
639 $insertCount[$nTable]++;
640 }
641 }
642 return array($crTables, $insertCount);
643 }
644
645 /**
646 * Extracts all insert statements from $statement array where content is inserted into $table
647 *
648 * @param array $statements Array of SQL statements
649 * @param string $table Table name
650 * @return array Array of INSERT INTO statements where table match $table
651 */
652 public function getTableInsertStatements($statements, $table) {
653 $outStatements = array();
654 foreach ($statements as $line => $lineContent) {
655 $reg = array();
656 if (preg_match('/^insert[[:space:]]*into[[:space:]]*[`]?([[:alnum:]_]*)[`]?/i', substr($lineContent, 0, 100), $reg)) {
657 $nTable = trim($reg[1]);
658 if ($nTable && !strcmp($table, $nTable)) {
659 $outStatements[] = $lineContent;
660 }
661 }
662 }
663 return $outStatements;
664 }
665
666 /**
667 * Performs the queries passed from the input array.
668 *
669 * @param array $arr Array of SQL queries to execute.
670 * @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)
671 * @return mixed Array with error message from database if any occurred. Otherwise TRUE if everything was executed successfully.
672 */
673 public function performUpdateQueries($arr, $keyArr) {
674 $result = array();
675 if (is_array($arr)) {
676 foreach ($arr as $key => $string) {
677 if (isset($keyArr[$key]) && $keyArr[$key]) {
678 $res = $GLOBALS['TYPO3_DB']->admin_query($string);
679 if ($res === FALSE) {
680 $result[$key] = $GLOBALS['TYPO3_DB']->sql_error();
681 } elseif (is_resource($res)) {
682 $GLOBALS['TYPO3_DB']->sql_free_result($res);
683 }
684 }
685 }
686 }
687 if (count($result) > 0) {
688 return $result;
689 } else {
690 return TRUE;
691 }
692 }
693
694 /**
695 * Returns list of tables in the database
696 *
697 * @return array List of tables.
698 * @see t3lib_db::admin_get_tables()
699 */
700 public function getListOfTables() {
701 $whichTables = $GLOBALS['TYPO3_DB']->admin_get_tables(TYPO3_db);
702 foreach ($whichTables as $key => &$value) {
703 $value = $key;
704 }
705 unset($value);
706 return $whichTables;
707 }
708
709 }
710
711
712 ?>