Disabling some or all controls in group field - controllable via pageTS
[Packages/TYPO3.CMS.git] / t3lib / class.t3lib_install.php
index a81ed1a..5ac005f 100755 (executable)
@@ -2,7 +2,7 @@
 /***************************************************************
 *  Copyright notice
 *
-*  (c) 1999-2004 Kasper Skaarhoj (kasper@typo3.com)
+*  (c) 1999-2008 Kasper Skaarhoj (kasperYYYY@typo3.com)
 *  All rights reserved
 *
 *  This script is part of the TYPO3 project. The TYPO3 project is
  *
  * $Id$
  *
- * @author     Kasper Skaarhoj <kasper@typo3.com>
+ * @author     Kasper Skaarhoj <kasperYYYY@typo3.com>
  */
 /**
  * [CLASS/FUNCTION INDEX of SCRIPT]
  *
  *
  *
- *   80: class t3lib_install
+ *   83: class t3lib_install
+ *  108:     function t3lib_install()
  *
  *              SECTION: Writing to localconf.php
- *  118:     function setValueInLocalconfFile(&$line_array, $variable, $value)
- *  168:     function writeToLocalconf_control($inlines='')
- *  220:     function checkForBadString($string)
- *  233:     function slashValueForSingleDashes($value)
+ *  132:     function setValueInLocalconfFile(&$line_array, $variable, $value)
+ *  183:     function writeToLocalconf_control($inlines='',$absFullPath='')
+ *  253:     function checkForBadString($string)
+ *  266:     function slashValueForSingleDashes($value)
  *
  *              SECTION: SQL
- *  258:     function getFieldDefinitions_sqlContent($sqlContent)
- *  296:     function getFieldDefinitions_database()
- *  340:     function getDatabaseExtra($FDsrc, $FDcomp, $onlyTableList='')
- *  385:     function getUpdateSuggestions($diffArr,$keyList='extra,diff')
- *  481:     function assembleFieldDefinition($row)
- *  510:     function getStatementArray($sqlcode,$removeNonSQL=0,$query_regex='')
- *  550:     function getCreateTables($statements, $insertCountFlag=0)
- *  574:     function getTableInsertStatements($statements, $table)
- *  594:     function performUpdateQueries($arr,$keyArr)
- *  610:     function getListOfTables()
- *  626:     function generateUpdateDatabaseForm_checkboxes($arr,$label,$checked=1,$iconDis=0,$currentValue=array(),$cVfullMsg=0)
+ *  291:     function getFieldDefinitions_fileContent($fileContent)
+ *  359:     function getFieldDefinitions_sqlContent_parseTypes(&$total)
+ *  406:     function getFieldDefinitions_database()
+ *  450:     function getDatabaseExtra($FDsrc, $FDcomp, $onlyTableList='')
+ *  496:     function getUpdateSuggestions($diffArr,$keyList='extra,diff')
+ *  589:     function assembleFieldDefinition($row)
+ *  611:     function getStatementArray($sqlcode,$removeNonSQL=0,$query_regex='')
+ *  649:     function getCreateTables($statements, $insertCountFlag=0)
+ *  683:     function getTableInsertStatements($statements, $table)
+ *  704:     function performUpdateQueries($arr,$keyArr)
+ *  720:     function getListOfTables()
+ *  736:     function generateUpdateDatabaseForm_checkboxes($arr,$label,$checked=1,$iconDis=0,$currentValue=array(),$cVfullMsg=0)
  *
- * TOTAL FUNCTIONS: 15
+ * TOTAL FUNCTIONS: 17
  * (This index is automatically created/updated by the extension "extdeveval")
  *
  */
