[TASK][Install] Move sql handling to own class
authorChristian Kuhn <lolli@schwarzbu.ch>
Fri, 8 Jul 2011 17:03:20 +0000 (19:03 +0200)
committerChristian Kuhn <lolli@schwarzbu.ch>
Sat, 9 Jul 2011 09:55:29 +0000 (11:55 +0200)
Decouple sql parsing, compare and update code from
t3lib_install to own t3lib_install_Sql class.

Change-Id: I60b8342f8c0041dddb64da7f48b0aec544dfbe32
Resolves: #27998
Reviewed-on: http://review.typo3.org/3156
Reviewed-by: Susanne Moog
Tested-by: Susanne Moog
Reviewed-by: Georg Ringer
Tested-by: Georg Ringer
Reviewed-by: Christian Kuhn
Tested-by: Christian Kuhn
t3lib/class.t3lib_install.php
t3lib/class.t3lib_install_sql.php [new file with mode: 0644]
t3lib/core_autoload.php
typo3/sysext/em/classes/database/class.tx_em_database.php
typo3/sysext/em/classes/install/class.tx_em_install.php
typo3/sysext/install/mod/class.tx_install.php

index 4baa376..eecb52f 100644 (file)
@@ -43,14 +43,6 @@ 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 array Tables starting with this name are ignored during compare
-        */
-       var $ignoreTablePrefixes = array(
-               'cachingframework_',
-       );
 
        var $dbUpdateCheckboxPrefix = 'TYPO3_INSTALL[database_update]'; // Prefix for checkbox fields when updating database.
        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.
