Beispiel #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');
}
Beispiel #2
0
function EditSearchMethod()
{
    global $txt, $context, $modSettings, $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';
    // Load any apis.
    $context['search_apis'] = loadSearchAPIs();
    if (!empty($_REQUEST['sa']) && $_REQUEST['sa'] == 'removecustom') {
        checkSession('get');
        db_extend();
        $tables = smf_db_list_tables(false, $db_prefix . 'log_search_words');
        if (!empty($tables)) {
            smf_db_query('
				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();
        updateSettings(array('search_index' => empty($_POST['search_index']) || !in_array($_POST['search_index'], array('custom', 'sphinx', 'sphinxql')) && !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 = smf_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 = smf_db_query('
				SHOW TABLE STATUS
				LIKE {string:table_name}', array('table_name' => str_replace('_', '\\_', $db_prefix) . 'messages'));
        }
        if ($request !== false && mysql_num_rows($request) == 1) {
            // Only do this if the user has permission to execute this query.
            $row = mysql_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'];
            mysql_free_result($request);
        }
        // Now check the custom index table, if it exists at all.
        if (preg_match('~^`(.+?)`\\.(.+?)$~', $db_prefix, $match) !== 0) {
            $request = smf_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 = smf_db_query('
				SHOW TABLE STATUS
				LIKE {string:table_name}', array('table_name' => str_replace('_', '\\_', $db_prefix) . 'log_search_words'));
        }
        if ($request !== false && mysql_num_rows($request) == 1) {
            // Only do this if the user has permission to execute this query.
            $row = mysql_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'];
            mysql_free_result($request);
        }
    } 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'];
}