See changelog (2004-08-21). Localization features, Tab menus, few bugs fixed.
[Packages/TYPO3.CMS.git] / t3lib / class.t3lib_install.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 1999-2004 Kasper Skaarhoj (kasper@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 <kasper@typo3.com>
33 */
34 /**
35 * [CLASS/FUNCTION INDEX of SCRIPT]
36 *
37 *
38 *
39 * 80: class t3lib_install
40 *
41 * SECTION: Writing to localconf.php
42 * 118: function setValueInLocalconfFile(&$line_array, $variable, $value)
43 * 168: function writeToLocalconf_control($inlines='')
44 * 220: function checkForBadString($string)
45 * 233: function slashValueForSingleDashes($value)
46 *
47 * SECTION: SQL
48 * 258: function getFieldDefinitions_sqlContent($sqlContent)
49 * 296: function getFieldDefinitions_database()
50 * 340: function getDatabaseExtra($FDsrc, $FDcomp, $onlyTableList='')
51 * 385: function getUpdateSuggestions($diffArr,$keyList='extra,diff')
52 * 481: function assembleFieldDefinition($row)
53 * 510: function getStatementArray($sqlcode,$removeNonSQL=0,$query_regex='')
54 * 550: function getCreateTables($statements, $insertCountFlag=0)
55 * 574: function getTableInsertStatements($statements, $table)
56 * 594: function performUpdateQueries($arr,$keyArr)
57 * 610: function getListOfTables()
58 * 626: function generateUpdateDatabaseForm_checkboxes($arr,$label,$checked=1,$iconDis=0,$currentValue=array(),$cVfullMsg=0)
59 *
60 * TOTAL FUNCTIONS: 15
61 * (This index is automatically created/updated by the extension "extdeveval")
62 *
63 */
64
65
66
67
68
69
70
71
72 require_once(PATH_t3lib.'class.t3lib_sqlparser.php');
73
74 /**
75 * Class to setup values in localconf.php and verify the TYPO3 DB tables/fields
76 *
77 * @author Kasper Skaarhoj <kasper@typo3.com>
78 * @package TYPO3
79 * @subpackage t3lib
80 */
81 class t3lib_install {
82
83 // External, Static
84 var $updateIdentity = ''; // Set to string which identifies the script using this class.
85 var $deletedPrefixKey = 'zzz_deleted_'; // Prefix used for tables/fields when deleted/renamed.
86 var $dbUpdateCheckboxPrefix = 'TYPO3_INSTALL[database_update]'; // Prefix for checkbox fields when updating database.
87 var $mysqlVersion = '3.22'; // 3.22 or 3.23. If set to 3.23, then the expected format of the incoming sql-dump files are changed. 'DEFAULT' and 'NOT NULL' are reversed in order and 'DEFAULT' is lowercase. Also auto_incremented fields are without default definitions.
88 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.
89 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
90 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.
91 var $backPath = '../'; // Backpath (used for icons etc.)
92
93 var $multiplySize = 1; // Multiplier of SQL field size (for char, varchar and text fields)
94
95 // Internal, dynamic:
96 var $setLocalconf = 0; // Used to indicate that a value is change in the line-array of localconf and that it should be written.
97 var $messages = array(); // Used to set (error)messages from the executing functions like mail-sending, writing Localconf and such
98 var $touchedLine = 0; // updated with line in localconf.php file that was changed.
99
100
101
102
103
104 /**
105 * Constructor
106 */
107 function t3lib_install() {
108 if ($GLOBALS['TYPO3_CONF_VARS']['SYS']['multiplyDBfieldSize']>= 1 && $GLOBALS['TYPO3_CONF_VARS']['SYS']['multiplyDBfieldSize']<=5) {
109 $this->multiplySize = (double)$GLOBALS['TYPO3_CONF_VARS']['SYS']['multiplyDBfieldSize'];
110 }
111 }
112
113
114
115 /**************************************
116 *
117 * Writing to localconf.php
118 *
119 **************************************/
120
121 /**
122 * This functions takes an array with lines from localconf.php, finds a variable and inserts the new value.
123 *
124 * @param array $line_array the localconf.php file exploded into an array by linebreaks. (see writeToLocalconf_control())
125 * @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.
126 * @param string $value Is the value to be insert for the variable
127 * @return void
128 * @see writeToLocalconf_control()
129 */
130 function setValueInLocalconfFile(&$line_array, $variable, $value) {
131 if (!$this->checkForBadString($value)) return 0;
132
133 // Initialize:
134 $found = 0;
135 $this->touchedLine = '';
136 $commentKey = '## ';
137 $inArray = in_array($commentKey.$this->localconf_editPointToken,$line_array);
138 $tokenSet = ($this->localconf_editPointToken && !$inArray); // Flag is set if the token should be set but is not yet...
139 $stopAtToken = ($this->localconf_editPointToken && $inArray);
140 $comment = ' Modified or inserted by '.$this->updateIdentity.'.';
141
142 // Search for variable name:
143 if (!$this->localconf_addLinesOnly && !$tokenSet) {
144 $line_array = array_reverse($line_array);
145 foreach($line_array as $k => $v) {
146 $v2 = trim($v);
147 if ($stopAtToken && !strcmp($v2,$commentKey.$this->localconf_editPointToken)) break; // If stopAtToken and token found, break out of the loop..
148 if (!strcmp(substr($v2,0,strlen($variable.' ')),$variable.' ')) {
149 $mainparts = explode($variable,$v,2);
150 if (count($mainparts)==2) { // should ALWAYS be....
151 $subparts = explode('//',$mainparts[1],2);
152 $line_array[$k] = $mainparts[0].$variable." = '".$this->slashValueForSingleDashes($value)."'; ".('//'.$comment.str_replace($comment,'',$subparts[1]));
153 $this->touchedLine = count($line_array)-$k-1;
154 $found = 1;
155 break;
156 }
157 }
158 }
159 $line_array = array_reverse($line_array);
160 }
161 if (!$found) {
162 if ($tokenSet) {
163 $line_array[] = $commentKey.$this->localconf_editPointToken;
164 $line_array[] = '';
165 }
166 $line_array[] = $variable." = '".$this->slashValueForSingleDashes($value)."'; // ".$comment;
167 $this->touchedLine = -1;
168 }
169 $this->messages[] = $variable." = '".htmlspecialchars($value)."'";
170 $this->setLocalconf = 1;
171 }
172
173 /**
174 * Writes or returns lines from localconf.php
175 *
176 * @param array Array of lines to write back to localconf.php. Possibly
177 * @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)
178 * @see setValueInLocalconfFile()
179 */
180 function writeToLocalconf_control($inlines='') {
181 $writeToLocalconf_dat['file'] = PATH_typo3conf.'localconf.php';
182
183 // Checking write state of localconf.php:
184 if (!$this->allowUpdateLocalConf) {
185 die("->allowUpdateLocalConf flag in the install object is not set and therefore 'localconf.php' cannot be altered.");
186 }
187 if (!@is_writeable($writeToLocalconf_dat['file'])) {
188 die($writeToLocalconf_dat['file'].' is not writeable!');
189 }
190
191 // Splitting localconf.php file into lines:
192 $lines = explode(chr(10),trim(t3lib_div::getUrl($writeToLocalconf_dat['file'])));
193 $writeToLocalconf_dat['endLine'] = array_pop($lines); // Getting "? >" ending.
194
195 // Checking if "updated" line was set by this tool - if so remove old line.
196 $updatedLine = array_pop($lines);
197 $writeToLocalconf_dat['updatedText'] = '// Updated by '.$this->updateIdentity.' ';
198 if (!strstr($updatedLine, $writeToLocalconf_dat['updatedText'])) {
199 array_push($lines,$updatedLine);
200 }
201
202 if (is_array($inlines)) { // Setting a line and write:
203 // Setting configuration
204 $updatedLine = $writeToLocalconf_dat['updatedText'].date('d-m-Y H:i:s');
205 array_push($inlines,$updatedLine);
206 array_push($inlines,$writeToLocalconf_dat['endLine']);
207
208 if ($this->setLocalconf) {
209 t3lib_div::writeFile($writeToLocalconf_dat['file'],implode(chr(10),$inlines));
210
211 if (strcmp(t3lib_div::getUrl($writeToLocalconf_dat['file']), implode(chr(10),$inlines))) {
212 die('typo3temp/localconf.php was NOT updated properly (written content didn\'t match file content) - maybe write access problem?');
213 }
214
215 $this->messages[]= 'Configuration written to typo3conf/localconf.php';
216 return 'continue';
217 } else {
218 return 'nochange';
219 }
220 } else { // Return lines found in localconf.php
221 return $lines;
222 }
223 }
224
225 /**
226 * Checking for linebreaks in the string
227 *
228 * @param string String to test
229 * @return boolean Returns TRUE if string is OK
230 * @see setValueInLocalconfFile()
231 */
232 function checkForBadString($string) {
233 if (ereg('['.chr(10).chr(13).']',$string)){
234 return FALSE;
235 } else return TRUE;
236 }
237
238 /**
239 * Replaces ' with \' and \ with \\
240 *
241 * @param string Input value
242 * @return string Output value
243 * @see setValueInLocalconfFile()
244 */
245 function slashValueForSingleDashes($value) {
246 return str_replace("'","\'",str_replace('\\','\\\\',$value));
247 }
248
249
250
251
252
253
254
255
256
257
258 /*************************************
259 *
260 * SQL
261 *
262 *************************************/
263
264 /**
265 * Reads the field definitions for the input sql-file string
266 *
267 * @param string $sqlContent: Should be a string read from an sql-file made with 'mysqldump [database_name] -d'
268 * @return array Array with information about table.
269 */
270 function getFieldDefinitions_sqlContent($sqlContent) {
271 $lines = t3lib_div::trimExplode(chr(10), $sqlContent,1);
272 $isTable = '';
273
274 foreach($lines as $value) {
275 if ($value[0]!='#') {
276 if (!$isTable) {
277 $parts = explode(' ',$value);
278 if ($parts[0]=='CREATE' && $parts[1]=='TABLE') {
279 $isTable = $parts[2];
280 if (TYPO3_OS=='WIN') { // tablenames are always lowercase on windows!
281 $isTable = strtolower($isTable);
282 }
283 }
284 } else {
285 if (substr($value,0,1)==')' && substr($value,-1)==';') {
286 $isTable = '';
287 } else {
288 $lineV = ereg_replace(',$','',$value);
289 $parts = explode(' ',$lineV,2);
290 if ($parts[0]!='PRIMARY' && $parts[0]!='KEY' && $parts[0]!='UNIQUE') {
291 $total[$isTable]['fields'][$parts[0]] = $parts[1];
292 } else {
293 $newParts = explode(' ',$parts[1],2);
294 $total[$isTable]['keys'][($parts[0]=='PRIMARY'?$parts[0]:$newParts[0])] = $lineV;
295 }
296 }
297 }
298 }
299 }
300
301 $this->getFieldDefinitions_sqlContent_parseTypes($total);
302 return $total;
303 }
304
305 /**
306 * Multiplies varchars/tinytext fields in size according to $this->multiplySize
307 * 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.
308 *
309 * @param array Total array (from getFieldDefinitions_sqlContent())
310 * @return void
311 * @see getFieldDefinitions_sqlContent()
312 * @access private
313 */
314 function getFieldDefinitions_sqlContent_parseTypes(&$total) {
315
316 $mSize = (double)$this->multiplySize;
317 if ($mSize > 1) {
318
319 // Init SQL parser:
320 $sqlParser = t3lib_div::makeInstance('t3lib_sqlparser');
321 foreach($total as $table => $cfg) {
322 foreach($cfg['fields'] as $fN => $fType) {
323 $orig_fType = $fType;
324 $fInfo = $sqlParser->parseFieldDef($fType);
325
326 switch($fInfo['fieldType']) {
327 case 'char':
328 case 'varchar':
329 $newSize = round($fInfo['value']*$mSize);
330
331 if ($newSize <= 255) {
332 $fInfo['value'] = $newSize;
333 } else {
334 $fInfo = array(
335 'fieldType' => 'text',
336 'featureIndex' => array(
337 'NOTNULL' => array(
338 'keyword' => 'NOT NULL'
339 )
340 )
341 );
342 }
343 break;
344 case 'tinytext':
345 $fInfo['fieldType'] = 'text';
346 break;
347 }
348
349 $total[$table]['fields'][$fN] = $sqlParser->compileFieldCfg($fInfo);
350 if ($sqlParser->parse_error) die($sqlParser->parse_error);
351 }
352 }
353 }
354 }
355
356 /**
357 * Reads the field definitions for the current database
358 *
359 * @return array Array with information about table.
360 */
361 function getFieldDefinitions_database() {
362 $total = array();
363 $GLOBALS['TYPO3_DB']->sql_select_db(TYPO3_db);
364 echo $GLOBALS['TYPO3_DB']->sql_error();
365
366 $tables = $GLOBALS['TYPO3_DB']->admin_get_tables(TYPO3_db);
367 foreach($tables as $tableName) {
368
369 // Fields:
370 $fieldInformation = $GLOBALS['TYPO3_DB']->admin_get_fields($tableName);
371 foreach($fieldInformation as $fN => $fieldRow) {
372 $total[$tableName]['fields'][$fN] = $this->assembleFieldDefinition($fieldRow);
373 }
374
375 // Keys:
376 $keyInformation = $GLOBALS['TYPO3_DB']->admin_get_keys($tableName);
377 foreach($keyInformation as $kN => $keyRow) {
378 $tempKeys[$tableName][$keyRow['Key_name']][$keyRow['Seq_in_index']] = $keyRow['Column_name'];
379 $tempKeysPrefix[$tableName][$keyRow['Key_name']]= ($keyRow['Key_name']=='PRIMARY'?'PRIMARY KEY':($keyRow['Non_unique']?'KEY':'UNIQUE').' '.$keyRow['Key_name']);
380 }
381 }
382
383 // Compile information:
384 if (is_array($tempKeys)) {
385 foreach($tempKeys as $table => $keyInf) {
386 foreach($keyInf as $kName => $index) {
387 ksort($index);
388 $total[$table]['keys'][$kName] = $tempKeysPrefix[$table][$kName].' ('.implode(',',$index).')';
389 }
390 }
391 }
392
393 return $total;
394 }
395
396 /**
397 * Compares two arrays with field information and returns information about fields that are MISSING and fields that have CHANGED.
398 * FDsrc and FDcomp can be switched if you want the list of stuff to remove rather than update.
399 *
400 * @param array Field definitions, source (from getFieldDefinitions_sqlContent())
401 * @param array Field definitions, comparison. (from getFieldDefinitions_database())
402 * @param string Table names (in list) which is the ONLY one observed.
403 * @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
404 */
405 function getDatabaseExtra($FDsrc, $FDcomp, $onlyTableList='') {
406 $extraArr = array();
407 $diffArr = array();
408
409 if (is_array($FDsrc)) {
410 foreach($FDsrc as $table => $info) {
411 if (!strlen($onlyTableList) || t3lib_div::inList($onlyTableList, $table)) {
412 if (!isset($FDcomp[$table])) {
413 $extraArr[$table] = $info; // If the table was not in the FDcomp-array, the result array is loaded with that table.
414 $extraArr[$table]['whole_table']=1;
415 } else {
416 $keyTypes = explode(',','fields,keys');
417 foreach($keyTypes as $theKey) {
418 if (is_array($info[$theKey])) {
419 foreach($info[$theKey] as $fieldN => $fieldC) {
420 if (!isset($FDcomp[$table][$theKey][$fieldN])) {
421 $extraArr[$table][$theKey][$fieldN] = $fieldC;
422 } elseif (strcmp($FDcomp[$table][$theKey][$fieldN], $fieldC)){
423 $diffArr[$table][$theKey][$fieldN] = $fieldC;
424 $diffArr_cur[$table][$theKey][$fieldN] = $FDcomp[$table][$theKey][$fieldN];
425 }
426 }
427 }
428 }
429 }
430 }
431 }
432 }
433
434 $output = array(
435 'extra' => $extraArr,
436 'diff' => $diffArr,
437 'diff_currentValues' => $diffArr_cur
438 );
439
440 return $output;
441 }
442
443 /**
444 * Returns an array with SQL-statements that is needed to update according to the diff-array
445 *
446 * @param array Array with differences of current and needed DB settings. (from getDatabaseExtra())
447 * @param string List of fields in diff array to take notice of.
448 * @return array Array of SQL statements (organized in keys depending on type)
449 */
450 function getUpdateSuggestions($diffArr,$keyList='extra,diff') {
451 $statements = array();
452 $deletedPrefixKey = $this->deletedPrefixKey;
453 $remove = 0;
454 if ($keyList == 'remove') {
455 $remove = 1;
456 $keyList = 'extra';
457 }
458 $keyList = explode(',',$keyList);
459 foreach($keyList as $theKey) {
460 if (is_array($diffArr[$theKey])) {
461 foreach($diffArr[$theKey] as $table => $info) {
462 $whole_table = array();
463 if (is_array($info['fields'])) {
464 foreach($info['fields'] as $fN => $fV) {
465 if ($info['whole_table']) {
466 $whole_table[]=$fN.' '.$fV;
467 } else {
468 if ($theKey=='extra') {
469 if ($remove) {
470 if (substr($fN,0,strlen($deletedPrefixKey))!=$deletedPrefixKey) {
471 $statement = 'ALTER TABLE '.$table.' CHANGE '.$fN.' '.$deletedPrefixKey.$fN.' '.$fV.';';
472 $statements['change'][md5($statement)] = $statement;
473 } else {
474 $statement = 'ALTER TABLE '.$table.' DROP '.$fN.';';
475 $statements['drop'][md5($statement)] = $statement;
476 }
477 } else {
478 $statement = 'ALTER TABLE '.$table.' ADD '.$fN.' '.$fV.';';
479 $statements['add'][md5($statement)] = $statement;
480 }
481 } elseif ($theKey=='diff') {
482 $statement = 'ALTER TABLE '.$table.' CHANGE '.$fN.' '.$fN.' '.$fV.';';
483 $statements['change'][md5($statement)] = $statement;
484 $statements['change_currentValue'][md5($statement)] = $diffArr['diff_currentValues'][$table]['fields'][$fN];
485 }
486 }
487 }
488 }
489 if (is_array($info['keys'])) {
490 foreach($info['keys'] as $fN => $fV) {
491 if ($info['whole_table']) {
492 if ($fN=='PRIMARY') {
493 $whole_table[] = $fV;
494 } else {
495 $whole_table[] = $fV;
496 }
497 } else {
498 if ($theKey=='extra') {
499 if ($remove) {
500 $statement = 'ALTER TABLE '.$table.($fN=='PRIMARY' ? ' DROP PRIMARY KEY' : ' DROP KEY '.$fN).';';
501 $statements['drop'][md5($statement)] = $statement;
502 } else {
503 $statement = 'ALTER TABLE '.$table.' ADD '.$fV.';';
504 $statements['add'][md5($statement)] = $statement;
505 }
506 } elseif ($theKey=='diff') {
507 $statement = 'ALTER TABLE '.$table.($fN=='PRIMARY' ? ' DROP PRIMARY KEY' : ' DROP KEY '.$fN).';';
508 $statements['change'][md5($statement)] = $statement;
509 $statement = 'ALTER TABLE '.$table.' ADD '.$fV.';';
510 $statements['change'][md5($statement)] = $statement;
511 }
512 }
513 }
514 }
515 if ($info['whole_table']) {
516 if ($remove) {
517 if (substr($table,0,strlen($deletedPrefixKey))!=$deletedPrefixKey) {
518 $statement = 'ALTER TABLE '.$table.' RENAME '.$deletedPrefixKey.$table.';';
519 $statements['change_table'][md5($statement)]=$statement;
520 } else {
521 $statement = 'DROP TABLE '.$table.';';
522 $statements['drop_table'][md5($statement)]=$statement;
523 }
524 // count:
525 $res = $GLOBALS['TYPO3_DB']->exec_SELECTquery('count(*)', $table, '');
526 list($count) = $GLOBALS['TYPO3_DB']->sql_fetch_row($res);
527 $statements['tables_count'][md5($statement)] = $count?'Records in table: '.$count:'';
528 } else {
529 $statement = 'CREATE TABLE '.$table." (\n".implode(",\n",$whole_table)."\n) TYPE=MyISAM;";
530 $statements['create_table'][md5($statement)]=$statement;
531 }
532 }
533 }
534 }
535 }
536
537 return $statements;
538 }
539
540 /**
541 * Converts a result row with field information into the SQL field definition string
542 *
543 * @param array MySQL result row.
544 * @return string Field definition
545 */
546 function assembleFieldDefinition($row) {
547 $field[] = $row['Type'];
548 if ($this->mysqlVersion=='3.23' && !$row['Null']) {
549 $field[] = 'NOT NULL';
550 }
551 if (!strstr($row['Type'],'blob') && !strstr($row['Type'],'text')) {
552 // This code will return a default value if the sql-file version is not 3.23 and the field is not auto-incremented. In 3.23 files, the auto-incremented fields do not have a default definition.
553 // Furthermore if the file is 3.22 the default value of auto-incremented fields are expected to always be zero which is why the default value is passed through intval(). Thus it should work on 3.23 MySQL servers.
554 if ($this->mysqlVersion!='3.23' || !stristr($row['Extra'],'auto_increment')) {
555 $field[] = ($this->mysqlVersion=='3.23'?'default':'DEFAULT')." '".(stristr($row['Extra'],'auto_increment')?intval($row['Default']):addslashes($row['Default']))."'";
556 }
557 }
558 if ($this->mysqlVersion!='3.23' && !$row['Null']) {
559 $field[] = 'NOT NULL';
560 }
561 if ($row['Extra']) {
562 $field[] = $row['Extra'];
563 }
564 return implode(' ',$field);
565 }
566
567 /**
568 * Returns an array where every entry is a single sql-statement. Input must be formatted like an ordinary MySQL-dump files
569 *
570 * @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.
571 * @param boolean If set, non-sql (like comments and blank lines) are not included in the final product)
572 * @param string Regex to filter SQL lines to include.
573 * @return array Array of SQL statements.
574 */
575 function getStatementArray($sqlcode,$removeNonSQL=0,$query_regex='') {
576 $sqlcodeArr = explode(chr(10),$sqlcode);
577
578 // Based on the assumption that the sql-dump has
579 $statementArray = array();
580 $statementArrayPointer = 0;
581
582 foreach($sqlcodeArr as $line => $linecontent) {
583
584 // Setting MySQL Version if necessary.
585 if (substr(trim($linecontent),0,16)=='# Server version') {
586 $this->mysqlVersion = doubleval(substr(trim($linecontent),16));
587 }
588 $is_set = 0;
589
590 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
591 $statementArray[$statementArrayPointer].= $linecontent;
592 $is_set = 1;
593 }
594 if (substr(trim($linecontent),-1)==';') {
595 if (isset($statementArray[$statementArrayPointer])) {
596 if (!trim($statementArray[$statementArrayPointer]) || ($query_regex && !eregi($query_regex,trim($statementArray[$statementArrayPointer])))) {
597 unset($statementArray[$statementArrayPointer]);
598 }
599 }
600 $statementArrayPointer++;
601 } elseif ($is_set) {
602 $statementArray[$statementArrayPointer].=chr(10);
603 }
604 }
605 return $statementArray;
606 }
607
608 /**
609 * Returns tables to create and how many records in each
610 *
611 * @param array Array of SQL statements to analyse.
612 * @param boolean If set, will count number of INSERT INTO statements following that table definition
613 * @return array Array with table definitions in index 0 and count in index 1
614 */
615 function getCreateTables($statements, $insertCountFlag=0) {
616 $crTables = array();
617 foreach($statements as $line => $linecontent) {
618 if (eregi('^create[[:space:]]*table[[:space:]]*([[:alnum:]_]*)',substr($linecontent,0,100),$reg)) {
619 $table = trim($reg[1]);
620 if ($table) {
621 if (TYPO3_OS=='WIN') {$table=strtolower($table);} // tablenames are always lowercase on windows!
622 $crTables[$table] = $linecontent;
623 }
624 } elseif ($insertCountFlag && eregi('^insert[[:space:]]*into[[:space:]]*([[:alnum:]_]*)',substr($linecontent,0,100),$reg)) {
625 $nTable = trim($reg[1]);
626 $insertCount[$nTable]++;
627 }
628 }
629 return array($crTables,$insertCount);
630 }
631
632 /**
633 * Extracts all insert statements from $statement array where content is inserted into $table
634 *
635 * @param array Array of SQL statements
636 * @param string Table name
637 * @return array Array of INSERT INTO statements where table match $table
638 */
639 function getTableInsertStatements($statements, $table) {
640 $outStatements=array();
641 foreach($statements as $line => $linecontent) {
642 if (eregi('^insert[[:space:]]*into[[:space:]]*([[:alnum:]_]*)',substr($linecontent,0,100),$reg)) {
643 $nTable = trim($reg[1]);
644 if ($nTable && !strcmp($table,$nTable)) {
645 $outStatements[]=$linecontent;
646 }
647 }
648 }
649 return $outStatements;
650 }
651
652 /**
653 * Performs the queries passed from the input array.
654 *
655 * @param array Array of SQL queries to execute.
656 * @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)
657 * @return void
658 */
659 function performUpdateQueries($arr,$keyArr) {
660 if (is_array($arr)) {
661 foreach($arr as $key => $string) {
662 if (isset($keyArr[$key]) && $keyArr[$key]) {
663 $GLOBALS['TYPO3_DB']->admin_query($string);
664 }
665 }
666 }
667 }
668
669 /**
670 * Returns list of tables in the database
671 *
672 * @return array List of tables.
673 * @see t3lib_db::admin_get_tables()
674 */
675 function getListOfTables() {
676 $whichTables = $GLOBALS['TYPO3_DB']->admin_get_tables(TYPO3_db);
677 return $whichTables;
678 }
679
680 /**
681 * Creates a table which checkboxes for updating database.
682 *
683 * @param array Array of statements (key / value pairs where key is used for the checkboxes)
684 * @param string Label for the table.
685 * @param boolean If set, then checkboxes are set by default.
686 * @param boolean If set, then icons are shown.
687 * @param array Array of "current values" for each key/value pair in $arr. Shown if given.
688 * @param boolean If set, will show the prefix "Current value" if $currentValue is given.
689 * @return string HTML table with checkboxes for update. Must be wrapped in a form.
690 */
691 function generateUpdateDatabaseForm_checkboxes($arr,$label,$checked=1,$iconDis=0,$currentValue=array(),$cVfullMsg=0) {
692 $out = array();
693 if (is_array($arr)) {
694 foreach($arr as $key => $string) {
695 $ico = '';
696 if ($iconDis) {
697 if (stristr($string,' user_')) {
698 $ico.= '<img src="'.$this->backPath.'t3lib/gfx/icon_warning.gif" width="18" height="16" align="top" alt="" /><strong>(USER) </strong>';
699 }
700 if (stristr($string,' app_')) {
701 $ico.= '<img src="'.$this->backPath.'t3lib/gfx/icon_warning.gif" width="18" height="16" align="top" alt="" /><strong>(APP) </strong>';
702 }
703 if (stristr($string,' ttx_') || stristr($string,' tx_')) {
704 $ico.= '<img src="'.$this->backPath.'t3lib/gfx/icon_warning.gif" width="18" height="16" align="top" alt="" /><strong>(EXT) </strong>';
705 }
706 }
707 $out[]='
708 <tr>
709 <td valign="top"><input type="checkbox" name="'.$this->dbUpdateCheckboxPrefix.'['.$key.']" value="1"'.($checked?' checked="checked"':'').' /></td>
710 <td nowrap="nowrap">'.nl2br($ico.htmlspecialchars($string)).'</td>
711 </tr>';
712 if (isset($currentValue[$key])) {
713 $out[]='
714 <tr>
715 <td valign="top"></td>
716 <td nowrap="nowrap" style="color : #666666;">'.nl2br((!$cVfullMsg?"Current value: ":"").'<em>'.$currentValue[$key].'</em>').'</td>
717 </tr>';
718 }
719 }
720
721 // Compile rows:
722 $content = '
723 <!-- Update database fields / tables -->
724 <h3>'.$label.'</h3>
725 <table border="0" cellpadding="2" cellspacing="2" class="update-db-fields">'.implode('',$out).'
726 </table>';
727 }
728
729 return $content;
730 }
731 }
732
733 if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['t3lib/class.t3lib_install.php']) {
734 include_once($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['t3lib/class.t3lib_install.php']);
735 }
736 ?>