Skip to content

Commit

Permalink
Custom field id lookup by name now case-insensitive
Browse files Browse the repository at this point in the history
The $g_cache_name_to_id_map now uses lowercase custom field names as key
which prevents APPLICATION ERROR 1300 Custom field not found when using
a case-sensitive database.

This is a follow-up on issue #22555, which did not properly fix the
problem (see commit c612d8d).

Fixes #29413

Ported from master-2.25 branch commit 6b9f683,
replacing `if(isset())` construct by null-coalescing operator.
  • Loading branch information
dregad committed Dec 25, 2021
1 parent 2a47199 commit 1c4d152
Showing 1 changed file with 18 additions and 19 deletions.
37 changes: 18 additions & 19 deletions core/custom_field_api.php
Expand Up @@ -169,8 +169,9 @@ function custom_field_cache_array_rows( array $p_cf_id_array = null ) {
$t_ids_not_found = $c_cf_id_array;
while( $t_row = db_fetch_array( $t_result ) ) {
$c_id = (int)$t_row['id'];
$c_name = mb_strtolower($t_row['name']);
$g_cache_custom_field[$c_id] = $t_row;
$g_cache_name_to_id_map[$t_row['name']] = $c_id;
$g_cache_name_to_id_map[$c_name] = $c_id;
$g_cache_custom_field[$c_id]['linked_projects'] = array();
unset( $t_ids_not_found[$c_id] );
}
Expand Down Expand Up @@ -776,9 +777,12 @@ function custom_field_delete_all_values( $p_bug_id ) {

/**
* Get the id of the custom field with the specified name.
* false is returned if no custom field found with the specified name.
*
* Custom field name lookup is case insensitive. Returns false if no custom
* field is found with the specified name.
*
* @param string $p_field_name Custom field name.
* @return boolean|integer false or custom field id
* @return int|false custom field id
* @access public
*/
function custom_field_get_id_from_name( $p_field_name ) {
Expand All @@ -788,24 +792,19 @@ function custom_field_get_id_from_name( $p_field_name ) {
return false;
}

if( isset( $g_cache_name_to_id_map[$p_field_name] ) ) {
return $g_cache_name_to_id_map[$p_field_name];
}

db_param_push();
$t_query = 'SELECT id FROM {custom_field} WHERE name=' . db_param();
$t_result = db_query( $t_query, array( $p_field_name ) );

$t_row = db_fetch_array( $t_result );

if( !$t_row ) {
$g_cache_name_to_id_map[$p_field_name] = false;
return false;
$p_field_name = mb_strtolower($p_field_name);
if( !isset( $g_cache_name_to_id_map[$p_field_name] ) ) {
# Build cache of lowercase custom fields names to id
if( !$g_cache_name_to_id_map ) {
$t_query = new DbQuery( "SELECT id, name FROM {custom_field}" );
foreach( $t_query->fetch_all() as $t_row ) {
$t_name = mb_strtolower($t_row['name']);
$g_cache_name_to_id_map[$t_name] = $t_row['id'];
}
}
}

$g_cache_name_to_id_map[$p_field_name] = $t_row['id'];

return $t_row['id'];
return $g_cache_name_to_id_map[$p_field_name] ?? false;
}

/**
Expand Down

0 comments on commit 1c4d152

Please sign in to comment.