Well, i'm posting some code changes with the related comments. I think they are self-explanatory:
(all problems are in book_delete_delete, with these changes worked like charm :- )
function book_delete_delete($bid, &$context) {
if (!isset($context['sandbox']['progress'])) {
$context['sandbox']['max'] = db_query("SELECT COUNT(nid) FROM {book} WHERE bid = :bid", array(':bid' => $bid))->fetchField();
$context['sandbox']['progress'] = 0;
$context['sandbox']['highest_nid'] = 0;
$context['sandbox']['bid'] = $bid;
}
// Delete 5 nodes at a time. This needs to be set fairly low as there may be
// many search index deletions for each node.
$limit = 5;
$result = db_query_range("SELECT nid FROM {book} WHERE bid = :bid AND nid > :nid AND nid <> bid ORDER BY nid ASC", 0, $limit, array(
':bid' => $context['sandbox']['bid'],
':nid' => $context['sandbox']['highest_nid'],
))->fetchCol();
//DEFINE $nids!!!
$nids = $result;
if (!empty($nids)) {
node_delete_multiple($nids);
}
// Update our progress information.
$context['sandbox']['progress'] += count($nids);
// don't let php produce a warning if the array is empty!
// let's say the book has 11 pages:
// the first 2 operations return populated $nids
// but the thirdone returns empty $nids:
// the only page left is the root page (nid = bid)!
$context['sandbox']['highest_nid'] = !empty($nids) ? array_pop($nids) : 0;
// Delete the top book node last.
if ($context['sandbox']['progress'] == $context['sandbox']['max'] - 1) {
node_delete($context['sandbox']['bid']);
$context['sandbox']['progress']++;
}
// Multistep processing : report progress.
if ($context['sandbox']['progress'] != $context['sandbox']['max']) {
$context['finished'] = $context['sandbox']['progress'] / $context['sandbox']['max'];
}
else {
// inform that we are done otherwise we are entering in a eternal loop(!)
// becouse the $context['finished'] will never equals 1!!!
$context['finished'] = 1;
}
}
Comments
Comment #1
jody lynnPlease submit your changes as a patch.
Comment #2
neoglez commentedI did it with diff (couldn't connect to git from my laptop).
Comment #3
neoglez commentedComment #4
jody lynnI changed this to +++ book_delete.module and applied this patch and did a new diff (attached), which gave this much more useful patch where I can see your actual changes.
Powered by Dreditor.
Comment #5
jody lynnReview of your changes via the above regenerated patch:
Good catch here, but the comment is not necessary.
This comment is overkill, (put comments like this in the issue queue not as a discussion in the code) and this can be handled more simply by putting it in the above if (!empty($nids))
If $context['finished'] is not set, it is assumed to be finished. See http://api.drupal.org/api/drupal/includes--form.inc/group/batch/7
Powered by Dreditor.
Comment #6
jody lynnI committed the patch below. Let me know how it works if you can test it.
Comment #7
neoglez commentedWow! It works great, it seems like an alpha ;-)
Some points:
I commented the code to call the attention on the bugs.
Jap, you're right, we don't need to set $content['finished'].
I couldn't apply the patch, (i'm still learning git:) i was getting this error:
fatal: git diff header lacks filename information when removing 1 leading pathname components line 5 but i did all changes manually.
Tested under Win and Debian, with Ajax & without --> everything OK!
There is no need (niether) of batch_process (http://api.drupal.org/api/drupal/includes--form.inc/function/batch_proce...).
There is no need to check
when they are equal $content['finished'] = 1, and that's what we want (also tested this), the only situation we could have is division by zero so the check would be if ($context['sandbox']['max'] != 0), anyway not great deal.
Comment #8
jody lynnOk, so sounds like we can remove that last if statement entirely. $context['sandbox']['max'] should never be 0 because there should always be at least the book node itself if you're managing to be here deleting a book.
One more patch to do that.
Comment #9
jody lynnI pushed the last patch as well.
Comment #10
neoglez commentedTested under the conditions described in #7 point 3 --> everything OK!
Thanks!