PDA

View Full Version : How does "Execution Order" in PHP code function?


Skyrider
05-21-2015, 03:47 PM
I closed / removed my previous thread regarding this, thinking I wouldn't be needing it anymore.. but I think I was wrong. I've wrote a code, and give each of them an Execution order number..

First one: 1
Second plugin: 2

Running a very identical code, with:
$dataman->set('usergroupid', xx);

included. Now for some reason, after logging in (hook: login_process on both plugins) the second plugin with execution order 2 appears to beat execution order 1. I've tested both plugins and they work as intended..

Now.. the question is, why is the second one being executed faster then the first one? I included a:

IF $vbulletin->userinfo['usergroupid'] == xx)

to prevent any other usergroups being moved other than the one I selected. Even if I increase the second plugin's execution order, it still beats the first order. Can someone please explain why it does this?

cellarius
05-21-2015, 05:19 PM
I'm not sure I'm following you.

The execution order is just the order of execution (sorry to state the obvious), meaning:
execution order 1: executed first.
execution order 2: executed second.
The same as if you would put both codes into a php file one after the other, or if you would put both code lines into one plugin.

If you run virtually the same code with just a different value, than the second run will just overwrite the first.

Skyrider
05-21-2015, 08:09 PM
Already mentioned that in my post at the bottom:

to prevent any other usergroups being moved other than the one I selected.

yadacodehere

I included a code to prevent that second in overriding it.

Part of the first code:


$dataman =& datamanager_init('User', $vbulletin, ERRTYPE_STANDARD);
$dataman->set_existing($vbulletin->userinfo);

if xxxxxx AND $vbulletin->userinfo['usergroupid'] == 13)

{
$dataman->set('usergroupid', 47);
$dataman->save();
}

Second code:


$dataman =& datamanager_init('User', $vbulletin, ERRTYPE_STANDARD);
$dataman->set_existing($vbulletin->userinfo);

if xxxx AND $vbulletin->userinfo['usergroupid'] == 13)

{
$dataman->set('usergroupid', 37);
$dataman->save();
}

As you can see, both has an IF check if the user is in usergroup 13, and the second code moves the user to that usergroup.. Now, unless I did something wrong with the coding here.. the second code should not override the first one at all. As I mentioned in my post above, separately they work as a charm. (one disabled, other enabled, and the other way around).

kh99
05-21-2015, 08:24 PM
Well, like cellarius said, it's just the order they are executed in. All the code for all plugins on a given hook gets built in to one string, according to the execution order, then it's exec'd when that hook is reached. I've never heard anyone complain that the execution order didn't work right.

Is there a reason you have more than one plugin on a hook, rather than putting it all in one? I've done that myself sometimes just to organize things, but if you're having problems you could try putting it all in one plugin. In any case, we'd probably have to see more code to help you figure out what's going on.

Skyrider
05-21-2015, 08:28 PM
As you mentioned, doing it to organize things. Easier overview. I've put 'most' of the code in the above post (as an edit).. the xxx's are just other variables to check. But that's stating the obvious. The xxx's in both are almost identical, except for 1 check. But dunno why the second would beat the first execution order.

kh99
05-21-2015, 08:40 PM
So how did you determine that they were running in the wrong order?

If you wanted to you could actually see the code that's being executed by using something like var_dump($hook).

Skyrider
05-21-2015, 08:48 PM
So how did you determine that they were running in the wrong order?

If you wanted to you could actually see the code that's being executed by using something like var_dump($hook).
Well, seeing they are 2 different usergroups in the plugins.. it's easy to check where the user has been moved to. Also set up 2 different notices in those 2 groups. I multiple time ran the same check, execution 2 beats 1 (from my view anyway). Even if I alter execution to eg, 4, and leave the first one on 1, 4 beats 1.

I'm not a 'pro' regarding all of this.. Still learning, that's for sure.

Never used var_dump($hook) before, would the code display on a single page and only that single plugin? or....

kh99
05-21-2015, 09:31 PM
Yeah, using var_dump() will output whenever the code is run, and the way vbulletin works is it builds all the output into a string then sends it at the end, so the result would be that something you print out will come out before anything else and likely cause errors as well. Sometimes for debugging i do something like var_dump() a variable then call die, and you'll get a page with nothing else on it, but of course you only want to do that on a test forum. You could also open a file and write stuff to it, then look at the file after. Anyway, if you do decide to try to display that, $hook has all the code for that hook so you only need to var_dump it in one of the plugins.

You might want to temporarily put all the code in one plugin and disable the others, just to rule out the multiple plugins as a potential source of the problem.

Skyrider
05-22-2015, 07:29 AM
Guess I'll combine them instead with if/else. Works for me, but finding it very strange the execution order is not working properly.

