Please upgrade here. These earlier versions are no longer being updated and have security issues.
HackerOne users: Testing against this community violates our program's Terms of Service and will result in your bounty being denied.
Options

BUG (?): Plugin Setup() gets called twice when plugin is enabled?

sprockettsprockett New
edited December 2010 in Vanilla 2.0 - 2.8
I've making a plugin and realised that for some reason all my table initialisation queries contained within Setup() get called twice?
In other words, anytime a plugin is "Enabled" within settings page, the Setup() function of the plugin gets called twice.

To recreate this:
1. you can create a test table "gdn_test" with 1 integer field
2. Inside any plugin's SETUP() function, you can add this:

$SQL = Gdn::SQL();
$SQL->Update('test')
->Set('test', 'test+1', FALSE)->Put();

Disable the plugin and reenable it. You will realise that the value in the TEST field gets increased by 2 every time.

Am I doing something wrongly or is this a bug?

Thanks...
Tagged:

Comments

  • Options
    I have isolated the double call to some code in the dispatcher:

    // Call the requested method on the controller - error out if not defined. if ($PluginManagerHasReplacementMethod || method_exists($Controller, $ControllerMethod)) { // call_user_func_array is too slow!! //call_user_func_array(array($Controller, $ControllerMethod), $this->_ControllerMethodArgs); if ($PluginManagerHasReplacementMethod) { try { Gdn::PluginManager()->CallNewMethod($Controller, $Controller->ControllerName, $ControllerMethod); } catch (Exception $Ex) { $Controller->RenderException($Ex); } } else { $Args = $this->_ControllerMethodArgs; $Count = count($Args); //Setup() of Plugin gets called again here: try { call_user_func_array(array($Controller, $ControllerMethod), $Args); } catch (Exception $Ex) { $Controller->RenderException($Ex); exit(); } } } else { Gdn::Request()->WithRoute('Default404'); return $this->Dispatch(); }
  • Options
    ok apologies for the multiple posts, i had assumed this was a task too complicated for me to debug (php beginner), but I have maanged to find what is causing the problem.

    The Settings page calls (via jquery) a "testaddon" function, which tries to initialise the addon before the addon is actually "enabled".

    This function is located in dashboard\controllers\class.settingscontroller.php.

    So effectively, the plugin's setup() will be called twice. Once for testing, and once for the actual enabling of the plugin.

    In most cases this would not necessarily result in an error. However, if there is anything done within SETUP() of your plugin that should only be done ONCE (in my case, it is prepopulation of a DB table), there will possibly be erroneous results.

    A workaround for this would probably be NOT to include any oneoff initialisation functions in the SETUP(), but to branch it off separately to another "initialise" button to be done only after successful enabling of the plugin.
Sign In or Register to comment.