@@ -58,24 +50,21 @@ class t3lib_install {
        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.
        var $messages = array(); // Used to set (error)messages from the executing functions like mail-sending, writing Localconf and such
        var $touchedLine = 0; // updated with line in localconf.php file that was changed.
 
+       /**
+        * @var t3lib_install_Sql Instance of SQL handler
+        */
+       protected $sqlHandler = NULL;
 
        /**
         * Constructor function
-        *
-        * @return      void
         */
-       function __construct() {
-               if ($GLOBALS['TYPO3_CONF_VARS']['SYS']['multiplyDBfieldSize'] >= 1 && $GLOBALS['TYPO3_CONF_VARS']['SYS']['multiplyDBfieldSize'] <= 5) {
-                       $this->multiplySize = (double) $GLOBALS['TYPO3_CONF_VARS']['SYS']['multiplyDBfieldSize'];
-               }
+       public function __construct() {
+               $this->sqlHandler = t3lib_div::makeInstance('t3lib_install_Sql');
        }
 
 
@@ -267,9 +256,9 @@ 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)
+        * @param mixed $inlines Array of lines to write back to localconf.php. Possibly
+        * @param string $absFullPath 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 = '', $absFullPath = '') {
@@ -329,8 +318,8 @@ class t3lib_install {
        /**
         * Writes lines to localconf.php.
         *
-        * @param array Array of lines to write back to localconf.php
-        * @param string Absolute path of alternative file to use (Notice: this path is not validated in terms of being inside 'TYPO3 space')
+        * @param array $lines Array of lines to write back to localconf.php
+        * @param string $absFullPath Absolute path of alternative file to use (Notice: this path is not validated in terms of being inside 'TYPO3 space')
         * @return boolean TRUE if method succeeded, otherwise FALSE
         */
        public function writeToLocalconf(array $lines, $absFullPath = '') {
@@ -409,8 +398,8 @@ class t3lib_install {
        /**
         * Replaces ' with \' and \ with \\
         *
-        * @param       string          Input value
-        * @return      string          Output value
+        * @param string $value Input value
+        * @return string Output value
         * @see setValueInLocalconfFile()
         */
        function slashValueForSingleDashes($value) {
@@ -431,642 +420,145 @@ class t3lib_install {
        /**
         * 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.
+        * @param $fileContent 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.6, will be removed in 4.8, use method from t3lib_install_Sql instead
         */
        function getFieldDefinitions_fileContent($fileContent) {
-               $lines = t3lib_div::trimExplode(LF, $fileContent, 1);
-               $table = '';
-               $total = array();
-
-               foreach ($lines as $value) {
-                       if (substr($value, 0, 1) == '#') {
-                               continue; // Ignore comments
-                       }
-
-                       if (!strlen($table)) {
-                               $parts = t3lib_div::trimExplode(' ', $value, TRUE);
-                               if (strtoupper($parts[0]) === 'CREATE' && strtoupper($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) == ';') {
-                                       $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 {
-                                                       // 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 {
-                                                       $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;
-
-                                                       // This is a protection against doing something stupid: Only allow clearing of cache_* and index_* tables.
-                                               if (preg_match('/^(cache|index)_/', $table)) {
-                                                               // Suggest to truncate (clear) this table
-                                                       $total[$table]['extra']['CLEAR'] = 1;
-                                               }
-                                       }
-                               }
-                       }
-               }
-
-               $this->getFieldDefinitions_sqlContent_parseTypes($total);
-               return $total;
+               t3lib_div::logDeprecatedFunction();
+               return $this->sqlHandler->getFieldDefinitions_fileContent($fileContent);
        }
 
        /**
         * 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_fileContent())
+        * @param array $total Total array (from getFieldDefinitions_fileContent())
         * @return      void
         * @access private
         * @see getFieldDefinitions_fileContent()
+        * @deprecated Since TYPO3 4.6, will be removed in 4.8
         */
        function getFieldDefinitions_sqlContent_parseTypes(&$total) {
-
-               $mSize = (double) $this->multiplySize;
-               if ($mSize > 1) {
-
-                               // Init SQL parser:
-                       $sqlParser = t3lib_div::makeInstance('t3lib_sqlparser');
-                       foreach ($total as $table => $cfg) {
-                               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'
-                                                                                       )
-                                                                               )
-                                                                       );
-                                                                               // 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] . '(' . implode(',', $keys) . ')' . $match[3];
-                                                                               }
-                                                                       }
-                                                               }
-                                                       break;
-                                                       case 'tinytext':
-                                                               $fInfo['fieldType'] = 'text';
-                                                       break;
-                                               }
-
-                                               $total[$table]['fields'][$fN] = $sqlParser->compileFieldCfg($fInfo);
-                                               if ($sqlParser->parse_error) {
-                                                       throw new RuntimeException(
-                                                               'TYPO3 Fatal Error: ' . $sqlParser->parse_error,
-                                                               1270853961
-                                                       );
-                                               }
-                                       }
-                               }
-                       }
-               }
+                       // This method is protected in t3lib_install_Sql
+               t3lib_div::logDeprecatedFunction();
        }
 
        /**
         * Look up the default collation for specified character set based on "SHOW CHARACTER SET" output
         *
-        * @param       string          Character set
-        * @return      string          Corresponding default collation
+        * @param string $charset Character set
+        * @return string Corresponding default collation
+        * @deprecated Since TYPO3 4.6, will be removed in 4.8, use method from t3lib_install_Sql instead
         */
        function getCollationForCharset($charset) {
-                       // Load character sets, if not cached already
-               if (!count($this->character_sets)) {
-                       if (method_exists($GLOBALS['TYPO3_DB'], 'admin_get_charsets')) {
-                               $this->character_sets = $GLOBALS['TYPO3_DB']->admin_get_charsets();
-                       } else {
-                               $this->character_sets[$charset] = array(); // Add empty element to avoid that the check will be repeated
-                       }
-               }
-
-               $collation = '';
-               if (isset($this->character_sets[$charset]['Default collation'])) {
-                       $collation = $this->character_sets[$charset]['Default collation'];
-               }
-
-               return $collation;
+               t3lib_div::logDeprecatedFunction();
+               return $this->sqlHandler->getCollationForCharset($charset);
        }
 
        /**
         * Reads the field definitions for the current database
         *
-        * @return      array           Array with information about table.
+        * @return      array Array with information about table.
+        * @deprecated Since TYPO3 4.6, will be removed in 4.8, use method from t3lib_install_Sql instead
         */
        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 => $tableStatus) {
-
-                               // Fields:
-                       $fieldInformation = $GLOBALS['TYPO3_DB']->admin_get_fields($tableName);
-                       foreach ($fieldInformation as $fN => $fieldRow) {
-                               $total[$tableName]['fields'][$fN] = $this->assembleFieldDefinition($fieldRow);
-                       }
-
-                               // Keys:
-                       $keyInformation = $GLOBALS['TYPO3_DB']->admin_get_keys($tableName);
-
-                       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 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) . ')';
-                               }
-                       }
-               }
-
-               return $total;
+               t3lib_div::logDeprecatedFunction();
+               return $this->sqlHandler->getFieldDefinitions_database();
        }
 
        /**
         * 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_fileContent())
-        * @param       array           Field definitions, comparison. (from getFieldDefinitions_database())
-        * @param       string          Table names (in list) which is the ONLY one observed.
-        * @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
+        * @param array $FDsrc Field definitions, source (from getFieldDefinitions_fileContent())
+        * @param array $FDcomp Field definitions, comparison. (from getFieldDefinitions_database())
+        * @param string $onlyTableList Table names (in list) which is the ONLY one observed.
+        * @param boolean $ignoreNotNullWhenComparing 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
+        * @deprecated Since TYPO3 4.6, will be removed in 4.8, use method from t3lib_install_Sql instead
         */
        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])) {
-                                               $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(',', '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;
-                                                                       } 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];
-                                                                               }
-                                                                       }
-                                                               }
-                                                       }
-                                               }
-                                       }
-                               }
-                       }
-               }
-
-               $output = array(
-                       'extra' => $extraArr,
-                       'diff' => $diffArr,
-                       'diff_currentValues' => $diffArr_cur
-               );
-
-               return $output;
+               t3lib_div::logDeprecatedFunction();
+               return $this->sqlHandler->getDatabaseExtra($FDsrc, $FDcomp, $onlyTableList, $ignoreNotNullWhenComparing);
        }
 
        /**
         * Returns an array with SQL-statements that is needed to update according to the diff-array
         *
-        * @param       array           Array with differences of current and needed DB settings. (from getDatabaseExtra())
-        * @param       string          List of fields in diff array to take notice of.
-        * @return      array           Array of SQL statements (organized in keys depending on type)
+        * @param array $diffArr Array with differences of current and needed DB settings. (from getDatabaseExtra())
+        * @param string $keyList List of fields in diff array to take notice of.
+        * @return array Array of SQL statements (organized in keys depending on type)
+        * @deprecated Since TYPO3 4.6, will be removed in 4.8, use method from t3lib_install_Sql instead
         */
        function getUpdateSuggestions($diffArr, $keyList = 'extra,diff') {
-               $statements = array();
-               $deletedPrefixKey = $this->deletedPrefixKey;
-               $remove = 0;
-               if ($keyList == 'remove') {
-                       $remove = 1;
-                       $keyList = 'extra';
-               }
-               $keyList = explode(',', $keyList);
-               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']) {
-                                                               $whole_table[] = $fN . ' ' . $fV;
-                                                       } else {
-                                                                       // Special case to work around MySQL problems when adding auto_increment fields:
-                                                               if (stristr($fV, 'auto_increment')) {
-                                                                               // The field can only be set "auto_increment" if there exists a PRIMARY key of that field already.
-                                                                               // The check does not look up which field is primary but just assumes it must be the field with the auto_increment value...
-                                                                       if (isset($diffArr['extra'][$table]['keys']['PRIMARY'])) {
-                                                                                       // Remove "auto_increment" from the statement - it will be suggested in a 2nd step after the primary key was created
-                                                                               $fV = str_replace(' auto_increment', '', $fV);
-                                                                       } else {
-                                                                                       // In the next step, attempt to clear the table once again (2 = force)
-                                                                               $info['extra']['CLEAR'] = 2;
-                                                                       }
-                                                               }
-                                                               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 {
-                                                                                       $statement = 'ALTER TABLE ' . $table . ' DROP ' . $fN . ';';
-                                                                                       $statements['drop'][md5($statement)] = $statement;
-                                                                               }
-                                                                       } else {
-                                                                               $statement = 'ALTER TABLE ' . $table . ' ADD ' . $fN . ' ' . $fV . ';';
-                                                                               $statements['add'][md5($statement)] = $statement;
-                                                                       }
-                                                               } elseif ($theKey == 'diff') {
-                                                                       $statement = 'ALTER TABLE ' . $table . ' CHANGE ' . $fN . ' ' . $fN . ' ' . $fV . ';';
-                                                                       $statements['change'][md5($statement)] = $statement;
-                                                                       $statements['change_currentValue'][md5($statement)] = $diffArr['diff_currentValues'][$table]['fields'][$fN];
-                                                               }
-                                                       }
-                                               }
-                                       }
-                                       if (is_array($info['keys'])) {
-                                               foreach ($info['keys'] as $fN => $fV) {
-                                                       if ($info['whole_table']) {
-                                                               $whole_table[] = $fV;
-                                                       } else {
-                                                               if ($theKey == 'extra') {
-                                                                       if ($remove) {
-                                                                               $statement = 'ALTER TABLE ' . $table . ($fN == 'PRIMARY' ? ' DROP PRIMARY KEY' : ' DROP KEY ' . $fN) . ';';
-                                                                               $statements['drop'][md5($statement)] = $statement;
-                                                                       } else {
-                                                                               $statement = 'ALTER TABLE ' . $table . ' ADD ' . $fV . ';';
-                                                                               $statements['add'][md5($statement)] = $statement;
-                                                                       }
-                                                               } elseif ($theKey == 'diff') {
-                                                                       $statement = 'ALTER TABLE ' . $table . ($fN == 'PRIMARY' ? ' DROP PRIMARY KEY' : ' DROP KEY ' . $fN) . ';';
-                                                                       $statements['change'][md5($statement)] = $statement;
-                                                                       $statement = 'ALTER TABLE ' . $table . ' ADD ' . $fV . ';';
-                                                                       $statements['change'][md5($statement)] = $statement;
-                                                               }
-                                                       }
-                                               }
-                                       }
-                                       if (is_array($info['extra'])) {
-                                               $extras = array();
-                                               $extras_currentValue = array();
-                                               $clear_table = FALSE;
-
-                                               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']
-                                                                       if ($fN == 'CLEAR') {
-                                                                                       // Truncate table must happen later, not now
-                                                                                       // Valid values for CLEAR: 1=only clear if keys are missing, 2=clear anyway (force)
-                                                                               if (count($info['keys']) || $fV == 2) {
-                                                                                       $clear_table = TRUE;
-                                                                               }
-                                                                               continue;
-                                                                       } else {
-                                                                               $extras[] = $fN . '=' . $fV;
-                                                                               $extras_currentValue[] = $fN . '=' . $diffArr['diff_currentValues'][$table]['extra'][$fN];
-                                                                       }
-                                                               }
-                                                       }
-                                               }
-                                               if ($clear_table) {
-                                                       $statement = 'TRUNCATE TABLE ' . $table . ';';
-                                                       $statements['clear_table'][md5($statement)] = $statement;
-                                               }
-                                               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) {
-                                                                       // Ignore table operation if the table name starts with a
-                                                                       // prefix that should be ignored
-                                                               $ignoreTable = FALSE;
-                                                               foreach ($this->ignoreTablePrefixes as $ignorePrefix) {
-                                                                       if (substr($table, 0, strlen($ignorePrefix)) === $ignorePrefix) {
-                                                                               $ignoreTable = TRUE;
-                                                                               break;
-                                                                       }
-                                                               }
-                                                               if (!$ignoreTable) {
-                                                                       $statement = 'ALTER TABLE ' . $table . ' RENAME ' . $deletedPrefixKey . $table . ';';
-                                                                       $statements['change_table'][md5($statement)] = $statement;
-                                                               }
-                                                       } else {
-                                                               $statement = 'DROP TABLE ' . $table . ';';
-                                                               $statements['drop_table'][md5($statement)] = $statement;
-                                                       }
-                                                               // count:
-                                                       $count = $GLOBALS['TYPO3_DB']->exec_SELECTcountRows('*', $table);
-                                                       $statements['tables_count'][md5($statement)] = $count ? 'Records in table: ' . $count : '';
-                                               } else {
-                                                       $statement = 'CREATE TABLE ' . $table . " (\n" . implode(",\n", $whole_table) . "\n)";
-                                                       if ($info['extra']) {
-                                                               foreach ($info['extra'] as $k => $v) {
-                                                                       if ($k == 'COLLATE' || $k == 'CLEAR') {
-                                                                               continue; // Skip these special statements. TODO: collation support is currently disabled (needs more testing)
-                                                                       }
-                                                                       $statement .= ' ' . $k . '=' . $v; // Add extra attributes like ENGINE, CHARSET, etc.
-                                                               }
-                                                       }
-                                                       $statement .= ';';
-                                                       $statements['create_table'][md5($statement)] = $statement;
-                                               }
-                                       }
-                               }
-                       }
-               }
-
-               return $statements;
+               t3lib_div::logDeprecatedFunction();
+               return $this->sqlHandler->getUpdateSuggestions($diffArr, $keyList);
        }
 
        /**
         * Converts a result row with field information into the SQL field definition string
         *
-        * @param       array           MySQL result row
-        * @return      string          Field definition
+        * @param array $row MySQL result row
+        * @return string Field definition
+        * @deprecated Since TYPO3 4.6, will be removed in 4.8, use method from t3lib_install_Sql instead
         */
        function assembleFieldDefinition($row) {
-               $field = array($row['Type']);
-
-               if ($row['Null'] == 'NO') {
-                       $field[] = 'NOT NULL';
-               }
-               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 ($row['Extra']) {
-                       $field[] = $row['Extra'];
-               }
-
-               return implode(' ', $field);
+               t3lib_div::logDeprecatedFunction();
+               return $this->sqlHandler->assembleFieldDefinition($row);
        }
 
        /**
         * Returns an array where every entry is a single SQL-statement. Input must be formatted like an ordinary MySQL-dump files.
         *
-        * @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
+        * @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 $removeNonSQL If set, non-SQL content (like comments and blank lines) is not included in the final output
+        * @param string $query_regex Regex to filter SQL lines to include
+        * @return array Array of SQL statements
+        * @deprecated Since TYPO3 4.6, will be removed in 4.8, use method from t3lib_install_Sql instead
         */