@@ -74,23 +76,24 @@ require_once(PATH_t3lib.'class.t3lib_sqlparser.php');
 /**
  * Class to setup values in localconf.php and verify the TYPO3 DB tables/fields
  *
- * @author     Kasper Skaarhoj <kasper@typo3.com>
+ * @author     Kasper Skaarhoj <kasperYYYY@typo3.com>
  * @package TYPO3
  * @subpackage t3lib
  */
 class t3lib_install {
 
+
                // External, Static
        var $updateIdentity = '';                                       // Set to string which identifies the script using this class.
        var $deletedPrefixKey = 'zzz_deleted_';         // Prefix used for tables/fields when deleted/renamed.
        var $dbUpdateCheckboxPrefix = 'TYPO3_INSTALL[database_update]'; // Prefix for checkbox fields when updating database.
-       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.
        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.
        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
        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.
        var $backPath = '../';                          // Backpath (used for icons etc.)
 
        var $multiplySize = 1;                          // Multiplier of SQL field size (for char, varchar and text fields)
+       var $character_sets = array();                  // Caching output of $GLOBALS['TYPO3_DB']->admin_get_charsets()
 
                // Internal, dynamic:
        var $setLocalconf = 0;                          // Used to indicate that a value is change in the line-array of localconf and that it should be written.
@@ -98,8 +101,16 @@ class t3lib_install {
        var $touchedLine = 0;                           // updated with line in localconf.php file that was changed.
 
 
-
-
+       /**
+        * Constructor function
+        *
+        * @return      void
+        */
+       function t3lib_install()        {
+               if ($GLOBALS['TYPO3_CONF_VARS']['SYS']['multiplyDBfieldSize']>= 1 && $GLOBALS['TYPO3_CONF_VARS']['SYS']['multiplyDBfieldSize']<=5)      {
+                       $this->multiplySize = (double)$GLOBALS['TYPO3_CONF_VARS']['SYS']['multiplyDBfieldSize'];
+               }
+       }
 
 
 
@@ -107,6 +118,7 @@ class t3lib_install {
         *
         * Writing to localconf.php
         *
+
         **************************************/
 
        /**
@@ -140,7 +152,7 @@ class t3lib_install {
                                        $mainparts = explode($variable,$v,2);
                                        if (count($mainparts)==2)       {       // should ALWAYS be....
                                                $subparts = explode('//',$mainparts[1],2);
-                                               $line_array[$k] = $mainparts[0].$variable." = '".$this->slashValueForSingleDashes($value)."';       ".('//'.$comment.str_replace($comment,'',$subparts[1]));
+                                               $line_array[$k] = $mainparts[0].$variable." = '".$this->slashValueForSingleDashes($value)."';   ".('//'.$comment.str_replace($comment,'',$subparts[1]));
                                                $this->touchedLine = count($line_array)-$k-1;
                                                $found = 1;
                                                break;
@@ -165,46 +177,66 @@ class t3lib_install {
         * Writes or returns lines from localconf.php
         *
         * @param       array           Array of lines to write back to localconf.php. Possibly
+        * @param       string          Absolute path of alternative file to use (Notice: this path is not validated in terms of being inside 'TYPO3 space')
         * @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)
         * @see setValueInLocalconfFile()
         */
-       function writeToLocalconf_control($inlines='')  {
-               $writeToLocalconf_dat['file'] = PATH_typo3conf.'localconf.php';
+       function writeToLocalconf_control($inlines='',$absFullPath='')  {
+               $tmpExt = '.TMP.php';
+               $writeToLocalconf_dat['file'] = $absFullPath ? $absFullPath : PATH_typo3conf.'localconf.php';
+               $writeToLocalconf_dat['tmpfile'] = $writeToLocalconf_dat['file'].$tmpExt;
 
                        // Checking write state of localconf.php:
                if (!$this->allowUpdateLocalConf)       {
-                       die("->allowUpdateLocalConf flag in the install object is not set and therefore 'localconf.php' cannot be altered.");
+                       die('->allowUpdateLocalConf flag in the install object is not set and therefore "localconf.php" cannot be altered.');
                }
-               if (!@is_writeable($writeToLocalconf_dat['file']))      {
-                       die($writeToLocalconf_dat['file'].' is not writeable!');
+               if (!@is_writable($writeToLocalconf_dat['file']))       {
+                       die($writeToLocalconf_dat['file'].' is not writable!');
                }
 
                                // Splitting localconf.php file into lines:
-               $lines = explode(chr(10),trim(t3lib_div::getUrl($writeToLocalconf_dat['file'])));
+               $lines = explode(chr(10),str_replace(chr(13),'',trim(t3lib_div::getUrl($writeToLocalconf_dat['file']))));
                $writeToLocalconf_dat['endLine'] = array_pop($lines);   // Getting "? >" ending.
 
                        // Checking if "updated" line was set by this tool - if so remove old line.
                $updatedLine = array_pop($lines);
                $writeToLocalconf_dat['updatedText'] = '// Updated by '.$this->updateIdentity.' ';
+
                if (!strstr($updatedLine, $writeToLocalconf_dat['updatedText']))        {
                        array_push($lines,$updatedLine);
                }
 
                if (is_array($inlines)) {       // Setting a line and write:
                                // Setting configuration
-                       $updatedLine = $writeToLocalconf_dat['updatedText'].date('d-m-Y H:i:s');
+                       $updatedLine = $writeToLocalconf_dat['updatedText'].date($GLOBALS['TYPO3_CONF_VARS']['SYS']['ddmmyy'].' H:i:s');
                        array_push($inlines,$updatedLine);
                        array_push($inlines,$writeToLocalconf_dat['endLine']);
 
                        if ($this->setLocalconf)        {
-                               t3lib_div::writeFile($writeToLocalconf_dat['file'],implode($inlines,chr(10)));
-
-                               if (strcmp(t3lib_div::getUrl($writeToLocalconf_dat['file']), implode($inlines,chr(10))))        {
-                                       die('typo3temp/localconf.php was NOT updated properly (written content didn\'t match file content) - maybe write access problem?');
+                               $success = FALSE;
+                               if (!t3lib_div::writeFile($writeToLocalconf_dat['tmpfile'],implode(chr(10),$inlines)))  {
+                                       $msg = 'typo3conf/localconf.php'.$tmpExt.' could not be written - maybe a write access problem?';
+                               }
+                               elseif (strcmp(t3lib_div::getUrl($writeToLocalconf_dat['tmpfile']), implode(chr(10),$inlines))) {
+                                       @unlink($writeToLocalconf_dat['tmpfile']);
+                                       $msg = 'typo3conf/localconf.php'.$tmpExt.' was NOT written properly (written content didn\'t match file content) - maybe a disk space problem?';
+                               }
+                               elseif (!@copy($writeToLocalconf_dat['tmpfile'],$writeToLocalconf_dat['file'])) {
+                                       $msg = 'typo3conf/localconf.php could not be replaced by typo3conf/localconf.php'.$tmpExt.' - maybe a write access problem?';
                                }
+                               else {
+                                       @unlink($writeToLocalconf_dat['tmpfile']);
+                                       $success = TRUE;
+                                       $msg = 'Configuration written to typo3conf/localconf.php';
+                               }
+                               $this->messages[] = $msg;
 
-                               $this->messages[]= 'Configuration written to typo3conf/localconf.php';
-                               return 'continue';
+                               if ($success)   {
+                                       return 'continue';
+                               } else {
+                                       t3lib_div::sysLog($msg, 'Core', 3);
+                                       return 'nochange';
+                               }
                        } else {
                                return 'nochange';
                        }
@@ -221,9 +253,7 @@ class t3lib_install {
         * @see setValueInLocalconfFile()
         */
        function checkForBadString($string)     {
-               if (ereg('['.chr(10).chr(13).']',$string)){
-                       return FALSE;
-               } else return TRUE;
+               return preg_match('/['.chr(10).chr(13).']/',$string) ? FALSE : TRUE;
        }
 
        /**
@@ -234,7 +264,11 @@ class t3lib_install {
         * @see setValueInLocalconfFile()
         */
        function slashValueForSingleDashes($value)      {
-               return str_replace("'","\'",str_replace('\\','\\\\',$value));
+               $value = str_replace("'.chr(10).'", '###INSTALL_TOOL_LINEBREAK###', $value);
+               $value = str_replace("'","\'",str_replace('\\','\\\\',$value));
+               $value = str_replace('###INSTALL_TOOL_LINEBREAK###', "'.chr(10).'", $value);
+
+               return $value;
        }
 
 
@@ -253,37 +287,86 @@ class t3lib_install {
         *************************************/
 
        /**
-        * Reads the field definitions for the input sql-file string
+        * Reads the field definitions for the input SQL-file string
         *
-        * @param       string          $sqlContent: Should be a string read from an sql-file made with 'mysqldump [database_name] -d'
+        * @param       string          Should be a string read from an SQL-file made with 'mysqldump [database_name] -d'
         * @return      array           Array with information about table.
         */
-       function getFieldDefinitions_sqlContent($sqlContent)    {
-               $lines = t3lib_div::trimExplode(chr(10), $sqlContent,1);
-               $isTable = '';
-
-               foreach($lines as $value)       {
-                       if ($value[0]!='#')     {
-                               if (!$isTable)  {
-                                       $parts = explode(' ',$value);
-                                       if ($parts[0]=='CREATE' && $parts[1]=='TABLE')  {
-                                               $isTable = $parts[2];
-                                               if (TYPO3_OS=='WIN') {  // tablenames are always lowercase on windows!
-                                                       $isTable = strtolower($isTable);
-                                               }
+       function getFieldDefinitions_fileContent($fileContent)  {
+               $lines = t3lib_div::trimExplode(chr(10), $fileContent, 1);
+               $table = '';
+               $total = array();
+
+               foreach ($lines as $value) {
+                       if (substr($value,0,1)=='#') {
+                               continue;       // Ignore comments
+                       }
+
+                       if (!strlen($table)) {
+                               $parts = explode(' ',$value);
+                               if ($parts[0]=='CREATE' && $parts[1]=='TABLE')  {
+                                       $table = str_replace( '`', '', $parts[2]);
+                                       if (TYPO3_OS=='WIN') {  // tablenames are always lowercase on windows!
+                                               $table = strtolower($table);
                                        }
-                               } else {
-                                       if (substr($value,0,1)==')' && substr($value,-1)==';')  {
-                                               $isTable = '';
+                               }
+                       } else {
+                               if (substr($value,0,1)==')' && substr($value,-1)==';')  {
+                                       $ttype = array();
+                                       if (preg_match('/(ENGINE|TYPE)[ ]*=[ ]*([a-zA-Z]*)/',$value,$ttype)) {
+                                               $total[$table]['extra']['ENGINE'] = $ttype[2];
+                                       } // Otherwise, just do nothing: If table engine is not defined, just accept the system default.
+
+                                               // Set the collation, if specified
+                                       if (preg_match('/(COLLATE)[ ]*=[ ]*([a-zA-z0-9_-]+)/', $value, $tcollation)) {
+                                               $total[$table]['extra']['COLLATE'] = $tcollation[2];
                                        } else {
-                                               $lineV = ereg_replace(',$','',$value);
-                                               $parts = explode(' ',$lineV,2);
-                                               if ($parts[0]!='PRIMARY' && $parts[0]!='KEY' && $parts[0]!='UNIQUE')    {
-                                                       $total[$isTable]['fields'][$parts[0]] = $parts[1];
+                                                       // 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)
+                                               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
+                                                       $charset = $tcharset[2];
                                                } else {
-                                                       $newParts = explode(' ',$parts[1],2);
-                                                       $total[$isTable]['keys'][($parts[0]=='PRIMARY'?$parts[0]:$newParts[0])] = $lineV;
+                                                       $charset = $GLOBALS['TYPO3_DB']->default_charset;       // Fallback to default charset
+                                               }
+                                               $total[$table]['extra']['COLLATE'] = $this->getCollationForCharset($charset);
+                                       }
+
+                                       $table = '';    // Remove table marker and start looking for the next "CREATE TABLE" statement
+                               } else {
+                                       $lineV = preg_replace('/,$/','',$value);        // Strip trailing commas
+                                       $lineV = str_replace('`', '', $lineV);
+                                       $lineV = str_replace('  ', ' ', $lineV);        // Remove double blanks
+
+                                       $parts = explode(' ', $lineV, 2);
+                                       if (!preg_match('/(PRIMARY|UNIQUE|FULLTEXT|INDEX|KEY)/',$parts[0])) {   // Field definition
+
+                                                       // Make sure there is no default value when auto_increment is set
+                                               if (stristr($parts[1],'auto_increment')) {
+                                                       $parts[1] = preg_replace('/ default \'0\'/i','',$parts[1]);
+                                               }
+                                                       // "default" is always lower-case
+                                               if (stristr($parts[1], ' DEFAULT '))    {
+                                                       $parts[1] = str_ireplace(' DEFAULT ', ' default ', $parts[1]);
+                                               }
+
+                                                       // Change order of "default" and "null" statements
+                                               $parts[1] = preg_replace('/(.*) (default .*) (NOT NULL)/', '$1 $3 $2', $parts[1]);
+                                               $parts[1] = preg_replace('/(.*) (default .*) (NULL)/', '$1 $3 $2', $parts[1]);
+
+                                               $key = $parts[0];
+                                               $total[$table]['fields'][$key] = $parts[1];
+
+                                       } else {        // Key definition
+                                               $search = array('/UNIQUE (INDEX|KEY)/', '/FULLTEXT (INDEX|KEY)/', '/INDEX/');
+                                               $replace = array('UNIQUE', 'FULLTEXT', 'KEY');
+                                               $lineV = preg_replace($search, $replace, $lineV);
+
+                                               if (preg_match('/PRIMARY|UNIQUE|FULLTEXT/', $parts[0])) {
+                                                       $parts[1] = preg_replace('/^(KEY|INDEX) /', '', $parts[1]);
                                                }
+
+                                               $newParts = explode(' ',$parts[1],2);
+                                               $key = $parts[0]=='PRIMARY' ? $parts[0] : $newParts[0];
+                                               $total[$table]['keys'][$key] = $lineV;
                                        }
                                }
                        }
@@ -297,10 +380,10 @@ class t3lib_install {
         * Multiplies varchars/tinytext fields in size according to $this->multiplySize
         * 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.
         *
-        * @param       array           Total array (from getFieldDefinitions_sqlContent())
+        * @param       array           Total array (from getFieldDefinitions_fileContent())
         * @return      void
-        * @see getFieldDefinitions_sqlContent()
         * @access private
+        * @see getFieldDefinitions_fileContent()
         */
        function getFieldDefinitions_sqlContent_parseTypes(&$total)     {
 
@@ -310,73 +393,144 @@ class t3lib_install {
                                // Init SQL parser:
                        $sqlParser = t3lib_div::makeInstance('t3lib_sqlparser');
                        foreach($total as $table => $cfg)       {
-                               foreach($cfg['fields'] as $fN => $fType)        {
-                                       $orig_fType = $fType;
-                                       $fInfo = $sqlParser->parseFieldDef($fType);
-
-                                       switch($fInfo['fieldType'])     {
-                                               case 'char':
-                                               case 'varchar':
-                                                       $newSize = round($fInfo['value']*$mSize);
-
-                                                       if ($newSize <= 255)    {
-                                                               $fInfo['value'] = $newSize;
-                                                       } else {
-                                                               $fInfo = array(
-                                                                       'fieldType' => 'text',
-                                                                       'featureIndex' => array(
-                                                                               'NOTNULL' => array(
-                                                                                       'keyword' => 'NOT NULL'
+                               if (is_array($cfg['fields'])) {
+                                       foreach($cfg['fields'] as $fN => $fType)        {
+                                               $orig_fType = $fType;
+                                               $fInfo = $sqlParser->parseFieldDef($fType);
+
+                                               switch($fInfo['fieldType'])     {
+                                                       case 'char':
+                                                       case 'varchar':
+                                                               $newSize = round($fInfo['value']*$mSize);
+
+                                                               if ($newSize <= 255)    {
+                                                                       $fInfo['value'] = $newSize;
+                                                               } else {
+                                                                       $fInfo = array(
+                                                                               'fieldType' => 'text',
+                                                                               'featureIndex' => array(
+                                                                                       'NOTNULL' => array(
+                                                                                               'keyword' => 'NOT NULL'
+                                                                                       )
                                                                                )
-                                                                       )
-                                                               );
-                                                       }
-                                               break;
-                                               case 'tinytext':
-                                                       $fInfo['fieldType'] = 'text';
-                                               break;
-                                       }
+                                                                       );
+                                                                               // Change key definition if necessary (must use "prefix" on TEXT columns)
+                                                                       if (is_array($cfg['keys'])) {
+                                                                               foreach ($cfg['keys'] as $kN => $kType) {
+                                                                                       $match = array();
+                                                                                       preg_match('/^([^(]*)\(([^)]+)\)(.*)/', $kType, $match);
+                                                                                       $keys = array();
+                                                                                       foreach (t3lib_div::trimExplode(',',$match[2]) as $kfN) {
+                                                                                               if ($fN == $kfN)        {
+                                                                                                       $kfN .= '('.$newSize.')';
+                                                                                               }
+                                                                                               $keys[] = $kfN;
+                                                                                       }
+                                                                                       $total[$table]['keys'][$kN] = $match[1].'('.join(',',$keys).')'.$match[3];
+                                                                               }
+                                                                       }
+                                                               }
+                                                       break;
+                                                       case 'tinytext':
+                                                               $fInfo['fieldType'] = 'text';
+                                                       break;
+                                               }
 
-                                       $total[$table]['fields'][$fN] = $sqlParser->compileFieldCfg($fInfo);
-                                       if ($sqlParser->parse_error)    die($sqlParser->parse_error);
+                                               $total[$table]['fields'][$fN] = $sqlParser->compileFieldCfg($fInfo);
+                                               if ($sqlParser->parse_error)    die($sqlParser->parse_error);
+                                       }
                                }
                        }
                }
        }
 
        /**
+        * Look up the default collation for specified character set based on "SHOW CHARACTER SET" output
+        *
+        * @param       string          Character set
+        * @return      string          Corresponding default collation
+        */
+       function getCollationForCharset($charset)       {
+                       // Load character sets, if not cached already
+               if (!count($this->character_sets)) {
+                       $this->character_sets = $GLOBALS['TYPO3_DB']->admin_get_charsets();
+               }
+
+               if (isset($this->character_sets[$charset]['Default collation'])) {
+                       $collation = $this->character_sets[$charset]['Default collation'];
+               }
+
+               return $collation;
+       }
+
+       /**
         * Reads the field definitions for the current database
         *
         * @return      array           Array with information about table.
         */
        function getFieldDefinitions_database() {
                $total = array();
+               $tempKeys = array();
+               $tempKeysPrefix = array();
+
                $GLOBALS['TYPO3_DB']->sql_select_db(TYPO3_db);
                echo $GLOBALS['TYPO3_DB']->sql_error();
 
                $tables = $GLOBALS['TYPO3_DB']->admin_get_tables(TYPO3_db);
-               foreach($tables as $tableName)  {
+               foreach ($tables as $tableName => $tableStatus) {
 
                                // Fields:
                        $fieldInformation = $GLOBALS['TYPO3_DB']->admin_get_fields($tableName);
-                       foreach($fieldInformation as $fN => $fieldRow)  {
+                       foreach ($fieldInformation as $fN => $fieldRow) {
                                $total[$tableName]['fields'][$fN] = $this->assembleFieldDefinition($fieldRow);
                        }
 
                                // Keys:
                        $keyInformation = $GLOBALS['TYPO3_DB']->admin_get_keys($tableName);
-                       foreach($keyInformation as $kN => $keyRow)      {
-                               $tempKeys[$tableName][$keyRow['Key_name']][$keyRow['Seq_in_index']] = $keyRow['Column_name'];
-                               $tempKeysPrefix[$tableName][$keyRow['Key_name']]= ($keyRow['Key_name']=='PRIMARY'?'PRIMARY KEY':($keyRow['Non_unique']?'KEY':'UNIQUE').' '.$keyRow['Key_name']);
+
+                       foreach ($keyInformation as $keyRow) {
+                               $keyName = $keyRow['Key_name'];
+                               $colName = $keyRow['Column_name'];
+                               if ($keyRow['Sub_part']) {
+                                       $colName.= '('.$keyRow['Sub_part'].')';
+                               }
+                               $tempKeys[$tableName][$keyName][$keyRow['Seq_in_index']] = $colName;
+                               if ($keyName=='PRIMARY') {
+                                       $prefix = 'PRIMARY KEY';
+                               } else {
+                                       if ($keyRow['Index_type']=='FULLTEXT') {
+                                               $prefix = 'FULLTEXT';
+                                       } elseif ($keyRow['Non_unique']) {
+                                               $prefix = 'KEY';
+                                       } else {
+                                               $prefix = 'UNIQUE';
+                                       }
+                                       $prefix.= ' '.$keyName;
+                               }
+                               $tempKeysPrefix[$tableName][$keyName] = $prefix;
+                       }
+
+                               // Table status (storage engine, collaction, etc.)
+                       if (is_array($tableStatus)) {
+                               $tableExtraFields = array(
+                                       'Engine' => 'ENGINE',
+                                       'Collation' => 'COLLATE',
+                               );
+
+                               foreach ($tableExtraFields as $mysqlKey=>$internalKey) {
+                                       if (isset($tableStatus[$mysqlKey])) {
+                                               $total[$tableName]['extra'][$internalKey] = $tableStatus[$mysqlKey];
+                                       }
+                               }
                        }
                }
 
-                       // Compile information:
-               if (is_array($tempKeys))        {
-                       foreach($tempKeys as $table => $keyInf) {
-                               foreach($keyInf as $kName => $index) {
+                       // Compile key information:
+               if (count($tempKeys)) {
+                       foreach ($tempKeys as $table => $keyInf) {
+                               foreach ($keyInf as $kName => $index) {
                                        ksort($index);
-                                       $total[$table]['keys'][$kName] = $tempKeysPrefix[$table][$kName].' ('.implode($index,',').')';
+                                       $total[$table]['keys'][$kName] = $tempKeysPrefix[$table][$kName].' ('.implode(',',$index).')';
                                }
                        }
                }
@@ -388,31 +542,44 @@ class t3lib_install {
         * Compares two arrays with field information and returns information about fields that are MISSING and fields that have CHANGED.
         * FDsrc and FDcomp can be switched if you want the list of stuff to remove rather than update.
         *
-        * @param       array           Field definitions, source (from getFieldDefinitions_sqlContent())
+        * @param       array           Field definitions, source (from getFieldDefinitions_fileContent())
         * @param       array           Field definitions, comparison. (from getFieldDefinitions_database())
         * @param       string          Table names (in list) which is the ONLY one observed.
-        * @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
+        * @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.
+        * @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
         */
-       function getDatabaseExtra($FDsrc, $FDcomp, $onlyTableList=''  {
+       function getDatabaseExtra($FDsrc, $FDcomp, $onlyTableList='',$ignoreNotNullWhenComparing=true)  {
                $extraArr = array();
                $diffArr = array();
 
-               if (is_array($FDsrc))   {
-                       foreach($FDsrc as $table => $info) {
-                               if (!strlen($onlyTableList) || t3lib_div::inList($onlyTableList, $table))       {
-                                       if (!isset($FDcomp[$table]))    {
+               if (is_array($FDsrc)) {
+                       foreach ($FDsrc as $table => $info) {
+                               if (!strlen($onlyTableList) || t3lib_div::inList($onlyTableList, $table)) {
+                                       if (!isset($FDcomp[$table])) {
                                                $extraArr[$table] = $info;              // If the table was not in the FDcomp-array, the result array is loaded with that table.
                                                $extraArr[$table]['whole_table']=1;
                                        } else {
-                                               $keyTypes = explode(',','fields,keys');
-                                               foreach($keyTypes as $theKey)   {
-                                                       if (is_array($info[$theKey]))   {
-                                                               foreach($info[$theKey] as $fieldN => $fieldC) {
-                                                                       if (!isset($FDcomp[$table][$theKey][$fieldN]))  {
+                                               $keyTypes = explode(',','extra,fields,keys');
+                                               foreach ($keyTypes as $theKey) {
+                                                       if (is_array($info[$theKey])) {
+                                                               foreach ($info[$theKey] as $fieldN => $fieldC) {
+                                                                       $fieldN = str_replace('`','',$fieldN);
+                                                                       if ($fieldN=='COLLATE') {
+                                                                               continue;       // TODO: collation support is currently disabled (needs more testing)
+                                                                       }
+
+                                                                       if (!isset($FDcomp[$table][$theKey][$fieldN])) {
                                                                                $extraArr[$table][$theKey][$fieldN] = $fieldC;
-                                                                       } elseif (strcmp($FDcomp[$table][$theKey][$fieldN], $fieldC)){
-                                                                               $diffArr[$table][$theKey][$fieldN] = $fieldC;
-                                                                               $diffArr_cur[$table][$theKey][$fieldN] = $FDcomp[$table][$theKey][$fieldN];
+                                                                       } else {
+                                                                               $fieldC = trim($fieldC);
+                                                                               if ($ignoreNotNullWhenComparing) {
+                                                                                       $fieldC = str_replace(' NOT NULL', '', $fieldC);
+                                                                                       $FDcomp[$table][$theKey][$fieldN] = str_replace(' NOT NULL', '', $FDcomp[$table][$theKey][$fieldN]);
+                                                                               }
+                                                                               if ($fieldC !== $FDcomp[$table][$theKey][$fieldN]) {
+                                                                                       $diffArr[$table][$theKey][$fieldN] = $fieldC;
+                                                                                       $diffArr_cur[$table][$theKey][$fieldN] = $FDcomp[$table][$theKey][$fieldN];
+                                                                               }
                                                                        }
                                                                }
                                                        }
@@ -447,18 +614,18 @@ class t3lib_install {
                        $keyList = 'extra';
                }
                $keyList = explode(',',$keyList);
-               foreach($keyList as $theKey)    {
-                       if (is_array($diffArr[$theKey]))        {
-                               foreach($diffArr[$theKey] as $table => $info) {
+               foreach ($keyList as $theKey) {
+                       if (is_array($diffArr[$theKey])) {
+                               foreach ($diffArr[$theKey] as $table => $info) {
                                        $whole_table = array();
-                                       if (is_array($info['fields']))  {
-                                               foreach($info['fields'] as $fN => $fV) {
-                                                       if ($info['whole_table'])       {
+                                       if (is_array($info['fields'])) {
+                                               foreach ($info['fields'] as $fN => $fV) {
+                                                       if ($info['whole_table']) {
                                                                $whole_table[]=$fN.' '.$fV;
                                                        } else {
-                                                               if ($theKey=='extra')   {
-                                                                       if ($remove)    {
-                                                                               if (substr($fN,0,strlen($deletedPrefixKey))!=$deletedPrefixKey) {
+                                                               if ($theKey=='extra') {
+                                                                       if ($remove) {
+                                                                               if (substr($fN,0,strlen($deletedPrefixKey))!=$deletedPrefixKey) {
                                                                                        $statement = 'ALTER TABLE '.$table.' CHANGE '.$fN.' '.$deletedPrefixKey.$fN.' '.$fV.';';
                                                                                        $statements['change'][md5($statement)] = $statement;
                                                                                } else {
@@ -477,17 +644,13 @@ class t3lib_install {
                                                        }
                                                }
                                        }
-                                       if (is_array($info['keys']))    {
-                                               foreach($info['keys'] as $fN => $fV) {
-                                                       if ($info['whole_table'])       {
-                                                               if ($fN=='PRIMARY')     {
-                                                                       $whole_table[] = $fV;
-                                                               } else {
-                                                                       $whole_table[] = $fV;
-                                                               }
+                                       if (is_array($info['keys'])) {
+                                               foreach ($info['keys'] as $fN => $fV) {
+                                                       if ($info['whole_table']) {
+                                                               $whole_table[] = $fV;
                                                        } else {
-                                                               if ($theKey=='extra')   {
-                                                                       if ($remove)    {
+                                                               if ($theKey=='extra') {
+                                                                       if ($remove) {
                                                                                $statement = 'ALTER TABLE '.$table.($fN=='PRIMARY' ? ' DROP PRIMARY KEY' : ' DROP KEY '.$fN).';';
                                                                                $statements['drop'][md5($statement)] = $statement;
                                                                        } else {
@@ -503,22 +666,47 @@ class t3lib_install {
                                                        }
                                                }
                                        }
-                                       if ($info['whole_table'])       {
-                                               if ($remove)    {
-                                                       if (substr($table,0,strlen($deletedPrefixKey))!=$deletedPrefixKey)      {
+                                       if (is_array($info['extra'])) {
+                                               $extras = array();
+                                               $extras_currentValue = array();
+                                               foreach ($info['extra'] as $fN => $fV) {
+
+                                                               // Only consider statements which are missing in the database but don't remove existing properties
+                                                       if (!$remove) {
+                                                               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']
+                                                                       $extras[] = $fN.'='.$fV;
+                                                                       $extras_currentValue[] = $fN.'='.$diffArr['diff_currentValues'][$table]['extra'][$fN];
+                                                               }
+                                                       }
+                                               }
+                                               if (count($extras)) {
+                                                       $statement = 'ALTER TABLE '.$table.' '.implode(' ',$extras).';';
+                                                       $statements['change'][md5($statement)] = $statement;
+                                                       $statements['change_currentValue'][md5($statement)] = implode(' ',$extras_currentValue);
+                                               }
+                                       }
+                                       if ($info['whole_table']) {
+                                               if ($remove) {
+                                                       if (substr($table,0,strlen($deletedPrefixKey))!=$deletedPrefixKey) {
                                                                $statement = 'ALTER TABLE '.$table.' RENAME '.$deletedPrefixKey.$table.';';
-                                                               $statements['change_table'][md5($statement)]=$statement;
+                                                               $statements['change_table'][md5($statement)] = $statement;
                                                        } else {
                                                                $statement = 'DROP TABLE '.$table.';';
-                                                               $statements['drop_table'][md5($statement)]=$statement;
+                                                               $statements['drop_table'][md5($statement)] = $statement;
                                                        }
-                                                       // count:
+                                                               // count:
                                                        $res = $GLOBALS['TYPO3_DB']->exec_SELECTquery('count(*)', $table, '');
                                                        list($count) = $GLOBALS['TYPO3_DB']->sql_fetch_row($res);
                                                        $statements['tables_count'][md5($statement)] = $count?'Records in table: '.$count:'';
                                                } else {
-                                                       $statement = 'CREATE TABLE '.$table." (\n".implode($whole_table,",\n")."\n) TYPE=MyISAM;";
-                                                       $statements['create_table'][md5($statement)]=$statement;
+                                                       $statement = 'CREATE TABLE '.$table." (\n".implode(",\n",$whole_table)."\n)";
+                                                       if ($info['extra']) {
+                                                               foreach ($info['extra'] as $k=>$v) {
+                                                                       $statement.= ' '.$k.'='.$v;     // Add extra attributes like ENGINE, CHARSET, etc.
+                                                               }
+                                                       }
+                                                       $statement.= ';';
+                                                       $statements['create_table'][md5($statement)] = $statement;
                                                }
                                        }
                                }
@@ -531,68 +719,68 @@ class t3lib_install {
        /**
         * Converts a result row with field information into the SQL field definition string
         *
-        * @param       array           MySQL result row.
+        * @param       array           MySQL result row
         * @return      string          Field definition
         */
        function assembleFieldDefinition($row)  {
-               $field[] = $row['Type'];
-               if ($this->mysqlVersion=='3.23' && !$row['Null'])       {
+               $field = array($row['Type']);
+
+               if ($row['Null']=='NO') {
                        $field[] = 'NOT NULL';
                }
-               if (!strstr($row['Type'],'blob') && !strstr($row['Type'],'text'))       {
-                       // 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.
-                       // 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.
-                       if ($this->mysqlVersion!='3.23' || !stristr($row['Extra'],'auto_increment'))    {
-                               $field[] = ($this->mysqlVersion=='3.23'?'default':'DEFAULT')." '".(stristr($row['Extra'],'auto_increment')?intval($row['Default']):addslashes($row['Default']))."'";
+               if (!strstr($row['Type'],'blob') && !strstr($row['Type'],'text')) {
+                               // Add a default value if the field is not auto-incremented (these fields never have a default definition)
+                       if (!stristr($row['Extra'],'auto_increment')) {
+                               $field[] = 'default \''.addslashes($row['Default']).'\'';
                        }
                }
-               if ($this->mysqlVersion!='3.23' && !$row['Null'])       {
-                       $field[] = 'NOT NULL';
-               }
-               if ($row['Extra'])      {
+               if ($row['Extra']) {
                        $field[] = $row['Extra'];
                }
-               return implode($field,' ');
+
+               return implode(' ',$field);
        }
 
        /**
-        * Returns an array where every entry is a single sql-statement. Input must be formatted like an ordinary MySQL-dump files
+        * Returns an array where every entry is a single SQL-statement. Input must be formatted like an ordinary MySQL-dump files.
         *
-        * @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.
-        * @param       boolean         If set, non-sql (like comments and blank lines) are not included in the final product)
-        * @param       string          Regex to filter SQL lines to include.
-        * @return      array           Array of SQL statements.
+        * @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.
+        * @param       boolean         If set, non-SQL content (like comments and blank lines) is not included in the final output
+        * @param       string          Regex to filter SQL lines to include
+        * @return      array           Array of SQL statements
         */
        function getStatementArray($sqlcode,$removeNonSQL=0,$query_regex='')    {
-               $sqlcodeArr = explode(chr(10),$sqlcode);
+               $sqlcodeArr = explode(chr(10), $sqlcode);
 
-               // Based on the assumption that the sql-dump has
+                       // Based on the assumption that the sql-dump has
                $statementArray = array();
                $statementArrayPointer = 0;
 
-               foreach($sqlcodeArr as $line => $linecontent)   {
+               foreach ($sqlcodeArr as $line => $lineContent) {
+                       $is_set = 0;
 
-                               // Setting MySQL Version if necessary.
-                       if (substr(trim($linecontent),0,16)=='# Server version')        {
-                               $this->mysqlVersion = doubleval(substr(trim($linecontent),16));
+                               // auto_increment fields cannot have a default value!
+                       if (stristr($lineContent,'auto_increment')) {
+                               $lineContent = preg_replace('/ default \'0\'/i', '', $lineContent);
                        }
-                       $is_set = 0;
 
-                       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
-                               $statementArray[$statementArrayPointer].= $linecontent;
+                       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
+                               $statementArray[$statementArrayPointer].= $lineContent;
                                $is_set = 1;
                        }
-                       if (substr(trim($linecontent),-1)==';') {
-                               if (isset($statementArray[$statementArrayPointer]))     {
-                                       if (!trim($statementArray[$statementArrayPointer]) || ($query_regex && !eregi($query_regex,trim($statementArray[$statementArrayPointer]))))     {
+                       if (substr(trim($lineContent),-1)==';') {
+                               if (isset($statementArray[$statementArrayPointer])) {
+                                       if (!trim($statementArray[$statementArrayPointer]) || ($query_regex && !eregi($query_regex,trim($statementArray[$statementArrayPointer])))) {
                                                unset($statementArray[$statementArrayPointer]);
                                        }
                                }
                                $statementArrayPointer++;
+
                        } elseif ($is_set) {
-                               $statementArray[$statementArrayPointer].=chr(10);
+                               $statementArray[$statementArrayPointer].= chr(10);
                        }
                }
+
                return $statementArray;
        }
 
@@ -603,20 +791,33 @@ class t3lib_install {
         * @param       boolean         If set, will count number of INSERT INTO statements following that table definition
         * @return      array           Array with table definitions in index 0 and count in index 1
         */
-       function getCreateTables($statements, $insertCountFlag=0)       {
+       function getCreateTables($statements, $insertCountFlag=0) {
                $crTables = array();
-               foreach($statements as $line => $linecontent)   {
-                       if (eregi('^create[[:space:]]*table[[:space:]]*([[:alnum:]_]*)',substr($linecontent,0,100),$reg))       {
+               $insertCount = array();
+               foreach ($statements as $line => $lineContent) {
+                       $reg = array();
+                       if (eregi('^create[[:space:]]*table[[:space:]]*[`]?([[:alnum:]_]*)[`]?',substr($lineContent,0,100),$reg)) {
                                $table = trim($reg[1]);
                                if ($table)     {
-                                       if (TYPO3_OS=='WIN')    {$table=strtolower($table);}    // tablenames are always lowercase on windows!
-                                       $crTables[$table] = $linecontent;
+                                               // table names are always lowercase on Windows!
+                                       if (TYPO3_OS == 'WIN') {
+                                               $table = strtolower($table);
+                                       }
+                                       $sqlLines = explode(chr(10), $lineContent);
+                                       foreach ($sqlLines as $k=>$v) {
+                                               if (stristr($v,'auto_increment')) {
+                                                       $sqlLines[$k] = preg_replace('/ default \'0\'/i', '', $v);
+                                               }
+                                       }
+                                       $lineContent = implode(chr(10), $sqlLines);
+                                       $crTables[$table] = $lineContent;
                                }
-                       } elseif ($insertCountFlag && eregi('^insert[[:space:]]*into[[:space:]]*([[:alnum:]_]*)',substr($linecontent,0,100),$reg))      {
+                       } elseif ($insertCountFlag && eregi('^insert[[:space:]]*into[[:space:]]*[`]?([[:alnum:]_]*)[`]?',substr($lineContent,0,100),$reg)) {
                                $nTable = trim($reg[1]);
                                $insertCount[$nTable]++;
                        }
                }
+
                return array($crTables,$insertCount);
        }
 
@@ -629,11 +830,12 @@ class t3lib_install {
         */
        function getTableInsertStatements($statements, $table)  {
                $outStatements=array();
-               foreach($statements as $line => $linecontent)   {
-                       if (eregi('^insert[[:space:]]*into[[:space:]]*([[:alnum:]_]*)',substr($linecontent,0,100),$reg))        {
+               foreach($statements as $line => $lineContent) {
+                       $reg = array();
+                       if (preg_match('/^insert[[:space:]]*into[[:space:]]*[`]?([[:alnum:]_]*)[`]?/i',substr($lineContent,0,100),$reg)) {
                                $nTable = trim($reg[1]);
                                if ($nTable && !strcmp($table,$nTable)) {
-                                       $outStatements[]=$linecontent;
+                                       $outStatements[] = $lineContent;
                                }
                        }
                }
@@ -645,16 +847,27 @@ class t3lib_install {
         *
         * @param       array           Array of SQL queries to execute.
         * @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)
-        * @return      void
+        * @return      mixed           Array with error message from database if any occured. Otherwise true if everything was executed successfully.
         */
        function performUpdateQueries($arr,$keyArr)     {
+               $result = array();
                if (is_array($arr))     {
                        foreach($arr as $key => $string)        {
                                if (isset($keyArr[$key]) && $keyArr[$key])      {
-                                       $GLOBALS['TYPO3_DB']->admin_query($string);
+                                       $res = $GLOBALS['TYPO3_DB']->admin_query($string);
+                                       if ($res === false)     {
+                                               $result[$key] = $GLOBALS['TYPO3_DB']->sql_error();
+                                       } elseif (is_resource($res)) {
+                                               $GLOBALS['TYPO3_DB']->sql_free_result($res);
+                                       }
                                }
                        }
                }
+               if (count($result) > 0) {
+                       return $result;
+               } else {
+                       return true;
+               }
        }
 
        /**
@@ -665,7 +878,7 @@ class t3lib_install {
         */
        function getListOfTables()      {
                $whichTables = $GLOBALS['TYPO3_DB']->admin_get_tables(TYPO3_db);
-               return $whichTables;
+               return array_keys($whichTables);
        }
 
        /**
@@ -686,19 +899,19 @@ class t3lib_install {
                                $ico = '';
                                if ($iconDis)   {
                                        if (stristr($string,' user_'))  {
-                                               $ico.= '<img src="'.$this->backPath.'t3lib/gfx/icon_warning.gif" width="18" height="16" align="top" alt="" /><strong>(USER) </strong>';
+                                               $ico.= '<img src="'.$this->backPath.'gfx/icon_warning.gif" width="18" height="16" align="top" alt="" /><strong>(USER) </strong>';
                                        }
                                        if (stristr($string,' app_'))   {
-                                               $ico.= '<img src="'.$this->backPath.'t3lib/gfx/icon_warning.gif" width="18" height="16" align="top" alt="" /><strong>(APP) </strong>';
+                                               $ico.= '<img src="'.$this->backPath.'gfx/icon_warning.gif" width="18" height="16" align="top" alt="" /><strong>(APP) </strong>';
                                        }
                                        if (stristr($string,' ttx_') || stristr($string,' tx_'))        {
-                                               $ico.= '<img src="'.$this->backPath.'t3lib/gfx/icon_warning.gif" width="18" height="16" align="top" alt="" /><strong>(EXT) </strong>';
+                                               $ico.= '<img src="'.$this->backPath.'gfx/icon_warning.gif" width="18" height="16" align="top" alt="" /><strong>(EXT) </strong>';
                                        }
                                }
                                $out[]='
                                        <tr>
-                                               <td valign="top"><input type="checkbox" name="'.$this->dbUpdateCheckboxPrefix.'['.$key.']" value="1"'.($checked?' checked="checked"':'').' /></td>
-                                               <td nowrap="nowrap">'.nl2br($ico.htmlspecialchars($string)).'</td>
+                                               <td valign="top"><input type="checkbox" id="db-'.$key.'" name="'.$this->dbUpdateCheckboxPrefix.'['.$key.']" value="1"'.($checked?' checked="checked"':'').' /></td>
+                                               <td nowrap="nowrap"><label for="db-'.$key.'">'.nl2br($ico.htmlspecialchars($string)).'</label></td>
                                        </tr>';
                                if (isset($currentValue[$key])) {
                                        $out[]='
@@ -719,6 +932,17 @@ class t3lib_install {
 
                return $content;
        }
+
+       /**
+        * Reads the field definitions for the input SQL-file string
+        *
+        * @param       string          Should be a string read from an SQL-file made with 'mysqldump [database_name] -d'
+        * @return      array           Array with information about table.
+        * @deprecated  since TYPO3 4.2 Use ->getFieldDefinitions_fileContent() instead!
+        */
+       function getFieldDefinitions_sqlContent($fileContent)   {
+               return $this->getFieldDefinitions_fileContent($fileContent);
+       }
 }
 
 if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['t3lib/class.t3lib_install.php'])  {