diff options
author | Karl 'vollmerk' Vollmer <vollmer@ampache.org> | 2008-05-08 06:44:22 +0000 |
---|---|---|
committer | Karl 'vollmerk' Vollmer <vollmer@ampache.org> | 2008-05-08 06:44:22 +0000 |
commit | 5318a90e4a4fddfb7762feb2bb8bc6dc4802fb7d (patch) | |
tree | b5c890628234e42457dbd18f08feae9ca74081f3 /lib/class/dba.class.php | |
parent | efd932b9fde167ec1a54809fe5fd9286f29eda2c (diff) | |
download | ampache-5318a90e4a4fddfb7762feb2bb8bc6dc4802fb7d.tar.gz ampache-5318a90e4a4fddfb7762feb2bb8bc6dc4802fb7d.tar.bz2 ampache-5318a90e4a4fddfb7762feb2bb8bc6dc4802fb7d.zip |
add in functionality to reset db charset also updated the update function to take into account the collation
Diffstat (limited to 'lib/class/dba.class.php')
-rw-r--r-- | lib/class/dba.class.php | 70 |
1 files changed, 70 insertions, 0 deletions
diff --git a/lib/class/dba.class.php b/lib/class/dba.class.php index e9d0304d..6ead8b8b 100644 --- a/lib/class/dba.class.php +++ b/lib/class/dba.class.php @@ -251,6 +251,76 @@ class Dba { } // auto_init + /** + * reset_db_charset + * This cruises through the database and trys to set the charset to the current + * site charset, this is an admin function that can be run by an administrator + * this can mess up data if you switch between charsets that are not overlapping + * a catalog verify must be re-run to correct them + */ + public static function reset_db_charset() { + + // MySQL translte real charset names into fancy smancy MySQL land names + switch (strtoupper(Config::get('site_charset'))) { + case 'EUC-KR': + $target_charset = 'euckr'; + $target_collation = 'euckr_korean_ci'; + break; + case 'CP932': + $target_charset = 'sjis'; + $target_collation = 'sjis_japanese_ci'; + break; + case 'KOI8-U': + $target_charset = 'koi8u'; + $target_collation = 'koi8u_general_ci'; + break; + case 'KOI8-R': + $target_charset = 'koi8r'; + $target_collation = 'koi8r_general_ci'; + break; + case 'ISO-8859': + $target_charset = 'latin2'; + $target_collation = 'latin2_general_ci'; + break; + default; + case 'UTF-8': + $target_charset = 'utf8'; + $target_collation = 'utf8_unicode_ci'; + break; + } // end mysql charset translation + + // Alter the charset for the entire database + $sql = "ALTER DATABASE `" . Config::get('database_name') . "` DEFAULT CHARACTER SET $target_charset COLLATE $target_collation"; + $db_results = Dba::query($sql); + + $sql = "SHOW TABLES"; + $db_results = Dba::query($sql); + + // Go through the tables! + while ($row = Dba::fetch_row($db_results)) { + $sql = "DESCRIBE `" . $row['0'] . "`"; + $describe_results = Dba::query($sql); + + // Change the tables default charset and colliation + $sql = "ALTER TABLE `" . $row['0'] . "` DEFAULT CHARACTER SET $target_charset COLLATE $target_collation"; + $alter_table = Dba::query($sql); + + // Itterate through the columns of the table + while ($table = Dba::fetch_assoc($describe_results)) { + if (strstr($table['Type'],'varchar') OR strstr($table['Type'],'enum') OR strstr($table['Table'],'text')) { + $sql = "ALTER TABLE `" . $row['0'] . "` MODIFY `" . $table['Field'] . "` " . $table['Type'] . " CHARACTER SET " . $target_charset; + $charset_results = Dba::query($sql); + if (!$charset_results) { + debug_event('CHARSET','Unable to update the charset of ' . $table['Field'] . '.' . $table['Type'] . ' to ' . $target_charset,'3'); + } // if it fails + } // if its a varchar + } // end columns + + } // end tables + + + } // reset_db_charset + } // dba class ?> |