Changeset 1160
- Timestamp:
- 12/07/11 18:14:11 (6 months ago)
- File:
-
- 1 edited
-
trunk/scripts/check-attachments (modified) (5 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/scripts/check-attachments
r1071 r1160 9 9 log_warn("This script consumes tons of memory so errors might occur"); 10 10 11 $res = db_query("SELECT `id`, `name`, `page` FROM ia_file");11 $res = db_query("SELECT `id`, `name`, `page`, `size` FROM ia_file"); 12 12 13 13 $extra_files = array_flip(glob(IA_ROOT_DIR . "attach/*")); 14 15 14 $extra_atts = array(); 16 15 $total_files = count($extra_files); 17 16 $total_atts = 0; 17 $mismatched_sizes = array(); 18 18 19 19 // Check attachments. … … 26 26 $extra_atts[] = $att; 27 27 } 28 if (filesize($fname) != $att['size']) { 29 $att['file_size'] = filesize($fname); 30 $mismatched_sizes[] = $att; 31 } 28 32 } 29 33 … … 34 38 log_print("Database and attach dir are in perfect sync, $total_files total files."); 35 39 } else { 36 log_print("There are are".count($extra_files)." out of ".$total_files." disk files with no db entry.");40 log_print("There are ".count($extra_files)." out of ".$total_files." disk files with no db entry."); 37 41 if (count($extra_files)) { 38 42 if (read_bool("Do you want to see a list?", false)) { … … 48 52 } 49 53 50 log_print("There are are".count($extra_atts)." out of ".$total_atts." db files with no disk file.");54 log_print("There are ".count($extra_atts)." out of ".$total_atts." db files with no disk file."); 51 55 if (count($extra_atts)) { 52 56 if (read_bool("Do you want to see a list?", false)) { … … 65 69 unset($extra_atts); 66 70 unset($extra_files); 71 72 if (count($mismatched_sizes) != 0) { 73 log_print('There are ' . count($mismatched_sizes) . ' files with ' . 74 'mismatching sizes on disk and in the db'); 75 if (read_bool('Do you want to see a list?', false)) { 76 foreach ($mismatched_sizes as $att) { 77 log_print(sprintf('page %s name %s file_size %s db_size %s', 78 $att['page'], $att['name'], $att['file_size'], 79 $att['size'])); 80 } 81 } 82 if (read_bool('Do you want to try to fix this?', false)) { 83 foreach ($mismatched_sizes as $att) { 84 if ($att['file_size'] == 0 && $att['size'] != 0) { 85 // File was likely uploaded when disk was out of space. 86 // Nothing we can do, delete it. 87 log_print(sprintf('Deleting attachment page %s name %s', 88 $att['page'], $att['name'])); 89 attachment_delete_by_id($att['id']); 90 continue; 91 } 92 93 $safe_update = false; 94 if (starts_with($att['name'], 'grader_test') && 95 $att['file_size'] == $att['size'] + 1) { 96 // A script was run that added missing trailing endlines 97 // to grader tests. 98 $safe_update = true; 99 } 100 if ($att['file_size'] != 0 && $att['size'] == 0) { 101 $safe_update = true; 102 } 103 104 if ($safe_update) { 105 $new_att = attachment_get_by_id($att['id']); 106 $new_att['size'] = $att['file_size']; 107 attachment_update($new_att['id'], $new_att['name'], 108 $new_att['size'], $new_att['mime_type'], 109 $new_att['page'], $new_att['user_id'], 110 $new_att['remote_ip_info']); 111 continue; 112 } 113 log_print(sprintf('I don\'t know how to handle attachment page ' . 114 $att['page'] . ' name ' . $att['name'])); 115 } 116 } 117 } 67 118 68 119 // Again, in case we changed something.
Note: See TracChangeset
for help on using the changeset viewer.
![[infoarena] development](/chrome/site/logo.png)