-       function getStatementArray($sqlcode, $removeNonSQL = 0, $query_regex = '') {
-               $sqlcodeArr = explode(LF, $sqlcode);
-
-                       // Based on the assumption that the sql-dump has
-               $statementArray = array();
-               $statementArrayPointer = 0;
-
-               foreach ($sqlcodeArr as $line => $lineContent) {
-                       $is_set = 0;
-
-                               // auto_increment fields cannot have a default value!
-                       if (stristr($lineContent, 'auto_increment')) {
-                               $lineContent = preg_replace('/ default \'0\'/i', '', $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 && !preg_match('/' . $query_regex . '/i', trim($statementArray[$statementArrayPointer])))) {
-                                               unset($statementArray[$statementArrayPointer]);
-                                       }
-                               }
-                               $statementArrayPointer++;
-
-                       } elseif ($is_set) {
-                               $statementArray[$statementArrayPointer] .= LF;
-                       }
-               }
-
-               return $statementArray;
+       function getStatementArray($sqlcode, $removeNonSQL = FALSE, $query_regex = '') {
+               t3lib_div::logDeprecatedFunction();
+               return $this->sqlHandler->getStatementArray($sqlcode, $removeNonSQL, $query_regex);
        }
 
        /**
         * Returns tables to create and how many records in each
         *
-        * @param       array           Array of SQL statements to analyse.
-        * @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
+        * @param array $statements Array of SQL statements to analyse.
+        * @param boolean $insertCountFlag 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
+        * @deprecated Since TYPO3 4.6, will be removed in 4.8, use method from t3lib_install_Sql instead
         */
