Added feature #11148: Remove static require_once() to t3lib and tslib and use autoloa...
[Packages/TYPO3.CMS.git] / t3lib / class.t3lib_install.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 1999-2009 Kasper Skaarhoj (kasperYYYY@typo3.com)
6 * All rights reserved
7 *
8 * This script is part of the TYPO3 project. The TYPO3 project is
9 * free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * The GNU General Public License can be found at
15 * http://www.gnu.org/copyleft/gpl.html.
16 * A copy is found in the textfile GPL.txt and important notices to the license
17 * from the author is found in LICENSE.txt distributed with these scripts.
18 *
19 *
20 * This script is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
24 *
25 * This copyright notice MUST APPEAR in all copies of the script!
26 ***************************************************************/
27 /**
28 * Class to setup values in localconf.php and verify the TYPO3 DB tables/fields
29 *
30 * $Id$
31 *
32 * @author Kasper Skaarhoj <kasperYYYY@typo3.com>
33 */
34 /**
35 * [CLASS/FUNCTION INDEX of SCRIPT]
36 *
37 *
38 *
39 * 83: class t3lib_install
40 * 108: function t3lib_install()
41 *
42 * SECTION: Writing to localconf.php
43 * 132: function setValueInLocalconfFile(&$line_array, $variable, $value)
44 * 183: function writeToLocalconf_control($inlines='',$absFullPath='')
45 * 253: function checkForBadString($string)
46 * 266: function slashValueForSingleDashes($value)
47 *
48 * SECTION: SQL
49 * 291: function getFieldDefinitions_fileContent($fileContent)
50 * 359: function getFieldDefinitions_sqlContent_parseTypes(&$total)
51 * 406: function getFieldDefinitions_database()
52 * 450: function getDatabaseExtra($FDsrc, $FDcomp, $onlyTableList='')
53 * 496: function getUpdateSuggestions($diffArr,$keyList='extra,diff')
54 * 589: function assembleFieldDefinition($row)
55 * 611: function getStatementArray($sqlcode,$removeNonSQL=0,$query_regex='')
56 * 649: function getCreateTables($statements, $insertCountFlag=0)
57 * 683: function getTableInsertStatements($statements, $table)
58 * 704: function performUpdateQueries($arr,$keyArr)
59 * 720: function getListOfTables()
60 * 736: function generateUpdateDatabaseForm_checkboxes($arr,$label,$checked=1,$iconDis=0,$currentValue=array(),$cVfullMsg=0)
61 *
62 * TOTAL FUNCTIONS: 17
63 * (This index is automatically created/updated by the extension "extdeveval")
64 *
65 */
66
67
68
69
70
71
72
73
74
75 /**
76 * Class to setup values in localconf.php and verify the TYPO3 DB tables/fields
77 *
78 * @author Kasper Skaarhoj <kasperYYYY@typo3.com>
79 * @package TYPO3
80 * @subpackage t3lib
81 */
82 class t3lib_install {
83
84
85 // External, Static
86 var $updateIdentity = ''; // Set to string which identifies the script using this class.
87 var $deletedPrefixKey = 'zzz_deleted_'; // Prefix used for tables/fields when deleted/renamed.
88 var $dbUpdateCheckboxPrefix = 'TYPO3_INSTALL[database_update]'; // Prefix for checkbox fields when updating database.
89 var $localconf_addLinesOnly = 0; // If this is set, modifications to localconf.php is done by adding new lines to the array only. If unset, existing values are recognized and changed.
90 var $localconf_editPointToken = 'INSTALL SCRIPT EDIT POINT TOKEN - all lines after this points may be changed by the install script!'; // If set and addLinesOnly is disabled, lines will be change only if they are after this token (on a single line!) in the file
91 var $allowUpdateLocalConf = 0; // If true, this class will allow the user to update the localconf.php file. Is set true in the init.php file.
92 var $backPath = '../'; // Backpath (used for icons etc.)
93
94 var $multiplySize = 1; // Multiplier of SQL field size (for char, varchar and text fields)
95 var $character_sets = array(); // Caching output of $GLOBALS['TYPO3_DB']->admin_get_charsets()
96
97 // Internal, dynamic:
98 var $setLocalconf = 0; // Used to indicate that a value is change in the line-array of localconf and that it should be written.
99 var $messages = array(); // Used to set (error)messages from the executing functions like mail-sending, writing Localconf and such
100 var $touchedLine = 0; // updated with line in localconf.php file that was changed.
101
102
103 /**
104 * Constructor function
105 *
106 * @return void
107 */
108 function t3lib_install() {
109 if ($GLOBALS['TYPO3_CONF_VARS']['SYS']['multiplyDBfieldSize']>= 1 && $GLOBALS['TYPO3_CONF_VARS']['SYS']['multiplyDBfieldSize']<=5) {
110 $this->multiplySize = (double)$GLOBALS['TYPO3_CONF_VARS']['SYS']['multiplyDBfieldSize'];
111 }
112 }
113
114
115
116 /**************************************
117 *
118 * Writing to localconf.php
119 *
120
121 **************************************/
122
123 /**
124 * This functions takes an array with lines from localconf.php, finds a variable and inserts the new value.
125 *
126 * @param array $line_array the localconf.php file exploded into an array by linebreaks. (see writeToLocalconf_control())
127 * @param string $variable The variable name to find and substitute. This string must match the first part of a trimmed line in the line-array. Matching is done backwards so the last appearing line will be substituted.
128 * @param string $value Is the value to be insert for the variable
129 * @return void
130 * @see writeToLocalconf_control()
131 */
132 function setValueInLocalconfFile(&$line_array, $variable, $value) {
133 if (!$this->checkForBadString($value)) return 0;
134
135 // Initialize:
136 $found = 0;
137 $this->touchedLine = '';
138 $commentKey = '## ';
139 $inArray = in_array($commentKey.$this->localconf_editPointToken,$line_array);
140 $tokenSet = ($this->localconf_editPointToken && !$inArray); // Flag is set if the token should be set but is not yet...
141 $stopAtToken = ($this->localconf_editPointToken && $inArray);
142 $comment = ' Modified or inserted by '.$this->updateIdentity.'.';
143
144 // Search for variable name:
145 if (!$this->localconf_addLinesOnly && !$tokenSet) {
146 $line_array = array_reverse($line_array);
147 foreach($line_array as $k => $v) {
148 $v2 = trim($v);
149 if ($stopAtToken && !strcmp($v2,$commentKey.$this->localconf_editPointToken)) break; // If stopAtToken and token found, break out of the loop..
150 if (!strcmp(substr($v2,0,strlen($variable.' ')),$variable.' ')) {
151 $mainparts = explode($variable,$v,2);
152 if (count($mainparts)==2) { // should ALWAYS be....
153 $subparts = explode('//',$mainparts[1],2);
154 $line_array[$k] = $mainparts[0].$variable." = '".$this->slashValueForSingleDashes($value)."'; ".('//'.$comment.str_replace($comment,'',$subparts[1]));
155 $this->touchedLine = count($line_array)-$k-1;
156 $found = 1;
157 break;
158 }
159 }
160 }
161 $line_array = array_reverse($line_array);
162 }
163 if (!$found) {
164 if ($tokenSet) {
165 $line_array[] = $commentKey.$this->localconf_editPointToken;
166 $line_array[] = '';
167 }
168 $line_array[] = $variable." = '".$this->slashValueForSingleDashes($value)."'; // ".$comment;
169 $this->touchedLine = -1;
170 }
171 $this->messages[] = $variable." = '".htmlspecialchars($value)."'";
172 $this->setLocalconf = 1;
173 }
174
175 /**
176 * Writes or returns lines from localconf.php
177 *
178 * @param array Array of lines to write back to localconf.php. Possibly
179 * @param string Absolute path of alternative file to use (Notice: this path is not validated in terms of being inside 'TYPO3 space')
180 * @return mixed If $inlines is not an array it will return an array with the lines from localconf.php. Otherwise it will return a status string, either "continue" (updated) or "nochange" (not updated)
181 * @see setValueInLocalconfFile()
182 */
183 function writeToLocalconf_control($inlines='',$absFullPath='') {
184 $tmpExt = '.TMP.php';
185 $writeToLocalconf_dat['file'] = $absFullPath ? $absFullPath : PATH_typo3conf.'localconf.php';
186 $writeToLocalconf_dat['tmpfile'] = $writeToLocalconf_dat['file'].$tmpExt;
187
188 // Checking write state of localconf.php:
189 if (!$this->allowUpdateLocalConf) {
190 die('->allowUpdateLocalConf flag in the install object is not set and therefore "localconf.php" cannot be altered.');
191 }
192 if (!@is_writable($writeToLocalconf_dat['file'])) {
193 die($writeToLocalconf_dat['file'].' is not writable!');
194 }
195
196 // Splitting localconf.php file into lines:
197 $lines = explode(chr(10),str_replace(chr(13),'',trim(t3lib_div::getUrl($writeToLocalconf_dat['file']))));
198 $writeToLocalconf_dat['endLine'] = array_pop($lines); // Getting "? >" ending.
199
200 // Checking if "updated" line was set by this tool - if so remove old line.
201 $updatedLine = array_pop($lines);
202 $writeToLocalconf_dat['updatedText'] = '// Updated by '.$this->updateIdentity.' ';
203
204 if (!strstr($updatedLine, $writeToLocalconf_dat['updatedText'])) {
205 array_push($lines,$updatedLine);
206 }
207
208 if (is_array($inlines)) { // Setting a line and write:
209 // Setting configuration
210 $updatedLine = $writeToLocalconf_dat['updatedText'].date($GLOBALS['TYPO3_CONF_VARS']['SYS']['ddmmyy'].' H:i:s');
211 array_push($inlines,$updatedLine);
212 array_push($inlines,$writeToLocalconf_dat['endLine']);
213
214 if ($this->setLocalconf) {
215 $success = FALSE;
216 if (!t3lib_div::writeFile($writeToLocalconf_dat['tmpfile'],implode(chr(10),$inlines))) {
217 $msg = 'typo3conf/localconf.php'.$tmpExt.' could not be written - maybe a write access problem?';
218 }
219 elseif (strcmp(t3lib_div::getUrl($writeToLocalconf_dat['tmpfile']), implode(chr(10),$inlines))) {
220 @unlink($writeToLocalconf_dat['tmpfile']);
221 $msg = 'typo3conf/localconf.php'.$tmpExt.' was NOT written properly (written content didn\'t match file content) - maybe a disk space problem?';
222 }
223 elseif (!@copy($writeToLocalconf_dat['tmpfile'],$writeToLocalconf_dat['file'])) {
224 $msg = 'typo3conf/localconf.php could not be replaced by typo3conf/localconf.php'.$tmpExt.' - maybe a write access problem?';
225 }
226 else {
227 @unlink($writeToLocalconf_dat['tmpfile']);
228 $success = TRUE;
229 $msg = 'Configuration written to typo3conf/localconf.php';
230 }
231 $this->messages[] = $msg;
232
233 if ($success) {
234 return 'continue';
235 } else {
236 t3lib_div::sysLog($msg, 'Core', 3);
237 return 'nochange';
238 }
239 } else {
240 return 'nochange';
241 }
242 } else { // Return lines found in localconf.php
243 return $lines;
244 }
245 }
246
247 /**
248 * Checking for linebreaks in the string
249 *
250 * @param string String to test
251 * @return boolean Returns TRUE if string is OK
252 * @see setValueInLocalconfFile()
253 */
254 function checkForBadString($string) {
255 return preg_match('/['.chr(10).chr(13).']/',$string) ? FALSE : TRUE;
256 }
257
258 /**
259 * Replaces ' with \' and \ with \\
260 *
261 * @param string Input value
262 * @return string Output value
263 * @see setValueInLocalconfFile()
264 */
265 function slashValueForSingleDashes($value) {
266 $value = str_replace("'.chr(10).'", '###INSTALL_TOOL_LINEBREAK###', $value);
267 $value = str_replace("'","\'",str_replace('\\','\\\\',$value));
268 $value = str_replace('###INSTALL_TOOL_LINEBREAK###', "'.chr(10).'", $value);
269
270 return $value;
271 }
272
273
274
275
276
277
278
279
280
281
282 /*************************************
283 *
284 * SQL
285 *
286 *************************************/
287
288 /**
289 * Reads the field definitions for the input SQL-file string
290 *
291 * @param string Should be a string read from an SQL-file made with 'mysqldump [database_name] -d'
292 * @return array Array with information about table.
293 */
294 function getFieldDefinitions_fileContent($fileContent) {
295 $lines = t3lib_div::trimExplode(chr(10), $fileContent, 1);
296 $table = '';
297 $total = array();
298
299 foreach ($lines as $value) {
300 if (substr($value,0,1)=='#') {
301 continue; // Ignore comments
302 }
303
304 if (!strlen($table)) {
305 $parts = explode(' ',$value);
306 if ($parts[0]=='CREATE' && $parts[1]=='TABLE') {
307 $table = str_replace( '`', '', $parts[2]);
308 if (TYPO3_OS=='WIN') { // tablenames are always lowercase on windows!
309 $table = strtolower($table);
310 }
311 }
312 } else {
313 if (substr($value,0,1)==')' && substr($value,-1)==';') {
314 $ttype = array();
315 if (preg_match('/(ENGINE|TYPE)[ ]*=[ ]*([a-zA-Z]*)/',$value,$ttype)) {
316 $total[$table]['extra']['ENGINE'] = $ttype[2];
317 } // Otherwise, just do nothing: If table engine is not defined, just accept the system default.
318
319 // Set the collation, if specified
320 if (preg_match('/(COLLATE)[ ]*=[ ]*([a-zA-z0-9_-]+)/', $value, $tcollation)) {
321 $total[$table]['extra']['COLLATE'] = $tcollation[2];
322 } else {
323 // 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)
324 if (preg_match('/(CHARSET|CHARACTER SET)[ ]*=[ ]*([a-zA-z0-9_-]+)/', $value, $tcharset)) { // Note: Keywords "DEFAULT CHARSET" and "CHARSET" are the same, so "DEFAULT" can just be ignored
325 $charset = $tcharset[2];
326 } else {
327 $charset = $GLOBALS['TYPO3_DB']->default_charset; // Fallback to default charset
328 }
329 $total[$table]['extra']['COLLATE'] = $this->getCollationForCharset($charset);
330 }
331
332 $table = ''; // Remove table marker and start looking for the next "CREATE TABLE" statement
333 } else {
334 $lineV = preg_replace('/,$/','',$value); // Strip trailing commas
335 $lineV = str_replace('`', '', $lineV);
336 $lineV = str_replace(' ', ' ', $lineV); // Remove double blanks
337
338 $parts = explode(' ', $lineV, 2);
339 if (!preg_match('/(PRIMARY|UNIQUE|FULLTEXT|INDEX|KEY)/',$parts[0])) { // Field definition
340
341 // Make sure there is no default value when auto_increment is set
342 if (stristr($parts[1],'auto_increment')) {
343 $parts[1] = preg_replace('/ default \'0\'/i','',$parts[1]);
344 }
345 // "default" is always lower-case
346 if (stristr($parts[1], ' DEFAULT ')) {
347 $parts[1] = str_ireplace(' DEFAULT ', ' default ', $parts[1]);
348 }
349
350 // Change order of "default" and "null" statements
351 $parts[1] = preg_replace('/(.*) (default .*) (NOT NULL)/', '$1 $3 $2', $parts[1]);
352 $parts[1] = preg_replace('/(.*) (default .*) (NULL)/', '$1 $3 $2', $parts[1]);
353
354 $key = $parts[0];
355 $total[$table]['fields'][$key] = $parts[1];
356
357 } else { // Key definition
358 $search = array('/UNIQUE (INDEX|KEY)/', '/FULLTEXT (INDEX|KEY)/', '/INDEX/');
359 $replace = array('UNIQUE', 'FULLTEXT', 'KEY');
360 $lineV = preg_replace($search, $replace, $lineV);
361
362 if (preg_match('/PRIMARY|UNIQUE|FULLTEXT/', $parts[0])) {
363 $parts[1] = preg_replace('/^(KEY|INDEX) /', '', $parts[1]);
364 }
365
366 $newParts = explode(' ',$parts[1],2);
367 $key = $parts[0]=='PRIMARY' ? $parts[0] : $newParts[0];
368
369 $total[$table]['keys'][$key] = $lineV;
370
371 // This is a protection against doing something stupid: Only allow clearing of cache_* and index_* tables.
372 if (preg_match('/^(cache|index)_/',$table)) {
373 // Suggest to truncate (clear) this table
374 $total[$table]['extra']['CLEAR'] = 1;
375 }
376 }
377 }
378 }
379 }
380
381 $this->getFieldDefinitions_sqlContent_parseTypes($total);
382 return $total;
383 }
384
385 /**
386 * Multiplies varchars/tinytext fields in size according to $this->multiplySize
387 * 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.
388 *
389 * @param array Total array (from getFieldDefinitions_fileContent())
390 * @return void
391 * @access private
392 * @see getFieldDefinitions_fileContent()
393 */
394 function getFieldDefinitions_sqlContent_parseTypes(&$total) {
395
396 $mSize = (double)$this->multiplySize;
397 if ($mSize > 1) {
398
399 // Init SQL parser:
400 $sqlParser = t3lib_div::makeInstance('t3lib_sqlparser');
401 foreach($total as $table => $cfg) {
402 if (is_array($cfg['fields'])) {
403 foreach($cfg['fields'] as $fN => $fType) {
404 $orig_fType = $fType;
405 $fInfo = $sqlParser->parseFieldDef($fType);
406
407 switch($fInfo['fieldType']) {
408 case 'char':
409 case 'varchar':
410 $newSize = round($fInfo['value']*$mSize);
411
412 if ($newSize <= 255) {
413 $fInfo['value'] = $newSize;
414 } else {
415 $fInfo = array(
416 'fieldType' => 'text',
417 'featureIndex' => array(
418 'NOTNULL' => array(
419 'keyword' => 'NOT NULL'
420 )
421 )
422 );
423 // Change key definition if necessary (must use "prefix" on TEXT columns)
424 if (is_array($cfg['keys'])) {
425 foreach ($cfg['keys'] as $kN => $kType) {
426 $match = array();
427 preg_match('/^([^(]*)\(([^)]+)\)(.*)/', $kType, $match);
428 $keys = array();
429 foreach (t3lib_div::trimExplode(',',$match[2]) as $kfN) {
430 if ($fN == $kfN) {
431 $kfN .= '('.$newSize.')';
432 }
433 $keys[] = $kfN;
434 }
435 $total[$table]['keys'][$kN] = $match[1].'('.implode(',',$keys).')'.$match[3];
436 }
437 }
438 }
439 break;
440 case 'tinytext':
441 $fInfo['fieldType'] = 'text';
442 break;
443 }
444
445 $total[$table]['fields'][$fN] = $sqlParser->compileFieldCfg($fInfo);
446 if ($sqlParser->parse_error) die($sqlParser->parse_error);
447 }
448 }
449 }
450 }
451 }
452
453 /**
454 * Look up the default collation for specified character set based on "SHOW CHARACTER SET" output
455 *
456 * @param string Character set
457 * @return string Corresponding default collation
458 */
459 function getCollationForCharset($charset) {
460 // Load character sets, if not cached already
461 if (!count($this->character_sets)) {
462 if (method_exists($GLOBALS['TYPO3_DB'],'admin_get_charsets')) {
463 $this->character_sets = $GLOBALS['TYPO3_DB']->admin_get_charsets();
464 } else {
465 $this->character_sets[$charset] = array(); // Add empty element to avoid that the check will be repeated
466 }
467 }
468
469 $collation = '';
470 if (isset($this->character_sets[$charset]['Default collation'])) {
471 $collation = $this->character_sets[$charset]['Default collation'];
472 }
473
474 return $collation;
475 }
476
477 /**
478 * Reads the field definitions for the current database
479 *
480 * @return array Array with information about table.
481 */
482 function getFieldDefinitions_database() {
483 $total = array();
484 $tempKeys = array();
485 $tempKeysPrefix = array();
486
487 $GLOBALS['TYPO3_DB']->sql_select_db(TYPO3_db);
488 echo $GLOBALS['TYPO3_DB']->sql_error();
489
490 $tables = $GLOBALS['TYPO3_DB']->admin_get_tables(TYPO3_db);
491 foreach ($tables as $tableName => $tableStatus) {
492
493 // Fields:
494 $fieldInformation = $GLOBALS['TYPO3_DB']->admin_get_fields($tableName);
495 foreach ($fieldInformation as $fN => $fieldRow) {
496 $total[$tableName]['fields'][$fN] = $this->assembleFieldDefinition($fieldRow);
497 }
498
499 // Keys:
500 $keyInformation = $GLOBALS['TYPO3_DB']->admin_get_keys($tableName);
501
502 foreach ($keyInformation as $keyRow) {
503 $keyName = $keyRow['Key_name'];
504 $colName = $keyRow['Column_name'];
505 if ($keyRow['Sub_part']) {
506 $colName.= '('.$keyRow['Sub_part'].')';
507 }
508 $tempKeys[$tableName][$keyName][$keyRow['Seq_in_index']] = $colName;
509 if ($keyName=='PRIMARY') {
510 $prefix = 'PRIMARY KEY';
511 } else {
512 if ($keyRow['Index_type']=='FULLTEXT') {
513 $prefix = 'FULLTEXT';
514 } elseif ($keyRow['Non_unique']) {
515 $prefix = 'KEY';
516 } else {
517 $prefix = 'UNIQUE';
518 }
519 $prefix.= ' '.$keyName;
520 }
521 $tempKeysPrefix[$tableName][$keyName] = $prefix;
522 }
523
524 // Table status (storage engine, collaction, etc.)
525 if (is_array($tableStatus)) {
526 $tableExtraFields = array(
527 'Engine' => 'ENGINE',
528 'Collation' => 'COLLATE',
529 );
530
531 foreach ($tableExtraFields as $mysqlKey=>$internalKey) {
532 if (isset($tableStatus[$mysqlKey])) {
533 $total[$tableName]['extra'][$internalKey] = $tableStatus[$mysqlKey];
534 }
535 }
536 }
537 }
538
539 // Compile key information:
540 if (count($tempKeys)) {
541 foreach ($tempKeys as $table => $keyInf) {
542 foreach ($keyInf as $kName => $index) {
543 ksort($index);
544 $total[$table]['keys'][$kName] = $tempKeysPrefix[$table][$kName].' ('.implode(',',$index).')';
545 }
546 }
547 }
548
549 return $total;
550 }
551
552 /**
553 * Compares two arrays with field information and returns information about fields that are MISSING and fields that have CHANGED.
554 * FDsrc and FDcomp can be switched if you want the list of stuff to remove rather than update.
555 *
556 * @param array Field definitions, source (from getFieldDefinitions_fileContent())
557 * @param array Field definitions, comparison. (from getFieldDefinitions_database())
558 * @param string Table names (in list) which is the ONLY one observed.
559 * @param boolean 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.
560 * @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
561 */
562 function getDatabaseExtra($FDsrc, $FDcomp, $onlyTableList='',$ignoreNotNullWhenComparing=true) {
563 $extraArr = array();
564 $diffArr = array();
565
566 if (is_array($FDsrc)) {
567 foreach ($FDsrc as $table => $info) {
568 if (!strlen($onlyTableList) || t3lib_div::inList($onlyTableList, $table)) {
569 if (!isset($FDcomp[$table])) {
570 $extraArr[$table] = $info; // If the table was not in the FDcomp-array, the result array is loaded with that table.
571 $extraArr[$table]['whole_table']=1;
572 } else {
573 $keyTypes = explode(',','extra,fields,keys');
574 foreach ($keyTypes as $theKey) {
575 if (is_array($info[$theKey])) {
576 foreach ($info[$theKey] as $fieldN => $fieldC) {
577 $fieldN = str_replace('`','',$fieldN);
578 if ($fieldN=='COLLATE') {
579 continue; // TODO: collation support is currently disabled (needs more testing)
580 }
581
582 if (!isset($FDcomp[$table][$theKey][$fieldN])) {
583 $extraArr[$table][$theKey][$fieldN] = $fieldC;
584 } else {
585 $fieldC = trim($fieldC);
586 if ($ignoreNotNullWhenComparing) {
587 $fieldC = str_replace(' NOT NULL', '', $fieldC);
588 $FDcomp[$table][$theKey][$fieldN] = str_replace(' NOT NULL', '', $FDcomp[$table][$theKey][$fieldN]);
589 }
590 if ($fieldC !== $FDcomp[$table][$theKey][$fieldN]) {
591 $diffArr[$table][$theKey][$fieldN] = $fieldC;
592 $diffArr_cur[$table][$theKey][$fieldN] = $FDcomp[$table][$theKey][$fieldN];
593 }
594 }
595 }
596 }
597 }
598 }
599 }
600 }
601 }
602
603 $output = array(
604 'extra' => $extraArr,
605 'diff' => $diffArr,
606 'diff_currentValues' => $diffArr_cur
607 );
608
609 return $output;
610 }
611
612 /**
613 * Returns an array with SQL-statements that is needed to update according to the diff-array
614 *
615 * @param array Array with differences of current and needed DB settings. (from getDatabaseExtra())
616 * @param string List of fields in diff array to take notice of.
617 * @return array Array of SQL statements (organized in keys depending on type)
618 */
619 function getUpdateSuggestions($diffArr,$keyList='extra,diff') {
620 $statements = array();
621 $deletedPrefixKey = $this->deletedPrefixKey;
622 $remove = 0;
623 if ($keyList == 'remove') {
624 $remove = 1;
625 $keyList = 'extra';
626 }
627 $keyList = explode(',',$keyList);
628 foreach ($keyList as $theKey) {
629 if (is_array($diffArr[$theKey])) {
630 foreach ($diffArr[$theKey] as $table => $info) {
631 $whole_table = array();
632 if (is_array($info['fields'])) {
633 foreach ($info['fields'] as $fN => $fV) {
634 if ($info['whole_table']) {
635 $whole_table[]=$fN.' '.$fV;
636 } else {
637 // Special case to work around MySQL problems when adding auto_increment fields:
638 if (stristr($fV, 'auto_increment')) {
639 // The field can only be set "auto_increment" if there exists a PRIMARY key of that field already.
640 // The check does not look up which field is primary but just assumes it must be the field with the auto_increment value...
641 if (isset($diffArr['extra'][$table]['keys']['PRIMARY'])) {
642 // Remove "auto_increment" from the statement - it will be suggested in a 2nd step after the primary key was created
643 $fV = str_replace(' auto_increment', '', $fV);
644 } else {
645 // In the next step, attempt to clear the table once again (2 = force)
646 $info['extra']['CLEAR'] = 2;
647 }
648 }
649 if ($theKey=='extra') {
650 if ($remove) {
651 if (substr($fN,0,strlen($deletedPrefixKey))!=$deletedPrefixKey) {
652 $statement = 'ALTER TABLE '.$table.' CHANGE '.$fN.' '.$deletedPrefixKey.$fN.' '.$fV.';';
653 $statements['change'][md5($statement)] = $statement;
654 } else {
655 $statement = 'ALTER TABLE '.$table.' DROP '.$fN.';';
656 $statements['drop'][md5($statement)] = $statement;
657 }
658 } else {
659 $statement = 'ALTER TABLE '.$table.' ADD '.$fN.' '.$fV.';';
660 $statements['add'][md5($statement)] = $statement;
661 }
662 } elseif ($theKey=='diff') {
663 $statement = 'ALTER TABLE '.$table.' CHANGE '.$fN.' '.$fN.' '.$fV.';';
664 $statements['change'][md5($statement)] = $statement;
665 $statements['change_currentValue'][md5($statement)] = $diffArr['diff_currentValues'][$table]['fields'][$fN];
666 }
667 }
668 }
669 }
670 if (is_array($info['keys'])) {
671 foreach ($info['keys'] as $fN => $fV) {
672 if ($info['whole_table']) {
673 $whole_table[] = $fV;
674 } else {
675 if ($theKey=='extra') {
676 if ($remove) {
677 $statement = 'ALTER TABLE '.$table.($fN=='PRIMARY' ? ' DROP PRIMARY KEY' : ' DROP KEY '.$fN).';';
678 $statements['drop'][md5($statement)] = $statement;
679 } else {
680 $statement = 'ALTER TABLE '.$table.' ADD '.$fV.';';
681 $statements['add'][md5($statement)] = $statement;
682 }
683 } elseif ($theKey=='diff') {
684 $statement = 'ALTER TABLE '.$table.($fN=='PRIMARY' ? ' DROP PRIMARY KEY' : ' DROP KEY '.$fN).';';
685 $statements['change'][md5($statement)] = $statement;
686 $statement = 'ALTER TABLE '.$table.' ADD '.$fV.';';
687 $statements['change'][md5($statement)] = $statement;
688 }
689 }
690 }
691 }
692 if (is_array($info['extra'])) {
693 $extras = array();
694 $extras_currentValue = array();
695 $clear_table = false;
696
697 foreach ($info['extra'] as $fN => $fV) {
698
699 // Only consider statements which are missing in the database but don't remove existing properties
700 if (!$remove) {
701 if (!$info['whole_table']) { // If the whole table is created at once, we take care of this later by imploding all elements of $info['extra']
702 if ($fN=='CLEAR') {
703 // Truncate table must happen later, not now
704 // Valid values for CLEAR: 1=only clear if keys are missing, 2=clear anyway (force)
705 if (count($info['keys']) || $fV==2) {
706 $clear_table = true;
707 }
708 continue;
709 } else {
710 $extras[] = $fN.'='.$fV;
711 $extras_currentValue[] = $fN.'='.$diffArr['diff_currentValues'][$table]['extra'][$fN];
712 }
713 }
714 }
715 }
716 if ($clear_table) {
717 $statement = 'TRUNCATE TABLE '.$table.';';
718 $statements['clear_table'][md5($statement)] = $statement;
719 }
720 if (count($extras)) {
721 $statement = 'ALTER TABLE '.$table.' '.implode(' ',$extras).';';
722 $statements['change'][md5($statement)] = $statement;
723 $statements['change_currentValue'][md5($statement)] = implode(' ',$extras_currentValue);
724 }
725 }
726 if ($info['whole_table']) {
727 if ($remove) {
728 if (substr($table,0,strlen($deletedPrefixKey))!=$deletedPrefixKey) {
729 $statement = 'ALTER TABLE '.$table.' RENAME '.$deletedPrefixKey.$table.';';
730 $statements['change_table'][md5($statement)] = $statement;
731 } else {
732 $statement = 'DROP TABLE '.$table.';';
733 $statements['drop_table'][md5($statement)] = $statement;
734 }
735 // count:
736 $count = $GLOBALS['TYPO3_DB']->exec_SELECTcountRows('*', $table);
737 $statements['tables_count'][md5($statement)] = $count?'Records in table: '.$count:'';
738 } else {
739 $statement = 'CREATE TABLE '.$table." (\n".implode(",\n",$whole_table)."\n)";
740 if ($info['extra']) {
741 foreach ($info['extra'] as $k=>$v) {
742 if ($k=='COLLATE' || $k=='CLEAR') {
743 continue; // Skip these special statements. TODO: collation support is currently disabled (needs more testing)
744 }
745 $statement.= ' '.$k.'='.$v; // Add extra attributes like ENGINE, CHARSET, etc.
746 }
747 }
748 $statement.= ';';
749 $statements['create_table'][md5($statement)] = $statement;
750 }
751 }
752 }
753 }
754 }
755
756 return $statements;
757 }
758
759 /**
760 * Converts a result row with field information into the SQL field definition string
761 *
762 * @param array MySQL result row
763 * @return string Field definition
764 */
765 function assembleFieldDefinition($row) {
766 $field = array($row['Type']);
767
768 if ($row['Null']=='NO') {
769 $field[] = 'NOT NULL';
770 }
771 if (!strstr($row['Type'],'blob') && !strstr($row['Type'],'text')) {
772 // Add a default value if the field is not auto-incremented (these fields never have a default definition)
773 if (!stristr($row['Extra'],'auto_increment')) {
774 $field[] = 'default \''.addslashes($row['Default']).'\'';
775 }
776 }
777 if ($row['Extra']) {
778 $field[] = $row['Extra'];
779 }
780
781 return implode(' ',$field);
782 }
783
784 /**
785 * Returns an array where every entry is a single SQL-statement. Input must be formatted like an ordinary MySQL-dump files.
786 *
787 * @param string 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.
788 * @param boolean If set, non-SQL content (like comments and blank lines) is not included in the final output
789 * @param string Regex to filter SQL lines to include
790 * @return array Array of SQL statements
791 */
792 function getStatementArray($sqlcode,$removeNonSQL=0,$query_regex='') {
793 $sqlcodeArr = explode(chr(10), $sqlcode);
794
795 // Based on the assumption that the sql-dump has
796 $statementArray = array();
797 $statementArrayPointer = 0;
798
799 foreach ($sqlcodeArr as $line => $lineContent) {
800 $is_set = 0;
801
802 // auto_increment fields cannot have a default value!
803 if (stristr($lineContent,'auto_increment')) {
804 $lineContent = preg_replace('/ default \'0\'/i', '', $lineContent);
805 }
806
807 if (!$removeNonSQL || (strcmp(trim($lineContent),'') && substr(trim($lineContent),0,1)!='#' && substr(trim($lineContent),0,2)!='--')) { // '--' is seen as mysqldump comments from server version 3.23.49
808 $statementArray[$statementArrayPointer].= $lineContent;
809 $is_set = 1;
810 }
811 if (substr(trim($lineContent),-1)==';') {
812 if (isset($statementArray[$statementArrayPointer])) {
813 if (!trim($statementArray[$statementArrayPointer]) || ($query_regex && !preg_match('/'.$query_regex.'/i',trim($statementArray[$statementArrayPointer])))) {
814 unset($statementArray[$statementArrayPointer]);
815 }
816 }
817 $statementArrayPointer++;
818
819 } elseif ($is_set) {
820 $statementArray[$statementArrayPointer].= chr(10);
821 }
822 }
823
824 return $statementArray;
825 }
826
827 /**
828 * Returns tables to create and how many records in each
829 *
830 * @param array Array of SQL statements to analyse.
831 * @param boolean If set, will count number of INSERT INTO statements following that table definition
832 * @return array Array with table definitions in index 0 and count in index 1
833 */
834 function getCreateTables($statements, $insertCountFlag=0) {
835 $crTables = array();
836 $insertCount = array();
837 foreach ($statements as $line => $lineContent) {
838 $reg = array();
839 if (preg_match('/^create[[:space:]]*table[[:space:]]*[`]?([[:alnum:]_]*)[`]?/i',substr($lineContent,0,100),$reg)) {
840 $table = trim($reg[1]);
841 if ($table) {
842 // table names are always lowercase on Windows!
843 if (TYPO3_OS == 'WIN') {
844 $table = strtolower($table);
845 }
846 $sqlLines = explode(chr(10), $lineContent);
847 foreach ($sqlLines as $k=>$v) {
848 if (stristr($v,'auto_increment')) {
849 $sqlLines[$k] = preg_replace('/ default \'0\'/i', '', $v);
850 }
851 }
852 $lineContent = implode(chr(10), $sqlLines);
853 $crTables[$table] = $lineContent;
854 }
855 } elseif ($insertCountFlag && preg_match('/^insert[[:space:]]*into[[:space:]]*[`]?([[:alnum:]_]*)[`]?/i',substr($lineContent,0,100),$reg)) {
856 $nTable = trim($reg[1]);
857 $insertCount[$nTable]++;
858 }
859 }
860
861 return array($crTables,$insertCount);
862 }
863
864 /**
865 * Extracts all insert statements from $statement array where content is inserted into $table
866 *
867 * @param array Array of SQL statements
868 * @param string Table name
869 * @return array Array of INSERT INTO statements where table match $table
870 */
871 function getTableInsertStatements($statements, $table) {
872 $outStatements=array();
873 foreach($statements as $line => $lineContent) {
874 $reg = array();
875 if (preg_match('/^insert[[:space:]]*into[[:space:]]*[`]?([[:alnum:]_]*)[`]?/i',substr($lineContent,0,100),$reg)) {
876 $nTable = trim($reg[1]);
877 if ($nTable && !strcmp($table,$nTable)) {
878 $outStatements[] = $lineContent;
879 }
880 }
881 }
882 return $outStatements;
883 }
884
885 /**
886 * Performs the queries passed from the input array.
887 *
888 * @param array Array of SQL queries to execute.
889 * @param array 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)
890 * @return mixed Array with error message from database if any occured. Otherwise true if everything was executed successfully.
891 */
892 function performUpdateQueries($arr,$keyArr) {
893 $result = array();
894 if (is_array($arr)) {
895 foreach($arr as $key => $string) {
896 if (isset($keyArr[$key]) && $keyArr[$key]) {
897 $res = $GLOBALS['TYPO3_DB']->admin_query($string);
898 if ($res === false) {
899 $result[$key] = $GLOBALS['TYPO3_DB']->sql_error();
900 } elseif (is_resource($res)) {
901 $GLOBALS['TYPO3_DB']->sql_free_result($res);
902 }
903 }
904 }
905 }
906 if (count($result) > 0) {
907 return $result;
908 } else {
909 return true;
910 }
911 }
912
913 /**
914 * Returns list of tables in the database
915 *
916 * @return array List of tables.
917 * @see t3lib_db::admin_get_tables()
918 */
919 function getListOfTables() {
920 $whichTables = $GLOBALS['TYPO3_DB']->admin_get_tables(TYPO3_db);
921 foreach ($whichTables as $key=>&$value) {
922 $value = $key;
923 }
924 return $whichTables;
925 }
926
927 /**
928 * Creates a table which checkboxes for updating database.
929 *
930 * @param array Array of statements (key / value pairs where key is used for the checkboxes)
931 * @param string Label for the table.
932 * @param boolean If set, then checkboxes are set by default.
933 * @param boolean If set, then icons are shown.
934 * @param array Array of "current values" for each key/value pair in $arr. Shown if given.
935 * @param boolean If set, will show the prefix "Current value" if $currentValue is given.
936 * @return string HTML table with checkboxes for update. Must be wrapped in a form.
937 */
938 function generateUpdateDatabaseForm_checkboxes($arr,$label,$checked=1,$iconDis=0,$currentValue=array(),$cVfullMsg=0) {
939 $out = array();
940 if (is_array($arr)) {
941 foreach($arr as $key => $string) {
942 $ico = '';
943 $warnings = array();
944
945 if ($iconDis) {
946 if (preg_match('/^TRUNCATE/i',$string)) {
947 $ico.= '<img src="'.$this->backPath.'gfx/icon_warning.gif" width="18" height="16" align="top" alt="" /><strong> </strong>';
948 $warnings['clear_table_info'] = 'Clearing the table is sometimes neccessary when adding new keys. In case of cache_* tables this should not hurt at all. However, use it with care.';
949 } elseif (stristr($string,' user_')) {
950 $ico.= '<img src="'.$this->backPath.'gfx/icon_warning.gif" width="18" height="16" align="top" alt="" /><strong>(USER) </strong>';
951 } elseif (stristr($string,' app_')) {
952 $ico.= '<img src="'.$this->backPath.'gfx/icon_warning.gif" width="18" height="16" align="top" alt="" /><strong>(APP) </strong>';
953 } elseif (stristr($string,' ttx_') || stristr($string,' tx_')) {
954 $ico.= '<img src="'.$this->backPath.'gfx/icon_warning.gif" width="18" height="16" align="top" alt="" /><strong>(EXT) </strong>';
955 }
956 }
957 $out[]='
958 <tr>
959 <td valign="top"><input type="checkbox" id="db-'.$key.'" name="'.$this->dbUpdateCheckboxPrefix.'['.$key.']" value="1"'.($checked?' checked="checked"':'').' /></td>
960 <td nowrap="nowrap"><label for="db-'.$key.'">'.nl2br($ico.htmlspecialchars($string)).'</label></td>
961 </tr>';
962 if (isset($currentValue[$key])) {
963 $out[]='
964 <tr>
965 <td valign="top"></td>
966 <td nowrap="nowrap" style="color : #666666;">'.nl2br((!$cVfullMsg?"Current value: ":"").'<em>'.$currentValue[$key].'</em>').'</td>
967 </tr>';
968 }
969 }
970 if (count($warnings)) {
971 $out[] = '
972 <tr>
973 <td valign="top"></td>
974 <td style="color : #666666;"><em>' . implode('<br />',$warnings) . '</em></td>
975 </tr>';
976 }
977
978 // Compile rows:
979 $content = '
980 <!-- Update database fields / tables -->
981 <h3>'.$label.'</h3>
982 <table border="0" cellpadding="2" cellspacing="2" class="update-db-fields">'.implode('',$out).'
983 </table>';
984 }
985
986 return $content;
987 }
988
989 /**
990 * Reads the field definitions for the input SQL-file string
991 *
992 * @param string Should be a string read from an SQL-file made with 'mysqldump [database_name] -d'
993 * @return array Array with information about table.
994 * @deprecated since TYPO3 4.2 Use ->getFieldDefinitions_fileContent() instead!
995 */
996 function getFieldDefinitions_sqlContent($fileContent) {
997 return $this->getFieldDefinitions_fileContent($fileContent);
998 }
999 }
1000
1001 if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['t3lib/class.t3lib_install.php']) {
1002 include_once($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['t3lib/class.t3lib_install.php']);
1003 }
1004
1005 ?>