Currently, if a module calls drupal_get_path_alias() in an implementation of hook_url_inbound_alter(), it will cause a cache miss on path aliases when viewing the front page.
drupal_lookup_path() uses current_path() as a $cid. This is fine as long as $_GET['q'] is populated, but if it isn't it tries to use an empty string as a cache id.
Normally it would be populated already, but drupal_path_initialize() calls drupal_get_normal_path() before setting $_GET['q'] in the case that it's empty (front page), so if any child function of drupal_get_normal_path() calls drupal_lookup_path(), it creates the cache miss.
I ran into this on a D7 site using the redirect module. The call chain went like this:
drupal_normal_path calls => redirect_url_inbound_alter => drupal_get_path_alias => drupal_lookup_path
The attatched patch fixes this by simply setting $_GET['q'] before calling drupal_normal_path().
Comment | File | Size | Author |
---|---|---|---|
#31 | getq-d7-1329914-31.patch | 2.01 KB | xjm |
#28 | getq-1329914-28.patch | 2.07 KB | xjm |
#22 | getq-grr-tests.patch | 1.41 KB | xjm |
#22 | getq-grr-combined.patch | 2.01 KB | xjm |
#19 | get-q-1329914-19-tests.patch | 1.41 KB | xjm |
Comments
Comment #1
catchComment #3
catchComment #4
moshe weitzman CreditAttribution: moshe weitzman commentedLooks good to me. This would be extremely hard to test for so RTBC.
Comment #5
amateescu CreditAttribution: amateescu commentedLooks like this needs to be backported to 7.x as well, tagging.
Comment #6
chx CreditAttribution: chx commentedThanks for working on this patch. It could use an automated test before it is committed. If you need help you can ask the friendly people in the #drupal-contribute IRC channel.
Also, it has a whitespace error (only one beginning space).
Comment #7
msonnabaum CreditAttribution: msonnabaum commentedIndentation fixed. Thanks chx.
Comment #8
chx CreditAttribution: chx commentedthe test is adding an if (!$path) { echo '$_GET[\'q\'] is set: ' . intval(isset($_GET['q'])) } to the inbound_alter in core/modules/simpletest/tests/url_alter_test.module i pointed out then adding a drupalGet('') to path.test and then asserting for $_GET['q'] is set: 1.
Comment #9
xjmI will attempt to parse chx's comment in #8 to add a test for this.
Setting NR just to send the patch to the bot.
Comment #10
xjmHad to switch some stuff around a little, but here's test-only and combined patches.
Comment #12
xjmOkay, totally rolled against my previous commit so those are going to fail to apply. Sorry. Here are the real ones.
Comment #13
apadernoMaybe I am saying something silly, but should not using a global variable that is set in
url_alter_test_url_inbound_alter()
and checked in the test be better?Comment #14
xjm@kiamlaluno -- I don't quite understand what you're saying; could you clarify?
I just rolled the test chx typed inline (more or less). Locally, it fails without the fix and passes with it.
Comment #15
xjmThough, looks like I accidentally deleted the inline comment here that explained what this hunk is for. I'll add that back after dinner.
Edit: Actually, I think I'll change it a little bit to be more self-documenting, like:
Comment #16
xjmComment #17
apadernoI was thinking of something similar to the following:
The test then would check the value of
$_url_alter_q
.Comment #18
xjmEr. I'm pretty sure that won't work. :)
Assertions checking test output like that are pretty standard. See, for example UrlAlterFunctionalTest::TestCurrentUrlRequestedPath(), which has a similar function to this guy.
Comment #19
xjmComment #21
xjmSlightly confused. That test does not fail locally.
Comment #22
xjmSlightly modified test to avoid using
exit
. The test-only fails locally, and the combined passes... but then, the previous versions did too.Comment #23
xjmComment #24
xjmHooray.
Comment #25
chx CreditAttribution: chx commentedWhat about summarizing OP in a comment in path init: "if a module calls drupal_get_path_alias() in an implementation of hook_url_inbound_alter(), it will cause a cache miss on path aliases when viewing the front page. Setting $_GET['q'] before calling drupal_normal_path() to avoid this." (i literally copypasted OP and deleted most i didnt change a word.)
Comment #26
msonnabaum CreditAttribution: msonnabaum commentedIs it really necessary? Seems odd to me to have a comment about a really obscure bug when the code change is so minimal.
Comment #27
xjmWell, on the other hand, I guess we might want to make sure no one reverts the fix in the future because they don't see the point. The comment could be more general.
Comment #28
xjmAnd hey, the indentation of the original fix was a space short, so bonus.
Comment #29
chx CreditAttribution: chx commentedLooks good.
Comment #30
catchLooks great! Committed/pushed to 8.x.
Moving to 7.x, will need a re-roll.
Comment #31
xjmD7 reroll.
Comment #32
webchickThat tells me the what, but not the why. I actually would like to see more of the why here, despite the obscurity of the bug.
So I changed that to (with chx's blessing):
Committed #31 (in full) to 7.x, and that comment (only) to 8.x. Marking fixed.