Skip to content

Commit

Permalink
Add project id param to project_*_name_unique()
Browse files Browse the repository at this point in the history
This optional parameter is used to exclude the given id from the
uniqueness check, allowing correct behavior (i.e. avoid error 701) 
when updating an existing project.

This was a problem on MySQL, because with utf8_general_ci collation,
accented characters are treated as identical, i.e. 'The' == 'thé'.

Fixes #22479
  • Loading branch information
dregad committed Mar 8, 2017
1 parent 77330cb commit f8b2510
Showing 1 changed file with 17 additions and 12 deletions.
29 changes: 17 additions & 12 deletions core/project_api.php
Expand Up @@ -241,30 +241,35 @@ function project_ensure_exists( $p_project_id ) {

/**
* check to see if project exists by name
* @param string $p_name The project name.
* @param string $p_name The project name.
* @param integer $p_exclude_id Optional project id to exclude from the check,
* to allow uniqueness check when updating.
* @return boolean
*/
function project_is_name_unique( $p_name ) {
function project_is_name_unique( $p_name, $p_exclude_id = null ) {
db_param_push();
$t_query = 'SELECT COUNT(*) FROM {project} WHERE name=' . db_param();
$t_result = db_query( $t_query, array( $p_name ) );

if( 0 == db_result( $t_result ) ) {
return true;
} else {
return false;
$t_param = array( $p_name );
if( $p_exclude_id ) {
$t_query .= ' AND id <> ' . db_param();
$t_param[] = (int)$p_exclude_id;
}
$t_result = db_query( $t_query, $t_param );

return 0 == db_result( $t_result );
}

/**
* check to see if project exists by id
* if it doesn't exist then error
* otherwise let execution continue undisturbed
* @param string $p_name The project name.
* @param string $p_name The project name.
* @param integer $p_exclude_id Optional project id to exclude from the check,
* to allow uniqueness check when updating.
* @return void
*/
function project_ensure_name_unique( $p_name ) {
if( !project_is_name_unique( $p_name ) ) {
function project_ensure_name_unique( $p_name, $p_exclude_id = null ) {
if( !project_is_name_unique( $p_name, $p_exclude_id ) ) {
trigger_error( ERROR_PROJECT_NAME_NOT_UNIQUE, ERROR );
}
}
Expand Down Expand Up @@ -442,7 +447,7 @@ function project_update( $p_project_id, $p_name, $p_description, $p_status, $p_v
}

if( strcasecmp( $p_name, $t_old_name ) != 0 ) {
project_ensure_name_unique( $p_name );
project_ensure_name_unique( $p_name, $p_project_id );
}

if( DATABASE !== config_get( 'file_upload_method', null, null, $p_project_id ) ) {
Expand Down

0 comments on commit f8b2510

Please sign in to comment.