Allow multiple values in a single variable_set() call

Crell - October 7, 2008 - 04:57
Project:Drupal
Version:7.x-dev
Component:base system
Category:feature request
Priority:normal
Assigned:Crell
Status:patch (code needs work)
Description

This is a fork off of #138544: Use variable_* with an array, 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. :-)

AttachmentSize
variable_set.patch16.45 KB
Testbed results
variable_set.patchfailedFailed: Failed to apply patch. Detailed results

#1

earnie - October 7, 2008 - 11:36

Code style good, need to test.

#2

fgm - October 17, 2008 - 16:11
Status:patch (code needs review)» patch (code needs work)

I 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 accept variable_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 ?

#3

Crell - October 17, 2008 - 17:11
Status:patch (code needs work)» patch (code needs review)

Hm. 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. :-(

#4

fgm - October 17, 2008 - 18:33

I 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.

#5

moshe weitzman - October 24, 2008 - 17:18
Status:patch (code needs review)» patch (code needs work)

Lovely 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.

#6

Crell - October 24, 2008 - 19:43

Hm, valid point that. How does one lock down a menu item to only run during simpletest, though?

#7

moshe weitzman - October 24, 2008 - 19:54

In the test, create a user that has 'access administration pages' and login as that user. then you can increase the access arguments.

#8

stokito - November 28, 2008 - 13:19
Status:patch (code needs work)» duplicate

http://drupal.org/node/138544#comment-1129772

#9

Crell - November 28, 2008 - 19:46
Status:duplicate» patch (code needs review)

This 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.

#10

System Message - November 28, 2008 - 20:00
Status:patch (code needs review)» patch (code needs work)

The last submitted patch failed testing.

 
 

Drupal is a registered trademark of Dries Buytaert.