2003-10-05 04:06:11 +02:00
< ? php
/************************************************************************** \
* eGroupWare - Setup - change / convert system - charset *
2003-12-19 13:34:45 +01:00
* http :// www . eGroupWare . org *
2003-10-05 04:06:11 +02:00
* Written by RalfBecker @ outdoor - training . de *
* -------------------------------------------- *
* This program 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 . *
\ **************************************************************************/
/* $Id$ */
2003-10-05 12:44:16 +02:00
$diagnostics = 1 ; // can be set to 0=non, 1=some (default for now), 2=all
2005-03-04 13:40:28 +01:00
$GLOBALS [ 'egw_info' ] = array (
'flags' => array (
'noheader' => True ,
'nonavbar' => True ,
'currentapp' => 'home' ,
'noapi' => True
));
2003-10-05 04:06:11 +02:00
include ( './inc/functions.inc.php' );
// Authorize the user to use setup app and load the database
// Does not return unless user is authorized
2005-03-04 13:40:28 +01:00
if ( ! $GLOBALS [ 'egw_setup' ] -> auth ( 'Config' ) || @ $_POST [ 'cancel' ])
2003-10-05 04:06:11 +02:00
{
Header ( 'Location: index.php' );
exit ;
}
2005-03-04 13:40:28 +01:00
$GLOBALS [ 'egw_setup' ] -> loaddb ();
2003-10-05 04:06:11 +02:00
2005-03-04 13:40:28 +01:00
$GLOBALS [ 'egw_setup' ] -> translation -> setup_translation_sql ();
$translation = & $GLOBALS [ 'egw_setup' ] -> translation -> sql ;
2003-10-05 12:44:16 +02:00
$translation -> translation ( True ); // to get the mbstring warnings
2003-10-05 04:06:11 +02:00
2005-03-04 13:40:28 +01:00
$tpl_root = $GLOBALS [ 'egw_setup' ] -> html -> setup_tpl_dir ( 'setup' );
2003-10-05 04:06:11 +02:00
$setup_tpl = CreateObject ( 'phpgwapi.Template' , $tpl_root );
$setup_tpl -> set_file ( array (
'T_head' => 'head.tpl' ,
'T_footer' => 'footer.tpl' ,
'T_system_charset' => 'system_charset.tpl' ,
));
$stage_title = lang ( 'Change system-charset' );
$stage_desc = lang ( 'This program will convert your database to a new system-charset.' );
2003-10-05 12:44:16 +02:00
if ( $diagnostics || !@ $_POST [ 'convert' ])
{
2005-03-04 13:40:28 +01:00
$GLOBALS [ 'egw_setup' ] -> html -> show_header ( $stage_title , False , 'config' , $GLOBALS [ 'egw_setup' ] -> ConfigDomain . '(' . $GLOBALS [ 'egw_domain' ][ $GLOBALS [ 'egw_setup' ] -> ConfigDomain ][ 'db_type' ] . ')' );
2003-10-05 12:44:16 +02:00
}
2003-10-05 04:06:11 +02:00
if ( @ $_POST [ 'convert' ])
{
if ( empty ( $_POST [ 'current_charset' ]))
{
$errors [] = lang ( 'You need to select your current charset!' );
2005-03-04 13:40:28 +01:00
$GLOBALS [ 'egw_setup' ] -> html -> show_header ( $stage_title , False , 'config' , $GLOBALS [ 'egw_setup' ] -> ConfigDomain . '(' . $GLOBALS [ 'egw_domain' ][ $GLOBALS [ 'egw_setup' ] -> ConfigDomain ][ 'db_type' ] . ')' );
2003-10-05 04:06:11 +02:00
}
else
{
2003-10-05 12:44:16 +02:00
convert_db ( $_POST [ 'current_charset' ], $_POST [ 'new_charset' ], $diagnostics );
2003-10-05 04:06:11 +02:00
2003-10-05 12:44:16 +02:00
if ( ! $diagnostics )
2003-10-05 04:06:11 +02:00
{
Header ( 'Location: index.php' );
}
2003-10-05 15:42:27 +02:00
echo '<h3>' . lang ( " Database successfully converted from '%1' to '%2' " , $_POST [ current_charset ], $_POST [ new_charset ]) . " </h3> \n " ;
echo '<p>' . lang ( 'Click <a href="index.php">here</a> to return to setup.' ) . " </p> \n " ;
2003-10-05 04:06:11 +02:00
exit ;
}
}
2003-10-05 12:44:16 +02:00
function convert_db ( $from , $to , $diagnostics = 1 )
2003-10-05 04:06:11 +02:00
{
2003-10-05 12:44:16 +02:00
if ( $diagnostics ) echo " <h3>Converting database from ' $from ' to ' $to '</h3> \n " ;
2003-10-05 04:06:11 +02:00
@ set_time_limit ( 0 ); // this might take a while
2005-03-04 13:40:28 +01:00
$db2 = $GLOBALS [ 'egw_setup' ] -> db ;
$setup_info = $GLOBALS [ 'egw_setup' ] -> detection -> get_versions ();
$setup_info = $GLOBALS [ 'egw_setup' ] -> detection -> get_db_versions ( $setup_info );
2003-10-05 04:06:11 +02:00
// Visit each app/setup dir, look for a phpgw_lang file
foreach ( $setup_info as $app => $data )
{
2003-10-05 12:44:16 +02:00
if ( $diagnostics ) echo " <p><b> $app </b>: " ;
2003-10-05 04:06:11 +02:00
if ( ! isset ( $data [ 'tables' ]) || ! count ( $data [ 'tables' ]) ||
2005-03-04 13:40:28 +01:00
! $GLOBALS [ 'egw_setup' ] -> app_registered ( $app ) ||
2003-10-05 12:44:16 +02:00
! ( $table_definitions = $db2 -> get_table_definitions ( $app )))
2003-10-05 04:06:11 +02:00
{
2003-10-05 12:44:16 +02:00
if ( $diagnostics ) echo " skipping (no tables or not installed)</p> \n " ;
2003-10-05 04:06:11 +02:00
continue ;
}
2003-10-05 12:44:16 +02:00
foreach ( $table_definitions as $table => $definition )
2003-10-05 04:06:11 +02:00
{
2005-07-11 20:02:19 +02:00
if ( $diagnostics ) { echo " <br />start converting table ' $table ' ... " ; }
2003-10-05 12:44:16 +02:00
$db2 -> set_column_definitions ( $definitions [ 'fd' ]);
2003-10-05 04:06:11 +02:00
$updates = 0 ;
2005-03-04 13:40:28 +01:00
$GLOBALS [ 'egw_setup' ] -> db -> query ( " SELECT * FROM $table " , __LINE__ , __FILE__ );
while ( $columns = $GLOBALS [ 'egw_setup' ] -> db -> row ( True ))
2003-10-05 04:06:11 +02:00
{
$update = array ();
foreach ( $columns as $name => $data )
{
switch ( $definition [ 'fd' ][ $name ][ 'type' ])
{
case 'char' :
case 'varchar' :
case 'text' :
case 'longtext' :
$converted = $GLOBALS [ 'translation' ] -> convert ( $data , $from , $to );
if ( $converted != $data )
{
$update [ $name ] = $converted ;
}
break ;
}
}
if ( count ( $update ))
{
if ( count ( $definition [ 'pk' ]))
{
2003-10-05 12:44:16 +02:00
$db2 -> query ( $query = " UPDATE $table SET " . $db2 -> column_data_implode ( ',' , $update ) . " WHERE " . $db2 -> column_data_implode ( ' AND ' , $columns , True , $definition [ 'pk' ]), __LINE__ , __FILE__ );
2003-10-05 04:06:11 +02:00
}
else
{
2003-10-05 12:44:16 +02:00
// if we have no primary key, we need to delete and re-write the row
$db2 -> query ( $query = " DELETE FROM $table WHERE " . $db2 -> column_data_implode ( ' AND ' , $columns ), __LINE__ , __FILE__ );
2005-07-11 20:02:19 +02:00
if ( $diagnostics > 1 ) echo " $query <br /> \n " ;
2003-10-05 12:44:16 +02:00
$db2 -> query ( $query = " INSERT INTO $table ( " . implode ( ',' , array_keys ( $columns )) . " ) VALUES ( " . $db2 -> column_data_implode ( ',' , array_merge ( $columns , $update ), False ) . " ) " , __LINE__ , __FILE__ );
2003-10-05 04:06:11 +02:00
}
2003-10-05 12:44:16 +02:00
if ( $diagnostics > 1 ) echo " $query <p> \n " ;
2003-10-05 04:06:11 +02:00
++ $updates ;
}
}
2003-10-05 12:44:16 +02:00
if ( $diagnostics )
2003-10-05 04:06:11 +02:00
{
2005-03-04 13:40:28 +01:00
$GLOBALS [ 'egw_setup' ] -> db -> query ( " SELECT count(*) FROM $table " , __LINE__ , __FILE__ );
$GLOBALS [ 'egw_setup' ] -> db -> next_record ();
$total = $GLOBALS [ 'egw_setup' ] -> db -> f ( 0 );
2003-10-05 04:06:11 +02:00
echo " done, $updates / $total rows updated " ;
}
}
}
2005-11-02 12:45:52 +01:00
$GLOBALS [ 'egw_setup' ] -> db -> insert ( $GLOBALS [ 'egw_setup' ] -> config_table , array (
'config_value' => $to ,
), array (
'config_app' => 'phpgwapi' ,
'config_name' => 'systemcharset' ,
), __LINE__ , __FILE__ );
2003-10-05 04:06:11 +02:00
}
$setup_tpl -> set_var ( 'stage_title' , $stage_title );
$setup_tpl -> set_var ( 'stage_desc' , $stage_desc );
2005-07-11 20:02:19 +02:00
$setup_tpl -> set_var ( 'error_msg' , is_array ( $errors ) ? implode ( '<br />' , $errors ) : ' ' );
2003-10-05 04:06:11 +02:00
$setup_tpl -> set_var ( 'lang_convert' , lang ( 'Convert' ));
$setup_tpl -> set_var ( 'lang_cancel' , lang ( 'Cancel' ));
$setup_tpl -> set_var ( 'lang_current' , lang ( 'Current system-charset' ));
$setup_tpl -> set_var ( 'lang_convert_to' , lang ( 'Charset to convert to' ));
2005-07-11 20:02:19 +02:00
$setup_tpl -> set_var ( 'lang_warning' , '<b>' . lang ( 'Setting the system-charset to UTF-8 (unicode) allows the coexistens of data from languages of different charsets.' ) . '</b><br />' .
2004-05-02 14:41:19 +02:00
lang ( 'If you use only languages of the same charset (eg. western european ones) you dont need to set a system-charset!' ));
2003-10-05 04:06:11 +02:00
$installed_charsets = $translation -> get_installed_charsets ();
if ( $translation -> system_charset || count ( $installed_charsets ) == 1 )
{
reset ( $installed_charsets );
list ( $current_charset ) = each ( $installed_charsets );
if ( $translation -> system_charset )
{
$current_charset = $translation -> system_charset ;
}
$setup_tpl -> set_var ( 'current_charset' , " <b> $current_charset </b> " .
2005-07-11 20:02:19 +02:00
" <input type= \" hidden \" name= \" current_charset \" value= \" $current_charset\ " /> \n " );
2003-10-05 04:06:11 +02:00
}
else
{
$options = '<option value="">' . lang ( 'select one...' ) . " </option> \n " ;
foreach ( $installed_charsets as $charset => $description )
{
$options .= " <option value= \" $charset\ " > $description </ option > \n " ;
}
$setup_tpl -> set_var ( 'current_charset' , " <select name= \" current_charset \" > \n $options </select> \n " );
}
2003-10-05 12:44:16 +02:00
if ( $translation -> system_charset == 'utf-8' || count ( $installed_charsets ) == 1 )
2003-10-05 04:06:11 +02:00
{
reset ( $installed_charsets );
list ( $other_charset ) = each ( $installed_charsets );
if ( ! $translation -> system_charset || $other_charset == $translation -> system_charset )
{
2003-10-05 12:44:16 +02:00
$other_charset = 'utf-8' ;
2003-10-05 04:06:11 +02:00
}
2005-07-11 20:02:19 +02:00
$setup_tpl -> set_var ( 'new_charset' , " <b> $other_charset </b><input type= \" hidden \" name= \" new_charset \" value= \" $other_charset\ " /> \n " );
2003-10-05 04:06:11 +02:00
}
else
{
2003-10-05 12:44:16 +02:00
if ( $translation -> system_charset != 'utf-8' )
2003-10-05 04:06:11 +02:00
{
2003-10-05 12:44:16 +02:00
$options = '<option value="utf-8">' . lang ( 'utf-8 (Unicode)' ) . " </option> \n " ;
2003-10-05 04:06:11 +02:00
}
foreach ( $installed_charsets as $charset => $description )
{
if ( $charset != $translation -> system_charset )
{
$options .= " <option value= \" $charset\ " > $description </ option > \n " ;
}
}
$setup_tpl -> set_var ( 'new_charset' , " <select name= \" new_charset \" > \n $options </select> \n " );
}
$setup_tpl -> pparse ( 'out' , 'T_system_charset' );
2005-03-04 13:40:28 +01:00
$GLOBALS [ 'egw_setup' ] -> html -> show_footer ();
2003-10-05 04:06:11 +02:00
?>