* $Id: utf_migration.php,v 1.2 2008/01/06 12:30:00 andig2 Exp $
*/
chdir('..');
require_once './core/functions.php';
require_once './core/encoding.php';
?>
Migrate database contents to UTF-8
Attention: Be sure to perform a backup before running the encoding migration!
1. Choose source and target encoding
/**
* SQL function
*/
function sql_native($sql_string)
{
global $config, $db_native;
if (!is_resource($db_native))
{
$db_native = mysqli_pconnect($config['db_server'], $config['db_user'], $config['db_password'], $config['db_database']);
if (mysqli_connect_error())
errorpage('DB Connection Error',
"Edit the database settings in ".CONFIG_FILE.".
Alternatively, consider running the installation script.
");
}
$res = mysqli_query($db_native, $sql_string);
// mysqli_db_query returns either positive result ressource or true/false for an insert/update statement
if ($res === false)
{
// report DB Problem
errorpage('Database Problem', mysqli_error($db_native)."\n
\n".$sql_string);
}
elseif ($res === true)
{
// on insert, return id of created record
$result = mysqli_insert_id($db_native);
}
else
{
// return associative result array
$result = array();
for ($i=0; $i'latin1', 'iso-8859-7'=>'latin7', 'iso-8859-9'=>'latin9', 'windows-1251'=>'cp1251', 'koi8-r'=>'koi8r', 'utf-8'=>'utf8');
$tables = array(TBL_DATA, TBL_ACTORS);
extract($_REQUEST);
$db_sourceencoding = $db_encodings[$sourceencoding];
$db_targetencoding = $db_encodings[$targetencoding];
if ($sourceencoding && $targetencoding && ($sourceencoding != $targetencoding))
{
# if (!preg_match('/^(\w\d)+$/', $sourceencoding)) die ('Security violation');
# if (!preg_match('/^(\w\d)+$/', $targetencoding)) die ('Security violation');
?>
2. Validate data correctness and execute
dump("Converting from $sourceencoding to $targetencoding");
sql_native("SET NAMES '".$db_targetencoding."'");
foreach ($tables as $table)
{
dump("Table: ".$table);
$res = sql_native('SELECT * FROM '.$table);
dump("Items: ".count($res)."
");
$enc = iconv_array($sourceencoding, $targetencoding, $res);
for ($i=0; $i$val)
{
if ($SQL) $SQL .= ', ';
$SQL .= $key.'='.db_encode($val);
}
$SQL = "UPDATE $table SET ".$SQL." WHERE id=".$id;
dump($SQL);
if (!$simulate) sql_native($SQL);
}
}
}
?>