$old_table_prefix = $db->table_prefix;
$db->set_table_prefix('');
$table = new Table;
$table1 = $db->show_create_table($db->escape_string($mybb->input['table']));
preg_match("#CHARSET=([a-zA-Z0-9_]+)\s?#i", $table1, $matches);
$charset = $matches[1];
$table->construct_cell("<strong>".$lang->sprintf($lang->converting_to_utf8, $mybb->input['table'], $charset)."</strong>");
$table->construct_row();
$table->construct_cell($lang->please_wait);
$table->construct_row();
$table->output($converting_table." {$mybb->input['table']}");
$db->set_table_prefix($old_table_prefix);
$page->output_footer(false);
$old_table_prefix = $db->table_prefix;
$db->set_table_prefix('');
flush();
$types = array(
'text' => 'blob',
'mediumtext' => 'mediumblob',
'longtext' => 'longblob',
'char' => 'varbinary',
'varchar' => 'varbinary',
'tinytext' => 'tinyblob'
);
// Get next table in list
$convert_to_binary = '';
$convert_to_utf8 = '';
$comma = '';
// Set table default charset
$db->write_query("ALTER TABLE {$mybb->input['table']} DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci");
// Fetch any fulltext keys
if($db->supports_fulltext($mybb->input['table']))
{
$table_structure = $db->show_create_table($mybb->input['table']);
switch($db->type)
{
case "mysql":
case "mysqli":
preg_match_all("#FULLTEXT KEY `?([a-zA-Z0-9_]+)`? \(([a-zA-Z0-9_`,']+)\)#i", $table_structure, $matches);
if(is_array($matches))
{
foreach($matches[0] as $key => $matched)
{
$db->write_query("ALTER TABLE {$mybb->input['table']} DROP INDEX {$matches[1][$key]}");
$fulltext_to_create[$matches[1][$key]] = $matches[2][$key];
}
}
}
}
// Find out which columns need converting and build SQL statements
$query = $db->query("SHOW FULL COLUMNS FROM {$mybb->input['table']}");
while($column = $db->fetch_array($query))
{
list($type) = explode('(', $column['Type']);
if(array_key_exists($type, $types))
{
// Build the actual strings for converting the columns
$names = "CHANGE `{$column['Field']}` `{$column['Field']}` ";
$attributes = " DEFAULT ";
if($column['Default'] == 'NULL')
{
$attributes .= "NULL ";
}
else
{
$attributes .= "'".$db->escape_string($column['Default'])."' ";
if($column['Null'] == 'YES')
{
$attributes .= 'NULL';
}
else
{
$attributes .= 'NOT NULL';
}
}
$convert_to_binary .= $comma.$names.preg_replace('/'.$type.'/i', $types[$type], $column['Type']).$attributes;
$convert_to_utf8 .= "{$comma}{$names}{$column['Type']} CHARACTER SET utf8 COLLATE utf8_general_ci{$attributes}";
$comma = ', ';
}
}
if(!empty($convert_to_binary))
{
// This converts the columns to UTF-8 while also doing the same for data
$db->write_query("ALTER TABLE {$mybb->input['table']} {$convert_to_binary}");
$db->write_query("ALTER TABLE {$mybb->input['table']} {$convert_to_utf8}");
}
// Any fulltext indexes to recreate?
if(is_array($fulltext_to_create))
{
foreach($fulltext_to_create as $name => $fields)
{
$db->create_fulltext_index($mybb->input['table'], $fields, $name);
}
}
$db->set_table_prefix($old_table_prefix);
$plugins->run_hooks("admin_tools_system_health_utf8_conversion_commit");
// Log admin action
log_admin_action($mybb->input['table']);
flash_message($lang->sprintf($lang->success_table_converted, $mybb->input['table']), 'success');