This is a fork off of #138544: Allow multiple variable deletes in variable_del(), which has drifted to become concerned mostly with variable_del(). As discussed there, this patch enhances variable_set() to also accept an array of values to set, which will be done in a single batch operation. The primary advantage of doing so is that it only clears the cache once, so it's a bit faster if you're setting a lot of variables. The previous syntax is unaffected and still works exactly as before.
The only place it's likely to make a huge difference is on system_settings_form() pages, but I went through and converted core anyway. I skipped some unit tests because, well, I don't think it really makes a difference there. :-)
Oh yeah, and the unit tests for variables have been updated to test both syntaxes, and the tests pass. :-)
Comment | File | Size | Author |
---|---|---|---|
#18 | 317930.patch | 7.64 KB | RobLoach |
#17 | drupal_variable_functions.patch | 7.54 KB | larowlan |
#13 | variable-set-array_317930_3.patch | 13.59 KB | Berdir |
#11 | variable-set-array_317930_v2.patch | 14.34 KB | joshmiller |
variable_set.patch | 16.45 KB | Crell | |
Comments
Comment #1
Anonymous (not verified) CreditAttribution: Anonymous commentedCode style good, need to test.
Comment #2
fgmI just had another idea in that vein: instead of registering variables with yet another hook, if should be possible to extend the syntax of variable_set() beyond what you just did. Instead of having it
variable_set($name, $value = NULL)
why not go further and acceptvariable_set($name, $value = NULL, $default = NULL). This would set the default if the third argument were present, or leave it unchanged otherwise. And maybe while we're at it, add the long-talked-about fourth argument defining prefetching to solve the problem outlined by DWW, and which other modules like chatroom have.
From there, variable_get() could also be modified like variable_get($name, $default = NULL). And using variable_get($name) would now use the default defined upon variable_set'ting, still leaving the current syntax available, to serve two purposes: backwards compatibility, and ability to change defaults on the fly without otherwise affecting the stored default.
I tend to think this is more elegant than adding a hook_variables.
What do you think of it ?
Comment #3
Crell CreditAttribution: Crell commentedHm. Actually I think tacking on still-more parameters to variable_set() is exactly the wrong way to go about it, is a step backward, and doesn't solve the underlying problem in the first place. It's also fundamentally flawed as you can never guarantee that you'd always be able to variable_set() before you'd variable_get().
So no, I'm much much MUCH more in favor of a registry-style hook_variables to centrally manage all of that sort of information. It's also well off-topic to this issue, which should be a nice and simple one. :-(
Comment #4
fgmI have to agree with the fact that it is slightly OT. But not with the other claims, especially the fact that "you can never guarantee": such variables should probably always be created in the .install file anyway, thus ensuring that they have a value at "run" time, while code running not running after the page full bootstrap process, could still use the second param in variable_get(). It would really reduce redundant code in a lot of places, and would still get us the same benefits as some hook_variables variant without the additional API clutter of yet one more core hook.
But you're right, your suggestion does not have to touch these larger issues, and can remain "atomic" as you suggested it.
Comment #5
moshe weitzman CreditAttribution: moshe weitzman commentedLovely patch. Perhaps I am paranoid, but I would like to see a stronger 'access arguments' for the test menu item $items['system-test/variable-get']. i could imagine that this module get stuck enabled on someone's site and now their variables are exposed. After that, this needs a verify that the tests pass and then RTBC.
Comment #6
Crell CreditAttribution: Crell commentedHm, valid point that. How does one lock down a menu item to only run during simpletest, though?
Comment #7
moshe weitzman CreditAttribution: moshe weitzman commentedIn the test, create a user that has 'access administration pages' and login as that user. then you can increase the access arguments.
Comment #8
stokito CreditAttribution: stokito commentedhttp://drupal.org/node/138544#comment-1129772
Comment #9
Crell CreditAttribution: Crell commentedThis is not a duplicate of that issue. It is a very deliberate fork as that issue was getting bogged down with other matters, as stated in the original post.
Comment #11
joshmillerCrell -
Since you've been helping so much on the sister issue #138544: Allow multiple variable deletes in variable_del(), I thought I would re-roll this against HEAD to help out.
I've reviewed the code and looks very solid. Even includes a test case (though, it needs some more testing according to @5,@6,@7).
Josh
Comment #13
BerdirRe-rolled against latest HEAD.
-
Comment #15
RobLoachYup.
Comment #16
larowlansee also #975118: Allow variable functions (variable_del, variable_get, variable_set) to receive an array of name/value pairs
Comment #17
larowlanhere is patch from #975118: Allow variable functions (variable_del, variable_get, variable_set) to receive an array of name/value pairs
Comment #18
RobLoachComment #19
Lars Toomre CreditAttribution: Lars Toomre commented@catch directed me to this issue from #987768: [PP-1] Optimize variable caching. In that issue, progress is being made toward building a cached incremental variables array. Part of that refactoring effort involves moving a data base call to variable_get() in the event of a missing $conf[] entry.
Reading the patch there led me to raise the question whether we did not want to introduce a variable helper function that bulk loads a limited number of variables from the {variables} table to $conf[] and cache based either on variable name pattern or a list of variable names. I similarly raised the thought that we should introduce bulk operations with functions like variables_set(array) and variables_del(array) [note plural format of variable].
I am not tied to idea of new plural functions, but thought it might make sense for explicitly different function calling patterns in the case of bulk operations. I am repeating those thoughts here with a hope to push along a solution for bulk variable set()/del() calls.
Comment #20
BerdirI think using separate functions sounds like a good idea, we've been trying to do similar things elsewhere too.
However, my naming suggestion would be variable_(get|set|del)_multiple(), then it is very similar to entity load functions for example and IMHO easier to find than with a different prefix.
Just like node_load() and node_load_multiple(), variable_get() could then simply be a wrapper function around variable_get_multiple().
Comment #21
RobLoachGrrrrr, in almost every other language, you can do things like:
I miss strict languages... As you can tell, I'm still against having multiple functions doing the same thing. #D8DX, please!
Comment #23
Lars Toomre CreditAttribution: Lars Toomre commentedAfter posting #19 above, I realized most of the discussion about function call format has occurred over in #138544: Allow multiple variable deletes in variable_del(). May I suggest that we focus the discussion about the function call patterns over there?
Then we can return here to implement whatever is agreed upon for the variable set() and del() functions. The proposed implementation of the variable_set() function has been broken off into #317930: Allow multiple values in a single variable_set() call and needs to be part of the function profile discussion as well.
+1 for variable_(get|set|del)_multiple() pattern as it would match many other Drupal calling patterns.
Edit: Just realized this was the multiple variable_get() thread... not the multiple set()/get() branch. Main thought about define function profiles in #138544: Allow multiple variable deletes in variable_del() still applies.
Comment #24
Lars Toomre CreditAttribution: Lars Toomre commentedSorry cross-post changed issue tags ...
Comment #25
brianV CreditAttribution: brianV commentedSetting as 'Won't Fix' as this is no longer really applicable in light of the CMI changes.