2005-11-04 19:35:09 +01:00
< ? php
/************************************************************************** \
* eGroupWare - Setup - fixes a mysql DB to match our system_charset *
* http :// www . eGroupWare . org *
* 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$ */
// if we are NOT called as part of an update script, behave like a regular setup script
if ( ! isset ( $GLOBALS [ 'egw_setup' ]) || ! is_object ( $GLOBALS [ 'egw_setup' ]))
{
$diagnostics = 1 ; // can be set to 0=non, 1=some (default for now), 2=all
$GLOBALS [ 'egw_info' ] = array (
'flags' => array (
'noheader' => True ,
'nonavbar' => True ,
'currentapp' => 'home' ,
'noapi' => True
));
include ( './inc/functions.inc.php' );
// Authorize the user to use setup app and load the database
// Does not return unless user is authorized
if ( ! $GLOBALS [ 'egw_setup' ] -> auth ( 'Config' ) || @ $_POST [ 'cancel' ])
{
Header ( 'Location: index.php' );
exit ;
}
$GLOBALS [ 'egw_setup' ] -> loaddb ();
$tpl_root = $GLOBALS [ 'egw_setup' ] -> html -> setup_tpl_dir ( 'setup' );
$setup_tpl = CreateObject ( 'phpgwapi.Template' , $tpl_root );
$setup_tpl -> set_file ( array (
'T_head' => 'head.tpl' ,
'T_footer' => 'footer.tpl' ,
));
$GLOBALS [ 'egw_setup' ] -> html -> show_header ( '' , False , 'config' , $GLOBALS [ 'egw_setup' ] -> ConfigDomain . '(' . $GLOBALS [ 'egw_domain' ][ $GLOBALS [ 'egw_setup' ] -> ConfigDomain ][ 'db_type' ] . ')' );
echo '<h3>' . 'Fix mysql DB to match the eGroupWare system_charset' . " </h3> \n " ;
$running_standalone = true ;
echo " <p>DB-Type=' { $GLOBALS [ 'egw_setup' ] -> db -> Type } ', eGroupWare system_charset=' { $GLOBALS [ 'egw_setup' ] -> system_charset } ', DB charset was ' { $GLOBALS [ 'egw_setup' ] -> db_charset_was } '</p> \n " ;
}
$db =& $GLOBALS [ 'egw_setup' ] -> db ;
$charset2mysql =& $GLOBALS [ 'egw_setup' ] -> db -> Link_ID -> charset2mysql ;
$mysql2charset = array_flip ( $charset2mysql );
$mysql_system_charset = isset ( $charset2mysql [ $GLOBALS [ 'egw_setup' ] -> system_charset ]) ?
$charset2mysql [ $GLOBALS [ 'egw_setup' ] -> system_charset ] : $GLOBALS [ 'egw_setup' ] -> system_charset ;
if ( substr ( $db -> Type , 0 , 5 ) == 'mysql' && $GLOBALS [ 'egw_setup' ] -> system_charset && $GLOBALS [ 'egw_setup' ] -> db_charset_was &&
$GLOBALS [ 'egw_setup' ] -> system_charset != $GLOBALS [ 'egw_setup' ] -> db_charset_was )
{
$tables = array ();
$db -> query ( " SHOW TABLE STATUS " , __LINE__ , __FILE__ );
while (( $row = $db -> row ( true )))
{
$tables [ $row [ 'Name' ]] = $row [ 'Collation' ];
}
$tables_modified = 0 ;
foreach ( $tables as $table => $collation )
{
$columns = array ();
$db -> query ( " SHOW FULL FIELDS FROM ` $table ` " , __LINE__ , __FILE__ );
while (( $row = $db -> row ( true )))
{
$columns [] = $row ;
}
//echo $table; _debug_array($columns);
$fulltext = $fulltext_back = array ();
$db -> query ( " SHOW KEYS FROM ` $table ` " , __LINE__ , __FILE__ );
while (( $row = $db -> row ( true )))
{
if ( $row [ 'Index_type' ] == 'FULLTEXT' )
{
$fulltext [ $row [ 'Column_name' ]] = $row [ 'Key_name' ];
}
}
$alter_table = $alter_table_back = array ();
foreach ( $columns as $column )
{
if ( $column [ 'Collation' ] && preg_match ( '/^(char|varchar|.*text)\(?([0-9]*)\)?$/i' , $column [ 'Type' ], $matches ))
{
list (, $type , $size ) = $matches ;
list ( $charset ) = explode ( '_' , $column [ 'Collation' ]);
if ( isset ( $mysql2charset [ $charset ])) $charset = $mysql2charset [ $charset ];
if ( $charset != $GLOBALS [ 'egw_setup' ] -> system_charset )
{
$col = $column [ 'Field' ];
if ( $type == 'varchar' || $type == 'char' ) // old schema_proc (pre 1.0.1) used also char
{
$type = 'varchar(' . $size . ')' ;
$bintype = 'varbinary(' . $size . ')' ;
}
else
{
$bintype = str_replace ( 'text' , 'blob' , $type );
}
//echo "<p>$table.$col $type CHARACTER SET $charset $default $null</p>\n";
$default = ! is_null ( $column [ 'Default' ]) ? " DEFAULT ' " . $column [ 'Default' ] . " ' " : '' ;
$null = $column [ 'Null' ] ? 'NULL' : 'NOT NULL' ;
if ( isset ( $fulltext [ $col ]))
{
$idx_name = $fulltext [ $col ];
$idx_cols = array ();
foreach ( $fulltext as $c => $i )
{
if ( $i == $idx_name )
{
$idx_cols [] = $c ;
unset ( $fulltext [ $c ]);
}
}
$fulltext_back [ $idx_name ] = $idx_cols ;
$alter_table [] = " DROP INDEX ` $idx_name ` " ;
}
$alter_table [] = " CHANGE ` $col ` ` $col ` $bintype $default $null " ;
$alter_table_back [] = " CHANGE ` $col ` ` $col ` $type CHARACTER SET $mysql_system_charset $default $null " ;
}
}
}
list ( $charset ) = explode ( '_' , $collation );
if ( isset ( $mysql2charset [ $charset ])) $charset = $mysql2charset [ $charset ];
if ( $charset != $GLOBALS [ 'egw_setup' ] -> system_charset )
{
$alter_table [] = " DEFAULT CHARACTER SET $mysql_system_charset " ;
}
if ( count ( $alter_table ))
{
$alter_table = " ALTER TABLE $table\n " . implode ( " , \n " , $alter_table );
2005-11-05 19:14:20 +01:00
if ( $running_standalone || $_REQUEST [ 'debug' ]) echo '<p>' . nl2br ( $alter_table ) . " </p> \n " ;
2005-11-04 19:35:09 +01:00
if ( ! $db -> query ( $alter_table , __LINE__ , __FILE__ ))
{
echo " <p>SQL Error: " . nl2br ( $alter_table ) . " </p> \n " ;
echo " <b> { $this -> db -> Type } Error</b>: { $this -> db -> Errno } ( { $this -> db -> Error } )</p> \n " ;
echo " <p>continuing ...</p> \n " ;
continue ;
}
foreach ( $fulltext_back as $idx_name => $idx_cols )
{
$alter_table_back [] = " ADD FULLTEXT ` $idx_name ` (` " . implode ( '`,`' , $idx_cols ) . " `) " ;
}
if ( count ( $alter_table_back ))
{
$alter_table_back = " ALTER TABLE $table\n " . implode ( " , \n " , $alter_table_back );
2005-11-05 19:14:20 +01:00
if ( $running_standalone || $_REQUEST [ 'debug' ]) echo '<p>' . nl2br ( $alter_table_back ) . " </p> \n " ;
2005-11-04 19:35:09 +01:00
if ( ! $db -> query ( $alter_table_back , __LINE__ , __FILE__ ))
{
echo " <p><b>SQL Error</b>: " . nl2br ( $alter_table_back ) . " </p> \n " ;
echo " <b> { $this -> db -> Type } Error</b>: { $this -> db -> Errno } ( { $this -> db -> Error } )</p> \n " ;
echo " <p>continuing ...</p> \n " ;
continue ;
}
}
++ $tables_modified ;
}
}
}
if ( $running_standalone )
{
echo " <p> $tables_modified tables changed to our system_charset { $GLOBALS [ 'egw_setup' ] -> system_charset } ( $mysql_system_charset )</p> \n " ;
$GLOBALS [ 'egw_setup' ] -> html -> show_footer ();
}