Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Piwik_Query() throws exception when used in a Tracker event hook #882

Closed
anush opened this issue Jul 26, 2009 · 9 comments
Closed

Piwik_Query() throws exception when used in a Tracker event hook #882

anush opened this issue Jul 26, 2009 · 9 comments
Assignees
Labels
Bug For errors / faults / flaws / inconsistencies etc. Major Indicates the severity or impact or benefit of an issue is much higher than normal but not critical.
Milestone

Comments

@anush
Copy link

anush commented Jul 26, 2009

I get the following exception when trying to use Piwik_Query() from within my plugin:

No entry is registered for key 'db'
#0 C:\\wamp\\www\\piwik\\core\\PluginsFunctions\\Sql.php(24): Zend_Registry::get(\'db\')
#1 C:\\wamp\\www\\piwik\\plugins\\MyPlugin\\MyPlugin.php(136): Piwik_Query(\'INSERT INTO my...\', Array)
#2 [internal function]: Piwik_MyPlugin->myHandler(Object(Piwik_Event_Notification))
#3 C:\\wamp\\www\\piwik\\libs\\Event\\Dispatcher.php(284): call_user_func_array(Array, Array)
#4 C:\\wamp\\www\\piwik\\core\\PluginsManager.php(491): Event_Dispatcher->postNotification(Object(Piwik_Event_Notification), true, false)
#5 C:\\wamp\\www\\piwik\\core\\Tracker\\Action.php(194): Piwik_PostEvent(\'Tracker.Action....\', Object(Piwik_Tracker_Action), Array)
#6 C:\\wamp\\www\\piwik\\core\\Tracker\\Visit.php(142): Piwik_Tracker_Action->record(\'23\', \'16\', 88)
#7 C:\\wamp\\www\\piwik\\core\\Tracker.php(64): Piwik_Tracker_Visit->handle()
#8 C:\\wamp\\www\\piwik\\piwik.php(59): Piwik_Tracker->main()
#9 {main}

Here is my hook registration:

function getListHooksRegistered()
{
    $hooks = array(
        'Tracker.Action.record' => 'myHandler',
        'Menu.add' => 'addMenu',
    );
    return $hooks;
}

And my handler implementation:

public function myHandler($notification)
{
    $trackerAction =& $notification->getNotificationObject();
    $idAction = $trackerAction->getIdAction();

    try
    {
        Piwik_Query('INSERT INTO my_table (idaction, myvalue) VALUES (?,?);', array($idAction, 0));
    }
    catch(Exception $e)
    {
        self::log($e->getMessage());
        self::log($e->getTraceAsString());
    }
} 

--- WORKAROUND ---

A workaround is available, but this deviates from the published "Security Checklist" which recommends the use of Piwik_Query().

Piwik_Tracker::getDatabase()->query($query, $params);
@robocoder
Copy link
Contributor

That's the intended behaviour. To keep it lightweight, the tracker (and tracker hooks) don't use Zend_Db.

Your "workaround" is the correct approach for a tracker hook.

@mattab
Copy link
Member

mattab commented Jul 27, 2009

I created #884 to list all missing documentation about functions to use.
please add a comment if you find more! we would like to make all docs up to date.

@anonymous-matomo-user
Copy link

Can you please explain what the workaround is or provide a link that does? Thanks

@anonymous-matomo-user
Copy link

Ha, nevermind. I don't know why I didn't see that before.

Piwik_Tracker::getDatabase()->query($query, $params);

@mattab
Copy link
Member

mattab commented Mar 16, 2010

reopening and setting as documentation update

@robocoder
Copy link
Contributor

If I'm not mistaken, I think [1970]+[2012] now allows Piwik_Query() to be called from a Tracker event hook.

@robocoder
Copy link
Contributor

Replying to vipsoft:

Whether or not it works depends on whether or not getCacheWebsiteAttributes() has already been called. For example, it wouldn't work for a listener on the 'Tracker.Visit.isExcluded' hook.

@mattab
Copy link
Member

mattab commented Apr 6, 2010

right, I think the idea would be to make the function call work in all cases, or document that Tracker functions must use Piwik_Tracker::getDatabase()->query($query, $params);

@robocoder
Copy link
Contributor

(In [2054]) fixes #882 - Piwik_Query() can now be used by tracker plugins;
fixes #1292 - remove (deprecated/unused) Piwik_Quote() since we use Zend_Db for prepared statements; add Piwik_FetchRow() for completeness

@anush anush added this to the Piwik 0.6 milestone Jul 8, 2014
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug For errors / faults / flaws / inconsistencies etc. Major Indicates the severity or impact or benefit of an issue is much higher than normal but not critical.
Projects
None yet
Development

No branches or pull requests

4 participants