Commit 1d05b380 authored by Chris Graham's avatar Chris Graham
Browse files

Fixed MANTIS-4339 (Implement code to rebuilt all CPF indexes)

parent 36e74be7
Loading
Loading
Loading
Loading
+0 −0

File changed.

Preview suppressed by a .gitattributes entry or the file's encoding is unsupported.

+7 −9
Original line number Diff line number Diff line
@@ -313,24 +313,22 @@ function install_cns($upgrade_from = null)
        $GLOBALS['FORUM_DB']->delete_index_if_exists('f_posts', 'posts_since');
        $GLOBALS['FORUM_DB']->create_index('f_posts', 'posts_since', array('p_time', 'p_cache_forum_id')); // p_cache_forum_id is used to not count PT posts

        // Fix up legacy issues with CPFs that we can no longer tolerate
        // Fix DB storage types
        $fields = $GLOBALS['FORUM_DB']->query_select('f_custom_fields', array('id', 'cf_type'));
        require_code('cns_members_action');
        foreach ($fields as $field) {
            $id = $field['id'];
            $type = $field['cf_type'];
            list($_type, $index) = get_cpf_storage_for($type);

            $id = $field['id'];

            $GLOBALS['FORUM_DB']->delete_index_if_exists('f_member_custom_fields', 'mcf' . strval($id));
            $GLOBALS['FORUM_DB']->delete_index_if_exists('f_member_custom_fields', '#mcf_ft_' . strval($id));

            if (substr(get_db_type(), 0, 5) == 'mysql') {
            if (strpos(get_db_type(), 'mysql') !== false) {
                $GLOBALS['SITE_DB']->query('SET sql_mode=\'\'', null, null, true); // Turn off strict mode
            }
            $GLOBALS['FORUM_DB']->alter_table_field('f_member_custom_fields', 'field_' . strval($id), $_type);

            build_cpf_indices($id, $index, $type, $_type);
        }

        require_code('cns_members_action2');
        rebuild_all_cpf_indices();
    }

    // If we have the forum installed to this db already, leave
+13 −9
Original line number Diff line number Diff line
@@ -669,11 +669,15 @@ function cns_make_custom_field($name, $locked = 0, $description = '', $default =
 * @param  boolean $index Whether an index is needed for search purposes (there may be other reasons though).
 * @param  ID_TEXT $type CPF type.
 * @param  ID_TEXT $_type Underlying field type.
 * @return boolean If the operation could succeed (false means limit hit)
 */
function build_cpf_indices($id, $index, $type, $_type)
{
    $indices_count = $GLOBALS['FORUM_DB']->query_select_value('db_meta_indices', 'COUNT(*)', array('i_table' => 'f_member_custom_fields'));
    if ($indices_count < 60) { // Could be 64 but trying to be careful here...
    if ($indices_count >= 60) { // Could be 64 but trying to be careful here...
        return false;
    }

    if ($index) {
        if ($_type != 'LONG_TEXT') {
            $GLOBALS['FORUM_DB']->create_index('f_member_custom_fields', 'mcf' . strval($id), array('field_' . strval($id)), 'mf_member_id');
@@ -684,5 +688,5 @@ function build_cpf_indices($id, $index, $type, $_type)
    } elseif ((strpos($type, 'trans') !== false) || ($type == 'posting_field')) { // for efficient joins
        $GLOBALS['FORUM_DB']->create_index('f_member_custom_fields', 'mcf' . strval($id), array('field_' . strval($id)), 'mf_member_id');
    }
    }
    return true;
}
+41 −0
Original line number Diff line number Diff line
@@ -2041,3 +2041,44 @@ function cns_delete_boiler_custom_field($field)
        cns_delete_custom_field($test);
    }
}

/**
 * Rebuild custom profile field indices.
 * It is possible for these to get in a mess, especially when upgrading from old versions, or tweaking which of the maximum of 60 to have.
 */
function rebuild_all_cpf_indices()
{
    $GLOBALS['NO_QUERY_LIMIT'] = true; // TODO: Change in v11

    $fields = $GLOBALS['FORUM_DB']->query_select('f_custom_fields', array('id', 'cf_type'), array(), 'ORDER BY cf_required+cf_show_on_join_form DESC,cf_public_view+cf_owner_set DESC,cf_order DESC'); // TODO: Respect searchable setting in v11

    // Delete existing indexes
    foreach ($fields as $field) {
        $id = $field['id'];

        $GLOBALS['FORUM_DB']->delete_index_if_exists('f_member_custom_fields', 'mcf' . strval($id));
        $GLOBALS['FORUM_DB']->delete_index_if_exists('f_member_custom_fields', '#mcf_ft_' . strval($id));
    }

    // Delete any stragglers (already deleted fields or inconsistent naming)
    $GLOBALS['FORUM_DB']->query_delete('db_meta_indices', array('i_table' => 'f_member_custom_fields'));
    if (strpos(get_db_type(), 'mysql') !== false) {
        $indexes = $GLOBALS['FORUM_DB']->query('SHOW INDEXES FROM ' . $GLOBALS['FORUM_DB']->get_table_prefix() . 'f_member_custom_fields WHERE Column_name<>\'mf_member_id\'');
        foreach ($indexes as $index) {
            $GLOBALS['FORUM_DB']->query('DROP INDEX ' . $index['Key_name'] . ' ON ' . $GLOBALS['FORUM_DB']->get_table_prefix() . 'f_member_custom_fields');
        }
    }

    // Rebuild indexes
    require_code('cns_members_action');
    foreach ($fields as $field) {
        $id = $field['id'];
        $type = $field['cf_type'];
        list($_type, $index) = get_cpf_storage_for($type);

        $okay = build_cpf_indices($id, $index, $type, $_type);
        if (!$okay) { // Limit was hit
            break;
        }
    }
}