We use cookies to improve your experience. No personal information is gathered and we don't serve ads. Cookies Policy.

ExpressionEngine Logo ExpressionEngine
Features Pricing Support Find A Developer
Partners Upgrades
Blog Add-Ons Learn
Docs Forums University
Log In or Sign Up
Log In Sign Up
ExpressionEngine Logo
Features Pro new Support Find A Developer
Partners Upgrades
Blog Add-Ons Learn
Docs Forums University Blog
  • Home
  • Forums

update_extension function called twice?

Development and Programming

Cocoaholic's avatar
Cocoaholic
445 posts
17 years ago
Cocoaholic's avatar Cocoaholic

Hi,

I am trying to update an extension from version 1.0 to 1.1 using the “update_extension” function. Therefore I need to add another hook call in version 1.1 so my code looks like this:

function update_extension($current='')
{
    global $DB;
    
    if ($current == '' OR $current == $this->version)
    {
        return FALSE;
    }
    
    if ($current < '1.1')
    {
        // Update from 1.0 to 1.1
        $DB->query($DB->insert_string('exp_extensions',
                array(
                'extension_id'    => '',
                'class'            => $this->classname,
                'method'        => 'fetch_group_name',
                'hook'            => 'publish_form_weblog_preferences',
                'settings'        => '',
                'priority'        => 10,
                'version'        => $this->version
                )
            )
        );
    }
    
    $data = array('version' => $this->version);
    $DB->query($DB->update_string('exp_extensions', $data, "class = '$this->classname'"));
}

The problem however is that the “update_extension” function is called twice, once in “cp.utilities.php” next in “core.extensions.php”

Because of this, the above query is run twice and I end up with a double entry for that extension hook in ext_extensions. I have seen this same behaviour in other extensions (not mine).

Is this a bug or am I missing something here?

Regards, Elwin

       
Paul Burdick's avatar
Paul Burdick
480 posts
17 years ago
Paul Burdick's avatar Paul Burdick

If the version number is updated correctly in exp_extensions, then why would that query be done twice, since it is in a conditional that checks the version number?

Further the update_extension() method is only called in core.extensions.php if the extension hook for that extension is being called, which should not be happening in cp.utilities.php.

       
Cocoaholic's avatar
Cocoaholic
445 posts
17 years ago
Cocoaholic's avatar Cocoaholic

Paul,

I did some more testing on 2 separate clean installs. Both version 1.5.2 an 1.6.1 show the same behaviour.

However, I narrowed it down to the “Extension Manager” page being the only page that calls the update function twice.

this is the code I have tested:

function update_extension($current='')
{
    global $DB;
    
    if ($current == '' || $current == $this->version)
    {
        return FALSE;
    }
    
    if ($current < '1.1')
    {
        // Update from 1.0 to 1.1
        error_log('updating '.$current.' to '.$this->version);
    }
    
    $data = array('version' => $this->version);
    $DB->query($DB->update_string('exp_extensions', $data, "class = '$this->classname'"));
}

an this is the result:

On any CP page:

[Tue Nov 27 20:34:47 2007] [error] [client ::1] updating 1.0 to 1.1, referer: http://localhost/ee161/system/index.php?S=0&C=myaccount

On Extensions Manager page:

[Tue Nov 27 20:31:58 2007] [error] [client ::1] updating 1.0 to 1.1, referer: http://localhost/ee161/system/index.php?S=0&C=admin&M=utilities&P=extensions_manager
[Tue Nov 27 20:31:58 2007] [error] [client ::1] updating 1.0 to 1.1, referer: http://localhost/ee161/system/index.php?S=0&C=admin&M=utilities&P=extensions_manager

I have the feeling that the $version variable isn’t updated in time.

When I log the $version variable in both “cp.utilities.php” and “core.extensions.php” I get the following:

[Tue Nov 27 20:40:24 2007] [error] [client ::1] cp.utilities.php: version 1.1, referer: http://localhost/ee161/system/index.php?S=0&C=admin&M=utilities&P=extensions_manager
[Tue Nov 27 20:40:24 2007] [error] [client ::1] core.extensions.php: version 1.0, referer: http://localhost/ee161/system/index.php?S=0&C=admin&M=utilities&P=extensions_manager

It might be just on my Mac, haven’t tested other servers yet. I’m running PHP Version 5.2.4 on Leopard.

       
Paul Burdick's avatar
Paul Burdick
480 posts
17 years ago
Paul Burdick's avatar Paul Burdick

I cannot duplicate. For some reason I do not think that your version update query is being done correctly. Are you sure $this->classname is set correctly?

       
Cocoaholic's avatar
Cocoaholic
445 posts
17 years ago
Cocoaholic's avatar Cocoaholic

Yes, I get the exact same result when not using $this->classname.

Just tested on another live Linux server, same problem.

I have seen this same thing happen with other people’s extensions so I’m pretty sure it is not just my code acting up.

So… for me, on more than one server, using different extensions, the update function is called twice on the “Extension Manager” page.

Thanks for looking into this…

Cheers, Elwin

       
Paul Burdick's avatar
Paul Burdick
480 posts
17 years ago
Paul Burdick's avatar Paul Burdick

Hm, well, I have not heard of this problem before and I cannot duplicate on a freshly written extension. The only way I could duplicate the problem was if the database was not updated correctly. If you want to send me your extension I can look at it and try to duplicate with it, or I probably could just look at the server, even though I am using Leopard with PHP 5.2.4 as well.

       
Paul Burdick's avatar
Paul Burdick
480 posts
17 years ago
Paul Burdick's avatar Paul Burdick

