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