Esempio n. 1
0
/**
 * Edit the search method and search index used.
 * Calculates the size of the current search indexes in use.
 * Allows to create and delete a fulltext index on the messages table.
 * Allows to delete a custom index (that CreateMessageIndex() created).
 * Called by ?action=admin;area=managesearch;sa=method.
 * Requires the admin_forum permission.
 *
 * @uses ManageSearch template, 'select_search_method' sub-template.
 */
function EditSearchMethod()
{
    global $txt, $context, $modSettings, $smcFunc, $db_type, $db_prefix;
    $context[$context['admin_menu_name']]['current_subsection'] = 'method';
    $context['page_title'] = $txt['search_method_title'];
    $context['sub_template'] = 'select_search_method';
    $context['supports_fulltext'] = $smcFunc['db_search_support']('fulltext');
    // Load any apis.
    $context['search_apis'] = loadSearchAPIs();
    // Detect whether a fulltext index is set.
    if ($context['supports_fulltext']) {
        detectFulltextIndex();
    }
    if (!empty($_REQUEST['sa']) && $_REQUEST['sa'] == 'createfulltext') {
        checkSession('get');
        validateToken('admin-msm', 'get');
        // Make sure it's gone before creating it.
        $smcFunc['db_query']('', '
			ALTER TABLE {db_prefix}messages
			DROP INDEX body', array('db_error_skip' => true));
        $smcFunc['db_query']('', '
			ALTER TABLE {db_prefix}messages
			ADD FULLTEXT body (body)', array());
        $context['fulltext_index'] = 'body';
    } elseif (!empty($_REQUEST['sa']) && $_REQUEST['sa'] == 'removefulltext' && !empty($context['fulltext_index'])) {
        checkSession('get');
        validateToken('admin-msm', 'get');
        $smcFunc['db_query']('', '
			ALTER TABLE {db_prefix}messages
			DROP INDEX ' . implode(',
			DROP INDEX ', $context['fulltext_index']), array('db_error_skip' => true));
        $context['fulltext_index'] = '';
        // Go back to the default search method.
        if (!empty($modSettings['search_index']) && $modSettings['search_index'] == 'fulltext') {
            updateSettings(array('search_index' => ''));
        }
    } elseif (!empty($_REQUEST['sa']) && $_REQUEST['sa'] == 'removecustom') {
        checkSession('get');
        validateToken('admin-msm', 'get');
        db_extend();
        $tables = $smcFunc['db_list_tables'](false, $db_prefix . 'log_search_words');
        if (!empty($tables)) {
            $smcFunc['db_search_query']('drop_words_table', '
				DROP TABLE {db_prefix}log_search_words', array());
        }
        updateSettings(array('search_custom_index_config' => '', 'search_custom_index_resume' => ''));
        // Go back to the default search method.
        if (!empty($modSettings['search_index']) && $modSettings['search_index'] == 'custom') {
            updateSettings(array('search_index' => ''));
        }
    } elseif (isset($_POST['save'])) {
        checkSession();
        validateToken('admin-msmpost');
        updateSettings(array('search_index' => empty($_POST['search_index']) || !in_array($_POST['search_index'], array('fulltext', 'custom')) && !isset($context['search_apis'][$_POST['search_index']]) ? '' : $_POST['search_index'], 'search_force_index' => isset($_POST['search_force_index']) ? '1' : '0', 'search_match_words' => isset($_POST['search_match_words']) ? '1' : '0'));
    }
    $context['table_info'] = array('data_length' => 0, 'index_length' => 0, 'fulltext_length' => 0, 'custom_index_length' => 0);
    // Get some info about the messages table, to show its size and index size.
    if ($db_type == 'mysql') {
        if (preg_match('~^`(.+?)`\\.(.+?)$~', $db_prefix, $match) !== 0) {
            $request = $smcFunc['db_query']('', '
				SHOW TABLE STATUS
				FROM {string:database_name}
				LIKE {string:table_name}', array('database_name' => '`' . strtr($match[1], array('`' => '')) . '`', 'table_name' => str_replace('_', '\\_', $match[2]) . 'messages'));
        } else {
            $request = $smcFunc['db_query']('', '
				SHOW TABLE STATUS
				LIKE {string:table_name}', array('table_name' => str_replace('_', '\\_', $db_prefix) . 'messages'));
        }
        if ($request !== false && $smcFunc['db_num_rows']($request) == 1) {
            // Only do this if the user has permission to execute this query.
            $row = $smcFunc['db_fetch_assoc']($request);
            $context['table_info']['data_length'] = $row['Data_length'];
            $context['table_info']['index_length'] = $row['Index_length'];
            $context['table_info']['fulltext_length'] = $row['Index_length'];
            $smcFunc['db_free_result']($request);
        }
        // Now check the custom index table, if it exists at all.
        if (preg_match('~^`(.+?)`\\.(.+?)$~', $db_prefix, $match) !== 0) {
            $request = $smcFunc['db_query']('', '
				SHOW TABLE STATUS
				FROM {string:database_name}
				LIKE {string:table_name}', array('database_name' => '`' . strtr($match[1], array('`' => '')) . '`', 'table_name' => str_replace('_', '\\_', $match[2]) . 'log_search_words'));
        } else {
            $request = $smcFunc['db_query']('', '
				SHOW TABLE STATUS
				LIKE {string:table_name}', array('table_name' => str_replace('_', '\\_', $db_prefix) . 'log_search_words'));
        }
        if ($request !== false && $smcFunc['db_num_rows']($request) == 1) {
            // Only do this if the user has permission to execute this query.
            $row = $smcFunc['db_fetch_assoc']($request);
            $context['table_info']['index_length'] += $row['Data_length'] + $row['Index_length'];
            $context['table_info']['custom_index_length'] = $row['Data_length'] + $row['Index_length'];
            $smcFunc['db_free_result']($request);
        }
    } elseif ($db_type == 'postgresql') {
        // In order to report the sizes correctly we need to perform vacuum (optimize) on the tables we will be using.
        db_extend();
        $temp_tables = $smcFunc['db_list_tables']();
        foreach ($temp_tables as $table) {
            if ($table == $db_prefix . 'messages' || $table == $db_prefix . 'log_search_words') {
                $smcFunc['db_optimize_table']($table);
            }
        }
        // PostGreSql has some hidden sizes.
        $request = $smcFunc['db_query']('', '
			SELECT relname, relpages * 8 *1024 AS "KB" FROM pg_class
			WHERE relname = {string:messages} OR relname = {string:log_search_words}
			ORDER BY relpages DESC', array('messages' => $db_prefix . 'messages', 'log_search_words' => $db_prefix . 'log_search_words'));
        if ($request !== false && $smcFunc['db_num_rows']($request) > 0) {
            while ($row = $smcFunc['db_fetch_assoc']($request)) {
                if ($row['relname'] == $db_prefix . 'messages') {
                    $context['table_info']['data_length'] = (int) $row['KB'];
                    $context['table_info']['index_length'] = (int) $row['KB'];
                    // Doesn't support fulltext
                    $context['table_info']['fulltext_length'] = $txt['not_applicable'];
                } elseif ($row['relname'] == $db_prefix . 'log_search_words') {
                    $context['table_info']['index_length'] = (int) $row['KB'];
                    $context['table_info']['custom_index_length'] = (int) $row['KB'];
                }
            }
            $smcFunc['db_free_result']($request);
        } else {
            // Didn't work for some reason...
            $context['table_info'] = array('data_length' => $txt['not_applicable'], 'index_length' => $txt['not_applicable'], 'fulltext_length' => $txt['not_applicable'], 'custom_index_length' => $txt['not_applicable']);
        }
    } else {
        $context['table_info'] = array('data_length' => $txt['not_applicable'], 'index_length' => $txt['not_applicable'], 'fulltext_length' => $txt['not_applicable'], 'custom_index_length' => $txt['not_applicable']);
    }
    // Format the data and index length in kilobytes.
    foreach ($context['table_info'] as $type => $size) {
        // If it's not numeric then just break.  This database engine doesn't support size.
        if (!is_numeric($size)) {
            break;
        }
        $context['table_info'][$type] = comma_format($context['table_info'][$type] / 1024) . ' ' . $txt['search_method_kilobytes'];
    }
    $context['custom_index'] = !empty($modSettings['search_custom_index_config']);
    $context['partial_custom_index'] = !empty($modSettings['search_custom_index_resume']) && empty($modSettings['search_custom_index_config']);
    $context['double_index'] = !empty($context['fulltext_index']) && $context['custom_index'];
    createToken('admin-msmpost');
    createToken('admin-msm', 'get');
}
Esempio n. 2
0
 /**
  * Edit the search method and search index used.
  *
  * What it does:
  * - Calculates the size of the current search indexes in use.
  * - Allows to create and delete a fulltext index on the messages table.
  * - Allows to delete a custom index (that action_create() created).
  * - Called by ?action=admin;area=managesearch;sa=method.
  * - Requires the admin_forum permission.
  *
  * @uses ManageSearch template, 'select_search_method' sub-template.
  */
 public function action_edit()
 {
     global $txt, $context, $modSettings;
     // Need to work with some db search stuffs
     $db_search = db_search();
     require_once SUBSDIR . '/ManageSearch.subs.php';
     $context[$context['admin_menu_name']]['current_subsection'] = 'method';
     $context['page_title'] = $txt['search_method_title'];
     $context['sub_template'] = 'select_search_method';
     $context['supports_fulltext'] = $db_search->search_support('fulltext');
     // Load any apis.
     $context['search_apis'] = $this->loadSearchAPIs();
     // Detect whether a fulltext index is set.
     if ($context['supports_fulltext']) {
         detectFulltextIndex();
     }
     if (!empty($_REQUEST['sa']) && $_REQUEST['sa'] == 'createfulltext') {
         checkSession('get');
         validateToken('admin-msm', 'get');
         $context['fulltext_index'] = 'body';
         alterFullTextIndex('{db_prefix}messages', $context['fulltext_index'], true);
     } elseif (!empty($_REQUEST['sa']) && $_REQUEST['sa'] == 'removefulltext' && !empty($context['fulltext_index'])) {
         checkSession('get');
         validateToken('admin-msm', 'get');
         alterFullTextIndex('{db_prefix}messages', $context['fulltext_index']);
         $context['fulltext_index'] = '';
         // Go back to the default search method.
         if (!empty($modSettings['search_index']) && $modSettings['search_index'] == 'fulltext') {
             updateSettings(array('search_index' => ''));
         }
     } elseif (!empty($_REQUEST['sa']) && $_REQUEST['sa'] == 'removecustom') {
         checkSession('get');
         validateToken('admin-msm', 'get');
         drop_log_search_words();
         updateSettings(array('search_custom_index_config' => '', 'search_custom_index_resume' => ''));
         // Go back to the default search method.
         if (!empty($modSettings['search_index']) && $modSettings['search_index'] == 'custom') {
             updateSettings(array('search_index' => ''));
         }
     } elseif (isset($_POST['save'])) {
         checkSession();
         validateToken('admin-msmpost');
         updateSettings(array('search_index' => empty($_POST['search_index']) || !in_array($_POST['search_index'], array('fulltext', 'custom')) && !isset($context['search_apis'][$_POST['search_index']]) ? '' : $_POST['search_index'], 'search_force_index' => isset($_POST['search_force_index']) ? '1' : '0', 'search_match_words' => isset($_POST['search_match_words']) ? '1' : '0'));
     }
     $table_info_defaults = array('data_length' => 0, 'index_length' => 0, 'fulltext_length' => 0, 'custom_index_length' => 0);
     // Get some info about the messages table, to show its size and index size.
     if (method_exists($db_search, 'membersTableInfo')) {
         $context['table_info'] = array_merge($table_info_defaults, $db_search->membersTableInfo());
     } else {
         // Here may be wolves.
         $context['table_info'] = array('data_length' => $txt['not_applicable'], 'index_length' => $txt['not_applicable'], 'fulltext_length' => $txt['not_applicable'], 'custom_index_length' => $txt['not_applicable']);
     }
     // Format the data and index length in kilobytes.
     foreach ($context['table_info'] as $type => $size) {
         // If it's not numeric then just break.  This database engine doesn't support size.
         if (!is_numeric($size)) {
             break;
         }
         $context['table_info'][$type] = comma_format($context['table_info'][$type] / 1024) . ' ' . $txt['search_method_kilobytes'];
     }
     $context['custom_index'] = !empty($modSettings['search_custom_index_config']);
     $context['partial_custom_index'] = !empty($modSettings['search_custom_index_resume']) && empty($modSettings['search_custom_index_config']);
     $context['double_index'] = !empty($context['fulltext_index']) && $context['custom_index'];
     createToken('admin-msmpost');
     createToken('admin-msm', 'get');
 }
Esempio n. 3
0
/**
 * Convert both data and database tables to UTF-8 character set.
 * It requires the admin_forum permission.
 * This only works if UTF-8 is not the global character set.
 * It supports all character sets used by SMF's language files.
 * It redirects to ?action=admin;area=maintain after finishing.
 * This action is linked from the maintenance screen (if it's applicable).
 * Accessed by ?action=admin;area=maintain;sa=database;activity=convertutf8.
 *
 * @uses the convert_utf8 sub template of the Admin template.
 */
function ConvertUtf8()
{
    global $scripturl, $context, $txt, $language, $db_character_set;
    global $modSettings, $user_info, $sourcedir, $smcFunc, $db_prefix;
    // Show me your badge!
    isAllowedTo('admin_forum');
    // The character sets used in SMF's language files with their db equivalent.
    $charsets = array('big5' => 'big5', 'gbk' => 'gbk', 'ISO-8859-1' => 'latin1', 'ISO-8859-2' => 'latin2', 'ISO-8859-9' => 'latin5', 'ISO-8859-15' => 'latin9', 'tis-620' => 'tis620', 'UTF-8' => 'utf8', 'windows-1251' => 'cp1251', 'windows-1253' => 'utf8', 'windows-1255' => 'utf8', 'windows-1256' => 'cp1256');
    // Get a list of character sets supported by your MySQL server.
    $request = $smcFunc['db_query']('', '
		SHOW CHARACTER SET', array());
    $db_charsets = array();
    while ($row = $smcFunc['db_fetch_assoc']($request)) {
        $db_charsets[] = $row['Charset'];
    }
    $smcFunc['db_free_result']($request);
    // Character sets supported by both MySQL and SMF's language files.
    $charsets = array_intersect($charsets, $db_charsets);
    // This is for the first screen telling backups is good.
    if (!isset($_POST['proceed'])) {
        validateToken('admin-maint');
        // Character set conversions are only supported as of MySQL 4.1.2.
        if (version_compare('4.1.2', preg_replace('~\\-.+?$~', '', $smcFunc['db_server_info']()), '>')) {
            fatal_lang_error('utf8_db_version_too_low');
        }
        // Use the messages.body column as indicator for the database charset.
        $request = $smcFunc['db_query']('', '
			SHOW FULL COLUMNS
			FROM {db_prefix}messages
			LIKE {string:body_like}', array('body_like' => 'body'));
        $column_info = $smcFunc['db_fetch_assoc']($request);
        $smcFunc['db_free_result']($request);
        // A collation looks like latin1_swedish. We only need the character set.
        list($context['database_charset']) = explode('_', $column_info['Collation']);
        $context['database_charset'] = in_array($context['database_charset'], $charsets) ? array_search($context['database_charset'], $charsets) : $context['database_charset'];
        // No need to convert to UTF-8 if it already is.
        if ($db_character_set === 'utf8' && !empty($modSettings['global_character_set']) && $modSettings['global_character_set'] === 'UTF-8') {
            fatal_lang_error('utf8_already_utf8');
        }
        // Detect whether a fulltext index is set.
        db_extend('search');
        if ($smcFunc['db_search_support']('fulltext')) {
            require_once $sourcedir . '/ManageSearch.php';
            detectFulltextIndex();
        }
        // Cannot do conversion if using a fulltext index
        if (!empty($modSettings['search_index']) && $modSettings['search_index'] == 'fulltext' || !empty($context['fulltext_index'])) {
            fatal_lang_error('utf8_cannot_convert_fulltext');
        }
        // Grab the character set from the default language file.
        loadLanguage('index', $language, true);
        $context['charset_detected'] = $txt['lang_character_set'];
        $context['charset_about_detected'] = sprintf($txt['utf8_detected_charset'], $language, $context['charset_detected']);
        // Go back to your own language.
        loadLanguage('index', $user_info['language'], true);
        // Show a warning if the character set seems not to be supported.
        if (!isset($charsets[strtr(strtolower($context['charset_detected']), array('utf' => 'UTF', 'iso' => 'ISO'))])) {
            $context['charset_warning'] = sprintf($txt['utf8_charset_not_supported'], $txt['lang_character_set']);
            // Default to ISO-8859-1.
            $context['charset_detected'] = 'ISO-8859-1';
        }
        $context['charset_list'] = array_keys($charsets);
        $context['page_title'] = $txt['utf8_title'];
        $context['sub_template'] = 'convert_utf8';
        createToken('admin-maint');
        return;
    }
    // After this point we're starting the conversion. But first: session check.
    checkSession();
    validateToken('admin-maint');
    createToken('admin-maint');
    // Translation table for the character sets not native for MySQL.
    $translation_tables = array('windows-1255' => array('0x81' => '\'\'', '0x8A' => '\'\'', '0x8C' => '\'\'', '0x8D' => '\'\'', '0x8E' => '\'\'', '0x8F' => '\'\'', '0x90' => '\'\'', '0x9A' => '\'\'', '0x9C' => '\'\'', '0x9D' => '\'\'', '0x9E' => '\'\'', '0x9F' => '\'\'', '0xCA' => '\'\'', '0xD9' => '\'\'', '0xDA' => '\'\'', '0xDB' => '\'\'', '0xDC' => '\'\'', '0xDD' => '\'\'', '0xDE' => '\'\'', '0xDF' => '\'\'', '0xFB' => '\'\'', '0xFC' => '\'\'', '0xFF' => '\'\'', '0xC2' => '0xFF', '0x80' => '0xFC', '0xE2' => '0xFB', '0xA0' => '0xC2A0', '0xA1' => '0xC2A1', '0xA2' => '0xC2A2', '0xA3' => '0xC2A3', '0xA5' => '0xC2A5', '0xA6' => '0xC2A6', '0xA7' => '0xC2A7', '0xA8' => '0xC2A8', '0xA9' => '0xC2A9', '0xAB' => '0xC2AB', '0xAC' => '0xC2AC', '0xAD' => '0xC2AD', '0xAE' => '0xC2AE', '0xAF' => '0xC2AF', '0xB0' => '0xC2B0', '0xB1' => '0xC2B1', '0xB2' => '0xC2B2', '0xB3' => '0xC2B3', '0xB4' => '0xC2B4', '0xB5' => '0xC2B5', '0xB6' => '0xC2B6', '0xB7' => '0xC2B7', '0xB8' => '0xC2B8', '0xB9' => '0xC2B9', '0xBB' => '0xC2BB', '0xBC' => '0xC2BC', '0xBD' => '0xC2BD', '0xBE' => '0xC2BE', '0xBF' => '0xC2BF', '0xD7' => '0xD7B3', '0xD1' => '0xD781', '0xD4' => '0xD7B0', '0xD5' => '0xD7B1', '0xD6' => '0xD7B2', '0xE0' => '0xD790', '0xEA' => '0xD79A', '0xEC' => '0xD79C', '0xED' => '0xD79D', '0xEE' => '0xD79E', '0xEF' => '0xD79F', '0xF0' => '0xD7A0', '0xF1' => '0xD7A1', '0xF2' => '0xD7A2', '0xF3' => '0xD7A3', '0xF5' => '0xD7A5', '0xF6' => '0xD7A6', '0xF7' => '0xD7A7', '0xF8' => '0xD7A8', '0xF9' => '0xD7A9', '0x82' => '0xE2809A', '0x84' => '0xE2809E', '0x85' => '0xE280A6', '0x86' => '0xE280A0', '0x87' => '0xE280A1', '0x89' => '0xE280B0', '0x8B' => '0xE280B9', '0x93' => '0xE2809C', '0x94' => '0xE2809D', '0x95' => '0xE280A2', '0x97' => '0xE28094', '0x99' => '0xE284A2', '0xC0' => '0xD6B0', '0xC1' => '0xD6B1', '0xC3' => '0xD6B3', '0xC4' => '0xD6B4', '0xC5' => '0xD6B5', '0xC6' => '0xD6B6', '0xC7' => '0xD6B7', '0xC8' => '0xD6B8', '0xC9' => '0xD6B9', '0xCB' => '0xD6BB', '0xCC' => '0xD6BC', '0xCD' => '0xD6BD', '0xCE' => '0xD6BE', '0xCF' => '0xD6BF', '0xD0' => '0xD780', '0xD2' => '0xD782', '0xE3' => '0xD793', '0xE4' => '0xD794', '0xE5' => '0xD795', '0xE7' => '0xD797', '0xE9' => '0xD799', '0xFD' => '0xE2808E', '0xFE' => '0xE2808F', '0x92' => '0xE28099', '0x83' => '0xC692', '0xD3' => '0xD783', '0x88' => '0xCB86', '0x98' => '0xCB9C', '0x91' => '0xE28098', '0x96' => '0xE28093', '0xBA' => '0xC3B7', '0x9B' => '0xE280BA', '0xAA' => '0xC397', '0xA4' => '0xE282AA', '0xE1' => '0xD791', '0xE6' => '0xD796', '0xE8' => '0xD798', '0xEB' => '0xD79B', '0xF4' => '0xD7A4', '0xFA' => '0xD7AA', '0xFF' => '0xD6B2', '0xFC' => '0xE282AC', '0xFB' => '0xD792'), 'windows-1253' => array('0x81' => '\'\'', '0x88' => '\'\'', '0x8A' => '\'\'', '0x8C' => '\'\'', '0x8D' => '\'\'', '0x8E' => '\'\'', '0x8F' => '\'\'', '0x90' => '\'\'', '0x98' => '\'\'', '0x9A' => '\'\'', '0x9C' => '\'\'', '0x9D' => '\'\'', '0x9E' => '\'\'', '0x9F' => '\'\'', '0xAA' => '\'\'', '0xD2' => '\'\'', '0xFF' => '\'\'', '0xCE' => '0xCE9E', '0xB8' => '0xCE88', '0xBA' => '0xCE8A', '0xBC' => '0xCE8C', '0xBE' => '0xCE8E', '0xBF' => '0xCE8F', '0xC0' => '0xCE90', '0xC8' => '0xCE98', '0xCA' => '0xCE9A', '0xCC' => '0xCE9C', '0xCD' => '0xCE9D', '0xCF' => '0xCE9F', '0xDA' => '0xCEAA', '0xE8' => '0xCEB8', '0xEA' => '0xCEBA', '0xEC' => '0xCEBC', '0xEE' => '0xCEBE', '0xEF' => '0xCEBF', '0xC2' => '0xFF', '0xBD' => '0xC2BD', '0xED' => '0xCEBD', '0xB2' => '0xC2B2', '0xA0' => '0xC2A0', '0xA3' => '0xC2A3', '0xA4' => '0xC2A4', '0xA5' => '0xC2A5', '0xA6' => '0xC2A6', '0xA7' => '0xC2A7', '0xA8' => '0xC2A8', '0xA9' => '0xC2A9', '0xAB' => '0xC2AB', '0xAC' => '0xC2AC', '0xAD' => '0xC2AD', '0xAE' => '0xC2AE', '0xB0' => '0xC2B0', '0xB1' => '0xC2B1', '0xB3' => '0xC2B3', '0xB5' => '0xC2B5', '0xB6' => '0xC2B6', '0xB7' => '0xC2B7', '0xBB' => '0xC2BB', '0xE2' => '0xCEB2', '0x80' => '0xD2', '0x82' => '0xE2809A', '0x84' => '0xE2809E', '0x85' => '0xE280A6', '0x86' => '0xE280A0', '0xA1' => '0xCE85', '0xA2' => '0xCE86', '0x87' => '0xE280A1', '0x89' => '0xE280B0', '0xB9' => '0xCE89', '0x8B' => '0xE280B9', '0x91' => '0xE28098', '0x99' => '0xE284A2', '0x92' => '0xE28099', '0x93' => '0xE2809C', '0x94' => '0xE2809D', '0x95' => '0xE280A2', '0x96' => '0xE28093', '0x97' => '0xE28094', '0x9B' => '0xE280BA', '0xAF' => '0xE28095', '0xB4' => '0xCE84', '0xC1' => '0xCE91', '0xC3' => '0xCE93', '0xC4' => '0xCE94', '0xC5' => '0xCE95', '0xC6' => '0xCE96', '0x83' => '0xC692', '0xC7' => '0xCE97', '0xC9' => '0xCE99', '0xCB' => '0xCE9B', '0xD0' => '0xCEA0', '0xD1' => '0xCEA1', '0xD3' => '0xCEA3', '0xD4' => '0xCEA4', '0xD5' => '0xCEA5', '0xD6' => '0xCEA6', '0xD7' => '0xCEA7', '0xD8' => '0xCEA8', '0xD9' => '0xCEA9', '0xDB' => '0xCEAB', '0xDC' => '0xCEAC', '0xDD' => '0xCEAD', '0xDE' => '0xCEAE', '0xDF' => '0xCEAF', '0xE0' => '0xCEB0', '0xE1' => '0xCEB1', '0xE3' => '0xCEB3', '0xE4' => '0xCEB4', '0xE5' => '0xCEB5', '0xE6' => '0xCEB6', '0xE7' => '0xCEB7', '0xE9' => '0xCEB9', '0xEB' => '0xCEBB', '0xF0' => '0xCF80', '0xF1' => '0xCF81', '0xF2' => '0xCF82', '0xF3' => '0xCF83', '0xF4' => '0xCF84', '0xF5' => '0xCF85', '0xF6' => '0xCF86', '0xF7' => '0xCF87', '0xF8' => '0xCF88', '0xF9' => '0xCF89', '0xFA' => '0xCF8A', '0xFB' => '0xCF8B', '0xFC' => '0xCF8C', '0xFD' => '0xCF8D', '0xFE' => '0xCF8E', '0xFF' => '0xCE92', '0xD2' => '0xE282AC'));
    // Make some preparations.
    if (isset($translation_tables[$_POST['src_charset']])) {
        $replace = '%field%';
        foreach ($translation_tables[$_POST['src_charset']] as $from => $to) {
            $replace = 'REPLACE(' . $replace . ', ' . $from . ', ' . $to . ')';
        }
    }
    // Grab a list of tables.
    if (preg_match('~^`(.+?)`\\.(.+?)$~', $db_prefix, $match) === 1) {
        $queryTables = $smcFunc['db_query']('', '
			SHOW TABLE STATUS
			FROM `' . strtr($match[1], array('`' => '')) . '`
			LIKE {string:table_name}', array('table_name' => str_replace('_', '\\_', $match[2]) . '%'));
    } else {
        $queryTables = $smcFunc['db_query']('', '
			SHOW TABLE STATUS
			LIKE {string:table_name}', array('table_name' => str_replace('_', '\\_', $db_prefix) . '%'));
    }
    while ($table_info = $smcFunc['db_fetch_assoc']($queryTables)) {
        // Just to make sure it doesn't time out.
        if (function_exists('apache_reset_timeout')) {
            @apache_reset_timeout();
        }
        $table_charsets = array();
        // Loop through each column.
        $queryColumns = $smcFunc['db_query']('', '
			SHOW FULL COLUMNS
			FROM ' . $table_info['Name'], array());
        while ($column_info = $smcFunc['db_fetch_assoc']($queryColumns)) {
            // Only text'ish columns have a character set and need converting.
            if (strpos($column_info['Type'], 'text') !== false || strpos($column_info['Type'], 'char') !== false) {
                $collation = empty($column_info['Collation']) || $column_info['Collation'] === 'NULL' ? $table_info['Collation'] : $column_info['Collation'];
                if (!empty($collation) && $collation !== 'NULL') {
                    list($charset) = explode('_', $collation);
                    if (!isset($table_charsets[$charset])) {
                        $table_charsets[$charset] = array();
                    }
                    $table_charsets[$charset][] = $column_info;
                }
            }
        }
        $smcFunc['db_free_result']($queryColumns);
        // Only change the column if the data doesn't match the current charset.
        if (count($table_charsets) === 1 && key($table_charsets) !== $charsets[$_POST['src_charset']] || count($table_charsets) > 1) {
            $updates_blob = '';
            $updates_text = '';
            foreach ($table_charsets as $charset => $columns) {
                if ($charset !== $charsets[$_POST['src_charset']]) {
                    foreach ($columns as $column) {
                        $updates_blob .= '
							CHANGE COLUMN `' . $column['Field'] . '` `' . $column['Field'] . '` ' . strtr($column['Type'], array('text' => 'blob', 'char' => 'binary')) . ($column['Null'] === 'YES' ? ' NULL' : ' NOT NULL') . (strpos($column['Type'], 'char') === false ? '' : ' default \'' . $column['Default'] . '\'') . ',';
                        $updates_text .= '
							CHANGE COLUMN `' . $column['Field'] . '` `' . $column['Field'] . '` ' . $column['Type'] . ' CHARACTER SET ' . $charsets[$_POST['src_charset']] . ($column['Null'] === 'YES' ? '' : ' NOT NULL') . (strpos($column['Type'], 'char') === false ? '' : ' default \'' . $column['Default'] . '\'') . ',';
                    }
                }
            }
            // Change the columns to binary form.
            $smcFunc['db_query']('', '
				ALTER TABLE {raw:table_name}{raw:updates_blob}', array('table_name' => $table_info['Name'], 'updates_blob' => substr($updates_blob, 0, -1)));
            // Convert the character set if MySQL has no native support for it.
            if (isset($translation_tables[$_POST['src_charset']])) {
                $update = '';
                foreach ($table_charsets as $charset => $columns) {
                    foreach ($columns as $column) {
                        $update .= '
							' . $column['Field'] . ' = ' . strtr($replace, array('%field%' => $column['Field'])) . ',';
                    }
                }
                $smcFunc['db_query']('', '
					UPDATE {raw:table_name}
					SET {raw:updates}', array('table_name' => $table_info['Name'], 'updates' => substr($update, 0, -1)));
            }
            // Change the columns back, but with the proper character set.
            $smcFunc['db_query']('', '
				ALTER TABLE {raw:table_name}{raw:updates_text}', array('table_name' => $table_info['Name'], 'updates_text' => substr($updates_text, 0, -1)));
        }
        // Now do the actual conversion (if still needed).
        if ($charsets[$_POST['src_charset']] !== 'utf8') {
            $smcFunc['db_query']('', '
				ALTER TABLE {raw:table_name}
				CONVERT TO CHARACTER SET utf8', array('table_name' => $table_info['Name']));
        }
    }
    $smcFunc['db_free_result']($queryTables);
    call_integration_hook('integrate_convert_utf8');
    // Let the settings know we have a new character set.
    updateSettings(array('global_character_set' => 'UTF-8', 'previousCharacterSet' => empty($translation_tables[$_POST['src_charset']]) ? $charsets[$_POST['src_charset']] : $translation_tables[$_POST['src_charset']]));
    // Store it in Settings.php too because it's needed before db connection.
    require_once $sourcedir . '/Subs-Admin.php';
    updateSettingsFile(array('db_character_set' => '\'utf8\''));
    // The conversion might have messed up some serialized strings. Fix them!
    require_once $sourcedir . '/Subs-Charset.php';
    fix_serialized_columns();
    redirectexit('action=admin;area=maintain;done=convertutf8');
}