I'm working on the Skeleton Outlines module, which lets users define templates of entire books. It works by storing node data and then calling drupal_execute() on that data to construct the book outline.
The problem is that book_get_books() keeps a static variable of all books. So the first time it's called, it works as expected, but then subsequent calls in the same request can fail because the newly-created book doesn't exist in the cache. This patch adds a reset parameter to book_get_books().
I will be working on a test for this soon, but if there is something obvious I've missed then please let me know :)
I've attached patches for D7 and D6, and I imagine it would make sense to backport to D5 as well.
Comment | File | Size | Author |
---|---|---|---|
#26 | 360377-reset_book_cache-26.patch | 987 bytes | pwolanin |
#17 | 360377-17-reset_book_cache.patch | 1.08 KB | janusman |
#11 | 360377_reset_book_cache_6.x-11.patch | 1.71 KB | janusman |
#4 | 360377_reset_book_cache_6.x_2.patch | 1.45 KB | deviantintegral |
#2 | reset_book_cache_7.x_2.patch | 3.43 KB | deviantintegral |
Comments
Comment #1
deviantintegral CreditAttribution: deviantintegral commentedHere is the start of a test. It's still broken in that somehow the nodes are being created in the actual DB, not in the simpletest DB. I'll work on this again in the next few days.
Comment #2
deviantintegral CreditAttribution: deviantintegral commentedHere is a patch with a working test. The test is dependent on #297972: drupal_execute is incompatible with batch API.
In book.module:
book_get_books(TRUE)
is called to reset the cache.And in book.test:
createBookDrupalExecute()
is created, as we can't use drupalPost() to expose the bug.Review / testing / comments would all be appreciated!
Comment #3
moshe weitzman CreditAttribution: moshe weitzman commentedYet another use case for a centralized static variable cache.
Comment #4
deviantintegral CreditAttribution: deviantintegral commentedHere is a patch for Drupal 6 as well. It is pretty much the same except that it excludes the test.
A centralized cache does make sense, especially if it had some way of easily exposing to developers that something is cached per request. It's not obvious until you look inside book_get_books() that a static comes into play, leading to much frustration.
Comment #6
deviantintegral CreditAttribution: deviantintegral commentedSetting to needs review as the bot tried to test the 6.x patch I posted.
Comment #7
Wies Hubbers CreditAttribution: Wies Hubbers commentedI have the same issue here! I add book dynamically and I use book_get_books to get to them.
Comment #8
deviantintegral CreditAttribution: deviantintegral commentedThe Drupal 7 issue was addressed with the implementation of drupal_static over at #480412: Update book module to use drupal_static().
@wies, can you review the patch against Drupal 6?
Comment #9
idflood CreditAttribution: idflood commentedI applied the patch to Drupal 6 head without any error. ( no further testing yet ). How can I help?
Comment #10
deviantintegral CreditAttribution: deviantintegral commentedThanks idflood; if you could provide feedback about the code itself, so others reading this issue have more info about what you reviewed. Some good guidelines are here: http://drupal.org/patch/review
Comment #11
janusman CreditAttribution: janusman commentedI did some testing and I think the earlier patch missed one reset case. New patch for current DRUPAL-6 checkout.
Comment #12
janusman CreditAttribution: janusman commentedRe: #8 ... I don't think the D7 issue was addressed *fully*; while it's true that drupal_static is used, and that means tests now reset that static variable, a delete/update/insert action in nodeapi does not clear the $all_items in book_get_books; thus Batch API runs, or other modifications to the book tree will not be able to get updated data from book_get_books().
So IMO this means the patch in #11 should be ported to D7 too. Thoughts?
Comment #13
pwolanin CreditAttribution: pwolanin commentedsubscribe
Comment #14
IrishGringo CreditAttribution: IrishGringo commentedI am thinking of using skeleton, but I do not know if the latest version of DRUPAL still requires the patch. can I get an update on if the patch is required please
Comment #15
deviantintegral CreditAttribution: deviantintegral commented@janusman, that makes sense, though someone will have to try it to confirm.
@IrishGringo, Drupal 6 certainly requires the patch for Skeleton. Please try the one uploaded in #11. And if you get a chance, try syncronizing changes where the book root page is deleted, as I think that might be what will trigger the case mentioned in #11.
Comment #16
pwolanin CreditAttribution: pwolanin commentedIf it should be in D7, let's fix it there first?
Looks simple enough patch for D6, though I might use a pattern like:
Comment #17
janusman CreditAttribution: janusman commentedOk, agreed this should get fixed in D7 first. Here's a patch.
Comment #19
deviantintegral CreditAttribution: deviantintegral commented#17: 360377-17-reset_book_cache.patch queued for re-testing.
Comment #20
deviantintegral CreditAttribution: deviantintegral commentedI've requested a retest as it apparently failed in contact.test, but the failing line is
function testSiteWideContact() {
Comment #21
janusman CreditAttribution: janusman commentedWell... tests pass. Hopefully someone else will weigh in and mark this RTBC.
Comment #22
janusman CreditAttribution: janusman commentedMoving to D8.
Comment #23
janusman CreditAttribution: janusman commentedAlso, needs work since patch won't apply anymore.
Comment #24
btopro CreditAttribution: btopro commentedbump. any thoughts on implementing this? Tried it in D6 and last patch worked for me. D7 patch passed validation, seems like a worthy improvement for both branches and relatively simple
Comment #25
pwolanin CreditAttribution: pwolanin commentedbugs have to be fixed in HEAD first
Comment #26
pwolanin CreditAttribution: pwolanin commented@janusman- the patch seems to apply fine (with offset). Here's a re-roll against 8.x.
Comment #27
eileen CreditAttribution: eileen commented#4: 360377_reset_book_cache_6.x_2.patch queued for re-testing.
Comment #28
pwolanin CreditAttribution: pwolanin commentedI think this is pretty trivial - marking rtbc since it's still passing tests.
Comment #29
deviantintegral CreditAttribution: deviantintegral commentedAgreed on the RTBC.
Comment #30
catchtagging for backport.
Comment #31
webchickI can't think of a reasonable way to automatically test this, so committed and pushed to 8.x and 7.x. Thanks.