. /** * This upgrade moves attachments from the database to the disk * @package MantisBT * @copyright Copyright (C) 2000 - 2002 Kenzaburo Ito - kenito@300baud.org * @copyright Copyright (C) 2002 - 2012 MantisBT Team - mantisbt-dev@lists.sourceforge.net * @link http://www.mantisbt.org */ /** * MantisBT Core API's */ require_once( dirname( dirname( __FILE__ ) ) . DIRECTORY_SEPARATOR . 'core.php' ); access_ensure_global_level( config_get_global( 'admin_site_threshold' ) ); // Move type should be attachment or project. $f_move_type = gpc_get( 'doc' ); function get_prefix( $file_path ) { if( substr( $file_path, 0, 1 ) == '/' ) { # Unix absolute return ''; } if( substr( $file_path, 0, 1 ) == '\\' ) { # Windows absolute return ''; } if( substr( $file_path, 1, 2 ) == ':\\' ) { # Windows absolute return ''; } return dirname( dirname( __FILE__ ) ) . DIRECTORY_SEPARATOR; } # ------ move file attachments to issues from database to disk # select non-empty data fields # match with the project to get the file path # store the file in the correct folder # # Assumptions: only supports storage in local file system (not FTP) # file paths are set up and working # # Re-running this is safe because the data # is not removed from the database until it is successfully copied. # function upgrade_move_att2disk( $p_source ) { # $p_source is the string "attachment" or "project" if( $p_source == 'attachment' ) { $t_file_table = db_get_table( 'mantis_bug_file_table' ); $t_bug_label = "Bug"; } if( $p_source == 'project' ) { $t_file_table = db_get_table( 'mantis_project_file_table' ); $t_bug_label = "Project"; } # check that the source was valid if( !isset( $t_file_table ) ) { echo 'Failure: Internal Error: File source not set'; return; } # check that the destination is set up properly $t_upload_method = config_get_global( 'file_upload_method' ); if( $t_upload_method <> DISK ) { echo 'Failure: Upload Method is not DISK'; return; } $query = 'SELECT * FROM ' . $t_file_table . ' WHERE content <> \'\''; $result = @db_query_bound( $query ); if( false == $result ) { echo '

No attachments need to be moved.

'; return; } $count = db_num_rows( $result ); echo '

Found ' . $count . ' attachments to be moved.

'; $t_failures = 0; if( $count > 0 ) { echo ''; # Headings echo ''; } for( $i = 0;$i < $count;$i++ ) { $t_row = db_fetch_array( $result ); // trace bug id back to project to determine the proper file path if( $p_source == 'attachment' ) { $t_project_id = bug_get_field( $t_row['bug_id'], 'project_id' ); $t_bug_id = $t_row['bug_id']; } else { $t_project_id = (int) $t_row['project_id']; $t_bug_id = $t_project_id; } $t_file_path = project_get_field( $t_project_id, 'file_path' ); $prefix = get_prefix( $t_file_path ); $t_real_file_path = $prefix . $t_file_path; $c_filename = file_clean_name( $t_row['filename'] ); printf( "\n'; } echo '
' . $t_bug_label . 'AttachmentStatus
%8d%s", helper_alternate_class(), $t_bug_id, $t_row['filename'] ); if( is_blank( $t_real_file_path ) || !file_exists( $t_real_file_path ) || !is_dir( $t_real_file_path ) || !is_writable( $t_real_file_path ) ) { echo 'Destination ' . $t_real_file_path . ' not writable'; $t_failures++; } else { $t_file_name = $t_real_file_path . $c_filename; // write file to disk store after adjusting the path if( file_put_contents( $t_file_name, $t_row['content'] ) ) { // successful, update database /** @todo do we want to check the size of data transfer matches here? */ $c_new_file_name = $t_file_path . $c_filename; $query2 = "UPDATE $t_file_table SET diskfile = " . db_param() . ", folder = " . db_param() . ", content = '' WHERE id = " . db_param(); $update = @db_query_bound( $query2, Array( $c_new_file_name, $t_file_path, $t_row['id'] ) ); if( !$update ) { echo 'database update failed'; $t_failures++; } else { echo 'moved to ' . $t_file_name; } } else { echo 'copy to ' . $t_file_name . ' failed'; $t_failures++; } } echo '

' . $count . ' attachments processed, ' . $t_failures . ' failures'; } # --------------------- # main code # if( $f_move_type == 'attachment' ) { $t_type = 'Attachments'; } else { if( $f_move_type == 'project' ) { $t_type = 'Project Files'; } else { echo "

Invalid value '$f_move_type' for parameter 'doc'.

"; exit; } } ?> MantisBT Administration - Move <?php echo $t_type?> to Disk
Move to Disk


Completed...

'; ?>