diff options
Diffstat (limited to 'modules')
-rw-r--r-- | modules/book/book.module | 4 | ||||
-rw-r--r-- | modules/comment/comment.module | 137 | ||||
-rw-r--r-- | modules/forum/forum.module | 4 | ||||
-rw-r--r-- | modules/node/node.module | 184 | ||||
-rw-r--r-- | modules/path/path.module | 4 | ||||
-rw-r--r-- | modules/poll/poll.module | 6 | ||||
-rw-r--r-- | modules/statistics/statistics.module | 2 | ||||
-rw-r--r-- | modules/system/system.module | 52 | ||||
-rw-r--r-- | modules/taxonomy/taxonomy.module | 4 | ||||
-rw-r--r-- | modules/upload/upload.module | 31 |
10 files changed, 268 insertions, 160 deletions
diff --git a/modules/book/book.module b/modules/book/book.module index 5f6b4abd0..d0a565951 100644 --- a/modules/book/book.module +++ b/modules/book/book.module @@ -466,10 +466,10 @@ function book_nodeapi(&$node, $op, $teaser, $page) { } break; case 'delete revision': - db_query('DELETE FROM {book} WHERE vid = %d', $node->vid); + drupal_delete_add_query('DELETE FROM {book} WHERE vid = %d', $node->vid); break; case 'delete': - db_query('DELETE FROM {book} WHERE nid = %d', $node->nid); + drupal_delete_add_query('DELETE FROM {book} WHERE nid = %d', $node->nid); break; } } diff --git a/modules/comment/comment.module b/modules/comment/comment.module index 1c9002dca..bb1dee560 100644 --- a/modules/comment/comment.module +++ b/modules/comment/comment.module @@ -232,7 +232,8 @@ function comment_menu() { $items['comment/delete'] = array( 'title' => 'Delete comment', - 'page callback' => 'comment_delete', + 'page callback' => 'drupal_get_form', + 'page arguments' => array('comment_delete', 2), 'access arguments' => array('administer comments'), 'type' => MENU_CALLBACK, ); @@ -474,8 +475,8 @@ function comment_nodeapi(&$node, $op, $arg = 0) { break; case 'delete': - db_query('DELETE FROM {comments} WHERE nid = %d', $node->nid); - db_query('DELETE FROM {node_comment_statistics} WHERE nid = %d', $node->nid); + drupal_delete_add_query('DELETE FROM {comments} WHERE nid = %d', $node->nid); + drupal_delete_add_query('DELETE FROM {node_comment_statistics} WHERE nid = %d', $node->nid); break; case 'update index': @@ -1093,50 +1094,44 @@ function comment_render($node, $cid = 0) { /** * Menu callback; delete a comment. */ -function comment_delete($cid = NULL) { +function comment_delete(&$form_state, $cid = NULL) { $comment = db_fetch_object(db_query('SELECT c.*, u.name AS registered_name, u.uid FROM {comments} c INNER JOIN {users} u ON u.uid = c.uid WHERE c.cid = %d', $cid)); $comment->name = $comment->uid ? $comment->registered_name : $comment->name; - $output = ''; - if (is_object($comment) && is_numeric($comment->cid)) { - $output = drupal_get_form('comment_confirm_delete', $comment); + drupal_delete_initiate('comment', $comment->cid); + drupal_delete_add_callback( + array( + 'comment_delete_post' => array($comment), + // Clear the cache so an anonymous poster can see the node being deleted. + 'cache_clear_all' => array(), + ) + ); + + // Delete comment and its replies. + _comment_delete_thread($comment); + + return drupal_delete_confirm( + array( + 'question' => t('Are you sure you want to delete the comment %title?', array('%title' => $comment->subject)), + 'path' => 'node/'. $comment->nid, + 'description' => t('Any replies to this comment will be lost. This action cannot be undone.'), + 'destination' => 'node/'. $comment->nid, + ) + ); } else { - drupal_set_message(t('The comment no longer exists.')); + drupal_set_message(t('The comment no longer exists.'), 'error'); + drupal_goto('<front>'); } - - return $output; -} - -function comment_confirm_delete(&$form_state, $comment) { - $form = array(); - $form['#comment'] = $comment; - return confirm_form( - $form, - t('Are you sure you want to delete the comment %title?', array('%title' => $comment->subject)), - 'node/'. $comment->nid, - t('Any replies to this comment will be lost. This action cannot be undone.'), - t('Delete'), - t('Cancel'), - 'comment_confirm_delete'); } -function comment_confirm_delete_submit($form, &$form_state) { - drupal_set_message(t('The comment and all its replies have been deleted.')); +function comment_delete_post($comment) { - $comment = $form['#comment']; - - // Delete comment and its replies. - _comment_delete_thread($comment); + drupal_set_message(t('The comment %subject and all its replies have been deleted.', array('%subject' => $comment->subject))); + watchdog('content', 'Comment: deleted %subject and all its replies.', array('%subject' => $comment->subject)); _comment_update_node_statistics($comment->nid); - - // Clear the cache so an anonymous user sees that his comment was deleted. - cache_clear_all(); - - $form_state['redirect'] = "node/$comment->nid"; - return; } /** @@ -1291,45 +1286,78 @@ function theme_comment_admin_overview($form) { function comment_multiple_delete_confirm(&$form_state) { $edit = $form_state['post']; - $form['comments'] = array('#prefix' => '<ul>', '#suffix' => '</ul>', '#tree' => TRUE); + $form['comments']['#tree'] = TRUE; + $form['prefix'] = array('#value' => '<ul>'); // array_filter() returns only elements with actual values $comment_counter = 0; - foreach (array_filter($edit['comments']) as $cid => $value) { + foreach (array_filter($edit['comments']) as $cid) { $comment = _comment_load($cid); if (is_object($comment) && is_numeric($comment->cid)) { $subject = db_result(db_query('SELECT subject FROM {comments} WHERE cid = %d', $cid)); - $form['comments'][$cid] = array('#type' => 'hidden', '#value' => $cid, '#prefix' => '<li>', '#suffix' => check_plain($subject) .'</li>'); + // This is a placeholder -- preserves proper ordering in the confirm form. + $form["comment_$cid"] = array(); + + // Start a new package for each comment. + drupal_delete_initiate('comment', $comment->cid); + drupal_delete_add_callback(array('comment_delete_post' => array($comment))); + + _comment_delete_thread($comment); + + // Add the confirm form piece for this comment. + drupal_delete_add_form_elements( + array( + "comment_$cid" => array( + '#value' => check_plain($subject), + '#prefix' => '<li>', + '#suffix' => "</li>\n", + ), + 'comments' => array( + "comment_$cid" => array( + '#type' => 'hidden', + '#value' => $cid, + ) + ) + ) + ); + $comment_counter++; } } - $form['operation'] = array('#type' => 'hidden', '#value' => 'delete'); + $form['suffix'] = array('#value' => '</ul>'); + $form['operation'] = array( + '#type' => 'hidden', + '#value' => 'delete' + ); + + // New package for the main admin callback. + drupal_delete_initiate('comment', 'admin'); + + // Add the main callback for the mass deletion last. + drupal_delete_add_callback(array('comment_multiple_delete_confirm_post' => array())); if (!$comment_counter) { drupal_set_message(t('There do not appear to be any comments to delete or your selected comment was deleted by another administrator.')); drupal_goto('admin/content/comment'); } else { - return confirm_form($form, - t('Are you sure you want to delete these comments and all their children?'), - 'admin/content/comment', t('This action cannot be undone.'), - t('Delete comments'), t('Cancel')); + return drupal_delete_confirm( + array( + 'form' => $form, + 'question' => t('Are you sure you want to delete these comments and all their children?'), + 'path' => 'admin/content/comment', + 'yes' => t('Delete all'), + 'destination' => 'admin/content/comment', + ) + ); } } /** - * Perform the actual comment deletion. + * Post deletion callback for multiple comment deletion. */ -function comment_multiple_delete_confirm_submit($form, &$form_state) { - if ($form_state['values']['confirm']) { - foreach ($form_state['values']['comments'] as $cid => $value) { - $comment = _comment_load($cid); - _comment_delete_thread($comment); - _comment_update_node_statistics($comment->nid); - } +function comment_multiple_delete_confirm_post() { cache_clear_all(); drupal_set_message(t('The comments have been deleted.')); - } - drupal_goto('admin/content/comment'); } /** @@ -1892,8 +1920,7 @@ function _comment_delete_thread($comment) { } // Delete the comment: - db_query('DELETE FROM {comments} WHERE cid = %d', $comment->cid); - watchdog('content', 'Comment: deleted %subject.', array('%subject' => $comment->subject)); + drupal_delete_add_query('DELETE FROM {comments} WHERE cid = %d', $comment->cid); comment_invoke_comment($comment, 'delete'); diff --git a/modules/forum/forum.module b/modules/forum/forum.module index 37126bd0a..893cc3821 100644 --- a/modules/forum/forum.module +++ b/modules/forum/forum.module @@ -169,7 +169,7 @@ function forum_perm() { function forum_nodeapi(&$node, $op, $teaser, $page) { switch ($op) { case 'delete revision': - db_query('DELETE FROM {forum} WHERE vid = %d', $node->vid); + drupal_delete_add_query('DELETE FROM {forum} WHERE vid = %d', $node->vid); break; } } @@ -453,7 +453,7 @@ function forum_insert($node) { * Implementation of hook_delete(). */ function forum_delete(&$node) { - db_query('DELETE FROM {forum} WHERE nid = %d', $node->nid); + drupal_delete_add_query('DELETE FROM {forum} WHERE nid = %d', $node->nid); } /** diff --git a/modules/node/node.module b/modules/node/node.module index c253610d9..7bc971b97 100644 --- a/modules/node/node.module +++ b/modules/node/node.module @@ -1685,29 +1685,67 @@ function theme_node_admin_nodes($form) { function node_multiple_delete_confirm(&$form_state) { $edit = $form_state['post']; - $form['nodes'] = array('#prefix' => '<ul>', '#suffix' => '</ul>', '#tree' => TRUE); - // array_filter returns only elements with TRUE values - foreach (array_filter($edit['nodes']) as $nid => $value) { - $title = db_result(db_query('SELECT title FROM {node} WHERE nid = %d', $nid)); - $form['nodes'][$nid] = array('#type' => 'hidden', '#value' => $nid, '#prefix' => '<li>', '#suffix' => check_plain($title) ."</li>\n"); + $form['nodes']['#tree'] = TRUE; + $form['prefix'] = array('#value' => '<ul>'); + + // array_filter() returns only elements with TRUE values. + $nids = array_filter($edit['nodes']); + $nodes = db_query('SELECT nid, title, type FROM {node} WHERE nid IN(%s)', implode(', ', $nids)); + while ($node = db_fetch_object($nodes)) { + + // This is a placeholder -- preserves proper ordering in the confirm form. + $form["node_$node->nid"] = array(); + + // Start a new package for each node. + drupal_delete_initiate('node', $node->nid); + + // Add the confirm form piece for this node. + drupal_delete_add_form_elements( + array( + "node_$node->nid" => array( + '#value' => check_plain($node->title), + '#prefix' => '<li>', + '#suffix' => "</li>\n", + ), + 'nodes' => array( + "node_$node->nid" => array( + '#type' => 'hidden', + '#value' => $node->nid, + ) + ) + ) + ); + drupal_delete_add_callback(array('node_delete_post' => array($node->nid, $node->title, $node->type))); + node_delete($node->nid); } - $form['operation'] = array('#type' => 'hidden', '#value' => 'delete'); + $form['suffix'] = array('#value' => '</ul>'); + + $form['operation'] = array( + '#type' => 'hidden', + '#value' => 'delete', + ); - return confirm_form($form, - t('Are you sure you want to delete these items?'), - 'admin/content/node', t('This action cannot be undone.'), - t('Delete all'), t('Cancel')); + // New package for the main admin callback. + drupal_delete_initiate('node', 'admin'); + + // Add the main callback for the mass deletion last. + drupal_delete_add_callback(array('node_multiple_delete_confirm_post' => array())); + + return drupal_delete_confirm( + array( + 'form' => $form, + 'question' => t('Are you sure you want to delete these items?'), + 'path' => 'admin/content/node', + 'yes' => t('Delete all'), + 'destination' => 'admin/content/node', + ) + ); } -function node_multiple_delete_confirm_submit($form, &$form_state) { - if ($form_state['values']['confirm']) { - foreach ($form_state['values']['nodes'] as $nid => $value) { - node_delete($nid); - } - drupal_set_message(t('The items have been deleted.')); - } - $form_state['redirect'] = 'admin/content/node'; - return; +function node_multiple_delete_confirm_post() { + // Clear the cache so an anonymous poster can see the nodes being deleted. + cache_clear_all(); + drupal_set_message(t('The items have been deleted.')); } /** @@ -1789,32 +1827,44 @@ function node_revision_revert($nid, $revision) { * revision is deleted. */ function node_revision_delete($nid, $revision) { - if (user_access('administer nodes')) { - $node = node_load($nid); - if (node_access('delete', $node)) { - // Don't delete the current revision - if ($revision != $node->vid) { - $node = node_load($nid, $revision); - - db_query("DELETE FROM {node_revisions} WHERE nid = %d AND vid = %d", $nid, $revision); - node_invoke_nodeapi($node, 'delete revision'); - drupal_set_message(t('Deleted %title revision %revision.', array('%title' => $node->title, '%revision' => $revision))); - watchdog('content', '@type: deleted %title revision %revision.', array('@type' => $node->type, '%title' => $node->title, '%revision' => $revision)); - } + $node = node_load($nid); - else { - drupal_set_message(t('Deletion failed. You tried to delete the current revision.')); - } - if (db_result(db_query('SELECT COUNT(vid) FROM {node_revisions} WHERE nid = %d', $nid)) > 1) { - drupal_goto("node/$nid/revisions"); - } - else { - drupal_goto("node/$nid"); - } + if (user_access('administer nodes') && node_access('delete', $node)) { + + // Don't delete the current revision + if ($revision != $node->vid) { + $node = node_load($nid, $revision); + + drupal_delete_initiate('node_revision', $revision); + drupal_delete_add_callback(array('node_revision_delete_post' => array($node, $revision))); + drupal_delete_add_query('DELETE FROM {node_revisions} WHERE vid = %d', $revision); + node_invoke_nodeapi($node, 'delete revision'); + drupal_delete_execute(); + } + else { + drupal_set_message(t('Deletion failed. You tried to delete the current revision.')); } } + else { + drupal_access_denied(); + } +} - drupal_access_denied(); +/** + * Post revision deletion opertations. + */ +function node_revision_delete_post($node, $revision) { + + drupal_set_message(t('Deleted %title revision %revision.', array('%title' => $node->title, '%revision' => $revision))); + watchdog('content', '@type: deleted %title revision %revision.', array('@type' => t($node->type), '%title' => $node->title, '%revision' => $revision)); + + + if (db_result(db_query('SELECT COUNT(vid) FROM {node_revisions} WHERE nid = %d', $node->nid)) > 1) { + drupal_goto("node/$node->nid/revisions"); + } + else { + drupal_goto("node/$node->nid"); + } } /** @@ -2371,25 +2421,25 @@ function node_form_submit($form, &$form_state) { * Menu callback -- ask for confirmation of node deletion */ function node_delete_confirm(&$form_state, $node) { - $form['nid'] = array('#type' => 'value', '#value' => $node->nid); - return confirm_form($form, - t('Are you sure you want to delete %title?', array('%title' => $node->title)), - isset($_GET['destination']) ? $_GET['destination'] : 'node/'. $node->nid, - t('This action cannot be undone.'), - t('Delete'), t('Cancel')); -} + drupal_delete_initiate('node', $node->nid); + drupal_delete_add_callback( + array( + 'node_delete_post' => array($node->nid, $node->title, $node->type), + // Clear the cache so an anonymous poster can see the node being deleted. + 'cache_clear_all' => array(), + ) + ); -/** - * Execute node deletion - */ -function node_delete_confirm_submit($form, &$form_state) { - if ($form_state['values']['confirm']) { - node_delete($form_state['values']['nid']); - } + node_delete($node->nid, FALSE); - $form_state['redirect'] = '<front>'; - return; + return drupal_delete_confirm( + array( + 'question' => t('Are you sure you want to delete %title?', array('%title' => $node->title)), + 'path' => isset($_GET['destination']) ? $_GET['destination'] : 'node/'. $node->nid, + 'destination' => isset($_GET['destination']) ? $_GET['destination'] : '<front>', + ) + ); } /** @@ -2400,23 +2450,23 @@ function node_delete($nid) { $node = node_load($nid); if (node_access('delete', $node)) { - db_query('DELETE FROM {node} WHERE nid = %d', $node->nid); - db_query('DELETE FROM {node_revisions} WHERE nid = %d', $node->nid); + drupal_delete_add_query('DELETE FROM {node} WHERE nid = %d', $node->nid); + drupal_delete_add_query('DELETE FROM {node_revisions} WHERE nid = %d', $node->nid); // Call the node-specific callback (if any): node_invoke($node, 'delete'); node_invoke_nodeapi($node, 'delete'); + } +} - // Clear the cache so an anonymous poster can see the node being deleted. - cache_clear_all(); +function node_delete_post($nid, $title, $type) { - // Remove this node from the search index if needed. - if (function_exists('search_wipe')) { - search_wipe($node->nid, 'node'); - } - drupal_set_message(t('%title has been deleted.', array('%title' => $node->title))); - watchdog('content', '@type: deleted %title.', array('@type' => $node->type, '%title' => $node->title)); + // Remove this node from the search index if needed. + if (function_exists('search_wipe')) { + search_wipe($nid, 'node'); } + drupal_set_message(t('%title has been deleted.', array('%title' => $title))); + watchdog('content', '@type: deleted %title.', array('@type' => t($type), '%title' => $title)); } /** diff --git a/modules/path/path.module b/modules/path/path.module index b932a1b66..9900d6641 100644 --- a/modules/path/path.module +++ b/modules/path/path.module @@ -127,12 +127,12 @@ function path_admin_delete($pid = 0) { function path_set_alias($path = NULL, $alias = NULL, $pid = NULL, $language = '') { if ($path && !$alias) { // Delete based on path - db_query("DELETE FROM {url_alias} WHERE src = '%s' AND language = '%s'", $path, $language); + drupal_delete_add_query("DELETE FROM {url_alias} WHERE src = '%s' AND language = '%s'", $path, $language); drupal_clear_path_cache(); } else if (!$path && $alias) { // Delete based on alias - db_query("DELETE FROM {url_alias} WHERE dst = '%s' AND language = '%s'", $alias, $language); + drupal_delete_add_query("DELETE FROM {url_alias} WHERE dst = '%s' AND language = '%s'", $alias, $language); drupal_clear_path_cache(); } else if ($path && $alias) { diff --git a/modules/poll/poll.module b/modules/poll/poll.module index 7350fb0a1..ff5756f56 100644 --- a/modules/poll/poll.module +++ b/modules/poll/poll.module @@ -91,9 +91,9 @@ function poll_cron() { * Implementation of hook_delete(). */ function poll_delete($node) { - db_query("DELETE FROM {poll} WHERE nid = %d", $node->nid); - db_query("DELETE FROM {poll_choices} WHERE nid = %d", $node->nid); - db_query("DELETE FROM {poll_votes} WHERE nid = %d", $node->nid); + drupal_delete_add_query('DELETE FROM {poll} WHERE nid = %d', $node->nid); + drupal_delete_add_query('DELETE FROM {poll_choices} WHERE nid = %d', $node->nid); + drupal_delete_add_query('DELETE FROM {poll_votes} WHERE nid = %d', $node->nid); } /** diff --git a/modules/statistics/statistics.module b/modules/statistics/statistics.module index 69f65c326..fc91dfe91 100644 --- a/modules/statistics/statistics.module +++ b/modules/statistics/statistics.module @@ -553,7 +553,7 @@ function statistics_nodeapi(&$node, $op, $arg = 0) { switch ($op) { case 'delete': // clean up statistics table when node is deleted - db_query('DELETE FROM {node_counter} WHERE nid = %d', $node->nid); + drupal_delete_add_query('DELETE FROM {node_counter} WHERE nid = %d', $node->nid); } } diff --git a/modules/system/system.module b/modules/system/system.module index a2c3c6df5..3b221f204 100644 --- a/modules/system/system.module +++ b/modules/system/system.module @@ -2420,21 +2420,26 @@ function system_node_type($op, $info) { * block <em>foo</em>?"). * @param $path * The page to go to if the user denies the action. - * Can be either a drupal path, or an array with the keys 'path', 'query', 'fragment'. - * @param $description - * Additional text to display (defaults to "This action cannot be undone."). - * @param $yes - * A caption for the button which confirms the action (e.g. "Delete", - * "Replace", ...). - * @param $no - * A caption for the link which denies the action (e.g. "Cancel"). - * @param $name - * The internal name used to refer to the confirmation item. + * Can be either a Drupal path, or an array with the keys 'path', 'query', 'fragment'. + * @param $options + * An associative array of options, with the following key/value pairs: + * 'description' => Additional text to display. + * Default is "This action cannot be undone". + * 'yes' => A caption for the button which confirms the action (e.g. "Confirm", + * "Replace", ...). Default is "Delete". + * 'no' => A caption for the link which denies the action (e.g. "Cancel"). + * Default is "Cancel". + * 'name' => The internal name used to refer to the confirmation item. + * Default is "confirm". + * 'destination' => A destination page to go to after form submission -- the value can + * be of any form of the $goto argument accepted by drupal_redirect(). + * * @return * The form. */ -function confirm_form($form, $question, $path, $description = NULL, $yes = NULL, $no = NULL, $name = 'confirm') { - $description = isset($description) ? $description : t('This action cannot be undone.'); +function confirm_form($form, $question, $path, $options = array()) { + $description = isset($options['description']) ? $options['description'] : t('This action cannot be undone.'); + $name = isset($options['name']) ? $options['name'] : 'confirm'; // Prepare cancel link $query = $fragment = NULL; @@ -2443,25 +2448,38 @@ function confirm_form($form, $question, $path, $description = NULL, $yes = NULL, $fragment = isset($path['fragment']) ? $path['fragment'] : NULL; $path = isset($path['path']) ? $path['path'] : NULL; } - $cancel = l($no ? $no : t('Cancel'), $path, array('query' => $query, 'fragment' => $fragment)); + $cancel = l(isset($options['no']) ? $options['no'] : t('Cancel'), $path, array('query' => $query, 'fragment' => $fragment)); drupal_set_title($question); - // Confirm form fails duplication check, as the form values rarely change -- so skip it. - $form['#skip_duplicate_check'] = TRUE; - $form['#attributes'] = array('class' => 'confirmation'); $form['description'] = array('#value' => $description); $form[$name] = array('#type' => 'hidden', '#value' => 1); + if (isset($options['destination'])) { + $form['destination'] = array('#type' => 'value', '#value' => $options['destination']); + } $form['actions'] = array('#prefix' => '<div class="container-inline">', '#suffix' => '</div>'); - $form['actions']['submit'] = array('#type' => 'submit', '#value' => $yes ? $yes : t('Confirm')); + $form['actions']['submit'] = array('#type' => 'submit', '#value' => isset($options['yes']) ? $options['yes'] : t('Delete')); $form['actions']['cancel'] = array('#value' => $cancel); + $form['#theme'] = 'confirm_form'; return $form; } /** + * Executes confirmation pages for the Deletion API. + */ +function delete_confirm_submit($form, &$form_state) { + if ($form_state['values']['delete']) { + drupal_delete_execute(); + } + if (isset($form_state['values']['destination'])) { + $form_state['redirect'] = $form_state['values']['destination']; + } +} + +/** * Determine if a user is in compact mode. */ function system_admin_compact_mode() { diff --git a/modules/taxonomy/taxonomy.module b/modules/taxonomy/taxonomy.module index dc2508352..8db4a3457 100644 --- a/modules/taxonomy/taxonomy.module +++ b/modules/taxonomy/taxonomy.module @@ -876,14 +876,14 @@ function taxonomy_node_save($node, $terms) { * Remove associations of a node to its terms. */ function taxonomy_node_delete($node) { - db_query('DELETE FROM {term_node} WHERE nid = %d', $node->nid); + drupal_delete_add_query('DELETE FROM {term_node} WHERE nid = %d', $node->nid); } /** * Remove associations of a node to its terms. */ function taxonomy_node_delete_revision($node) { - db_query('DELETE FROM {term_node} WHERE vid = %d', $node->vid); + drupal_delete_add_query('DELETE FROM {term_node} WHERE vid = %d', $node->vid); } /** diff --git a/modules/upload/upload.module b/modules/upload/upload.module index cdc6cb563..460d4726a 100644 --- a/modules/upload/upload.module +++ b/modules/upload/upload.module @@ -575,31 +575,44 @@ function upload_delete($node) { } foreach ($files as $fid => $file) { - // Delete all files associated with the node - db_query('DELETE FROM {files} WHERE fid = %d', $fid); - file_delete($file->filepath); + // Delete all file revision information associated with the node + drupal_delete_add_query('DELETE FROM {files} WHERE fid = %d', $fid); } - // Delete all file revision information associated with the node - db_query('DELETE FROM {upload} WHERE nid = %d', $node->nid); + // Delete all files associated with the node + drupal_delete_add_query('DELETE FROM {upload} WHERE nid = %d', $node->nid); + + // Register a callback to delete the files. + drupal_delete_add_callback(array('upload_delete_post' => array($files))); } function upload_delete_revision($node) { if (is_array($node->files)) { + $files = array(); foreach ($node->files as $file) { // Check if the file will be used after this revision is deleted $count = db_result(db_query('SELECT COUNT(fid) FROM {upload} WHERE fid = %d', $file->fid)); // if the file won't be used, delete it if ($count < 2) { - db_query('DELETE FROM {files} WHERE fid = %d', $file->fid); - file_delete($file->filepath); + drupal_delete_add_query('DELETE FROM {files} WHERE fid = %d', $file->fid); + $files[$file->fid] = $file; } } } - // delete the revision - db_query('DELETE FROM {upload} WHERE vid = %d', $node->vid); + // Delete the revision. + drupal_delete_add_query('DELETE FROM {upload} WHERE vid = %d', $node->vid); + + // Register a callback to delete the files. + drupal_delete_add_callback(array('upload_delete_post' => array($files))); +} + +function upload_delete_post($files) { + foreach ($files as $file) { + // Delete all files associated with the node or revision. + file_delete($file->filepath); + } } function _upload_form($node) { |