/**
 * Once upon a time multi-select custom field types (checkbox and multiselect)
 * were stored in the database in the format of "option1|option2|option3" where
 * they should have been stored in a format of "|option1|option2|option3|".
 * Additionally, radio custom field types were being stored in the database
 * with an unnecessary vertical pipe prefix and suffix when there is only ever
 * one possible value that can be assigned to a radio field.
 */
function install_correct_multiselect_custom_fields_db_format()
{
    # Disable query logging even if enabled in config, due to possibility of mass spam
    $t_log_queries = install_set_log_queries();
    $t_value_table = db_get_table('mantis_custom_field_string_table');
    $t_field_table = db_get_table('mantis_custom_field_table');
    # Ensure multilist and checkbox custom field values have a vertical pipe |
    # as a prefix and suffix.
    $t_query = "SELECT v.field_id, v.bug_id, v.value from {$t_value_table} v\n\t\tLEFT JOIN {$t_field_table} c\n\t\tON v.field_id = c.id\n\t\tWHERE (c.type = " . CUSTOM_FIELD_TYPE_MULTILIST . " OR c.type = " . CUSTOM_FIELD_TYPE_CHECKBOX . ")\n\t\t\tAND v.value != ''\n\t\t\tAND v.value NOT LIKE '|%|'";
    $t_result = db_query_bound($t_query);
    while ($t_row = db_fetch_array($t_result)) {
        $c_field_id = (int) $t_row['field_id'];
        $c_bug_id = (int) $t_row['bug_id'];
        $c_value = '|' . rtrim(ltrim($t_row['value'], '|'), '|') . '|';
        $t_update_query = "UPDATE {$t_value_table}\n\t\t\tSET value = '{$c_value}'\n\t\t\tWHERE field_id = {$c_field_id}\n\t\t\t\tAND bug_id = {$c_bug_id}";
        $t_update_result = db_query_bound($t_update_query);
    }
    # Remove vertical pipe | prefix and suffix from radio custom field values.
    $t_query = "SELECT v.field_id, v.bug_id, v.value from {$t_value_table} v\n\t\tLEFT JOIN {$t_field_table} c\n\t\tON v.field_id = c.id\n\t\tWHERE c.type = " . CUSTOM_FIELD_TYPE_RADIO . "\n\t\t\tAND v.value != ''\n\t\t\tAND v.value LIKE '|%|'";
    $t_result = db_query_bound($t_query);
    while ($t_row = db_fetch_array($t_result)) {
        $c_field_id = (int) $t_row['field_id'];
        $c_bug_id = (int) $t_row['bug_id'];
        $c_value = rtrim(ltrim($t_row['value'], '|'), '|');
        $t_update_query = "UPDATE {$t_value_table}\n\t\t\tSET value = '{$c_value}'\n\t\t\tWHERE field_id = {$c_field_id}\n\t\t\t\tAND bug_id = {$c_bug_id}";
        $t_update_result = db_query_bound($t_update_query);
    }
    # Re-enable query logging if we disabled it
    install_set_log_queries($t_log_queries);
    # Return 2 because that's what ADOdb/DataDict does when things happen properly
    return 2;
}
/**
 * History table's field name column used to be 32 chars long (before 1.1.0a4),
 * while custom field names can be up to 64. This function updates history
 * records related to long custom fields to store the complete field name
 * instead of the truncated version
 *
 *
 * @return int 2, because that's what ADOdb/DataDict does when things happen properly
 */
