<div dir="ltr"><div style>I know we are going OT, but I also want to understand this;</div><div><br></div>what about:<div><br></div><div><pre class="" style="margin-top:0px;margin-bottom:0px;padding:0px 5px 16px;border-width:1px 1px 1px 15px;border-style:solid;border-color:rgb(204,204,204);font-size:12px;vertical-align:baseline;overflow:visible;width:auto;line-height:1.333;color:rgb(85,85,85);background-color:rgb(250,250,250);clear:none">
<span style="margin:0px;padding:0px;border:0px;vertical-align:baseline;color:rgb(0,0,0);font-weight:bold">function</span> mymodule_complex_calculation<span style="margin:0px;padding:0px;border:0px;vertical-align:baseline;color:rgb(0,153,0)">(</span><span style="margin:0px;padding:0px;border:0px;vertical-align:baseline;color:rgb(0,153,0)">)</span> <span style="margin:0px;padding:0px;border:0px;vertical-align:baseline;color:rgb(0,153,0)">{</span>
static <span style="margin:0px;padding:0px;border:0px;vertical-align:baseline;color:rgb(0,0,136)">$cache</span><span style="margin:0px;padding:0px;border:0px;vertical-align:baseline;color:rgb(51,153,51)">;</span>
<span style="margin:0px;padding:0px;border:0px;vertical-align:baseline;color:rgb(177,177,0)">if</span> <span style="margin:0px;padding:0px;border:0px;vertical-align:baseline;color:rgb(0,153,0)">(</span><span style="margin:0px;padding:0px;border:0px;vertical-align:baseline;color:rgb(153,0,0)">isset</span><span style="margin:0px;padding:0px;border:0px;vertical-align:baseline;color:rgb(0,153,0)">(</span><span style="margin:0px;padding:0px;border:0px;vertical-align:baseline;color:rgb(0,0,136)">$cache</span><span style="margin:0px;padding:0px;border:0px;vertical-align:baseline;color:rgb(0,153,0)">)</span><span style="margin:0px;padding:0px;border:0px;vertical-align:baseline;color:rgb(0,153,0)">)</span> <span style="margin:0px;padding:0px;border:0px;vertical-align:baseline;color:rgb(0,153,0)">{</span>
<span style="margin:0px;padding:0px;border:0px;vertical-align:baseline;color:rgb(177,177,0)">return</span> <span style="margin:0px;padding:0px;border:0px;vertical-align:baseline;color:rgb(0,0,136)">$cache</span><span style="margin:0px;padding:0px;border:0px;vertical-align:baseline;color:rgb(51,153,51)">;</span>
<span style="margin:0px;padding:0px;border:0px;vertical-align:baseline;color:rgb(0,153,0)">}</span>
<span style="margin:0px;padding:0px;border:0px;vertical-align:baseline;color:rgb(102,102,102);font-style:italic">// Otherwise we need to perform our calculations</span>
<span style="margin:0px;padding:0px;border:0px;vertical-align:baseline;color:rgb(102,102,102);font-style:italic">// ...some complex calculation that returns </span>
<span style="margin:0px;padding:0px;border:0px;vertical-align:baseline;color:rgb(0,0,136)">$cache</span> <span style="margin:0px;padding:0px;border:0px;vertical-align:baseline;color:rgb(51,153,51)">=</span> complex_calculation<span style="margin:0px;padding:0px;border:0px;vertical-align:baseline;color:rgb(0,153,0)">(</span><span style="margin:0px;padding:0px;border:0px;vertical-align:baseline;color:rgb(0,153,0)">)</span><span style="margin:0px;padding:0px;border:0px;vertical-align:baseline;color:rgb(51,153,51)">;</span>
<span style="margin:0px;padding:0px;border:0px;vertical-align:baseline;color:rgb(177,177,0)">return</span> <span style="margin:0px;padding:0px;border:0px;vertical-align:baseline;color:rgb(0,0,136)">$cache</span><span style="margin:0px;padding:0px;border:0px;vertical-align:baseline;color:rgb(51,153,51)">;</span>
<span style="margin:0px;padding:0px;border:0px;vertical-align:baseline;color:rgb(0,153,0)">}</span></pre></div><div class="gmail_extra"><br></div><div class="gmail_extra"><a href="http://techblog.zabuchy.net/2011/enhancing-drupal-performance-with-drupal_static/">http://techblog.zabuchy.net/2011/enhancing-drupal-performance-with-drupal_static/</a></div>
<div class="gmail_extra"><br></div><div class="gmail_extra">You are saying that if I instantiate a variable in "mymodule_impersonate()" it will not be available in "mymodule_unimpersonate()"?</div><div class="gmail_extra">
<br></div><div class="gmail_extra"><br><div class="gmail_quote">2013/1/21 Earnie Boyd <span dir="ltr"><<a href="mailto:earnie@users.sourceforge.net" target="_blank">earnie@users.sourceforge.net</a>></span><br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">
You "think" wrongly. A static variable has local scope to the<br>
function declaring the static variable.<br>
<div class=""><div class="h5"><br>
On Mon, Jan 21, 2013 at 11:03 AM, Lluís Forns <<a href="mailto:enboig@gmail.com">enboig@gmail.com</a>> wrote:<br>
> I think static are also available to other functions, use them to store<br>
> cached information across my code and they work.<br>
><br>
><br>
> 2013/1/21 Darren Oh <<a href="mailto:darrenoh@sidepotsinternational.com">darrenoh@sidepotsinternational.com</a>><br>
>><br>
>> Static variables are only saved for a single function. Global variables<br>
>> are available to all functions during a single page request. Session<br>
>> variables are available to all page requests during a session.<br>
>><br>
>> On Jan 21, 2013, at 10:08 AM, Lluís Forns wrote:<br>
>><br>
>> Darren, thanks for the link. What is the difference between using global<br>
>> or static to save the data?<br>
>><br>
>><br>
>><br>
>> 2013/1/21 Lluís Forns <<a href="mailto:enboig@gmail.com">enboig@gmail.com</a>><br>
>>><br>
>>> My code was from a module where impersonation recursively could happen,<br>
>>> this is why I use a stack to save user state. I am also saving it to session<br>
>>> and not using a static variable because I may uses batch functions.<br>
>>><br>
>>> But I see some problems right now:<br>
>>> - To be safe impersonation should be saved into static variables (and<br>
>>> "using session_save_session(FALSE);" ), so if anything goes wrong, $user is<br>
>>> not afected.<br>
>>> - If I want to be able to execute every Rules action in a batch, I need<br>
>>> to change user "per action"<br>
>>><br>
>>> Any idea of how to "inject" code before and after a rule action?<br>
>>><br>
>>><br>
>>> 2013/1/21 Walt Daniels <<a href="mailto:wdlists@gmail.com">wdlists@gmail.com</a>><br>
>>>><br>
>>>> We do the latter simple trick in our store code so we can show people<br>
>>>> the member prices. We have a dummy id that is a member so we switch to it,<br>
>>>> pick up the price and use it so we can show both member and non-member<br>
>>>> prices on the screen.<br>
>>>><br>
>>>><br>
>>>> On Mon, Jan 21, 2013 at 9:18 AM, Christian López Espínola<br>
>>>> <<a href="mailto:penyaskito@computer.org">penyaskito@computer.org</a>> wrote:<br>
>>>>><br>
>>>>> Haven't tested your code, but maybe you want to take a look at<br>
>>>>> masquerade_switch_user and masquerade_switch_back:<br>
>>>>> <a href="http://drupalcode.org/project/masquerade.git/blob/HEAD:/masquerade.module" target="_blank">http://drupalcode.org/project/masquerade.git/blob/HEAD:/masquerade.module</a>.<br>
>>>>><br>
>>>>> And if the impersonation is only for the given request, maybe just<br>
>>>>><br>
>>>>> global $user;<br>
>>>>> $previous = $user;<br>
>>>>> $user = user_load($uid);<br>
>>>>> // Do your stuff.<br>
>>>>> $user = $previous;<br>
>>>>><br>
>>>>> would work.<br>
>>>>><br>
>>>>><br>
>>>>><br>
>>>>> On Mon, Jan 21, 2013 at 10:37 AM, Lluís Forns <<a href="mailto:enboig@gmail.com">enboig@gmail.com</a>> wrote:<br>
>>>>>><br>
>>>>>> Some of my scheduled task needs to run as the user who triggered them<br>
>>>>>> (not anonymous neither admin); so I thought of writing two actions:<br>
>>>>>> - One to impersonate a user, which will receive uid as an argument.<br>
>>>>>> - Another to undo the impersonate.<br>
>>>>>> All the called actions should be between them.<br>
>>>>>><br>
>>>>>> Anybody see any fault in this idea or the actions code?<br>
>>>>>><br>
>>>>>> Thanks.<br>
>>>>>><br>
>>>>>> function wu_action_impersonate($uid) {<br>
>>>>>> global $user;<br>
>>>>>> if (!array_key_exists('wu_action_impersonate', $_SESSION)) {<br>
>>>>>> $_SESSION['wu_action_impersonate'] = array();<br>
>>>>>> }<br>
>>>>>> array_push($_SESSION['wu_action_impersonate'], $user);<br>
>>>>>> session_save_session(FALSE);<br>
>>>>>> $user = user_load($uid);<br>
>>>>>> user_load($uid);<br>
>>>>>> }<br>
>>>>>><br>
>>>>>> function wu_action_unimpersonate() {<br>
>>>>>> global $user;<br>
>>>>>> if (array_key_exists('wu_action_impersonate', $_SESSION)) {<br>
>>>>>> if (!empty($_SESSION['wu_action_impersonate'])) {<br>
>>>>>> $user = array_pop($_SESSION['wu_action_impersonate']);<br>
>>>>>> }<br>
>>>>>> if (empty($_SESSION['wu_action_impersonate'])) {<br>
>>>>>> unset($_SESSION['wu_action_impersonate']);<br>
>>>>>> }<br>
>>>>>> }<br>
>>>>>> session_save_session(TRUE);<br>
>>>>>> }<br>
>>>>>><br>
>>>>>> --<br>
>>>>>> *Ser freak no és imprescindible per ser informàtic, però ajuda.<br>
>>>>>> *La vida no ha de ser feliç, ha de ser plena.<br>
>>>>>> *Abans d'imprimir aquest missatge, pensa en el medi ambient.<br>
>>>>><br>
>>>>><br>
>>>>><br>
>>>>><br>
>>>>> --<br>
>>>>> Cheers,<br>
>>>>><br>
>>>>> Christian López Espínola <penyaskito AT computer DOT org><br>
>>>>> <a href="http://twitter.com/penyaskito" target="_blank">http://twitter.com/penyaskito</a> | <a href="http://penyaskito.com" target="_blank">http://penyaskito.com</a><br>
>>>><br>
>>>><br>
>>><br>
>>><br>
>>><br>
>>> --<br>
>>> *Ser freak no és imprescindible per ser informàtic, però ajuda.<br>
>>> *La vida no ha de ser feliç, ha de ser plena.<br>
>>> *Abans d'imprimir aquest missatge, pensa en el medi ambient.<br>
>><br>
>><br>
>><br>
>><br>
>> --<br>
>> *Ser freak no és imprescindible per ser informàtic, però ajuda.<br>
>> *La vida no ha de ser feliç, ha de ser plena.<br>
>> *Abans d'imprimir aquest missatge, pensa en el medi ambient.<br>
>><br>
>><br>
><br>
><br>
><br>
> --<br>
> *Ser freak no és imprescindible per ser informàtic, però ajuda.<br>
> *La vida no ha de ser feliç, ha de ser plena.<br>
> *Abans d'imprimir aquest missatge, pensa en el medi ambient.<br>
<br>
<br>
<br>
</div></div><span class=""><font color="#888888">--<br>
Earnie<br>
-- <a href="https://sites.google.com/site/earnieboyd" target="_blank">https://sites.google.com/site/earnieboyd</a><br>
</font></span></blockquote></div><br><br clear="all"><div><br></div>-- <br>*Ser freak no és imprescindible per ser informàtic, però ajuda.<br>*La vida no ha de ser feliç, ha de ser plena.<br>*Abans d'imprimir aquest missatge, pensa en el medi ambient.
</div></div>