Removed unsigned attributes from .sql files in the core, plus added features to the...
[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 $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.
91 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.
92 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
93 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.
94 var $backPath = '../'; // Backpath (used for icons etc.)
95
96 var $multiplySize = 1; // Multiplier of SQL field size (for char, varchar and text fields)
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
106 var $changedUnsignedFields = FALSE; // Switch this to TRUE if you wish the install tool to convert all unsigned fields (for 3.8.0 core) to signed integers (unsigned is depricated due to DBAL but doesn't hurt - usually...). It might be a good idea to flush cache-tables first including the tables from indexed search (which has to be re-build thereafter)
107 var $coreFieldsWithUnsignedKeywordRemovedIn380 = array(
108 'be_groups' => 'uid,pid,tstamp,crdate,cruser_id,hidden,inc_access_lists,deleted',
109 'be_sessions' => 'ses_userid,ses_tstamp',
110 'be_users' => 'uid,pid,tstamp,admin,disable,starttime,endtime,options,crdate,cruser_id,disableIPlock,deleted,lastlogin',
111 'cache_hash' => 'tstamp',
112 'cache_imagesizes' => 'imagewidth,imageheight',
113 'pages' => 't3ver_oid,t3ver_id,tstamp,sorting,deleted,perms_userid,perms_groupid,perms_user,perms_group,perms_everybody,editlock,crdate,cruser_id,doktype,hidden,starttime,endtime,urltype,shortcut,shortcut_mode,no_cache,layout,lastUpdated,cache_timeout,newUntil,no_search,SYS_LASTCHANGED,extendToSubpages,content_from_pid,mount_pid',
114 'sys_be_shortcuts' => 'uid,userid',
115 'sys_filemounts' => 'uid,pid,tstamp,base,hidden,deleted',
116 'sys_history' => 'uid',
117 'sys_lockedrecords' => 'uid,userid,tstamp',
118 'sys_log' => 'uid,userid,action,recuid,error,tstamp,type,details_nr',
119 'sys_language' => 'uid,pid,tstamp,hidden,static_lang_isocode',
120 'cache_pages' => 'id,page_id,reg1,tstamp,expires',
121 'cache_pagesection' => 'page_id,mpvar_hash,tstamp',
122 'cache_imagesizes' => 'imagewidth,imageheight',
123 'fe_groups' => 'uid,pid,tstamp,hidden,deleted',
124 'fe_session_data' => 'tstamp',
125 'fe_sessions' => 'ses_userid,ses_tstamp',
126 'fe_users' => 'uid,pid,tstamp,disable,starttime,endtime,crdate,cruser_id,deleted,module_sys_dmail_category,module_sys_dmail_html,fe_cruser_id,lastlogin,is_online',
127 'pages_language_overlay' => 't3ver_oid,t3ver_id,tstamp,crdate,cruser_id,sys_language_uid,hidden,starttime,endtime',
128 'static_template' => 'uid,pid,tstamp,crdate',
129 'sys_domain' => 'uid,pid,tstamp,hidden,sorting',
130 'sys_template' => 't3ver_oid,t3ver_id,tstamp,sorting,crdate,cruser_id,hidden,starttime,endtime,root,clear,deleted,includeStaticAfterBasedOn,static_file_mode',
131 'tt_content' => 't3ver_oid,t3ver_id,tstamp,hidden,sorting,imagewidth,imageorient,imagecols,imageborder,layout,deleted,cols,starttime,endtime,colPos,spaceBefore,spaceAfter,image_zoom,image_noRows,image_effects,image_compression,text_face,text_size,text_color,text_properties,table_border,table_cellspacing,table_cellpadding,table_bgColor,select_key,sectionIndex,linkToTop,filelink_size,section_frame,date,image_frames,recursive,imageheight,module_sys_dmail_category',
132 'sys_note' => 'uid,pid,deleted,tstamp,crdate,cruser,personal,category',
133 'sys_action' => 'uid,pid,tstamp,crdate,cruser_id,type',
134 'sys_action_asgr_mm' => 'uid_local,uid_foreign,sorting',
135 'index_phash' => 'data_page_id,data_page_reg1,data_page_type,tstamp',
136 'index_rel' => 'count,first,freq,flags',
137 'index_section' => 'rl0,rl1,rl2',
138 'index_stat_search' => 'feuser_id',
139 'index_config' => 'tstamp,crdate,cruser_id,hidden,starttime,type,depth,chashcalc',
140 );
141
142
143 /**
144 * Constructor function
145 *
146 * @return void
147 */
148 function t3lib_install() {
149 if ($GLOBALS['TYPO3_CONF_VARS']['SYS']['multiplyDBfieldSize']>= 1 && $GLOBALS['TYPO3_CONF_VARS']['SYS']['multiplyDBfieldSize']<=5) {
150 $this->multiplySize = (double)$GLOBALS['TYPO3_CONF_VARS']['SYS']['multiplyDBfieldSize'];
151 }
152
153
154 // Init this array:
155 foreach($this->coreFieldsWithUnsignedKeywordRemovedIn380 as $table => $fieldNameList) {
156 $this->coreFieldsWithUnsignedKeywordRemovedIn380[$table] = array_flip(t3lib_div::trimExplode(',',$fieldNameList,1));
157 }
158 }
159
160
161
162 /**************************************
163 *
164 * Writing to localconf.php
165 *
166
167 **************************************/
168
169 /**
170 * This functions takes an array with lines from localconf.php, finds a variable and inserts the new value.
171 *
172 * @param array $line_array the localconf.php file exploded into an array by linebreaks. (see writeToLocalconf_control())
173 * @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.
174 * @param string $value Is the value to be insert for the variable
175 * @return void
176 * @see writeToLocalconf_control()
177 */
178 function setValueInLocalconfFile(&$line_array, $variable, $value) {
179 if (!$this->checkForBadString($value)) return 0;
180
181 // Initialize:
182 $found = 0;
183 $this->touchedLine = '';
184 $commentKey = '## ';
185 $inArray = in_array($commentKey.$this->localconf_editPointToken,$line_array);
186 $tokenSet = ($this->localconf_editPointToken && !$inArray); // Flag is set if the token should be set but is not yet...
187 $stopAtToken = ($this->localconf_editPointToken && $inArray);
188 $comment = ' Modified or inserted by '.$this->updateIdentity.'.';
189
190 // Search for variable name:
191 if (!$this->localconf_addLinesOnly && !$tokenSet) {
192 $line_array = array_reverse($line_array);
193 foreach($line_array as $k => $v) {
194 $v2 = trim($v);
195 if ($stopAtToken && !strcmp($v2,$commentKey.$this->localconf_editPointToken)) break; // If stopAtToken and token found, break out of the loop..
196 if (!strcmp(substr($v2,0,strlen($variable.' ')),$variable.' ')) {
197 $mainparts = explode($variable,$v,2);
198 if (count($mainparts)==2) { // should ALWAYS be....
199 $subparts = explode('//',$mainparts[1],2);
200 $line_array[$k] = $mainparts[0].$variable." = '".$this->slashValueForSingleDashes($value)."'; ".('//'.$comment.str_replace($comment,'',$subparts[1]));
201 $this->touchedLine = count($line_array)-$k-1;
202 $found = 1;
203 break;
204 }
205 }
206 }
207 $line_array = array_reverse($line_array);
208 }
209 if (!$found) {
210 if ($tokenSet) {
211 $line_array[] = $commentKey.$this->localconf_editPointToken;
212 $line_array[] = '';
213 }
214 $line_array[] = $variable." = '".$this->slashValueForSingleDashes($value)."'; // ".$comment;
215 $this->touchedLine = -1;
216 }
217 $this->messages[] = $variable." = '".htmlspecialchars($value)."'";
218 $this->setLocalconf = 1;
219 }
220
221 /**
222 * Writes or returns lines from localconf.php
223 *
224 * @param array Array of lines to write back to localconf.php. Possibly
225 * @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)
226 * @see setValueInLocalconfFile()
227 */
228 function writeToLocalconf_control($inlines='') {
229 $writeToLocalconf_dat['file'] = PATH_typo3conf.'localconf.php';
230
231 // Checking write state of localconf.php:
232 if (!$this->allowUpdateLocalConf) {
233 die("->allowUpdateLocalConf flag in the install object is not set and therefore 'localconf.php' cannot be altered.");
234 }
235 if (!@is_writable($writeToLocalconf_dat['file'])) {
236 die($writeToLocalconf_dat['file'].' is not writable!');
237 }
238
239 // Splitting localconf.php file into lines:
240 $lines = explode(chr(10),trim(t3lib_div::getUrl($writeToLocalconf_dat['file'])));
241 $writeToLocalconf_dat['endLine'] = array_pop($lines); // Getting "? >" ending.
242
243 // Checking if "updated" line was set by this tool - if so remove old line.
244 $updatedLine = array_pop($lines);
245 $writeToLocalconf_dat['updatedText'] = '// Updated by '.$this->updateIdentity.' ';
246 if (!strstr($updatedLine, $writeToLocalconf_dat['updatedText'])) {
247 array_push($lines,$updatedLine);
248 }
249
250 if (is_array($inlines)) { // Setting a line and write:
251 // Setting configuration
252 $updatedLine = $writeToLocalconf_dat['updatedText'].date('d-m-Y H:i:s');
253 array_push($inlines,$updatedLine);
254 array_push($inlines,$writeToLocalconf_dat['endLine']);
255
256 if ($this->setLocalconf) {
257 t3lib_div::writeFile($writeToLocalconf_dat['file'],implode(chr(10),$inlines));
258
259 if (strcmp(t3lib_div::getUrl($writeToLocalconf_dat['file']), implode(chr(10),$inlines))) {
260 die('typo3conf/localconf.php was NOT updated properly (written content didn\'t match file content) - maybe write access problem?');
261 }
262
263 $this->messages[]= 'Configuration written to typo3conf/localconf.php';
264 return 'continue';
265 } else {
266 return 'nochange';
267 }
268 } else { // Return lines found in localconf.php
269 return $lines;
270 }
271 }
272
273 /**
274 * Checking for linebreaks in the string
275 *
276 * @param string String to test
277 * @return boolean Returns TRUE if string is OK
278 * @see setValueInLocalconfFile()
279 */
280 function checkForBadString($string) {
281 if (ereg('['.chr(10).chr(13).']',$string)){
282 return FALSE;
283 } else return TRUE;
284 }
285
286 /**
287 * Replaces ' with \' and \ with \\
288 *
289 * @param string Input value
290 * @return string Output value
291 * @see setValueInLocalconfFile()
292 */
293 function slashValueForSingleDashes($value) {
294 return str_replace("'","\'",str_replace('\\','\\\\',$value));
295 }
296
297
298
299
300
301
302
303
304
305
306 /*************************************
307 *
308 * SQL
309 *
310 *************************************/
311
312 /**
313 * Reads the field definitions for the input sql-file string
314 *
315 * @param string $sqlContent: Should be a string read from an sql-file made with 'mysqldump [database_name] -d'
316 * @return array Array with information about table.
317 */
318 function getFieldDefinitions_sqlContent($sqlContent) {
319 $lines = t3lib_div::trimExplode(chr(10), $sqlContent,1);
320 $isTable = '';
321
322 foreach($lines as $value) {
323 if ($value[0]!='#') {
324 if (!$isTable) {
325 $parts = explode(' ',$value);
326 if ($parts[0]=='CREATE' && $parts[1]=='TABLE') {
327 $isTable = $parts[2];
328 if (TYPO3_OS=='WIN') { // tablenames are always lowercase on windows!
329 $isTable = strtolower($isTable);
330 }
331 }
332 } else {
333 if (substr($value,0,1)==')' && substr($value,-1)==';') {
334 $isTable = '';
335 } else {
336 $lineV = ereg_replace(',$','',$value);
337 $parts = explode(' ',$lineV,2);
338 if ($parts[0]!='PRIMARY' && $parts[0]!='KEY' && $parts[0]!='UNIQUE') {
339 $total[$isTable]['fields'][$parts[0]] = $parts[1];
340 } else {
341 $newParts = explode(' ',$parts[1],2);
342 $total[$isTable]['keys'][($parts[0]=='PRIMARY'?$parts[0]:$newParts[0])] = $lineV;
343 }
344 }
345 }
346 }
347 }
348
349 $this->getFieldDefinitions_sqlContent_parseTypes($total);
350 return $total;
351 }
352
353 /**
354 * Multiplies varchars/tinytext fields in size according to $this->multiplySize
355 * 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.
356 *
357 * @param array Total array (from getFieldDefinitions_sqlContent())
358 * @return void
359 * @access private
360 * @see getFieldDefinitions_sqlContent()
361 */
362 function getFieldDefinitions_sqlContent_parseTypes(&$total) {
363
364 $mSize = (double)$this->multiplySize;
365 if ($mSize > 1) {
366
367 // Init SQL parser:
368 $sqlParser = t3lib_div::makeInstance('t3lib_sqlparser');
369 foreach($total as $table => $cfg) {
370 foreach($cfg['fields'] as $fN => $fType) {
371 $orig_fType = $fType;
372 $fInfo = $sqlParser->parseFieldDef($fType);
373
374 switch($fInfo['fieldType']) {
375 case 'char':
376 case 'varchar':
377 $newSize = round($fInfo['value']*$mSize);
378
379 if ($newSize <= 255) {
380 $fInfo['value'] = $newSize;
381 } else {
382 $fInfo = array(
383 'fieldType' => 'text',
384 'featureIndex' => array(
385 'NOTNULL' => array(
386 'keyword' => 'NOT NULL'
387 )
388 )
389 );
390 }
391 break;
392 case 'tinytext':
393 $fInfo['fieldType'] = 'text';
394 break;
395 }
396
397 $total[$table]['fields'][$fN] = $sqlParser->compileFieldCfg($fInfo);
398 if ($sqlParser->parse_error) die($sqlParser->parse_error);
399 }
400 }
401 }
402 }
403
404 /**
405 * Reads the field definitions for the current database
406 *
407 * @return array Array with information about table.
408 */
409 function getFieldDefinitions_database() {
410 $total = array();
411 $GLOBALS['TYPO3_DB']->sql_select_db(TYPO3_db);
412 echo $GLOBALS['TYPO3_DB']->sql_error();
413
414 $tables = $GLOBALS['TYPO3_DB']->admin_get_tables(TYPO3_db);
415 foreach($tables as $tableName) {
416
417 // Fields:
418 $fieldInformation = $GLOBALS['TYPO3_DB']->admin_get_fields($tableName);
419 foreach($fieldInformation as $fN => $fieldRow) {
420 $total[$tableName]['fields'][$fN] = $this->assembleFieldDefinition($fieldRow);
421 }
422
423 // Keys:
424 $keyInformation = $GLOBALS['TYPO3_DB']->admin_get_keys($tableName);
425 foreach($keyInformation as $kN => $keyRow) {
426 $tempKeys[$tableName][$keyRow['Key_name']][$keyRow['Seq_in_index']] = $keyRow['Column_name'];
427 $tempKeysPrefix[$tableName][$keyRow['Key_name']]= ($keyRow['Key_name']=='PRIMARY'?'PRIMARY KEY':($keyRow['Non_unique']?'KEY':'UNIQUE').' '.$keyRow['Key_name']);
428 }
429 }
430
431 // Compile information:
432 if (is_array($tempKeys)) {
433 foreach($tempKeys as $table => $keyInf) {
434 foreach($keyInf as $kName => $index) {
435 ksort($index);
436 $total[$table]['keys'][$kName] = $tempKeysPrefix[$table][$kName].' ('.implode(',',$index).')';
437 }
438 }
439 }
440
441 return $total;
442 }
443
444 /**
445 * Compares two arrays with field information and returns information about fields that are MISSING and fields that have CHANGED.
446 * FDsrc and FDcomp can be switched if you want the list of stuff to remove rather than update.
447 *
448 * @param array Field definitions, source (from getFieldDefinitions_sqlContent())
449 * @param array Field definitions, comparison. (from getFieldDefinitions_database())
450 * @param string Table names (in list) which is the ONLY one observed.
451 * @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
452 */
453 function getDatabaseExtra($FDsrc, $FDcomp, $onlyTableList='') {
454 $extraArr = array();
455 $diffArr = array();
456
457 if (is_array($FDsrc)) {
458 foreach($FDsrc as $table => $info) {
459 if (!strlen($onlyTableList) || t3lib_div::inList($onlyTableList, $table)) {
460 if (!isset($FDcomp[$table])) {
461 $extraArr[$table] = $info; // If the table was not in the FDcomp-array, the result array is loaded with that table.
462 $extraArr[$table]['whole_table']=1;
463 } else {
464 $keyTypes = explode(',','fields,keys');
465 foreach($keyTypes as $theKey) {
466 if (is_array($info[$theKey])) {
467 foreach($info[$theKey] as $fieldN => $fieldC) {
468
469 if (!$this->changedUnsignedFields && isset($this->coreFieldsWithUnsignedKeywordRemovedIn380[$table][$fieldN])) {
470 $FDcomp[$table][$theKey][$fieldN] = str_replace(' unsigned','',$FDcomp[$table][$theKey][$fieldN]);
471 unset($this->coreFieldsWithUnsignedKeywordRemovedIn380[$table][$fieldN]); // Just to verify that all fields are actually found!
472 }
473
474
475
476 if (!isset($FDcomp[$table][$theKey][$fieldN])) {
477 $extraArr[$table][$theKey][$fieldN] = $fieldC;
478 } elseif (strcmp($FDcomp[$table][$theKey][$fieldN], $fieldC) && strcmp($FDcomp[$table][$theKey][$fieldN], str_replace(' unsigned','',$fieldC))){
479 $diffArr[$table][$theKey][$fieldN] = $fieldC;
480 $diffArr_cur[$table][$theKey][$fieldN] = $FDcomp[$table][$theKey][$fieldN];
481 }
482 }
483 }
484 }
485 }
486 }
487 }
488 }
489
490 $output = array(
491 'extra' => $extraArr,
492 'diff' => $diffArr,
493 'diff_currentValues' => $diffArr_cur
494 );
495
496 return $output;
497 }
498
499 /**
500 * Returns an array with SQL-statements that is needed to update according to the diff-array
501 *
502 * @param array Array with differences of current and needed DB settings. (from getDatabaseExtra())
503 * @param string List of fields in diff array to take notice of.
504 * @return array Array of SQL statements (organized in keys depending on type)
505 */
506 function getUpdateSuggestions($diffArr,$keyList='extra,diff') {
507 $statements = array();
508 $deletedPrefixKey = $this->deletedPrefixKey;
509 $remove = 0;
510 if ($keyList == 'remove') {
511 $remove = 1;
512 $keyList = 'extra';
513 }
514 $keyList = explode(',',$keyList);
515 foreach($keyList as $theKey) {
516 if (is_array($diffArr[$theKey])) {
517 foreach($diffArr[$theKey] as $table => $info) {
518 $whole_table = array();
519 if (is_array($info['fields'])) {
520 foreach($info['fields'] as $fN => $fV) {
521 if ($info['whole_table']) {
522 if(!strcmp($fN,'uid')) {
523 if(strstr($fV,'auto_increment')) {
524 $fV = eregi_replace('default \'0\'','',$fV);
525 }
526 }
527 $whole_table[]=$fN.' '.$fV;
528 } else {
529 if ($theKey=='extra') {
530 if ($remove) {
531 if (substr($fN,0,strlen($deletedPrefixKey))!=$deletedPrefixKey) {
532 $statement = 'ALTER TABLE '.$table.' CHANGE '.$fN.' '.$deletedPrefixKey.$fN.' '.$fV.';';
533 $statements['change'][md5($statement)] = $statement;
534 } else {
535 $statement = 'ALTER TABLE '.$table.' DROP '.$fN.';';
536 $statements['drop'][md5($statement)] = $statement;
537 }
538 } else {
539 $statement = 'ALTER TABLE '.$table.' ADD '.$fN.' '.$fV.';';
540 $statements['add'][md5($statement)] = $statement;
541 }
542 } elseif ($theKey=='diff') {
543 $statement = 'ALTER TABLE '.$table.' CHANGE '.$fN.' '.$fN.' '.$fV.';';
544 $statements['change'][md5($statement)] = $statement;
545 $statements['change_currentValue'][md5($statement)] = $diffArr['diff_currentValues'][$table]['fields'][$fN];
546 }
547 }
548 }
549 }
550 if (is_array($info['keys'])) {
551 foreach($info['keys'] as $fN => $fV) {
552 if ($info['whole_table']) {
553 if ($fN=='PRIMARY') {
554 $whole_table[] = $fV;
555 } else {
556 $whole_table[] = $fV;
557 }
558 } else {
559 if ($theKey=='extra') {
560 if ($remove) {
561 $statement = 'ALTER TABLE '.$table.($fN=='PRIMARY' ? ' DROP PRIMARY KEY' : ' DROP KEY '.$fN).';';
562 $statements['drop'][md5($statement)] = $statement;
563 } else {
564 $statement = 'ALTER TABLE '.$table.' ADD '.$fV.';';
565 $statements['add'][md5($statement)] = $statement;
566 }
567 } elseif ($theKey=='diff') {
568 $statement = 'ALTER TABLE '.$table.($fN=='PRIMARY' ? ' DROP PRIMARY KEY' : ' DROP KEY '.$fN).';';
569 $statements['change'][md5($statement)] = $statement;
570 $statement = 'ALTER TABLE '.$table.' ADD '.$fV.';';
571 $statements['change'][md5($statement)] = $statement;
572 }
573 }
574 }
575 }
576 if ($info['whole_table']) {
577 if ($remove) {
578 if (substr($table,0,strlen($deletedPrefixKey))!=$deletedPrefixKey) {
579 $statement = 'ALTER TABLE '.$table.' RENAME '.$deletedPrefixKey.$table.';';
580 $statements['change_table'][md5($statement)]=$statement;
581 } else {
582 $statement = 'DROP TABLE '.$table.';';
583 $statements['drop_table'][md5($statement)]=$statement;
584 }
585 // count:
586 $res = $GLOBALS['TYPO3_DB']->exec_SELECTquery('count(*)', $table, '');
587 list($count) = $GLOBALS['TYPO3_DB']->sql_fetch_row($res);
588 $statements['tables_count'][md5($statement)] = $count?'Records in table: '.$count:'';
589 } else {
590 $statement = 'CREATE TABLE '.$table." (\n".implode(",\n",$whole_table)."\n) TYPE=MyISAM;";
591 $statements['create_table'][md5($statement)]=$statement;
592 }
593 }
594 }
595 }
596 }
597
598 return $statements;
599 }
600
601 /**
602 * Converts a result row with field information into the SQL field definition string
603 *
604 * @param array MySQL result row.
605 * @return string Field definition
606 */
607 function assembleFieldDefinition($row) {
608 $field[] = $row['Type'];
609 if ($this->mysqlVersion=='3.23' && !$row['Null']) {
610 $field[] = 'NOT NULL';
611 }
612 if (!strstr($row['Type'],'blob') && !strstr($row['Type'],'text')) {
613 // 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.
614 // 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.
615 if ($this->mysqlVersion!='3.23' || !stristr($row['Extra'],'auto_increment')) {
616 $field[] = ($this->mysqlVersion=='3.23'?'default':'DEFAULT')." '".(stristr($row['Extra'],'auto_increment')?intval($row['Default']):addslashes($row['Default']))."'";
617 }
618 }
619 if ($this->mysqlVersion!='3.23' && !$row['Null']) {
620 $field[] = 'NOT NULL';
621 }
622 if ($row['Extra']) {
623 $field[] = $row['Extra'];
624 }
625 return implode(' ',$field);
626 }
627
628 /**
629 * Returns an array where every entry is a single sql-statement. Input must be formatted like an ordinary MySQL-dump files
630 *
631 * @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.
632 * @param boolean If set, non-sql (like comments and blank lines) are not included in the final product)
633 * @param string Regex to filter SQL lines to include.
634 * @return array Array of SQL statements.
635 */
636 function getStatementArray($sqlcode,$removeNonSQL=0,$query_regex='') {
637 $sqlcodeArr = explode(chr(10),$sqlcode);
638
639 // Based on the assumption that the sql-dump has
640 $statementArray = array();
641 $statementArrayPointer = 0;
642
643 foreach($sqlcodeArr as $line => $linecontent) {
644
645 // Setting MySQL Version if necessary.
646 if (substr(trim($linecontent),0,16)=='# Server version') {
647 $this->mysqlVersion = doubleval(substr(trim($linecontent),16));
648 }
649 $is_set = 0;
650
651 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
652 $statementArray[$statementArrayPointer].= $linecontent;
653 $is_set = 1;
654 }
655 if (substr(trim($linecontent),-1)==';') {
656 if (isset($statementArray[$statementArrayPointer])) {
657 if (!trim($statementArray[$statementArrayPointer]) || ($query_regex && !eregi($query_regex,trim($statementArray[$statementArrayPointer])))) {
658 unset($statementArray[$statementArrayPointer]);
659 }
660 }
661 $statementArrayPointer++;
662 } elseif ($is_set) {
663 $statementArray[$statementArrayPointer].=chr(10);
664 }
665 }
666 return $statementArray;
667 }
668
669 /**
670 * Returns tables to create and how many records in each
671 *
672 * @param array Array of SQL statements to analyse.
673 * @param boolean If set, will count number of INSERT INTO statements following that table definition
674 * @return array Array with table definitions in index 0 and count in index 1
675 */
676 function getCreateTables($statements, $insertCountFlag=0) {
677 $crTables = array();
678 foreach($statements as $line => $linecontent) {
679 if (eregi('^create[[:space:]]*table[[:space:]]*([[:alnum:]_]*)',substr($linecontent,0,100),$reg)) {
680 $table = trim($reg[1]);
681 if ($table) {
682 if (TYPO3_OS=='WIN') {$table=strtolower($table);} // tablenames are always lowercase on windows!
683 $crTables[$table] = $linecontent;
684 }
685 } elseif ($insertCountFlag && eregi('^insert[[:space:]]*into[[:space:]]*([[:alnum:]_]*)',substr($linecontent,0,100),$reg)) {
686 $nTable = trim($reg[1]);
687 $insertCount[$nTable]++;
688 }
689 }
690 return array($crTables,$insertCount);
691 }
692
693 /**
694 * Extracts all insert statements from $statement array where content is inserted into $table
695 *
696 * @param array Array of SQL statements
697 * @param string Table name
698 * @return array Array of INSERT INTO statements where table match $table
699 */
700 function getTableInsertStatements($statements, $table) {
701 $outStatements=array();
702 foreach($statements as $line => $linecontent) {
703 if (eregi('^insert[[:space:]]*into[[:space:]]*([[:alnum:]_]*)',substr($linecontent,0,100),$reg)) {
704 $nTable = trim($reg[1]);
705 if ($nTable && !strcmp($table,$nTable)) {
706 $outStatements[]=$linecontent;
707 }
708 }
709 }
710 return $outStatements;
711 }
712
713 /**
714 * Performs the queries passed from the input array.
715 *
716 * @param array Array of SQL queries to execute.
717 * @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)
718 * @return void
719 */
720 function performUpdateQueries($arr,$keyArr) {
721 if (is_array($arr)) {
722 foreach($arr as $key => $string) {
723 if (isset($keyArr[$key]) && $keyArr[$key]) {
724 $GLOBALS['TYPO3_DB']->admin_query($string);
725 }
726 }
727 }
728 }
729
730 /**
731 * Returns list of tables in the database
732 *
733 * @return array List of tables.
734 * @see t3lib_db::admin_get_tables()
735 */
736 function getListOfTables() {
737 $whichTables = $GLOBALS['TYPO3_DB']->admin_get_tables(TYPO3_db);
738 return $whichTables;
739 }
740
741 /**
742 * Creates a table which checkboxes for updating database.
743 *
744 * @param array Array of statements (key / value pairs where key is used for the checkboxes)
745 * @param string Label for the table.
746 * @param boolean If set, then checkboxes are set by default.
747 * @param boolean If set, then icons are shown.
748 * @param array Array of "current values" for each key/value pair in $arr. Shown if given.
749 * @param boolean If set, will show the prefix "Current value" if $currentValue is given.
750 * @return string HTML table with checkboxes for update. Must be wrapped in a form.
751 */
752 function generateUpdateDatabaseForm_checkboxes($arr,$label,$checked=1,$iconDis=0,$currentValue=array(),$cVfullMsg=0) {
753 $out = array();
754 if (is_array($arr)) {
755 foreach($arr as $key => $string) {
756 $ico = '';
757 if ($iconDis) {
758 if (stristr($string,' user_')) {
759 $ico.= '<img src="'.$this->backPath.'t3lib/gfx/icon_warning.gif" width="18" height="16" align="top" alt="" /><strong>(USER) </strong>';
760 }
761 if (stristr($string,' app_')) {
762 $ico.= '<img src="'.$this->backPath.'t3lib/gfx/icon_warning.gif" width="18" height="16" align="top" alt="" /><strong>(APP) </strong>';
763 }
764 if (stristr($string,' ttx_') || stristr($string,' tx_')) {
765 $ico.= '<img src="'.$this->backPath.'t3lib/gfx/icon_warning.gif" width="18" height="16" align="top" alt="" /><strong>(EXT) </strong>';
766 }
767 }
768 $out[]='
769 <tr>
770 <td valign="top"><input type="checkbox" name="'.$this->dbUpdateCheckboxPrefix.'['.$key.']" value="1"'.($checked?' checked="checked"':'').' /></td>
771 <td nowrap="nowrap">'.nl2br($ico.htmlspecialchars($string)).'</td>
772 </tr>';
773 if (isset($currentValue[$key])) {
774 $out[]='
775 <tr>
776 <td valign="top"></td>
777 <td nowrap="nowrap" style="color : #666666;">'.nl2br((!$cVfullMsg?"Current value: ":"").'<em>'.$currentValue[$key].'</em>').'</td>
778 </tr>';
779 }
780 }
781
782 // Compile rows:
783 $content = '
784 <!-- Update database fields / tables -->
785 <h3>'.$label.'</h3>
786 <table border="0" cellpadding="2" cellspacing="2" class="update-db-fields">'.implode('',$out).'
787 </table>';
788 }
789
790 return $content;
791 }
792 }
793
794 if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['t3lib/class.t3lib_install.php']) {
795 include_once($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['t3lib/class.t3lib_install.php']);
796 }
797 ?>