/** * Moves attachments from the specified list of projects from database to disk * @param string $p_type Attachment type ('bug' or 'project') * @param array $p_projects List of projects to process * @return array summary of moves per project */ function move_attachments_to_disk($p_type, $p_projects) { if (empty($p_projects)) { return array(); } # Build the SQL query based on attachment type $t_file_table = db_get_table("mantis_{$p_type}_file_table"); switch ($p_type) { case 'project': $t_query = "SELECT f.*\n\t\t\t\tFROM {$t_file_table} f\n\t\t\t\tWHERE content <> ''\n\t\t\t\t AND f.project_id = " . db_param() . "\n\t\t\t\tORDER BY f.filename"; break; case 'bug': $t_bug_table = db_get_table('mantis_bug_table'); $t_query = "SELECT f.*\n\t\t\t\tFROM {$t_file_table} f\n\t\t\t\tJOIN {$t_bug_table} b ON b.id = f.bug_id\n\t\t\t\tWHERE content <> ''\n\t\t\t\t AND b.project_id = " . db_param() . "\n\t\t\t\tORDER BY f.bug_id, f.filename"; break; } # Process projects list foreach ($p_projects as $t_project) { # Retrieve attachments for the project $t_result = db_query_bound($t_query, array($t_project)); # Project upload path $t_upload_path = project_get_upload_path($t_project); if (is_blank($t_upload_path) || !file_exists($t_upload_path) || !is_dir($t_upload_path) || !is_writable($t_upload_path)) { # Invalid path $t_failures = db_num_rows($t_result); $t_data = "ERROR: Upload path '{$t_upload_path}' does not exist or is not writable"; } else { # Process attachments $t_failures = 0; $t_data = array(); if ($p_type == 'project') { $t_seed = config_get('document_files_prefix', null, ALL_USERS, $t_project) . $t_project; } while ($t_row = db_fetch_array($t_result)) { if ($p_type == 'bug') { $t_seed = $t_row['bug_id'] . $t_row['filename']; } $t_filename = $t_upload_path . file_generate_unique_name($t_seed, $t_upload_path); # write file to disk if (file_put_contents($t_filename, $t_row['content'])) { # successful, update database # @todo do we want to check the size of data transfer matches here? $t_update_query = "UPDATE {$t_file_table}\n\t\t\t\t\t\tSET diskfile = " . db_param() . ",\n\t\t\t\t\t\t\tfolder = " . db_param() . ",\n\t\t\t\t\t\t\tcontent = ''\n\t\t\t\t\t\tWHERE id = " . db_param(); $t_update_result = db_query_bound($t_update_query, array($t_filename, $t_upload_path, $t_row['id'])); if (!$t_update_result) { $t_status = 'Database update failed'; $t_failures++; } else { $t_status = "Moved to '{$t_filename}'"; } } else { $t_status = "Copy to '{$t_filename}' failed"; $t_failures++; } # Add the file and status to the list of processed attachments $t_file = array('id' => $t_row['id'], 'filename' => $t_row['filename'], 'status' => $t_status); if ($p_type == 'bug') { $t_file['bug_id'] = $t_row['bug_id']; } $t_data[] = $t_file; } } $t_moved[] = array('name' => project_get_name($t_project), 'path' => $t_upload_path, 'rows' => db_num_rows($t_result), 'failed' => $t_failures, 'data' => $t_data); } return $t_moved; }
/** * Moves attachments from the specified list of projects from database to disk * @param string $p_type Attachment type ('bug' or 'project'). * @param array $p_projects List of projects to process. * @return array summary of moves per project */ function move_attachments_to_disk($p_type, array $p_projects) { if (empty($p_projects)) { return array(); } # Build the SQL query based on attachment type switch ($p_type) { case 'project': $t_query = 'SELECT f.* FROM {project_file} f WHERE content <> \'\' AND f.project_id = ' . db_param() . ' ORDER BY f.filename'; break; case 'bug': $t_query = 'SELECT f.* FROM {bug_file} f JOIN {bug} b ON b.id = f.bug_id WHERE content <> \'\' AND b.project_id = ' . db_param() . ' ORDER BY f.bug_id, f.filename'; break; } # Process projects list foreach ($p_projects as $t_project) { # Retrieve attachments for the project $t_result = db_query($t_query, array($t_project)); # Project upload path $t_upload_path = project_get_upload_path($t_project); if (is_blank($t_upload_path) || !file_exists($t_upload_path) || !is_dir($t_upload_path) || !is_writable($t_upload_path)) { # Invalid path $t_failures = db_num_rows($t_result); $t_data = 'ERROR: Upload path \'' . $t_upload_path . '\' does not exist or is not writeable'; } else { # Process attachments $t_failures = 0; $t_data = array(); while ($t_row = db_fetch_array($t_result)) { $t_disk_filename = $t_upload_path . $t_row['diskfile']; if (file_exists($t_disk_filename)) { $t_status = 'Disk File Already Exists \'' . $t_disk_filename . '\''; $t_failures++; } else { # write file to disk if (file_put_contents($t_disk_filename, $t_row['content'])) { # successful, update database # @todo do we want to check the size of data transfer matches here? switch ($p_type) { case 'project': $t_update_query = 'UPDATE {project_file} SET folder = ' . db_param() . ', content = \'\' WHERE id = ' . db_param(); break; case 'bug': $t_update_query = 'UPDATE {bug_file} SET folder = ' . db_param() . ', content = \'\' WHERE id = ' . db_param(); break; } $t_update_result = db_query($t_update_query, array($t_upload_path, $t_row['id'])); if (!$t_update_result) { $t_status = 'Database update failed'; $t_failures++; } else { $t_status = 'Moved to \'' . $t_disk_filename . '\''; } } else { $t_status = 'Copy to \'' . $t_disk_filename . '\' failed'; $t_failures++; } } # Add the file and status to the list of processed attachments $t_file = array('id' => $t_row['id'], 'filename' => $t_row['filename'], 'status' => $t_status); if ($p_type == 'bug') { $t_file['bug_id'] = $t_row['bug_id']; } $t_data[] = $t_file; } } $t_moved[] = array('name' => project_get_name($t_project), 'path' => $t_upload_path, 'rows' => db_num_rows($t_result), 'failed' => $t_failures, 'data' => $t_data); } return $t_moved; }