kh99
05-22-2015, 08:34 AM
Oh, wait. were you saying that you depend on $vbulletin->userinfo['usergroupid'] == 13 being false in the second one to prevent it from running if the first one already did? Because using the datamanager to change the usergroup will not change $vbulletin->userinfo['usergroupid'] (I don't think).

Skyrider
05-22-2015, 09:22 AM
Oh, wait. were you saying that you depend on $vbulletin->userinfo['usergroupid'] == 13 being false in the second one to prevent it from running if the first one already did? Because using the datamanager to change the usergroup will not change $vbulletin->userinfo['usergroupid'] (I don't think).
Indeed. If the usergroupID is no longer 13 for the user on the second plugin, it shouldn't be executing at all. If that's the case, do you have a better recommendation for a code to move users to another group?

kh99
05-22-2015, 09:32 AM
Actually I'm sorry, I was wrong. It appears that the datamanager does in fact update the userinfo if you're changing the current user. I never knew that.

Skyrider
05-22-2015, 09:34 AM
You learn something everyday! :D.

Which still makes me curious why the execution order isn't working properly. ^^

Skyrider
05-27-2015, 09:45 AM
I'm pretty sure that the execution order is screwed up.

I have this as execution order 2,3 and even tried 4:
$userdata_rank->set('usergroupid', 42);
$userdata_rank->save();

at a specific hook when forum members link their steam account. Simple enough, move user to UsergroupID 42.

This works great, awesome!

Now I've added another plugin to check if the user has been flagged on SteamREP.com as a scammer.

I've disabled the usergroupID 42 move plugin to see after I steam linked my profile on the forums I would be banned.. and, it works!

Now, if I enable both of them (move usergroupdID execution order 2, 3 or 4) with the second steamrep ban plugin execution order 1. I get moved to usergroupID 42, rather than the specific steamrep banned usergroup of which is 26.

So, which made me wonder.. what if execution order 2, 3 or 4 beats 1? So I used the following code at the plugin with execution 2, 3 or 4 (tried all 3):

if (!in_array($vbulletin->userinfo['usergroupid'], array(26)) )
{
$userdata_rank->set('usergroupid', 42);
$userdata_rank->save();
}
26 is ignored, and tada..!! everything suddenly worked.

Now, once again.. why isn't execution order doing its job? Both of these plugins are using the exact same hook. Is it broken? Has anyone tested it? Does execution order depends on the amount of the code? If one is smaller than the other, second one is being executed faster? What's the deal?

cellarius
05-27-2015, 09:57 AM
Honestly, I find it hard to follow what exactly you are doing, but if I read you correctly, your logic and your expectations seem to be at fault.

What you're describing:

- plugin at exec order 1 that puts the user into ug 26.
- second plugin at exec order 2, 3 or 4 that puts the user into ug 42.

Both plugins are executed one after another. Result: The user ends up in ug 42. What else do you expect?

This is confirmed by your second bit of code. Now you tell the second plugin to only move the user into ug 42 if he has not prior been moved to ug 26. Then he stays in 26. If you don't do that check, every user is moved to 42, whatever his prior ug.

Again: That is correct and perfectly logical. I just don't see what else you could expect as a result based on the information you're giving.

Skyrider
05-27-2015, 10:12 AM
Indeed, I just have checked and noticed a flaw in my execution order.. so, I switched them.

The first plugin, with execution order 1:
$userdata_rank->set('usergroupid', 42);
$userdata_rank->save();

Should move any user that steam linked to usergroupID 42.

The second plugin now with execution order 2 but with a much larger code but ends up with this:

if (!in_array($vbulletin->userinfo['usergroupid'], array(6,7,5)) )

RESTOFTHECODE HERE

$dataman->set('usergroupid', 26);
$dataman->save();

^ Code above is not the full code, you get the idea what the code does. but basically means it ignores usergroupID 6,7 and 5 and moves to the user to usergroupID 26.

Oddly enough, when both plugins are active.. the end results are that I'm being moved into usergroupID 42. Which makes me wonder, why. The second plugin is designed to move the user to usergroupID 26 despite whatever usergroup he/she is in (but ignores the specified 3 usergroups).

Which makes me assume, that plugin 2 runs first, and then executes the first plugin with execution order 1. if I disable the first plugin (usergroupID 42), I'm being moved perfectly to usergroupID 26 that is executed by the second plugin.

So.. I'm expecting this:

First plugin -> Steam Link -> Move user to Usergroup 42
Second plugin -> Steam link -> Check steamrep.com -> User is flagged -> Move to usergroupID 26

Seeing the second plugin only ignores usergroupID 6,7 and 5 (which the user isn't even getting into) it should move the user IF the execution order was done properly.

Yet, as I stated.. I end up being in usergroupID 42, rather than 26.

From the way I see things, the execution order isn't working as it should.. It could be me, or the code. but as far as I'm concerned, both plugins are working perfectly if they are running separately. (eg, one is on, other is off)

cellarius
05-27-2015, 11:45 AM
It is your code. Has to be.

It's easy to make sure, and always a good idea to do testing with the most basic approach possible:

Create two plugins at login_process, with execution order set to 1 and 2 respectively.
echo "exec order 1 ";
echo "exec order 2 ";

This prints the expected result when logging in, on top of the page during the redirection process:
exec order 1 exec order 2

Whatever the issue is with your approach, it is not the execution order. It works as expected, and quite honestly, the execution order was introduced way back in the vB3.x days. I'm pretty sure a bug like that would have been found out at some point during the last, like, ten years.

squidsk
05-27-2015, 01:50 PM
The limited bit of code you're actually supplying is making it difficult to diagnose what's going on, but based on the first, more complete, code provided in the third post I would expect the user to remain in the usergroup they are set to in the plugin at execution order 1.

Why do you have the code in two separate plugins instead of in just a single plugin given the code is supposed to run serially (i.e. one after another) anyways?