-       function getCreateTables($statements, $insertCountFlag = 0) {
-               $crTables = array();
-               $insertCount = array();
-               foreach ($statements as $line => $lineContent) {
-                       $reg = array();
-                       if (preg_match('/^create[[:space:]]*table[[:space:]]*[`]?([[:alnum:]_]*)[`]?/i', substr($lineContent, 0, 100), $reg)) {
-                               $table = trim($reg[1]);
-                               if ($table) {
-                                               // table names are always lowercase on Windows!
-                                       if (TYPO3_OS == 'WIN') {
-                                               $table = strtolower($table);
-                                       }
-                                       $sqlLines = explode(LF, $lineContent);
-                                       foreach ($sqlLines as $k => $v) {
-                                               if (stristr($v, 'auto_increment')) {
-                                                       $sqlLines[$k] = preg_replace('/ default \'0\'/i', '', $v);
-                                               }
-                                       }
-                                       $lineContent = implode(LF, $sqlLines);
-                                       $crTables[$table] = $lineContent;
-                               }
-                       } elseif ($insertCountFlag && preg_match('/^insert[[:space:]]*into[[:space:]]*[`]?([[:alnum:]_]*)[`]?/i', substr($lineContent, 0, 100), $reg)) {
-                               $nTable = trim($reg[1]);
-                               $insertCount[$nTable]++;
-                       }
-               }
-
-               return array($crTables, $insertCount);
+       function getCreateTables($statements, $insertCountFlag = FALSE) {
+               t3lib_div::logDeprecatedFunction();
+               return $this->sqlHandler->getCreateTables($statements, $insertCountFlag);
        }
 
        /**
         * Extracts all insert statements from $statement array where content is inserted into $table
         *
-        * @param       array           Array of SQL statements
-        * @param       string          Table name
-        * @return      array           Array of INSERT INTO statements where table match $table
+        * @param array $statements Array of SQL statements
+        * @param string $table Table name
+        * @return array Array of INSERT INTO statements where table match $table
+        * @deprecated Since TYPO3 4.6, will be removed in 4.8, use method from t3lib_install_Sql instead
         */
        function getTableInsertStatements($statements, $table) {
-               $outStatements = array();
-               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;
-                               }
-                       }
-               }
-               return $outStatements;
+               t3lib_div::logDeprecatedFunction();
+               $this->sqlHandler->getTableInsertStatements($statements, $table);
        }
 
        /**
         * Performs the queries passed from the input array.
         *
-        * @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      mixed           Array with error message from database if any occured. Otherwise TRUE if everything was executed successfully.
+        * @param array $arr Array of SQL queries to execute.
+        * @param array $keyArr 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 mixed Array with error message from database if any occured. Otherwise TRUE if everything was executed successfully.
+        * @deprecated Since TYPO3 4.6, will be removed in 4.8, use method from t3lib_install_Sql instead
         */
        function performUpdateQueries($arr, $keyArr) {
-               $result = array();
-               if (is_array($arr)) {
-                       foreach ($arr as $key => $string) {
-                               if (isset($keyArr[$key]) && $keyArr[$key]) {
-                                       $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;
-               }
+               t3lib_div::logDeprecatedFunction();
+               $this->sqlHandler->performUpdateQueries($arr, $keyArr);
        }
 
        /**
@@ -1074,28 +566,25 @@ class t3lib_install {
         *
         * @return      array           List of tables.
         * @see t3lib_db::admin_get_tables()
+        * @deprecated Since TYPO3 4.6, will be removed in 4.8, use method from t3lib_install_Sql instead
         */
        function getListOfTables() {
-               $whichTables = $GLOBALS['TYPO3_DB']->admin_get_tables(TYPO3_db);
-               foreach ($whichTables as $key => &$value) {
-                       $value = $key;
-               }
-               unset($value);
-               return $whichTables;
+               t3lib_div::logDeprecatedFunction();
+               $this->sqlHandler->getListOfTables();
        }
 
        /**
         * Creates a table which checkboxes for updating database.
         *
-        * @param       array           Array of statements (key / value pairs where key is used for the checkboxes)
-        * @param       string          Label for the table.
-        * @param       boolean         If set, then checkboxes are set by default.
-        * @param       boolean         If set, then icons are shown.
-        * @param       array           Array of "current values" for each key/value pair in $arr. Shown if given.
-        * @param       boolean         If set, will show the prefix "Current value" if $currentValue is given.
-        * @return      string          HTML table with checkboxes for update. Must be wrapped in a form.
+        * @param array $arr Array of statements (key / value pairs where key is used for the checkboxes)
+        * @param string $label Label for the table.
+        * @param boolean $checked If set, then checkboxes are set by default.
+        * @param boolean $iconDis If set, then icons are shown.
+        * @param array $currentValue Array of "current values" for each key/value pair in $arr. Shown if given.
+        * @param boolean $cVfullMsg If set, will show the prefix "Current value" if $currentValue is given.
+        * @return string HTML table with checkboxes for update. Must be wrapped in a form.
         */
-       function generateUpdateDatabaseForm_checkboxes($arr, $label, $checked = 1, $iconDis = 0, $currentValue = array(), $cVfullMsg = 0) {
+       function generateUpdateDatabaseForm_checkboxes($arr, $label, $checked = TRUE, $iconDis = FALSE, $currentValue = array(), $cVfullMsg = FALSE) {
                $out = array();
                if (is_array($arr)) {
                        $tableId = uniqid('table');
diff --git a/t3lib/class.t3lib_install_sql.php b/t3lib/class.t3lib_install_sql.php
new file mode 100644 (file)
index 0000000..6c3247a
--- /dev/null
@@ -0,0 +1,748 @@
+<?php
+/***************************************************************
+ *  Copyright notice
+ *
+ *  (c) 2011 Christian Kuhn <lolli@schwarzbu.ch>
+ *  All rights reserved
+ *
+ *  This script is part of the TYPO3 project. The TYPO3 project is
+ *  free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation; either version 2 of the License, or
+ *  (at your option) any later version.
+ *
+ *  The GNU General Public License can be found at
+ *  http://www.gnu.org/copyleft/gpl.html.
+ *  A copy is found in the textfile GPL.txt and important notices to the license
+ *  from the author is found in LICENSE.txt distributed with these scripts.
+ *
+ *
+ *  This script is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  This copyright notice MUST APPEAR in all copies of the script!
+ ***************************************************************/
+
+/**
+ * Verify TYPO3 DB table structure. ainly used in install tool
+ * compare wizard and extension manager.
+ *
+ * @author Christian Kuhn <lolli@schwarzbu.ch>
+ * @package TYPO3
+ * @subpackage t3lib
+ */
+class t3lib_install_Sql {
+
+       /**
+        * @var string Prefix of deleted tables
+        */
+       protected $deletedPrefixKey = 'zzz_deleted_'; // Prefix used for tables/fields when deleted/renamed.
+
+       /**
+        * @var array Tables starting with this name are ignored during compare
+        */
+       protected $ignoreTablePrefixes = array(
+               'cachingframework_',
+       );
+
+       /**
+        * @var float|int Multiplier of SQL field size (for char, varchar and text fields)
+        */
+       protected $multiplySize = 1;
+
+       /**
+        * @var array Caching output of $GLOBALS['TYPO3_DB']->admin_get_charsets()
+        */
+       protected $character_sets = array();
+
+       /**
+        * Constructor function
+        */
+       public function __construct() {
+               if ($GLOBALS['TYPO3_CONF_VARS']['SYS']['multiplyDBfieldSize'] >= 1 && $GLOBALS['TYPO3_CONF_VARS']['SYS']['multiplyDBfieldSize'] <= 5) {
+                       $this->multiplySize = (double) $GLOBALS['TYPO3_CONF_VARS']['SYS']['multiplyDBfieldSize'];
+               }
+       }
+
+       /**
+        * Set prefix of deleted tables
+        *
+        * @param string $prefix Prefix string
+        */
+       public function setDeletedPrefixKey($prefix) {
+               $this->deletedPrefixKey = $prefix;
+       }
+
+       /**
+        * Get prefix of deleted tables
+        *
+        * @return string
+        */
+       public function getDeletedPrefixKey() {
+               return $this->deletedPrefixKey;
+       }
+
+       /**
+        * Reads the field definitions for the input SQL-file string
+        *
+        * @param string $fileContent Should be a string read from an SQL-file made with 'mysqldump [database_name] -d'
+        * @return array Array with information about table.
+        */
+       public function getFieldDefinitions_fileContent($fileContent) {
+               $lines = t3lib_div::trimExplode(LF, $fileContent, 1);
+               $table = '';
+               $total = array();
+
+               foreach ($lines as $value) {
+                       if (substr($value, 0, 1) == '#') {
+                               continue; // Ignore comments
+                       }
+
+                       if (!strlen($table)) {
+                               $parts = t3lib_div::trimExplode(' ', $value, TRUE);
+                               if (strtoupper($parts[0]) === 'CREATE' && strtoupper($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) == ';') {
+                                       $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 {
+                                                       // 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 {
+                                                       $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;
+
+                                                       // This is a protection against doing something stupid: Only allow clearing of cache_* and index_* tables.
+                                               if (preg_match('/^(cache|index)_/', $table)) {
+                                                               // Suggest to truncate (clear) this table
+                                                       $total[$table]['extra']['CLEAR'] = 1;
+                                               }
+                                       }
+                               }
+                       }
+               }
+
+               $this->getFieldDefinitions_sqlContent_parseTypes($total);
+               return $total;
+       }
+
+       /**
+        * 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 Total array (from getFieldDefinitions_fileContent())
+        * @return      void
+        * @access private
+        * @see getFieldDefinitions_fileContent()
+        */
+       protected function getFieldDefinitions_sqlContent_parseTypes(&$total) {
+
+               $mSize = (double) $this->multiplySize;
+               if ($mSize > 1) {
+
+                               /** @var $sqlParser t3lib_sqlparser */
+                       $sqlParser = t3lib_div::makeInstance('t3lib_sqlparser');
+                       foreach ($total as $table => $cfg) {
+                               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'
+                                                                                       )
+                                                                               )
+                                                                       );
+                                                                               // 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] . '(' . implode(',', $keys) . ')' . $match[3];
+                                                                               }
+                                                                       }
+                                                               }
+                                                       break;
+                                                       case 'tinytext':
+                                                               $fInfo['fieldType'] = 'text';
+                                                       break;
+                                               }
+
+                                               $total[$table]['fields'][$fN] = $sqlParser->compileFieldCfg($fInfo);
+                                               if ($sqlParser->parse_error) {
+                                                       throw new RuntimeException(
+                                                               'TYPO3 Fatal Error: ' . $sqlParser->parse_error,
+                                                               1270853961
+                                                       );
+                                               }
+                                       }
+                               }
+                       }
+               }
+       }
+
+       /**
+        * Look up the default collation for specified character set based on "SHOW CHARACTER SET" output
+        *
+        * @param string $charset Character set
+        * @return string Corresponding default collation
+        */
+       public function getCollationForCharset($charset) {
+                       // Load character sets, if not cached already
+               if (!count($this->character_sets)) {
+                       if (method_exists($GLOBALS['TYPO3_DB'], 'admin_get_charsets')) {
+                               $this->character_sets = $GLOBALS['TYPO3_DB']->admin_get_charsets();
+                       } else {
+                               $this->character_sets[$charset] = array(); // Add empty element to avoid that the check will be repeated
+                       }
+               }
+
+               $collation = '';
+               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.
+        */
+       public 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 => $tableStatus) {
+
+                               // Fields:
+                       $fieldInformation = $GLOBALS['TYPO3_DB']->admin_get_fields($tableName);
+                       foreach ($fieldInformation as $fN => $fieldRow) {
+                               $total[$tableName]['fields'][$fN] = $this->assembleFieldDefinition($fieldRow);
+                       }
+
+                               // Keys:
+                       $keyInformation = $GLOBALS['TYPO3_DB']->admin_get_keys($tableName);
+
+                       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 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) . ')';
+                               }
+                       }
+               }
+
+               return $total;
+       }
+
+       /**
+        * 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 $FDsrc Field definitions, source (from getFieldDefinitions_fileContent())
+        * @param array $FDcomp Field definitions, comparison. (from getFieldDefinitions_database())
+        * @param string $onlyTableList Table names (in list) which is the ONLY one observed.
+        * @param boolean $ignoreNotNullWhenComparing 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
+        */
+       public 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])) {
+                                               $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(',', '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;
+                                                                       } 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];
+                                                                               }
+                                                                       }
+                                                               }
+                                                       }
+                                               }
+                                       }
+                               }
+                       }
+               }
+
+               $output = array(
+                       'extra' => $extraArr,
+                       'diff' => $diffArr,
+                       'diff_currentValues' => $diffArr_cur
+               );
+
+               return $output;
+       }
+
+       /**
+        * Returns an array with SQL-statements that is needed to update according to the diff-array
+        *
+        * @param array $diffArr Array with differences of current and needed DB settings. (from getDatabaseExtra())
+        * @param string $keyList List of fields in diff array to take notice of.
+        * @return array Array of SQL statements (organized in keys depending on type)
+        */
+       public function getUpdateSuggestions($diffArr, $keyList = 'extra,diff') {
+               $statements = array();
+               $deletedPrefixKey = $this->deletedPrefixKey;
+               $remove = 0;
+               if ($keyList == 'remove') {
+                       $remove = 1;
+                       $keyList = 'extra';
+               }
+               $keyList = explode(',', $keyList);
+               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']) {
+                                                               $whole_table[] = $fN . ' ' . $fV;
+                                                       } else {
+                                                                       // Special case to work around MySQL problems when adding auto_increment fields:
+                                                               if (stristr($fV, 'auto_increment')) {
+                                                                               // The field can only be set "auto_increment" if there exists a PRIMARY key of that field already.
+                                                                               // The check does not look up which field is primary but just assumes it must be the field with the auto_increment value...
+                                                                       if (isset($diffArr['extra'][$table]['keys']['PRIMARY'])) {
+                                                                                       // Remove "auto_increment" from the statement - it will be suggested in a 2nd step after the primary key was created
+                                                                               $fV = str_replace(' auto_increment', '', $fV);
+                                                                       } else {
+                                                                                       // In the next step, attempt to clear the table once again (2 = force)
+                                                                               $info['extra']['CLEAR'] = 2;
+                                                                       }
+                                                               }
+                                                               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 {
+                                                                                       $statement = 'ALTER TABLE ' . $table . ' DROP ' . $fN . ';';
+                                                                                       $statements['drop'][md5($statement)] = $statement;
+                                                                               }
+                                                                       } else {
+                                                                               $statement = 'ALTER TABLE ' . $table . ' ADD ' . $fN . ' ' . $fV . ';';
+                                                                               $statements['add'][md5($statement)] = $statement;
+                                                                       }
+                                                               } elseif ($theKey == 'diff') {
+                                                                       $statement = 'ALTER TABLE ' . $table . ' CHANGE ' . $fN . ' ' . $fN . ' ' . $fV . ';';
+                                                                       $statements['change'][md5($statement)] = $statement;
+                                                                       $statements['change_currentValue'][md5($statement)] = $diffArr['diff_currentValues'][$table]['fields'][$fN];
+                                                               }
+                                                       }
+                                               }
+                                       }
+                                       if (is_array($info['keys'])) {
+                                               foreach ($info['keys'] as $fN => $fV) {
+                                                       if ($info['whole_table']) {
+                                                               $whole_table[] = $fV;
+                                                       } else {
+                                                               if ($theKey == 'extra') {
+                                                                       if ($remove) {
+                                                                               $statement = 'ALTER TABLE ' . $table . ($fN == 'PRIMARY' ? ' DROP PRIMARY KEY' : ' DROP KEY ' . $fN) . ';';
+                                                                               $statements['drop'][md5($statement)] = $statement;
+                                                                       } else {
+                                                                               $statement = 'ALTER TABLE ' . $table . ' ADD ' . $fV . ';';
+                                                                               $statements['add'][md5($statement)] = $statement;
+                                                                       }
+                                                               } elseif ($theKey == 'diff') {
+                                                                       $statement = 'ALTER TABLE ' . $table . ($fN == 'PRIMARY' ? ' DROP PRIMARY KEY' : ' DROP KEY ' . $fN) . ';';
+                                                                       $statements['change'][md5($statement)] = $statement;
+                                                                       $statement = 'ALTER TABLE ' . $table . ' ADD ' . $fV . ';';
+                                                                       $statements['change'][md5($statement)] = $statement;
+                                                               }
+                                                       }
+                                               }
+                                       }
+                                       if (is_array($info['extra'])) {
+                                               $extras = array();
+                                               $extras_currentValue = array();
+                                               $clear_table = FALSE;
+
+                                               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']
+                                                                       if ($fN == 'CLEAR') {
+                                                                                       // Truncate table must happen later, not now
+                                                                                       // Valid values for CLEAR: 1=only clear if keys are missing, 2=clear anyway (force)
+                                                                               if (count($info['keys']) || $fV == 2) {
+                                                                                       $clear_table = TRUE;
+                                                                               }
+                                                                               continue;
+                                                                       } else {
+                                                                               $extras[] = $fN . '=' . $fV;
+                                                                               $extras_currentValue[] = $fN . '=' . $diffArr['diff_currentValues'][$table]['extra'][$fN];
+                                                                       }
+                                                               }
+                                                       }
+                                               }
+                                               if ($clear_table) {
+                                                       $statement = 'TRUNCATE TABLE ' . $table . ';';
+                                                       $statements['clear_table'][md5($statement)] = $statement;
+                                               }
+                                               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) {
+                                                                       // Ignore table operation if the table name starts with a
+                                                                       // prefix that should be ignored
+                                                               $ignoreTable = FALSE;
+                                                               foreach ($this->ignoreTablePrefixes as $ignorePrefix) {
+                                                                       if (substr($table, 0, strlen($ignorePrefix)) === $ignorePrefix) {
+                                                                               $ignoreTable = TRUE;
+                                                                               break;
+                                                                       }
+                                                               }
+                                                               if (!$ignoreTable) {
+                                                                       $statement = 'ALTER TABLE ' . $table . ' RENAME ' . $deletedPrefixKey . $table . ';';
+                                                                       $statements['change_table'][md5($statement)] = $statement;
+                                                               }
+                                                       } else {
+                                                               $statement = 'DROP TABLE ' . $table . ';';
+                                                               $statements['drop_table'][md5($statement)] = $statement;
+                                                       }
+                                                               // count:
+                                                       $count = $GLOBALS['TYPO3_DB']->exec_SELECTcountRows('*', $table);
+                                                       $statements['tables_count'][md5($statement)] = $count ? 'Records in table: ' . $count : '';
+                                               } else {
+                                                       $statement = 'CREATE TABLE ' . $table . " (\n" . implode(",\n", $whole_table) . "\n)";
+                                                       if ($info['extra']) {
+                                                               foreach ($info['extra'] as $k => $v) {
+                                                                       if ($k == 'COLLATE' || $k == 'CLEAR') {
+                                                                               continue; // Skip these special statements. TODO: collation support is currently disabled (needs more testing)
+                                                                       }
+                                                                       $statement .= ' ' . $k . '=' . $v; // Add extra attributes like ENGINE, CHARSET, etc.
+                                                               }
+                                                       }
+                                                       $statement .= ';';
+                                                       $statements['create_table'][md5($statement)] = $statement;
+                                               }
+                                       }
+                               }
+                       }
+               }
+
+               return $statements;
+       }
+
+       /**
+        * Converts a result row with field information into the SQL field definition string
+        *
+        * @param array $row MySQL result row
+        * @return string Field definition
+        */
+       public function assembleFieldDefinition($row) {
+               $field = array($row['Type']);
+
+               if ($row['Null'] == 'NO') {
+                       $field[] = 'NOT NULL';
+               }
+               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 ($row['Extra']) {
+                       $field[] = $row['Extra'];
+               }
+
+               return implode(' ', $field);
+       }
+
+       /**
+        * 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 $removeNonSQL If set, non-SQL content (like comments and blank lines) is not included in the final output
+        * @param string $query_regex Regex to filter SQL lines to include
+        * @return array Array of SQL statements
+        */
+       public function getStatementArray($sqlcode, $removeNonSQL = FALSE, $query_regex = '') {
+               $sqlcodeArr = explode(LF, $sqlcode);
+
+                       // Based on the assumption that the sql-dump has
+               $statementArray = array();
+               $statementArrayPointer = 0;
+
+               foreach ($sqlcodeArr as $line => $lineContent) {
+                       $is_set = 0;
+
+                               // auto_increment fields cannot have a default value!
+                       if (stristr($lineContent, 'auto_increment')) {
+                               $lineContent = preg_replace('/ default \'0\'/i', '', $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 && !preg_match('/' . $query_regex . '/i', trim($statementArray[$statementArrayPointer])))) {
+                                               unset($statementArray[$statementArrayPointer]);
+                                       }
+                               }
+                               $statementArrayPointer++;
+
+                       } elseif ($is_set) {
+                               $statementArray[$statementArrayPointer] .= LF;
+                       }
+               }
+
+               return $statementArray;
+       }
+
+       /**
+        * Returns tables to create and how many records in each
+        *
+        * @param array $statements Array of SQL statements to analyse.
+        * @param boolean $insertCountFlag 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
+        */
+       public function getCreateTables($statements, $insertCountFlag = FALSE) {
+               $crTables = array();
+               $insertCount = array();
+               foreach ($statements as $line => $lineContent) {
+                       $reg = array();
+                       if (preg_match('/^create[[:space:]]*table[[:space:]]*[`]?([[:alnum:]_]*)[`]?/i', substr($lineContent, 0, 100), $reg)) {
+                               $table = trim($reg[1]);
+                               if ($table) {
+                                               // table names are always lowercase on Windows!
+                                       if (TYPO3_OS == 'WIN') {
+                                               $table = strtolower($table);
+                                       }
+                                       $sqlLines = explode(LF, $lineContent);
+                                       foreach ($sqlLines as $k => $v) {
+                                               if (stristr($v, 'auto_increment')) {
+                                                       $sqlLines[$k] = preg_replace('/ default \'0\'/i', '', $v);
+                                               }
+                                       }
+                                       $lineContent = implode(LF, $sqlLines);
+                                       $crTables[$table] = $lineContent;
+                               }
+                       } elseif ($insertCountFlag && preg_match('/^insert[[:space:]]*into[[:space:]]*[`]?([[:alnum:]_]*)[`]?/i', substr($lineContent, 0, 100), $reg)) {
+                               $nTable = trim($reg[1]);
+                               $insertCount[$nTable]++;
+                       }
+               }
+
+               return array($crTables, $insertCount);
+       }
+
+       /**
+        * Extracts all insert statements from $statement array where content is inserted into $table
+        *
+        * @param array $statements Array of SQL statements
+        * @param string $table Table name
+        * @return array Array of INSERT INTO statements where table match $table
+        */
+       public function getTableInsertStatements($statements, $table) {
+               $outStatements = array();
+               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;
+                               }
+                       }
+               }
+               return $outStatements;
+       }
+
+       /**
+        * Performs the queries passed from the input array.
+        *
+        * @param array $arr Array of SQL queries to execute.
+        * @param array $keyArr 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 mixed Array with error message from database if any occured. Otherwise TRUE if everything was executed successfully.
+        */
+       public function performUpdateQueries($arr, $keyArr) {
+               $result = array();
+               if (is_array($arr)) {
+                       foreach ($arr as $key => $string) {
+                               if (isset($keyArr[$key]) && $keyArr[$key]) {
+                                       $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;
+               }
+       }
+
+       /**
+        * Returns list of tables in the database
+        *
+        * @return array List of tables.
+        * @see t3lib_db::admin_get_tables()
+        */
+       public function getListOfTables() {
+               $whichTables = $GLOBALS['TYPO3_DB']->admin_get_tables(TYPO3_db);
+               foreach ($whichTables as $key => &$value) {
+                       $value = $key;
+               }
+               unset($value);
+               return $whichTables;
+       }
+}
+
+if (defined('TYPO3_MODE') && isset($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['XCLASS']['t3lib/class.t3lib_install_sql.php'])) {
+       include_once($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['XCLASS']['t3lib/class.t3lib_install_sql.php']);
+}
+
+?>
\ No newline at end of file
index 692bfaa..1e3b33c 100644 (file)
@@ -37,6 +37,7 @@ $t3libClasses = array(
        't3lib_htmlmail' => PATH_t3lib . 'class.t3lib_htmlmail.php',
        't3lib_iconworks' => PATH_t3lib . 'class.t3lib_iconworks.php',
        't3lib_install' => PATH_t3lib . 'class.t3lib_install.php',
+       't3lib_install_sql' => PATH_t3lib . 'class.t3lib_install_sql.php',
        't3lib_loaddbgroup' => PATH_t3lib . 'class.t3lib_loaddbgroup.php',
        't3lib_loadmodules' => PATH_t3lib . 'class.t3lib_loadmodules.php',
        't3lib_lock' => PATH_t3lib . 'class.t3lib_lock.php',
@@ -220,4 +221,4 @@ $t3libClasses = array(
 $tslibClasses = require(PATH_typo3 . 'sysext/cms/ext_autoload.php');
 
 return array_merge($t3libClasses, $tslibClasses);
-?>
\ No newline at end of file
+?>
index 1613d59..c1c50a8 100644 (file)
@@ -331,8 +331,8 @@ final class tx_em_Database {
         * @return      array           Array of tables and fields splitted.
         */
        function getTableAndFieldStructure($parts) {
-               // Instance of install tool
-               $instObj = new t3lib_install();
+                       /** @var $instObj t3lib_install_Sql */
+               $instObj = t3lib_div::makeInstance('t3lib_install_Sql');
                $dbFields = $instObj->getFieldDefinitions_database(TYPO3_db);
 
                $outTables = array();
@@ -448,7 +448,8 @@ final class tx_em_Database {
         * @see dumpTableAndFieldStructure()
         */
        function dumpStaticTables($tableList) {
-               $instObj = t3lib_div::makeInstance('t3lib_install');
+                       /** @var $instObj t3lib_install_Sql */
+               $instObj = t3lib_div::makeInstance('t3lib_install_Sql');
                $dbFields = $instObj->getFieldDefinitions_database(TYPO3_db);
 
                $out = '';
index 6b4226b..b714672 100644 (file)
@@ -53,6 +53,10 @@ class tx_em_Install {
         */
        public $install;
 
+       /**
+        * @var t3lib_install_Sql
+        */
+       protected $installerSql = NULL;
 
        /**
         * @var integer
@@ -74,6 +78,7 @@ class tx_em_Install {
                $this->parentObject = $parentObject;
                $this->api = t3lib_div::makeInstance('tx_em_API');
                $this->install = t3lib_div::makeInstance('t3lib_install');
+               $this->installerSql = t3lib_div::makeInstance('t3lib_install_Sql');
                $this->install->INSTALL = t3lib_div::_GP('TYPO3_INSTALL');
                $this->systemInstall = isset($GLOBALS['TYPO3_CONF_VARS']['EXT']['allowSystemInstall']) && $GLOBALS['TYPO3_CONF_VARS']['EXT']['allowSystemInstall'];
        }
@@ -821,20 +826,20 @@ class tx_em_Install {
                        $path = tx_em_Tools::getExtPath($extKey, $extInfo['type']);
                        $fileContent = t3lib_div::getUrl($path . 'ext_tables.sql');
 
-                       $FDfile = $this->install->getFieldDefinitions_fileContent($fileContent);
+                       $FDfile = $this->installerSql->getFieldDefinitions_fileContent($fileContent);
                        if (count($FDfile)) {
-                               $FDdb = $this->install->getFieldDefinitions_database(TYPO3_db);
-                               $diff = $this->install->getDatabaseExtra($FDfile, $FDdb);
-                               $update_statements = $this->install->getUpdateSuggestions($diff);
+                               $FDdb = $this->installerSql->getFieldDefinitions_database(TYPO3_db);
+                               $diff = $this->installerSql->getDatabaseExtra($FDfile, $FDdb);
+                               $update_statements = $this->installerSql->getUpdateSuggestions($diff);
 
                                $dbStatus['structure']['tables_fields'] = $FDfile;
                                $dbStatus['structure']['diff'] = $diff;
 
                                // Updating database...
                                if (!$infoOnly && is_array($this->install->INSTALL['database_update'])) {
-                                       $this->install->performUpdateQueries($update_statements['add'], $this->install->INSTALL['database_update']);
-                                       $this->install->performUpdateQueries($update_statements['change'], $this->install->INSTALL['database_update']);
-                                       $this->install->performUpdateQueries($update_statements['create_table'], $this->install->INSTALL['database_update']);
+                                       $this->installerSql->performUpdateQueries($update_statements['add'], $this->install->INSTALL['database_update']);
+                                       $this->installerSql->performUpdateQueries($update_statements['change'], $this->install->INSTALL['database_update']);
+                                       $this->installerSql->performUpdateQueries($update_statements['create_table'], $this->install->INSTALL['database_update']);
                                } else {
                                        $content .= $this->install->generateUpdateDatabaseForm_checkboxes(
                                                $update_statements['add'], $GLOBALS['LANG']->getLL('checkDBupdates_add_fields'));
@@ -850,8 +855,8 @@ class tx_em_Install {
                if (is_array($extInfo['files']) && in_array('ext_tables_static+adt.sql', $extInfo['files'])) {
                        $fileContent = t3lib_div::getUrl(tx_em_Tools::getExtPath($extKey, $extInfo['type']) . 'ext_tables_static+adt.sql');
 
-                       $statements = $this->install->getStatementarray($fileContent, 1);
-                       list($statements_table, $insertCount) = $this->install->getCreateTables($statements, 1);
+                       $statements = $this->installerSql->getStatementarray($fileContent, 1);
+                       list($statements_table, $insertCount) = $this->installerSql->getCreateTables($statements, 1);
 
                        // Execute import of static table content:
                        if (!$infoOnly && is_array($this->install->INSTALL['database_import'])) {
@@ -863,7 +868,7 @@ class tx_em_Install {
                                                $GLOBALS['TYPO3_DB']->admin_query($statements_table[$table]);
 
                                                if ($insertCount[$table]) {
-                                                       $statements_insert = $this->install->getTableInsertStatements($statements, $table);
+                                                       $statements_insert = $this->installerSql->getTableInsertStatements($statements, $table);
 
                                                        foreach ($statements_insert as $v) {
                                                                $GLOBALS['TYPO3_DB']->admin_query($v);
@@ -872,7 +877,7 @@ class tx_em_Install {
                                        }
                                }
                        } else {
-                               $whichTables = $this->install->getListOfTables();
+                               $whichTables = $this->installerSql->getListOfTables();
                                if (count($statements_table)) {
                                        $out = '';
                                        foreach ($statements_table as $table => $definition) {
@@ -1526,17 +1531,15 @@ class tx_em_Install {
         * @return      void
         */
        function forceDBupdates($extKey, $extInfo) {
-               $instObj = new t3lib_install;
-
                // Updating tables and fields?
                if (is_array($extInfo['files']) && in_array('ext_tables.sql', $extInfo['files'])) {
                        $fileContent = t3lib_div::getUrl(tx_em_Tools::getExtPath($extKey, $extInfo['type']) . 'ext_tables.sql');
 
-                       $FDfile = $instObj->getFieldDefinitions_fileContent($fileContent);
+                       $FDfile = $this->installerSql->getFieldDefinitions_fileContent($fileContent);
                        if (count($FDfile)) {
-                               $FDdb = $instObj->getFieldDefinitions_database(TYPO3_db);
-                               $diff = $instObj->getDatabaseExtra($FDfile, $FDdb);
-                               $update_statements = $instObj->getUpdateSuggestions($diff);
+                               $FDdb = $this->installerSql->getFieldDefinitions_database(TYPO3_db);
+                               $diff = $this->installerSql->getDatabaseExtra($FDfile, $FDdb);
+                               $update_statements = $this->installerSql->getUpdateSuggestions($diff);
 
                                foreach ((array) $update_statements['add'] as $string) {
                                        $GLOBALS['TYPO3_DB']->admin_query($string);
@@ -1554,8 +1557,8 @@ class tx_em_Install {
                if (is_array($extInfo['files']) && in_array('ext_tables_static+adt.sql', $extInfo['files'])) {
                        $fileContent = t3lib_div::getUrl(tx_em_Tools::getExtPath($extKey, $extInfo['type']) . 'ext_tables_static+adt.sql');
 
-                       $statements = $instObj->getStatementarray($fileContent, 1);
-                       list($statements_table, $insertCount) = $instObj->getCreateTables($statements, 1);
+                       $statements = $this->installerSql->getStatementarray($fileContent, 1);
+                       list($statements_table, $insertCount) = $this->installerSql->getCreateTables($statements, 1);
 
                        // Traverse the tables
                        foreach ($statements_table as $table => $query) {
@@ -1563,7 +1566,7 @@ class tx_em_Install {
                                $GLOBALS['TYPO3_DB']->admin_query($query);
 
                                if ($insertCount[$table]) {
-                                       $statements_insert = $instObj->getTableInsertStatements($statements, $table);
+                                       $statements_insert = $this->installerSql->getTableInsertStatements($statements, $table);
 
                                        foreach ($statements_insert as $v) {
                                                $GLOBALS['TYPO3_DB']->admin_query($v);
index e242263..06b02bf 100755 (executable)
@@ -871,7 +871,7 @@ REMOTE_ADDR was '".t3lib_div::getIndpEnv('REMOTE_ADDR')."' (".t3lib_div::getIndp
 
                        // only get the number of tables if it is not the first two steps in the 123-installer
                        // (= no DB connection yet)
-               $whichTables = ($this->step != 1 && $this->step != 2 ? $this->getListOfTables() : array());
+               $whichTables = ($this->step != 1 && $this->step != 2 ? $this->sqlHandler->getListOfTables() : array());
 
                $error_emptyDB = '
                        <p class="typo3-message message-error">
@@ -1569,7 +1569,7 @@ REMOTE_ADDR was '".t3lib_div::getIndpEnv('REMOTE_ADDR')."' (".t3lib_div::getIndp
                        </p>
                ');
 
-               $tables = $this->getListOfTables();
+               $tables = $this->sqlHandler->getListOfTables();
                $action = $this->INSTALL['cleanup_type'];
 
                if (($action == 'cache_imagesizes' || $action == 'all') && isset ($tables['cache_imagesizes'])) {
@@ -5317,7 +5317,7 @@ REMOTE_ADDR was '".t3lib_div::getIndpEnv('REMOTE_ADDR')."' (".t3lib_div::getIndp
                }
 
                        // Getting current tables
-               $whichTables=$this->getListOfTables();
+               $whichTables=$this->sqlHandler->getListOfTables();
 
 
                        // Getting number of static_template records
@@ -5514,30 +5514,30 @@ REMOTE_ADDR was '".t3lib_div::getIndpEnv('REMOTE_ADDR')."' (".t3lib_div::getIndp
                                                        LF,
                                                        $this->getStatementArray($tblFileContent,1,'^CREATE TABLE ')
                                                );
-                                               $FDfile = $this->getFieldDefinitions_fileContent($fileContent);
+                                               $FDfile = $this->sqlHandler->getFieldDefinitions_fileContent($fileContent);
                                                if (!count($FDfile)) {
                                                        die ("Error: There were no 'CREATE TABLE' definitions in the provided file");
                                                }
 
                                                        // Updating database...
                                                if (is_array($this->INSTALL['database_update'])) {
-                                                       $FDdb = $this->getFieldDefinitions_database();
-                                                       $diff = $this->getDatabaseExtra($FDfile, $FDdb);
-                                                       $update_statements = $this->getUpdateSuggestions($diff);
-                                                       $diff = $this->getDatabaseExtra($FDdb, $FDfile);
-                                                       $remove_statements = $this->getUpdateSuggestions($diff,'remove');
+                                                       $FDdb = $this->sqlHandler->getFieldDefinitions_database();
+                                                       $diff = $this->sqlHandler->getDatabaseExtra($FDfile, $FDdb);
+                                                       $update_statements = $this->sqlHandler->getUpdateSuggestions($diff);
+                                                       $diff = $this->sqlHandler->getDatabaseExtra($FDdb, $FDfile);
+                                                       $remove_statements = $this->sqlHandler->getUpdateSuggestions($diff,'remove');
 
                                                        $results = array();
-                                                       $results[] = $this->performUpdateQueries($update_statements['clear_table'], $this->INSTALL['database_update']);
+                                                       $results[] = $this->sqlHandler->performUpdateQueries($update_statements['clear_table'], $this->INSTALL['database_update']);
 
-                                                       $results[] = $this->performUpdateQueries($update_statements['add'], $this->INSTALL['database_update']);
-                                                       $results[] = $this->performUpdateQueries($update_statements['change'], $this->INSTALL['database_update']);
-                                                       $results[] = $this->performUpdateQueries($remove_statements['change'], $this->INSTALL['database_update']);
-                                                       $results[] = $this->performUpdateQueries($remove_statements['drop'], $this->INSTALL['database_update']);
+                                                       $results[] = $this->sqlHandler->performUpdateQueries($update_statements['add'], $this->INSTALL['database_update']);
+                                                       $results[] = $this->sqlHandler->performUpdateQueries($update_statements['change'], $this->INSTALL['database_update']);
+                                                       $results[] = $this->sqlHandler->performUpdateQueries($remove_statements['change'], $this->INSTALL['database_update']);
+                                                       $results[] = $this->sqlHandler->performUpdateQueries($remove_statements['drop'], $this->INSTALL['database_update']);
 
-                                                       $results[] = $this->performUpdateQueries($update_statements['create_table'], $this->INSTALL['database_update']);
-                                                       $results[] = $this->performUpdateQueries($remove_statements['change_table'], $this->INSTALL['database_update']);
-                                                       $results[] = $this->performUpdateQueries($remove_statements['drop_table'], $this->INSTALL['database_update']);
+                                                       $results[] = $this->sqlHandler->performUpdateQueries($update_statements['create_table'], $this->INSTALL['database_update']);
+                                                       $results[] = $this->sqlHandler->performUpdateQueries($remove_statements['change_table'], $this->INSTALL['database_update']);
+                                                       $results[] = $this->sqlHandler->performUpdateQueries($remove_statements['drop_table'], $this->INSTALL['database_update']);
 
                                                        $this->databaseUpdateErrorMessages = array();
                                                        foreach ($results as $resultSet) {
@@ -5550,13 +5550,13 @@ REMOTE_ADDR was '".t3lib_div::getIndpEnv('REMOTE_ADDR')."' (".t3lib_div::getIndp
                                                }
 
                                                        // Init again / first time depending...
-                                               $FDdb = $this->getFieldDefinitions_database();
+                                               $FDdb = $this->sqlHandler->getFieldDefinitions_database();
 
-                                               $diff = $this->getDatabaseExtra($FDfile, $FDdb);
-                                               $update_statements = $this->getUpdateSuggestions($diff);
+                                               $diff = $this->sqlHandler->getDatabaseExtra($FDfile, $FDdb);
+                                               $update_statements = $this->sqlHandler->getUpdateSuggestions($diff);
 
-                                               $diff = $this->getDatabaseExtra($FDdb, $FDfile);
-                                               $remove_statements = $this->getUpdateSuggestions($diff,'remove');
+                                               $diff = $this->sqlHandler->getDatabaseExtra($FDdb, $FDfile);
+                                               $remove_statements = $this->sqlHandler->getUpdateSuggestions($diff,'remove');
 
                                                $tLabel = 'Update database tables and fields';
 
@@ -5586,7 +5586,7 @@ REMOTE_ADDR was '".t3lib_div::getIndpEnv('REMOTE_ADDR')."' (".t3lib_div::getIndp
                                break;
                                case 'cmpTCA':
                                        $this->includeTCA();
-                                       $FDdb = $this->getFieldDefinitions_database();
+                                       $FDdb = $this->sqlHandler->getFieldDefinitions_database();
 
                                                // Displaying configured fields which are not in the database
                                        $tLabel='Tables and fields in $TCA, but not in database';
@@ -5746,8 +5746,8 @@ REMOTE_ADDR was '".t3lib_div::getIndpEnv('REMOTE_ADDR')."' (".t3lib_div::getIndp
                                        if ($tblFileContent) {
                                                $tLabel='Import SQL dump';
                                                        // Getting statement array from
-                                               $statements = $this->getStatementArray($tblFileContent,1);
-                                               list($statements_table, $insertCount) = $this->getCreateTables($statements,1);
+                                               $statements = $this->sqlHandler->getStatementArray($tblFileContent,1);
+                                               list($statements_table, $insertCount) = $this->sqlHandler->getCreateTables($statements,1);
 
                                                        // Updating database...
                                                if ($this->INSTALL['database_import_all']) {
@@ -5762,12 +5762,12 @@ REMOTE_ADDR was '".t3lib_div::getIndpEnv('REMOTE_ADDR')."' (".t3lib_div::getIndp
                                                                // fields defined in sysext/cms/ext_tables.sql for example.
                                                        $fileContent = implode(
                                                                LF,
-                                                               $this->getStatementArray($tblFileContent,1,'^CREATE TABLE ')
+                                                               $this->sqlHandler->getStatementArray($tblFileContent,1,'^CREATE TABLE ')
                                                        );
-                                                       $FDfile = $this->getFieldDefinitions_fileContent($fileContent);
-                                                       $FDdb = $this->getFieldDefinitions_database();
-                                                       $diff = $this->getDatabaseExtra($FDfile, $FDdb);
-                                                       $update_statements = $this->getUpdateSuggestions($diff);
+                                                       $FDfile = $this->sqlHandler->getFieldDefinitions_fileContent($fileContent);
+                                                       $FDdb = $this->sqlHandler->getFieldDefinitions_database();
+                                                       $diff = $this->sqlHandler->getDatabaseExtra($FDfile, $FDdb);
+                                                       $update_statements = $this->sqlHandler->getUpdateSuggestions($diff);
                                                        if (is_array($update_statements['add'])) {
                                                                foreach ($update_statements['add'] as $statement) {
                                                                        $res = $GLOBALS['TYPO3_DB']->admin_query($statement);
@@ -5816,7 +5816,7 @@ REMOTE_ADDR was '".t3lib_div::getIndpEnv('REMOTE_ADDR')."' (".t3lib_div::getIndp
                                                                        $res = $GLOBALS['TYPO3_DB']->admin_query($statements_table[$table]);
 
                                                                        if ($insertCount[$table]) {
-                                                                               $statements_insert = $this->getTableInsertStatements($statements, $table);
+                                                                               $statements_insert = $this->sqlHandler->getTableInsertStatements($statements, $table);
                                                                                foreach ($statements_insert as $k => $v) {
                                                                                        $res = $GLOBALS['TYPO3_DB']->admin_query($v);
                                                                                }
@@ -5835,7 +5835,7 @@ REMOTE_ADDR was '".t3lib_div::getIndpEnv('REMOTE_ADDR')."' (".t3lib_div::getIndp
 
                                                if (!$mode123Imported) {
                                                                // Re-Getting current tables - may have been changed during import
-                                                       $whichTables=$this->getListOfTables();
+                                                       $whichTables=$this->sqlHandler->getListOfTables();
 
                                                        if (count($statements_table)) {
                                                                reset($statements_table);
@@ -5959,7 +5959,7 @@ REMOTE_ADDR was '".t3lib_div::getIndpEnv('REMOTE_ADDR')."' (".t3lib_div::getIndp
                                                $tLabel = 'Import SQL dump';
                                                        // Getting statement array from
                                                $fileContent = t3lib_div::getUrl($actionParts[1]);
-                                               $statements = $this->getStatementArray($fileContent, 1);
+                                               $statements = $this->sqlHandler->getStatementArray($fileContent, 1);
                                                $maxL = 1000;
                                                $strLen = strlen($fileContent);
                                                $maxlen = 200+($maxL-t3lib_div::intInRange(($strLen-20000)/100,0,$maxL));
@@ -6709,7 +6709,7 @@ REMOTE_ADDR was '".t3lib_div::getIndpEnv('REMOTE_ADDR')."' (".t3lib_div::getIndp
         */
        function isBasicComplete($tLabel) {
                if ($this->mode=='123') {
-                       $tables = $this->getListOfTables();
+                       $tables = $this->sqlHandler->getListOfTables();
 
                        if (count($tables)) {
                                $beuser = $this->isBackendAdminUser();
@@ -6953,7 +6953,7 @@ REMOTE_ADDR was '".t3lib_div::getIndpEnv('REMOTE_ADDR')."' (".t3lib_div::getIndp
                                // Get the subpart for rows
                        $rowsSubpart = t3lib_parsehtml::getSubpart($template, '###ROWS###');
                        foreach ($arr as $fieldname => $fieldContent) {
-                               if (!t3lib_div::inList($excludeList,$fieldname) && substr($fieldname,0,strlen($this->deletedPrefixKey))!=$this->deletedPrefixKey && substr($fieldname,-1)!='.') {
+                               if (!t3lib_div::inList($excludeList,$fieldname) && substr($fieldname,0,strlen($this->sqlHandler->getDeletedPrefixKey()))!=$this->sqlHandler->getDeletedPrefixKey() && substr($fieldname,-1)!='.') {
                                        if ($arr[$fieldname.'.']) {
                                                        // Get the subpart for pre
                                                $preSubpart = t3lib_parsehtml::getSubpart($rowsSubpart, '###PRE###');