From e9becfa669f9d7e64440992daeaf937388d26686 Mon Sep 17 00:00:00 2001 From: rvelices Date: Tue, 9 Oct 2007 01:43:29 +0000 Subject: - PWG_CHARSET, DB_CHARSET and DB_COLLATE... utf-8 ready git-svn-id: http://piwigo.org/svn/trunk@2127 68402e56-0260-453c-a942-63ccdbb3a9ee --- install/db/65-database.php | 322 ++++++++++++++++++++++++++++++++++++ install/phpwebgallery_structure.sql | 2 +- 2 files changed, 323 insertions(+), 1 deletion(-) create mode 100644 install/db/65-database.php (limited to 'install') diff --git a/install/db/65-database.php b/install/db/65-database.php new file mode 100644 index 000000000..0f4ae0165 --- /dev/null +++ b/install/db/65-database.php @@ -0,0 +1,322 @@ + 'varbinary', + 'text' => 'blob', + 'mediumtext' => 'mediumblob', + 'longtext' => 'longblob'); + + $changes=array(); + foreach( $field_definitions as $row) + { + if ( !isset($row['Collation']) or $row['Collation']=='NULL' ) + continue; + list ($type) = explode('(', $row['Type']); + if (!isset($types[$type])) + continue; // no need + $binaryType = preg_replace('/'. $type .'/i', $types[$type], $row['Type'] ); + $changes[] = 'MODIFY COLUMN '.$row['Field'].' '.$binaryType; + } + if (count($changes)) + { + $query = 'ALTER TABLE '.$table.' '.implode(', ', $changes); + pwg_query($query); + } +} + +function upgrade65_change_table_to_charset($table, $field_definitions, $db_charset) +{ + $changes=array(); + foreach( $field_definitions as $row) + { + if ( !isset($row['Collation']) or $row['Collation']=='NULL' ) + continue; + $query = $row['Field'].' '.$row['Type']; + $query .= ' CHARACTER SET '.$db_charset; + if (strpos($row['Collation'],'_bin')!==false) + { + $query .= ' BINARY'; + } + if ($row['Null']!='YES') + { + $query.=' NOT NULL'; + if (isset($row['Default'])) + $query.=' DEFAULT "'.addslashes($row['Default']).'"'; + } + else + { + if (!isset($row['Default'])) + $query.=' DEFAULT NULL'; + else + $query.=' DEFAULT "'.addslashes($row['Default']).'"'; + } + + if ($row['Extra']=='auto_increment') + { + $query.=' auto_increment'; + } + $changes[] = 'MODIFY COLUMN '.$query; + } + + if (count($changes)) + { + $query = 'ALTER TABLE `'.$table.'` '.implode(', ', $changes); + pwg_query($query); + } +} + + +$upgrade_description = 'PWG charset migration'; +// +-----------------------------------------------------------------------+ +// | Upgrade content | +// +-----------------------------------------------------------------------+ +if ( !defined('PWG_CHARSET') ) +{ + $upgrade_log = ''; + +// +-----------------------------------------------------------------------+ +// load the config file + $config_file = PHPWG_ROOT_PATH.'include/mysql.inc.php'; + $config_file_contents = @file_get_contents($config_file); + if ($config_file_contents === false) + { + die('CANNOT LOAD '.$config_file); + } + $php_end_tag = strrpos($config_file_contents, '?'.'>'); + if ($php_end_tag === false) + { + die('CANNOT FIND PHP END TAG IN '.$config_file); + } + if (!is_writable($config_file)) + { + die('FILE NOT WRITABLE '.$config_file); + } + + +// +-----------------------------------------------------------------------+ +// load all the user languages + $all_langs=array(); + $query=' +SELECT language, COUNT(user_id) AS count FROM '.USER_INFOS_TABLE.' + GROUP BY language'; + $result = pwg_query($query); + while ( $row=mysql_fetch_assoc($result) ) + { + $lang = $row["language"]; + $lang_def = explode('.', $lang); + if ( count($lang_def)==2 ) + { + $new_lang = $lang_def[0]; + $charset = strtolower($lang_def[1]); + } + else + { + $new_lang = 'en_UK'; + $charset = 'iso-8859-1'; + } + $all_langs[$lang] = array( + 'count' => $row['count'], + 'new_lang' => $new_lang, + 'charset' => $charset, + ); + $upgrade_log .= ">>user_lang\t".$lang."\t".$row['count']."\n"; + } + $upgrade_log .= "\n"; + + +// +-----------------------------------------------------------------------+ +// get admin charset + include(PHPWG_ROOT_PATH . 'include/config_default.inc.php'); + @include(PHPWG_ROOT_PATH. 'include/config_local.inc.php'); + $admin_charset='iso-8859-1'; + $query=' +SELECT language FROM '.USER_INFOS_TABLE.' + WHERE user_id='.$conf['webmaster_id']; + $result = pwg_query($query); + if (mysql_num_rows($result)==0) + { + $query=' +SELECT language FROM '.USER_INFOS_TABLE.' + WHERE status="webmaster" and adviser="false" + LIMIT 1'; + $result = pwg_query($query); + } + + if ( $row=mysql_fetch_assoc($result) ) + { + $admin_charset = $all_langs[$row['language']]['charset']; + } + $upgrade_log .= ">>admin_charset\t".$admin_charset."\n"; + + +// +-----------------------------------------------------------------------+ +// get mysql version and structure of tables + $mysql_version = mysql_get_server_info(); + $upgrade_log .= ">>mysql_ver\t".$mysql_version."\n"; + + $all_tables = array(); + $query = 'SHOW TABLES LIKE "'.$prefixeTable.'%"'; + $result = pwg_query($query); + while ( $row=mysql_fetch_array($result) ) + { + array_push($all_tables, $row[0]); + } + + $all_tables_definition = array(); + foreach( $all_tables as $table) + { + $query = 'SHOW FULL COLUMNS FROM '.$table; + $result = pwg_query($query); + $field_definitions=array(); + while ( $row=mysql_fetch_array($result) ) + { + if ( !isset($row['Collation']) or $row['Collation']=='NULL' ) + continue; + array_push($field_definitions, $row); + } + $all_tables_definition[$table] = $field_definitions; + } + +// +-----------------------------------------------------------------------+ +// calculate the result and convert the tables + +//tables that can be converted without going through binary (they contain only ascii data) + $safe_tables=array('history','history_backup','history_summary','old_permalinks','plugins','rate','upgrade','user_cache','user_feed','user_infos','user_mail_notification', 'users', 'waiting','ws_access'); + $safe_tables=array_flip($safe_tables); + + $pwg_charset = 'iso-8859-1'; + $db_charset = 'latin1'; + $db_collate = ''; + if ( version_compare($mysql_version, '4.1', '<') ) + { // below 4.1 no charset support + $upgrade_log .= "< conversion\tnothing\n"; + } + elseif ($admin_charset=='iso-8859-1') + { + $pwg_charset = 'utf-8'; + $db_charset = 'utf8'; + foreach($all_tables as $table) + { + upgrade65_change_table_to_charset($table, $all_tables_definition[$table], 'utf8' ); + $query = 'ALTER TABLE '.$table.' DEFAULT CHARACTER SET utf8'; + pwg_query($query); + } + $upgrade_log .= "< conversion\tchange utf8\n"; + } +/*ALTER TABLE tbl_name CONVERT TO CHARACTER SET charset_name; (or change column character set) + +Warning: The preceding operation converts column values between the character sets. This is not what you want if you have a column in one character set (like latin1) but the stored values actually use some other, incompatible character set (like utf8). In this case, you have to do the following for each such column: + +ALTER TABLE t1 CHANGE c1 c1 BLOB; +ALTER TABLE t1 CHANGE c1 c1 TEXT CHARACTER SET utf8; +*/ + elseif ( $admin_charset=='utf-8') + { + $pwg_charset = 'utf-8'; + $db_charset = 'utf8'; + foreach($all_tables as $table) + { + if ( !isset($safe_tables[ substr($table, strlen($prefixeTable)) ]) ) + upgrade65_change_table_to_blob($table, $all_tables_definition[$table] ); + upgrade65_change_table_to_charset($table, $all_tables_definition[$table], 'utf8' ); + $query = 'ALTER TABLE '.$table.' DEFAULT CHARACTER SET utf8'; + pwg_query($query); + } + $upgrade_log .= "< conversion\tchange binary\n"; + $upgrade_log .= "< conversion\tchange utf8\n"; + } + elseif ( $admin_charset=='iso-8859-2'/*Central European*/) + { + $pwg_charset = 'utf-8'; + $db_charset = 'utf8'; + foreach($all_tables as $table) + { + if ( !isset($safe_tables[ substr($table, strlen($prefixeTable)) ]) ) + { + upgrade65_change_table_to_blob($table, $all_tables_definition[$table] ); + upgrade65_change_table_to_charset($table, $all_tables_definition[$table], 'latin2' ); + } + upgrade65_change_table_to_charset($table, $all_tables_definition[$table], 'utf8' ); + $query = 'ALTER TABLE '.$table.' DEFAULT CHARACTER SET utf8'; + pwg_query($query); + } + $upgrade_log .= "< conversion\tchange binary\n"; + $upgrade_log .= "< conversion\tchange latin2\n"; + $upgrade_log .= "< conversion\tchange utf8\n"; + } + + +// +-----------------------------------------------------------------------+ +// write the result to file and update #user_infos.language + $config_file_contents = + substr($config_file_contents, 0, $php_end_tag).' +define(\'PWG_CHARSET\', \''.$pwg_charset.'\'); +define(\'DB_CHARSET\', \''.$db_charset.'\'); +define(\'DB_COLLATE\', \'\'); +'.substr($config_file_contents, $php_end_tag); + + $fp = @fopen( $config_file, 'w' ); + @fputs($fp, $config_file_contents, strlen($config_file_contents)); + @fclose($fp); + + foreach ($all_langs as $old_lang=>$lang_data) + { + $query=' + UPDATE '.USER_INFOS_TABLE.' SET language="'.$lang_data['new_lang'].'" + WHERE language="'.$old_lang.'"'; + pwg_query($query); + } + + define('PWG_CHARSET', $pwg_charset); + define('DB_CHARSET', $db_charset); + define('DB_COLLATE', ''); + + echo $upgrade_log; + $fp = @fopen( PHPWG_ROOT_PATH.'upgrade65.log', 'w' ); + if ($fp) + { + @fputs($fp, $upgrade_log, strlen($upgrade_log)); + @fclose($fp); + } + +echo +"\n" +.'"'.$upgrade_description.'"'.' ended' +."\n" +; +} +else +{ + echo 'PWG_CHARSET already defined - nada'; +} +?> diff --git a/install/phpwebgallery_structure.sql b/install/phpwebgallery_structure.sql index 3a086c3c7..bb09e0fd1 100644 --- a/install/phpwebgallery_structure.sql +++ b/install/phpwebgallery_structure.sql @@ -382,7 +382,7 @@ CREATE TABLE `phpwebgallery_user_infos` ( `nb_line_page` tinyint(3) unsigned NOT NULL default '3', `status` enum('webmaster','admin','normal','generic','guest') NOT NULL default 'guest', `adviser` enum('true','false') NOT NULL default 'false', - `language` varchar(50) NOT NULL default 'en_UK.iso-8859-1', + `language` varchar(50) NOT NULL default 'en_UK', `maxwidth` smallint(6) default NULL, `maxheight` smallint(6) default NULL, `expand` enum('true','false') NOT NULL default 'false', -- cgit v1.2.3