Followup from http://drupal.org/node/168870 :
Filing this as 'bug' or 'feature request' might be debated :-)

With the new ability offered by the menu system to split modules in .inc files, module developpers stumble on the fact that 'batched' functions currently have to stay in the main .module file, even when the form triggering the batch has all its definition and handler functions in a .inc file. This is because batch processing does not happen at the form's original url (for which menu.inc handles additional file loading), but at '?q=batch' (no additional file loaded...

Attached patch lets batch definition arrays accept a 'file' key.
Unlike hook_menu / hook_theme that take a simple filename ('file' => 'system.admin.inc'), we have no way of knowing which module is involved, so the full path must be provided using drupal_get_path :

$batch = array(
    'operations' => array(...),
    'finished' => 'batch_example_finished',
    'file' => drupal_get_path('module', 'foo'). '/foo.admin.inc',
  );
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

Dries’s picture

I'm tempted to postpone this to D7. I've skimmed the code, and I can't spot significant code savings from this feature so I'm not entirely convinced this is (a) useful or (b) cruft. Feel free to prove me wrong though. :)

profix898’s picture

@Dries: There are no code savings for Drupal core because batch API is not really used there (except for update.php). However without this patch all contrib developers are forced to put batch api stuff into their main .module file, what leads to
a) inconsistent code distribution, because batch API callbacks that would functionality-wise belong to .inc files must be moved to .module for the only reason, that they wont work inside the .inc files
b) conflicts with the idea to split off 'unused code' for 'normal' page loads to reduce the memory usage and to speed up Drupal
c) a vague point ;) Its nowhere documented AFAIK that batch api functions cant live inside .inc files. As described in http://drupal.org/node/168870 the callback is simply not invoked without any notice or error message. It took me quite some time to figure out why it silently fails ... This patch introduces an additional property to batch, but it does not break the existing API.

@yched: Whats the $set_changed good for! I assume you want to include the file only once, right? But actually as you are using include_once() there is no need to handle this manually. include_once() is very fast if the file was included already. And you current use of $set_changes is faulty, because you set it initially TRUE and check it before inclusion, but it is actually never set FALSE (unless I overlooked that ;)).

yched’s picture

There are no code savings for Drupal core because batch API is not really used there (except for update.php)
locale.module uses batch API for automatic .po import, but it has not (yet ?) been split in .inc files, so it's not affected (yet ?).
There's also the 'batch node access rebuild' patch that I definitely need to get back to, but this one should not be affected either (node_access_rebuild is an api function, so the code stays in node.module)
But as profix898 mentioned, this will probably affect contrib. Batch API primary uses are in 'admin' area, so it is very likely people will stumble on this.

@profix898 : the code with $set_changed is correct - the variable already exists in the code, so it's used in other lines not visible in the patch :-). So since it's there already, we might as well use it to save the include_once call, even if not too expensive...

profix898’s picture

Great work yched. Patch applies cleanly, code looks good and most important it works as expected. I'm inclined to set RTBC, but I think we need at least one independent review (and of course a decision of a core committer). However its a small, but very valuable addition.

profix898’s picture

Anyone?

profix898’s picture

I really wonder nobody else tumbled over this, but I guess thats mainly because only a very few modules are using batches at all and nobody has started to port these to Drupal 6 yet. However I still hope we can get this in, so that I can remove the wrapper function to manually include the .inc file from my module(s) ...

yched’s picture

Status: Needs review » Reviewed & tested by the community

The code is RTBC, really; Whether it gets committed or not is up to Dries and Gabor.

I do think that not many modules had an opportunity to get bitten by that so far, because
- not every contrib module actually needs batches anyway
- maybe the batch API has not really been advertized properly (yet ?). The corresponding line in changelog.txt is is not really informative. Plus maybe I should add a paragraph in the "D5 -> D6 upgrade" handbook page.

Anyway, since batches natural use cases are admin tasks, (that is, code that we now encourage to split in separate .inc files), I do think the API is better off with this patch than without. If it does not get in, we should at least add a word about that in the PHPdoc for batch_set()

Gábor Hojtsy’s picture

Status: Reviewed & tested by the community » Needs work

Yes, as said, the $set_changed stuff does not look correct.

yched’s picture

Status: Needs work » Reviewed & tested by the community

Yet it _is_ correct :-)
Maybe it is surprising when just looking at the patch, but it is correct when you look at the code once patched. $set_changed is already used before this patch, I simply use it because it's gere.

If it seems too confusing, I can always write

+ if (isset($current_set['file']) && is_file($current_set['file'])) {
+      include_once($current_set['file']);
+    }

instead and keep 95% unneeded include_once calls.

Gábor Hojtsy’s picture

Status: Reviewed & tested by the community » Needs work

OK, make this use with the locale batches, and it actually makes locale module a bit more performant. We have at least two batches which need their functions in locale.module because we have no such functionality introduced by this patch.

yched’s picture

You set this back to CNW for the local.module batches to be moved to locale.inc, is that right ?

Gábor Hojtsy’s picture

Yes, make use of this, so we can justify adding it. That way, we save code and we show gain.

yched’s picture

Status: Needs work » Needs review
FileSize
12.01 KB

OK, attached patch also includes locale batch callbacks, moved from locale.module to locale.inc.