Actually, here is an idea. After the update query for the version number. Do a SELECT query and compare the version number to what it should be and see if you get anything.

       
Cocoaholic's avatar
Cocoaholic
445 posts
17 years ago
Cocoaholic's avatar Cocoaholic

Hi Paul,

I tried this code, which returned the new version 1.1 correctly… but still twice. The database gets updated correctly.

$data = array('version' => $this->version);
$DB->query($DB->update_string('exp_extensions', $data, "class = 'Ez_category_checkboxes'"));

$query = $DB->query("SELECT version FROM exp_extensions WHERE class = 'Ez_category_checkboxes'");

if ($query->num_rows > 0)
{
    foreach ($query->result as $row)
    {
        error_log($row['version']);
    }
}

the actual result was this:

[Wed Nov 28 00:05:47 2007] [error] [client ::1] 1.1, referer: http://localhost/ee161/system/index.php?S=0&C=admin&M=utilities&P=extensions_manager
[Wed Nov 28 00:05:47 2007] [error] [client ::1] 1.1, referer: http://localhost/ee161/system/index.php?S=0&C=admin&M=utilities&P=extensions_manager
[Wed Nov 28 00:05:47 2007] [error] [client ::1] core.extensions.php: version 1.0, referer: http://localhost/ee161/system/index.php?S=0&C=admin&M=utilities&P=extensions_manager

That last line clearly shows that core.extensions.php still sees the old 1.0 version instead of the 1.1 in the database.

I will email you the extension.

       
Paul Burdick's avatar
Paul Burdick
480 posts
17 years ago
Paul Burdick's avatar Paul Burdick

You know what is even more confusing? The fact that cp.utilities.php loops through the extension files and only calls the update_extension() method in once place. I am not quite sure how it could be calling it twice unless for some awful reason the method_exists() used to check that the update_extension() method exists in the Extension file is somehow calling it as well.

Perhaps try doing a debug_backtrace() as well to find how where the request is coming from. Oh, and double check that your error log file is completely empty before testing.

       
stinhambo's avatar
stinhambo
1,268 posts
17 years ago
stinhambo's avatar stinhambo

I tested this for Elwin and I get the word ‘update’ in the top left twice. I am running 1.61 with the latest build on Leopard and running MAMP Pro.

       
Cocoaholic's avatar
Cocoaholic
445 posts
17 years ago
Cocoaholic's avatar Cocoaholic

Thanks Hambo!

I sent the attached file to Steve and asked him to do the following:

1) install the extension, keep the Extension Manager page open! 2) change the version to 1.1 3) refresh the page an tell me what it reads top left of the page

The result should be “update”, but again, it was “update update” because the update function is being called twice.

@Paul, did you receive the extension I sent you? or is the emailaddress I have outdated.

       
Low's avatar
Low
407 posts
17 years ago
Low's avatar Low

I can confirm the same behaviour Cocoaholic and stinhambo describe, on a 1.6.0 and 1.6.1 installation, using xampp on WinXP (php 5.2.2). Two ‘update’ in the top left of the page using Elwin’s test extension.

       
Paul Burdick's avatar
Paul Burdick
480 posts
17 years ago
Paul Burdick's avatar Paul Burdick

I did not receive any email from you, Cocoaholic. Jones looked at it this morning though and he thinks he sees the problem now that we actually have your code in front of us.

Your extension is being called by both core.extensions.php and cp.utilities.php. The show_full_control_panel hook is called after the cp.utilities.php code is run. Unfortunately, the core.extensions.php has already loaded up the information for that extension before it is called, and it is called after it has been updated by cp.utilities.php. So, we are going to have to update some information in this specific case so that there is no tramping of toes.

       
Cocoaholic's avatar
Cocoaholic
445 posts
17 years ago
Cocoaholic's avatar Cocoaholic

Yeah, I sent it to your old @pmachine address so that was to be expected 😉

Thanks for digging further.

I will go with this update function for now, seems to work ok, but I’m pretty sure someone else could come up with something better.

function update_extension($current='')
{
    global $DB, $IN;
    
    if ($current == '' || $current == $this->version)
    {
        return FALSE;
    }
    
    // custom version check because we're using the show_full_control_panel_end hook
    $query = $DB->query("SELECT version FROM exp_extensions WHERE class = '".$this->classname."'");
    if ($query->num_rows > 0)
    {
        if ($query->row['version'] < $this->version)
        {
            
            if ($current < '1.1')
            {
                // update from 1.0 to 1.1
                echo 'update to 1.1';
            }
            
        }
    }
    
    $data = array('version' => $this->version);
    $DB->query($DB->update_string('exp_extensions', $data, "class = 'Ez_category_checkboxes'"));
    
}
       
Paul Burdick's avatar
Paul Burdick
480 posts
17 years ago
Paul Burdick's avatar Paul Burdick

OK, but core.extensions.php is going to run itself for the show_full_control_panel hook after your extension is loaded in cp.utilities.php, so this is actually not going to help stop the calling of update_extension() in core.extensions.php, which is where it is happening the second time.

       
1 2

Reply

Sign In To Reply

ExpressionEngine Home Features Pro Contact Version Support
Learn Docs University Forums
Resources Support Add-Ons Partners Blog
Privacy Terms Trademark Use License

Packet Tide owns and develops ExpressionEngine. © Packet Tide, All Rights Reserved.