function install_update_history_long_custom_fields()
{
    # Disable query logging even if enabled in config, due to possibility of mass spam
    $t_log_queries = install_set_log_queries();
    # Build list of custom field names longer than 32 chars for reference
    $t_custom_field_table = db_get_table('custom_field');
    $t_query = "SELECT name FROM {$t_custom_field_table}";
    $t_result = db_query_bound($t_query);
    while ($t_field = db_fetch_array($t_result)) {
        if (utf8_strlen($t_field[0]) > 32) {
            $t_custom_fields[utf8_substr($t_field[0], 0, 32)] = $t_field[0];
        }
    }
    if (!isset($t_custom_fields)) {
        # There are no custom fields longer than 32, nothing to do
        # Re-enable query logging if we disabled it
        install_set_log_queries($t_log_queries);
        return 2;
    }
    # Build list of standard fields to filter out from history
    # Fields mapping: category_id is actually logged in history as 'category'
    $t_standard_fields = array_replace(columns_get_standard(false), array('category_id'), array('category'));
    $t_field_list = "";
    foreach ($t_standard_fields as $t_field) {
        $t_field_list .= "'{$t_field}', ";
    }
    $t_field_list = rtrim($t_field_list, ', ');
    # Get the list of custom fields from the history table
    $t_history_table = db_get_table('bug_history');
    $t_query = "SELECT DISTINCT field_name\n\t\tFROM {$t_history_table}\n\t\tWHERE type = " . NORMAL_TYPE . "\n\t\tAND   field_name NOT IN ( {$t_field_list} )";
    $t_result = db_query_bound($t_query);
    # For each entry, update the truncated custom field name with its full name
    # if a matching custom field exists
    while ($t_field = db_fetch_array($t_result)) {
        # If field name's length is 32, then likely it was truncated so we try to match
        if (utf8_strlen($t_field[0]) == 32 && array_key_exists($t_field[0], $t_custom_fields)) {
            # Match found, update all history records with this field name
            $t_update_query = "UPDATE {$t_history_table}\n\t\t\t\tSET field_name = " . db_param() . "\n\t\t\t\tWHERE field_name = " . db_param();
            db_query_bound($t_update_query, array($t_custom_fields[$t_field[0]], $t_field[0]));
        }
    }
    # Re-enable query logging if we disabled it
    install_set_log_queries($t_log_queries);
    return 2;
}
/**
 * History table's field name column used to be 32 chars long (before 1.1.0a4),
 * while custom field names can be up to 64. This function updates history
 * records related to long custom fields to store the complete field name
 * instead of the truncated version
 *
 *
 * @return integer 2, because that's what ADOdb/DataDict does when things happen properly
 */
function install_update_history_long_custom_fields()
{
    # Disable query logging even if enabled in config, due to possibility of mass spam
    $t_log_queries = install_set_log_queries();
    # Build list of custom field names longer than 32 chars for reference
    $t_query = 'SELECT name FROM {custom_field}';
    $t_result = db_query($t_query);
    while ($t_field = db_fetch_array($t_result)) {
        if (utf8_strlen($t_field['name']) > 32) {
            $t_custom_fields[utf8_substr($t_field['name'], 0, 32)] = $t_field['name'];
        }
    }
    if (!isset($t_custom_fields)) {
        # There are no custom fields longer than 32, nothing to do
        # Re-enable query logging if we disabled it
        install_set_log_queries($t_log_queries);
        return 2;
    }
    # Build list of standard fields to filter out from history
    # This is as per result of columns_get_standard() at the time of this schema update
    # Fields mapping: category_id is actually logged in history as 'category'
    $t_standard_fields = array('id', 'project_id', 'reporter_id', 'handler_id', 'priority', 'severity', 'eta', 'os', 'reproducibility', 'status', 'resolution', 'projection', 'category', 'date_submitted', 'last_updated', 'os_build', 'platform', 'version', 'fixed_in_version', 'target_version', 'build', 'view_state', 'summary', 'sponsorship_total', 'due_date', 'description', 'steps_to_reproduce', 'additional_information', 'attachment_count', 'bugnotes_count', 'selection', 'edit', 'overdue');
    $t_field_list = '';
    foreach ($t_standard_fields as $t_field) {
        $t_field_list .= '\'' . $t_field . '\', ';
    }
    $t_field_list = rtrim($t_field_list, ', ');
    # Get the list of custom fields from the history table
    $t_query = 'SELECT DISTINCT field_name
		FROM {bug_history}
		WHERE type = ' . NORMAL_TYPE . '
		AND field_name NOT IN ( ' . $t_field_list . ' )';
    $t_result = db_query($t_query);
    # For each entry, update the truncated custom field name with its full name
    # if a matching custom field exists
    while ($t_field = db_fetch_array($t_result)) {
        # If field name's length is 32, then likely it was truncated so we try to match
        if (utf8_strlen($t_field['field_name']) == 32 && array_key_exists($t_field['field_name'], $t_custom_fields)) {
            # Match found, update all history records with this field name
            $t_update_query = 'UPDATE {bug_history}
				SET field_name = ' . db_param() . '
				WHERE field_name = ' . db_param();
            db_query($t_update_query, array($t_custom_fields[$t_field['field_name']], $t_field['field_name']));
        }
    }
    # Re-enable query logging if we disabled it
    install_set_log_queries($t_log_queries);
    return 2;
}