As a side note, since a large part of locale.inc is in fact locale.module split up, it would probably be a good thing to refactor it in a locale.admin.inc file in modules/locale, D6 way ? That's another story, obviously :-)

profix898’s picture

Status: Needs review » Needs work

needs to be re-rolled (1 out of 3 hunks FAILED in includes/batch.inc)

Also I dont see why stuff that I would usually expect in modules/locale/*.inc should be moved to (or already resides in) the 'global' includes/locale.inc. I agree with yched that it would be better to have this in locale.admin.inc or stg.

Gábor Hojtsy’s picture

@profix898: that is because locale.inc was added more then six years ago, when the something.admin.inc naming convention was apparently not in place :) At that time, this include file did not contain the rarely included parts of Drupal localization, but locale module was still literally years ahead of other modules with moving rarely used stuff out of the module itself. I myself have asked for a move of the locale.inc to the locale module more then a year ago: http://drupal.org/node/73507

yched’s picture

Status: Needs work » Needs review
FileSize
9.29 KB

the failing hunks are probably just offsets, but here is a rerolled patch .

yched’s picture

FileSize
11.88 KB

Oops - previous patch left out phpdoc update for batch_set() in form.inc.

profix898’s picture

Status: Needs review » Needs work

I hate to say, but still does not apply or actually fails completely now ;)

[thilo@profix drupal6-cvs]$ patch -p0 < batch_file_2.patch
patching file includes/batch.inc
patch: **** malformed patch at line 45: ### Eclipse Workspace Patch 1.0

@yched: Do you still have a modified copy on your system or should I apply and reroll manually?

yched’s picture

Won't be able to reroll before next weekend. Anyone ?

profix898’s picture

Status: Needs work » Needs review
FileSize
11.84 KB

OK. Here is the patch from #17 rerolled for latest HEAD.

profix898’s picture

Any chance to get this in? Developers will really miss this once they start to use batch API in contributed modules with D6. And as mentioned above they will not even get a notice why their batches dont work. At least it took me quite some time to figure out ...

Gábor Hojtsy’s picture

It is best to have Dries' opinion on this IMHO. Because the D6 architecture (with menu callbacks and all) are fitted around more include files, it would not be fortunate to require proxy functions for batches. But unfortunately we are past beta 1 now, so we need to be (more) careful.

yched’s picture

And as mentioned above they will not even get a notice why their batches dont work. At least it took me quite some time to figure out ...
If this patch does not make it, I'll provide an update to the PHPdoc for batch_set about possible issues with file inclusions.
But I'm all for it's inclusion, of course. As Gabor stressed out, this patch keeps the batch API current with the file handling possibilities of D6's menu system (which were not in yet when the batch API was developped and committed). Batched operations being mainly admin stuff, there's a good chance module authors will get bitten.

profix898’s picture

We still need a decision here. Gabor, Dries?
In case the proposed solution is rejected we should at least update the docs and/or add a function_exists() check for the callback.

yched’s picture

Another use case for this : http://drupal.org/node/164510#comment-311622
(call node_save for mass node operations on admin/content/nodes, making it a good candidate for batches - we would have to stick the batch operations in node.module, whereas they belong in node.admin.inc)

profix898’s picture

Bumping gently ;) It would be a shame to release D6 without this very simple yet effective and code-saving addition IMO.

Gábor Hojtsy’s picture

Status: Needs review » Needs work

I agree that this is useful and saves some code, but what is more important is that it improves consistency. I quickly reviewed the patch and noticed that locale.inc is include_once-d in locale.inc after the patch which is quite interesting...

It would be great to have a deeper review from yched. I am fine with the concept but the actual patch looks like needing some eyes looking closer :)

yched’s picture

Probably needs a re-roll anyway, plus the new 'batch mass node updates' on admin/content/node needs to be included as well.
I'm extremely short on time these next days, will try to tackle this asap.

Gábor Hojtsy’s picture

Better to keep this simple, and tackle any new batch operations in a different patch. Solve this problem quickly and save different issues to other patches.

yched’s picture

Status: Needs work » Needs review
FileSize
16.7 KB

Re-rolled, fixed erroneous includes if locale.inc from locale.inc, plus also adds a buch of long-due comments improvements in batch.inc (no functional change, except the new batch 'file' property we're dealing with)

yched’s picture

FileSize
18.71 KB

Oops, previous patch was missing the locale.module part.

yched’s picture

Also note that the sudden shrinking of the progressbar is _not_ a result of this patch (bug in the recent AHAH-upload related patches ? this has already been reported, er, somewhere else).

Tested locale import on new language enabled and new modules enabled, both work fine.

profix898’s picture

Status: Needs review » Reviewed & tested by the community

The patch still applies (with a small offset) and seems to work just fine (incl. auto-import of translations).

Gábor Hojtsy’s picture

Status: Reviewed & tested by the community » Fixed

Now looked through the code again twice and did not identify issues. The whitespace issues I noticed in a code comment actually turned out to be for consistency reasons. Thanks for this consistency improvement, especially for the performance improvements now possible with contributed modules using batches. Committed.

profix898’s picture

Awesome. Thanks :)

Anonymous’s picture

Status: Fixed » Closed (fixed)