* Merged changes from TYPO3_3-8 branch back
[Packages/TYPO3.CMS.git] / t3lib / class.t3lib_install.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 1999-2005 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 * 112: function t3lib_install()
41 *
42 * SECTION: Writing to localconf.php
43 * 136: function setValueInLocalconfFile(&$line_array, $variable, $value)
44 * 186: function writeToLocalconf_control($inlines='')
45 * 238: function checkForBadString($string)
46 * 251: function slashValueForSingleDashes($value)
47 *
48 * SECTION: SQL
49 * 276: function getFieldDefinitions_sqlContent($sqlContent)
50 * 320: function getFieldDefinitions_sqlContent_parseTypes(&$total)
51 * 367: function getFieldDefinitions_database()
52 * 411: function getDatabaseExtra($FDsrc, $FDcomp, $onlyTableList='')
53 * 456: function getUpdateSuggestions($diffArr,$keyList='extra,diff')
54 * 557: function assembleFieldDefinition($row)
55 * 586: function getStatementArray($sqlcode,$removeNonSQL=0,$query_regex='')
56 * 626: function getCreateTables($statements, $insertCountFlag=0)
57 * 650: function getTableInsertStatements($statements, $table)
58 * 670: function performUpdateQueries($arr,$keyArr)
59 * 686: function getListOfTables()
60 * 702: 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
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 $writeToLocalconf_dat['file'] = $absFullPath ? $absFullPath : PATH_typo3conf.'localconf.php';
185
186 // Checking write state of localconf.php:
187 if (!$this->allowUpdateLocalConf) {
188 die("->allowUpdateLocalConf flag in the install object is not set and therefore 'localconf.php' cannot be altered.");
189 }
190 if (!@is_writable($writeToLocalconf_dat['file'])) {
191 die($writeToLocalconf_dat['file'].' is not writable!');
192 }
193
194 // Splitting localconf.php file into lines:
195 $lines = explode(chr(10),trim(t3lib_div::getUrl($writeToLocalconf_dat['file'])));
196 $writeToLocalconf_dat['endLine'] = array_pop($lines); // Getting "? >" ending.
197
198 // Checking if "updated" line was set by this tool - if so remove old line.
199 $updatedLine = array_pop($lines);
200 $writeToLocalconf_dat['updatedText'] = '// Updated by '.$this->updateIdentity.' ';
201 if (!strstr($updatedLine, $writeToLocalconf_dat['updatedText'])) {
202 array_push($lines,$updatedLine);
203 }
204
205 if (is_array($inlines)) { // Setting a line and write:
206 // Setting configuration
207 $updatedLine = $writeToLocalconf_dat['updatedText'].date('d-m-Y H:i:s');
208 array_push($inlines,$updatedLine);
209 array_push($inlines,$writeToLocalconf_dat['endLine']);
210
211 if ($this->setLocalconf) {
212 t3lib_div::writeFile($writeToLocalconf_dat['file'],implode(chr(10),$inlines));
213
214 if (strcmp(t3lib_div::getUrl($writeToLocalconf_dat['file']), implode(chr(10),$inlines))) {
215 die('typo3conf/localconf.php was NOT updated properly (written content didn\'t match file content) - maybe write access problem?');
216 }
217
218 $this->messages[]= 'Configuration written to typo3conf/localconf.php';
219 return 'continue';
220 } else {
221 return 'nochange';
222 }
223 } else { // Return lines found in localconf.php
224 return $lines;
225 }
226 }
227
228 /**
229 * Checking for linebreaks in the string
230 *
231 * @param string String to test
232 * @return boolean Returns TRUE if string is OK
233 * @see setValueInLocalconfFile()
234 */
235 function checkForBadString($string) {
236 if (ereg('['.chr(10).chr(13).']',$string)){
237 return FALSE;
238 } else return TRUE;
239 }
240
241 /**
242 * Replaces ' with \' and \ with \\
243 *
244 * @param string Input value
245 * @return string Output value
246 * @see setValueInLocalconfFile()
247 */
248 function slashValueForSingleDashes($value) {
249 return str_replace("'","\'",str_replace('\\','\\\\',$value));
250 }
251
252
253
254
255
256
257
258
259
260
261 /*************************************
262 *
263 * SQL
264 *
265 *************************************/
266
267 /**
268 * Reads the field definitions for the input sql-file string
269 *
270 * @param string $sqlContent: Should be a string read from an sql-file made with 'mysqldump [database_name] -d'
271 * @return array Array with information about table.
272 */
273 function getFieldDefinitions_sqlContent($sqlContent) {
274 $lines = t3lib_div::trimExplode(chr(10), $sqlContent,1);
275 $isTable = '';
276
277 foreach($lines as $value) {
278 if ($value[0]!='#') {
279 if (!$isTable) {
280 $parts = explode(' ',$value);
281 if ($parts[0]=='CREATE' && $parts[1]=='TABLE') {
282 $isTable = $parts[2];
283 if (TYPO3_OS=='WIN') { // tablenames are always lowercase on windows!
284 $isTable = strtolower($isTable);
285 }
286 }
287 } else {
288 if (substr($value,0,1)==')' && substr($value,-1)==';') {
289 preg_match('/(ENGINE|TYPE)=([a-zA-Z]*)/',$value,$ttype);
290 $total[$isTable]['extra']['ttype'] = $ttype[2];
291 $isTable = '';
292 } else {
293 $lineV = ereg_replace(',$','',$value);
294 $parts = explode(' ',$lineV,2);
295
296 // Make sure there is no default value when auto_increment is set
297 if(stristr($parts[1],'auto_increment')) {
298 $parts[1] = preg_replace('/ default \'0\'/i','',$parts[1]);
299 }
300 // "default" is always lower-case
301 if(strstr($parts[1], ' DEFAULT ')) {
302 $parts[1] = str_replace(' DEFAULT ', ' default ', $parts[1]);
303 }
304 // Change order of "default" and "null" statements
305 $parts[1] = preg_replace('/(.*) (default .*) (NOT NULL)/', '$1 $3 $2', $parts[1]);
306 $parts[1] = preg_replace('/(.*) (default .*) (NULL)/', '$1 $3 $2', $parts[1]);
307
308 if ($parts[0]!='PRIMARY' && $parts[0]!='KEY' && $parts[0]!='UNIQUE') {
309 $total[$isTable]['fields'][$parts[0]] = $parts[1];
310 } else {
311 $newParts = explode(' ',$parts[1],2);
312 $total[$isTable]['keys'][($parts[0]=='PRIMARY'?$parts[0]:$newParts[0])] = $lineV;
313 }
314 }
315 }
316 }
317 }
318
319 $this->getFieldDefinitions_sqlContent_parseTypes($total);
320 return $total;
321 }
322
323 /**
324 * Multiplies varchars/tinytext fields in size according to $this->multiplySize
325 * 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.
326 *
327 * @param array Total array (from getFieldDefinitions_sqlContent())
328 * @return void
329 * @access private
330 * @see getFieldDefinitions_sqlContent()
331 */
332 function getFieldDefinitions_sqlContent_parseTypes(&$total) {
333
334 $mSize = (double)$this->multiplySize;
335 if ($mSize > 1) {
336
337 // Init SQL parser:
338 $sqlParser = t3lib_div::makeInstance('t3lib_sqlparser');
339 foreach($total as $table => $cfg) {
340 foreach($cfg['fields'] as $fN => $fType) {
341 $orig_fType = $fType;
342 $fInfo = $sqlParser->parseFieldDef($fType);
343
344 switch($fInfo['fieldType']) {
345 case 'char':
346 case 'varchar':
347 $newSize = round($fInfo['value']*$mSize);
348
349 if ($newSize <= 255) {
350 $fInfo['value'] = $newSize;
351 } else {
352 $fInfo = array(
353 'fieldType' => 'text',
354 'featureIndex' => array(
355 'NOTNULL' => array(
356 'keyword' => 'NOT NULL'
357 )
358 )
359 );
360 }
361 break;
362 case 'tinytext':
363 $fInfo['fieldType'] = 'text';
364 break;
365 }
366
367 $total[$table]['fields'][$fN] = $sqlParser->compileFieldCfg($fInfo);
368 if ($sqlParser->parse_error) die($sqlParser->parse_error);
369 }
370 }
371 }
372 }
373
374 /**
375 * Reads the field definitions for the current database
376 *
377 * @return array Array with information about table.
378 */
379 function getFieldDefinitions_database() {
380 $total = array();
381 $GLOBALS['TYPO3_DB']->sql_select_db(TYPO3_db);
382 echo $GLOBALS['TYPO3_DB']->sql_error();
383
384 $tables = $GLOBALS['TYPO3_DB']->admin_get_tables(TYPO3_db);
385 foreach($tables as $tableName) {
386
387 // Fields:
388 $fieldInformation = $GLOBALS['TYPO3_DB']->admin_get_fields($tableName);
389 foreach($fieldInformation as $fN => $fieldRow) {
390 $total[$tableName]['fields'][$fN] = $this->assembleFieldDefinition($fieldRow);
391 }
392
393 // Keys:
394 $keyInformation = $GLOBALS['TYPO3_DB']->admin_get_keys($tableName);
395 foreach($keyInformation as $kN => $keyRow) {
396 $tempKeys[$tableName][$keyRow['Key_name']][$keyRow['Seq_in_index']] = $keyRow['Column_name'];
397 $tempKeysPrefix[$tableName][$keyRow['Key_name']]= ($keyRow['Key_name']=='PRIMARY'?'PRIMARY KEY':($keyRow['Non_unique']?'KEY':'UNIQUE').' '.$keyRow['Key_name']);
398 }
399 }
400
401 // Compile information:
402 if (is_array($tempKeys)) {
403 foreach($tempKeys as $table => $keyInf) {
404 foreach($keyInf as $kName => $index) {
405 ksort($index);
406 $total[$table]['keys'][$kName] = $tempKeysPrefix[$table][$kName].' ('.implode(',',$index).')';
407 }
408 }
409 }
410
411 return $total;
412 }
413
414 /**
415 * Compares two arrays with field information and returns information about fields that are MISSING and fields that have CHANGED.
416 * FDsrc and FDcomp can be switched if you want the list of stuff to remove rather than update.
417 *
418 * @param array Field definitions, source (from getFieldDefinitions_sqlContent())
419 * @param array Field definitions, comparison. (from getFieldDefinitions_database())
420 * @param string Table names (in list) which is the ONLY one observed.
421 * @return array Returns an array with 1) all elements from $FSsrc that is not in $FDcomp (in key 'extra') and 2) all elements from $FSsrc that is difference from the ones in $FDcomp
422 */
423 function getDatabaseExtra($FDsrc, $FDcomp, $onlyTableList='') {
424 $extraArr = array();
425 $diffArr = array();
426
427 if (is_array($FDsrc)) {
428 foreach($FDsrc as $table => $info) {
429 if (!strlen($onlyTableList) || t3lib_div::inList($onlyTableList, $table)) {
430 if (!isset($FDcomp[$table])) {
431 $extraArr[$table] = $info; // If the table was not in the FDcomp-array, the result array is loaded with that table.
432 $extraArr[$table]['whole_table']=1;
433 } else {
434 $keyTypes = explode(',','fields,keys');
435 foreach($keyTypes as $theKey) {
436 if (is_array($info[$theKey])) {
437 foreach($info[$theKey] as $fieldN => $fieldC) {
438 if (!isset($FDcomp[$table][$theKey][$fieldN])) {
439 $extraArr[$table][$theKey][$fieldN] = $fieldC;
440 } elseif (strcmp($FDcomp[$table][$theKey][$fieldN], $fieldC)) {
441 $diffArr[$table][$theKey][$fieldN] = $fieldC;
442 $diffArr_cur[$table][$theKey][$fieldN] = $FDcomp[$table][$theKey][$fieldN];
443 }
444 }
445 }
446 }
447 }
448 }
449 }
450 }
451
452 $output = array(
453 'extra' => $extraArr,
454 'diff' => $diffArr,
455 'diff_currentValues' => $diffArr_cur
456 );
457
458 return $output;
459 }
460
461 /**
462 * Returns an array with SQL-statements that is needed to update according to the diff-array
463 *
464 * @param array Array with differences of current and needed DB settings. (from getDatabaseExtra())
465 * @param string List of fields in diff array to take notice of.
466 * @return array Array of SQL statements (organized in keys depending on type)
467 */
468 function getUpdateSuggestions($diffArr,$keyList='extra,diff') {
469 $statements = array();
470 $deletedPrefixKey = $this->deletedPrefixKey;
471 $remove = 0;
472 if ($keyList == 'remove') {
473 $remove = 1;
474 $keyList = 'extra';
475 }
476 $keyList = explode(',',$keyList);
477 foreach($keyList as $theKey) {
478 if (is_array($diffArr[$theKey])) {
479 foreach($diffArr[$theKey] as $table => $info) {
480 $whole_table = array();
481 if (is_array($info['fields'])) {
482 foreach($info['fields'] as $fN => $fV) {
483 if ($info['whole_table']) {
484 $whole_table[]=$fN.' '.$fV;
485 } else {
486 if ($theKey=='extra') {
487 if ($remove) {
488 if (substr($fN,0,strlen($deletedPrefixKey))!=$deletedPrefixKey) {
489 $statement = 'ALTER TABLE '.$table.' CHANGE '.$fN.' '.$deletedPrefixKey.$fN.' '.$fV.';';
490 $statements['change'][md5($statement)] = $statement;
491 } else {
492 $statement = 'ALTER TABLE '.$table.' DROP '.$fN.';';
493 $statements['drop'][md5($statement)] = $statement;
494 }
495 } else {
496 $statement = 'ALTER TABLE '.$table.' ADD '.$fN.' '.$fV.';';
497 $statements['add'][md5($statement)] = $statement;
498 }
499 } elseif ($theKey=='diff') {
500 $statement = 'ALTER TABLE '.$table.' CHANGE '.$fN.' '.$fN.' '.$fV.';';
501 $statements['change'][md5($statement)] = $statement;
502 $statements['change_currentValue'][md5($statement)] = $diffArr['diff_currentValues'][$table]['fields'][$fN];
503 }
504 }
505 }
506 }
507 if (is_array($info['keys'])) {
508 foreach($info['keys'] as $fN => $fV) {
509 if ($info['whole_table']) {
510 if ($fN=='PRIMARY') {
511 $whole_table[] = $fV;
512 } else {
513 $whole_table[] = $fV;
514 }
515 } else {
516 if ($theKey=='extra') {
517 if ($remove) {
518 $statement = 'ALTER TABLE '.$table.($fN=='PRIMARY' ? ' DROP PRIMARY KEY' : ' DROP KEY '.$fN).';';
519 $statements['drop'][md5($statement)] = $statement;
520 } else {
521 $statement = 'ALTER TABLE '.$table.' ADD '.$fV.';';
522 $statements['add'][md5($statement)] = $statement;
523 }
524 } elseif ($theKey=='diff') {
525 $statement = 'ALTER TABLE '.$table.($fN=='PRIMARY' ? ' DROP PRIMARY KEY' : ' DROP KEY '.$fN).';';
526 $statements['change'][md5($statement)] = $statement;
527 $statement = 'ALTER TABLE '.$table.' ADD '.$fV.';';
528 $statements['change'][md5($statement)] = $statement;
529 }
530 }
531 }
532 }
533 if ($info['whole_table']) {
534 if ($remove) {
535 if (substr($table,0,strlen($deletedPrefixKey))!=$deletedPrefixKey) {
536 $statement = 'ALTER TABLE '.$table.' RENAME '.$deletedPrefixKey.$table.';';
537 $statements['change_table'][md5($statement)]=$statement;
538 } else {
539 $statement = 'DROP TABLE '.$table.';';
540 $statements['drop_table'][md5($statement)]=$statement;
541 }
542 // count:
543 $res = $GLOBALS['TYPO3_DB']->exec_SELECTquery('count(*)', $table, '');
544 list($count) = $GLOBALS['TYPO3_DB']->sql_fetch_row($res);
545 $statements['tables_count'][md5($statement)] = $count?'Records in table: '.$count:'';
546 } else {
547 $statement = 'CREATE TABLE '.$table." (\n".implode(",\n",$whole_table)."\n)";
548 $statement .= ($info['extra']['ttype']) ? ' TYPE='.$info['extra']['ttype'].';' : ';';
549 $statements['create_table'][md5($statement)]=$statement;
550 }
551 }
552 }
553 }
554 }
555
556 return $statements;
557 }
558
559 /**
560 * Converts a result row with field information into the SQL field definition string
561 *
562 * @param array MySQL result row.
563 * @return string Field definition
564 */
565 function assembleFieldDefinition($row) {
566 $field[] = $row['Type'];
567 if (!$row['Null']) { $field[] = 'NOT NULL'; }
568 if (!strstr($row['Type'],'blob') && !strstr($row['Type'],'text')) {
569 // Add a default value if the field is not auto-incremented (these fields never have a default definition).
570 if (!stristr($row['Extra'],'auto_increment')) {
571 $field[] = 'default '."'".(addslashes($row['Default']))."'";
572 }
573 }
574 if ($row['Extra']) { $field[] = $row['Extra']; }
575
576 return implode(' ',$field);
577 }
578
579 /**
580 * Returns an array where every entry is a single sql-statement. Input must be formatted like an ordinary MySQL-dump files
581 *
582 * @param string $sqlcode The sql-file content. Provided that 1) every query in the input is ended with ';' and that a line in the file contains only one query or a part of a query.
583 * @param boolean If set, non-sql (like comments and blank lines) are not included in the final product)
584 * @param string Regex to filter SQL lines to include.
585 * @return array Array of SQL statements.
586 */
587 function getStatementArray($sqlcode,$removeNonSQL=0,$query_regex='') {
588 $sqlcodeArr = explode(chr(10),$sqlcode);
589
590 // Based on the assumption that the sql-dump has
591 $statementArray = array();
592 $statementArrayPointer = 0;
593
594 foreach($sqlcodeArr as $line => $linecontent) {
595 $is_set = 0;
596 if(stristr($linecontent,'auto_increment')) {
597 $linecontent = eregi_replace(' default \'0\'','',$linecontent);
598 }
599
600 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
601 $statementArray[$statementArrayPointer].= $linecontent;
602 $is_set = 1;
603 }
604 if (substr(trim($linecontent),-1)==';') {
605 if (isset($statementArray[$statementArrayPointer])) {
606 if (!trim($statementArray[$statementArrayPointer]) || ($query_regex && !eregi($query_regex,trim($statementArray[$statementArrayPointer])))) {
607 unset($statementArray[$statementArrayPointer]);
608 }
609 }
610 $statementArrayPointer++;
611 } elseif ($is_set) {
612 $statementArray[$statementArrayPointer].=chr(10);
613 }
614 }
615 return $statementArray;
616 }
617
618 /**
619 * Returns tables to create and how many records in each
620 *
621 * @param array Array of SQL statements to analyse.
622 * @param boolean If set, will count number of INSERT INTO statements following that table definition
623 * @return array Array with table definitions in index 0 and count in index 1
624 */
625 function getCreateTables($statements, $insertCountFlag=0) {
626 $crTables = array();
627 foreach($statements as $line => $linecontent) {
628 if (eregi('^create[[:space:]]*table[[:space:]]*([[:alnum:]_]*)',substr($linecontent,0,100),$reg)) {
629 $table = trim($reg[1]);
630 if ($table) {
631 if (TYPO3_OS=='WIN') { $table=strtolower($table); } // table names are always lowercase on Windows!
632 $sqlLines = explode(chr(10), $linecontent);
633 foreach($sqlLines as $k=>$v) {
634 if(stristr($v,'auto_increment')) {
635 $sqlLines[$k] = eregi_replace(' default \'0\'','',$v);
636 }
637 }
638 $linecontent = implode(chr(10), $sqlLines);
639 $crTables[$table] = $linecontent;
640 }
641 } elseif ($insertCountFlag && eregi('^insert[[:space:]]*into[[:space:]]*([[:alnum:]_]*)',substr($linecontent,0,100),$reg)) {
642 $nTable = trim($reg[1]);
643 $insertCount[$nTable]++;
644 }
645 }
646 return array($crTables,$insertCount);
647 }
648
649 /**
650 * Extracts all insert statements from $statement array where content is inserted into $table
651 *
652 * @param array Array of SQL statements
653 * @param string Table name
654 * @return array Array of INSERT INTO statements where table match $table
655 */
656 function getTableInsertStatements($statements, $table) {
657 $outStatements=array();
658 foreach($statements as $line => $linecontent) {
659 if (eregi('^insert[[:space:]]*into[[:space:]]*([[:alnum:]_]*)',substr($linecontent,0,100),$reg)) {
660 $nTable = trim($reg[1]);
661 if ($nTable && !strcmp($table,$nTable)) {
662 $outStatements[]=$linecontent;
663 }
664 }
665 }
666 return $outStatements;
667 }
668
669 /**
670 * Performs the queries passed from the input array.
671 *
672 * @param array Array of SQL queries to execute.
673 * @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)
674 * @return void
675 */
676 function performUpdateQueries($arr,$keyArr) {
677 if (is_array($arr)) {
678 foreach($arr as $key => $string) {
679 if (isset($keyArr[$key]) && $keyArr[$key]) {
680 $GLOBALS['TYPO3_DB']->admin_query($string);
681 }
682 }
683 }
684 }
685
686 /**
687 * Returns list of tables in the database
688 *
689 * @return array List of tables.
690 * @see t3lib_db::admin_get_tables()
691 */
692 function getListOfTables() {
693 $whichTables = $GLOBALS['TYPO3_DB']->admin_get_tables(TYPO3_db);
694 return $whichTables;
695 }
696
697 /**
698 * Creates a table which checkboxes for updating database.
699 *
700 * @param array Array of statements (key / value pairs where key is used for the checkboxes)
701 * @param string Label for the table.
702 * @param boolean If set, then checkboxes are set by default.
703 * @param boolean If set, then icons are shown.
704 * @param array Array of "current values" for each key/value pair in $arr. Shown if given.
705 * @param boolean If set, will show the prefix "Current value" if $currentValue is given.
706 * @return string HTML table with checkboxes for update. Must be wrapped in a form.
707 */
708 function generateUpdateDatabaseForm_checkboxes($arr,$label,$checked=1,$iconDis=0,$currentValue=array(),$cVfullMsg=0) {
709 $out = array();
710 if (is_array($arr)) {
711 foreach($arr as $key => $string) {
712 $ico = '';
713 if ($iconDis) {
714 if (stristr($string,' user_')) {
715 $ico.= '<img src="'.$this->backPath.'t3lib/gfx/icon_warning.gif" width="18" height="16" align="top" alt="" /><strong>(USER) </strong>';
716 }
717 if (stristr($string,' app_')) {
718 $ico.= '<img src="'.$this->backPath.'t3lib/gfx/icon_warning.gif" width="18" height="16" align="top" alt="" /><strong>(APP) </strong>';
719 }
720 if (stristr($string,' ttx_') || stristr($string,' tx_')) {
721 $ico.= '<img src="'.$this->backPath.'t3lib/gfx/icon_warning.gif" width="18" height="16" align="top" alt="" /><strong>(EXT) </strong>';
722 }
723 }
724 $out[]='
725 <tr>
726 <td valign="top"><input type="checkbox" name="'.$this->dbUpdateCheckboxPrefix.'['.$key.']" value="1"'.($checked?' checked="checked"':'').' /></td>
727 <td nowrap="nowrap">'.nl2br($ico.htmlspecialchars($string)).'</td>
728 </tr>';
729 if (isset($currentValue[$key])) {
730 $out[]='
731 <tr>
732 <td valign="top"></td>
733 <td nowrap="nowrap" style="color : #666666;">'.nl2br((!$cVfullMsg?"Current value: ":"").'<em>'.$currentValue[$key].'</em>').'</td>
734 </tr>';
735 }
736 }
737
738 // Compile rows:
739 $content = '
740 <!-- Update database fields / tables -->
741 <h3>'.$label.'</h3>
742 <table border="0" cellpadding="2" cellspacing="2" class="update-db-fields">'.implode('',$out).'
743 </table>';
744 }
745
746 return $content;
747 }
748 }
749
750 if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['t3lib/class.t3lib_install.php']) {
751 include_once($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['t3lib/class.t3lib_install.php']);
752 }
753 ?>