* Improvements to Install Tool:
authorMichael Stucki <michael.stucki@typo3.org>
Mon, 3 Mar 2008 16:20:49 +0000 (16:20 +0000)
committerMichael Stucki <michael.stucki@typo3.org>
Mon, 3 Mar 2008 16:20:49 +0000 (16:20 +0000)
  - Add character set support to t3lib_install

git-svn-id: https://svn.typo3.org/TYPO3v4/Core/trunk@3366 709f56b5-9817-0410-a4d7-c38de5d9e867

ChangeLog
t3lib/class.t3lib_db.php
t3lib/class.t3lib_install.php

index e38dcbb..da58720 100755 (executable)
--- a/ChangeLog
+++ b/ChangeLog
@@ -16,6 +16,7 @@
                - Cleanup in t3lib_install
                - Fixed bug #7589: FULLTEXT keys are not processed correctly
                - Detect differences in table engine and table character set during DB compare
+               - Add character set support to t3lib_install
        * Fix DB field types for static_tsconfig_help
 
 2008-03-03  Ingo Renner  <ingo@typo3.org>
index 65dea28..0055415 100755 (executable)
@@ -147,6 +147,12 @@ class t3lib_DB {
                // Default link identifier:
        var $link = FALSE;
 
+               // Default table engine, applies unless engine is explicitely set
+       var $default_engine = 'MyISAM';
+
+               // Default character set, applies unless character set or collation are explicitely set
+       var $default_charset = 'utf8';
+
 
 
 
@@ -1029,6 +1035,26 @@ class t3lib_DB {
        }
 
        /**
+        * Returns information about the character sets supported by the current DBM
+        * This function is important not only for the Install Tool but probably for DBALs as well since they might need to look up table specific information in order to construct correct queries. In such cases this information should probably be cached for quick delivery.
+        *
+        * This is used by the Install Tool to convert tables tables with non-UTF8 charsets
+        * Use in Install Tool only!
+        *
+        * @return      array           Array with Charset as key and an array of "Charset", "Description", "Default collation", "Maxlen" as values
+        */
+       function admin_get_charsets()   {
+               $output = array();
+
+               $columns_res = mysql_query('SHOW CHARACTER SET', $this->link);
+               while ($row = mysql_fetch_assoc($columns_res)) {
+                       $output[$row['Charset']] = $row;
+               }
+
+               return $output;
+       }
+
+       /**
         * mysql() wrapper function, used by the Install Tool and EM for all queries regarding management of the database!
         * Usage count/core: 10
         *
index b45f1f3..44b4864 100755 (executable)
@@ -93,6 +93,7 @@ class t3lib_install {
        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.
@@ -314,7 +315,22 @@ class t3lib_install {
                                        $ttype = array();
                                        if (preg_match('/(ENGINE|TYPE)=([a-zA-Z]*)/',$value,$ttype)) {
                                                $total[$table]['extra']['ENGINE'] = $ttype[2];
+                                       } else {
+                                               $total[$table]['extra']['ENGINE'] = $GLOBALS['TYPO3_DB']->default_engine;       // Fallback to default engine
+                                       }
+                                               // 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
@@ -430,6 +446,25 @@ class t3lib_install {
        }
 
        /**
+        * Look up the default collation for specified character set based on "SHOW CHARACTER SET" output
+        *
+        * @param       string          Character set
+        * @return      string          Corresponding default collation
+        */
+       function getCollationForCharset($charset)       {
+                       // Load character sets, if not cached already
+               if (!count($this->character_sets)) {
+                       $this->character_sets = $GLOBALS['TYPO3_DB']->admin_get_charsets();
+               }
+
+               if (isset($this->character_sets[$charset]['Default collation'])) {
+                       $collation = $this->character_sets[$charset]['Default collation'];
+               }
+
+               return $collation;
+       }
+
+       /**
         * Reads the field definitions for the current database
         *
         * @return      array           Array with information about table.