From d08dbe65f8566b5ef422c3a79bfe1f59564d3d66 Mon Sep 17 00:00:00 2001 From: Jacob Hoover Date: Tue, 12 Apr 2011 19:56:56 -0500 Subject: [PATCH] Patch for #11687 off of the 1.2.4 tag, including the mod to report orphans when looking at my_view_page. --- bug_actiongroup.php | 8 +++- core/file_api.php | 72 +++++++++++++++++++++++++ my_view_page.php | 1 + orphans.php | 144 +++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 224 insertions(+), 1 deletions(-) create mode 100644 orphans.php diff --git a/bug_actiongroup.php b/bug_actiongroup.php index 7d72793..3068082 100644 --- a/bug_actiongroup.php +++ b/bug_actiongroup.php @@ -28,6 +28,8 @@ require_once( 'bug_api.php' ); + require_once( 'file_api.php' ); + auth_ensure_user_authenticated(); helper_begin_long_process(); @@ -101,7 +103,11 @@ if ( access_has_bug_level( config_get( 'move_bug_threshold' ), $t_bug_id ) ) { /** @todo we need to issue a helper_call_custom_function( 'issue_update_validate', array( $t_bug_id, $t_bug_data, $f_bugnote_text ) ); */ $f_project_id = gpc_get_int( 'project_id' ); - bug_set_field( $t_bug_id, 'project_id', $f_project_id ); + + // Attempt to move disk based attachments to new project file directory. + file_move_bug_attachments( $t_bug_id, $f_project_id ); + + bug_set_field( $t_bug_id, 'project_id', $f_project_id ); helper_call_custom_function( 'issue_update_notify', array( $t_bug_id ) ); } else { $t_failed_ids[$t_bug_id] = lang_get( 'bug_actiongroup_access' ); diff --git a/core/file_api.php b/core/file_api.php index 3c61769..b7dddf9 100644 --- a/core/file_api.php +++ b/core/file_api.php @@ -853,3 +853,75 @@ function file_get_extension( $p_filename ) { } return $t_extension; } + +/** + * Move any attachments as needed when a bug is moved from project to project. + * + * @param int $p_bug_id ID of bug containing attachments to be moved + * @param int $p_project_id_to destination project ID for the bug + * @return null + */ +function file_move_bug_attachments( $p_bug_id, $p_project_id_to ) { + $t_project_id_from = bug_get_field( $p_bug_id, 'project_id' ); + if ( $t_project_id_from == $p_project_id_to ) { + return; + } + + $t_method = config_get( 'file_upload_method' ); + if ( $t_method != DISK ) { + return; + } + + if ( !file_bug_has_attachments( $p_bug_id ) ) { + return; + } + + $t_path_from = project_get_field( $t_project_id_from, 'file_path' ); + if ( is_blank( $t_path_from ) ) { + $t_path_from = config_get( 'absolute_path_default_upload_folder', null, null, $t_project_id_from ); + } + file_ensure_valid_upload_path( $t_path_from ); + $t_path_to = project_get_field( $p_project_id_to, 'file_path' ); + if ( is_blank( $t_path_to ) ) { + $t_path_to = config_get( 'absolute_path_default_upload_folder', null, null, $p_project_id_to ); + } + file_ensure_valid_upload_path( $t_path_to ); + if ( $t_path_from == $t_path_to ) { + return; + } + + # Initialize the update query to update a single row + $t_bug_file_table = db_get_table( 'mantis_bug_file_table' ); + $c_bug_id = db_prepare_int( $p_bug_id ); + $query_disk_attachment_update = "UPDATE $t_bug_file_table + SET folder=" . db_param() . " + WHERE bug_id=" . db_param() . " + AND id =" . db_param(); + + $t_attachment_rows = bug_get_attachments( $p_bug_id ); + $t_attachments_count = count( $t_attachment_rows ); + for ( $i = 0; $i < $t_attachments_count; $i++ ) { + $t_row = $t_attachment_rows[$i]; + $t_basename = basename( $t_row['diskfile'] ); + + $t_disk_file_name_from = file_path_combine( $t_path_from, $t_basename ); + $t_disk_file_name_to = file_path_combine( $t_path_to, $t_basename ); + + if ( file_exists($t_disk_file_name_from) ) { + if ( !file_exists( $t_disk_file_name_to ) ) { + chmod( $t_disk_file_name_from, 0775 ); + if ( !rename( $t_disk_file_name_from, $t_disk_file_name_to ) ) { + if ( !copy( $t_disk_file_name_from, $t_disk_file_name_to ) ) { + trigger_error( FILE_MOVE_FAILED, ERROR ); + } + file_delete_local( $t_disk_file_name_from ); + } + chmod( $t_disk_file_name_to, config_get( 'attachments_file_permissions' ) ); + db_query_bound( $query_disk_attachment_update, Array( db_prepare_string( $t_path_to ), $c_bug_id, db_prepare_int( $t_row['id'] ) ) ); + } else { + trigger_error( ERROR_FILE_DUPLICATE, ERROR ); + } + } + } +} + diff --git a/my_view_page.php b/my_view_page.php index d03764a..b315165 100644 --- a/my_view_page.php +++ b/my_view_page.php @@ -171,4 +171,5 @@ "; + print_bug_link($c_bug_id); + echo "$t_disk_file_name_from$filename$folder"; + } + } +} + +set_time_limit(60 * 2); +//error_log('calling bug_get_bugs'); +$project_ids = bug_get_bugs(); +$rowcount = count($project_ids); +//error_log("Found $rowcount bugs"); +echo ""; +for ( $i = 0; $i < $rowcount; $i++ ) { + file_verify_bug_attachments( $project_ids[$i]['bug_id'] ); + //if ($i % 1000 == 0) { + //error_log("Processing $i of $rowcount"); + //} +} +echo "
IDFileFileNamefolder
"; +?> -- 1.7.0.2.msysgit.0