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