Skip to content

Commit

Permalink
Tracker refactor + fix bug in bulk authentication + Cleanup some of T…
Browse files Browse the repository at this point in the history
…ransitions code refs matomo-org#472
  • Loading branch information
mattab committed Oct 21, 2013
1 parent b12d4b9 commit 3760ca1
Show file tree
Hide file tree
Showing 23 changed files with 365 additions and 316 deletions.
6 changes: 3 additions & 3 deletions core/Filechecks.php
Original file line number Diff line number Diff line change
Expand Up @@ -186,11 +186,11 @@ public static function getErrorMessageMissingPermissions($path)
$message = "Please check that the web server has enough permission to write to these files/directories:<br />";

if (SettingsServer::isWindows()) {
$message .= "On Windows, check that the folder is not read only and is writable.
$message .= "On Windows, check that the folder is not read only and is writable.\n
You can try to execute:<br />";
} else {
$message .= "For example, on a Linux server if your Apache httpd user
is www-data, you can try to execute:<br />"
is www-data, you can try to execute:<br />\n"
. "<code>chown -R www-data:www-data " . $path . "</code><br />";
}

Expand All @@ -208,7 +208,7 @@ public static function getErrorMessageMissingPermissions($path)
private static function getMakeWritableCommand($realpath)
{
if (SettingsServer::isWindows()) {
return "<code>cacls $realpath /t /g " . get_current_user() . ":f</code><br />";
return "<code>cacls $realpath /t /g " . get_current_user() . ":f</code><br />\n";
}
return "<code>chmod -R 0755 $realpath</code><br />";
}
Expand Down
5 changes: 4 additions & 1 deletion core/Log.php
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,10 @@ private function logToFile($level, $tag, $datetime, $message)
return;
}

file_put_contents($this->logToFilePath, $message . "\n", FILE_APPEND);
if(!file_put_contents($this->logToFilePath, $message . "\n", FILE_APPEND)) {
$message = Filechecks::getErrorMessageMissingPermissions($this->logToFilePath);
throw new \Exception( $message );
}
}

private function logToScreen($level, $tag, $datetime, $message)
Expand Down
6 changes: 4 additions & 2 deletions core/Tracker.php
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ private function authenticateBulkTrackingRequests($rawData)
if (isset($jsonData['requests'])) {
$this->requests = $jsonData['requests'];
}
$tokenAuth = Common::getRequestVar('token_auth', false, null, $jsonData);
$tokenAuth = Common::getRequestVar('token_auth', false, 'string', $jsonData);
if (empty($tokenAuth)) {
throw new Exception("token_auth must be specified when using Bulk Tracking Import. See <a href='http://piwik.org/docs/tracking-api/reference/'>Tracking Doc</a>");
}
Expand Down Expand Up @@ -244,6 +244,9 @@ public function main($args = null)
$this->initOutputBuffer();

if (!empty($this->requests)) {
// Request needs the Db to authenticate (if cache files not available)
self::connectDatabaseIfNotConnected();

foreach ($this->requests as $params) {
$request = new Request($params, $tokenAuth);
$isAuthenticated = $request->isAuthenticated();
Expand All @@ -252,7 +255,6 @@ public function main($args = null)
try {
if ($this->isVisitValid()) {

self::connectDatabaseIfNotConnected();

$visit = $this->getNewVisitObject();
$request->setForcedVisitorId(self::$forcedVisitorId);
Expand Down
124 changes: 57 additions & 67 deletions core/Tracker/Action.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@
use Piwik\Tracker;

/**
* Handles an action (page view, download or outlink) by the visitor.
* Parses the action name and URL from the request array, then records the action in the log table.
*
*
* @package Piwik
* @subpackage Tracker
Expand All @@ -27,7 +26,12 @@ class Action implements ActionInterface
{
const DB_COLUMN_CUSTOM_FLOAT = 'custom_float';

static public function make(Request $request)
/**
*
* @param Request $request
* @return ActionClickUrl|ActionPageview|ActionSiteSearch
*/
static public function factory(Request $request)
{
$downloadUrl = $request->getParam('download');
if (!empty($downloadUrl)) {
Expand All @@ -54,8 +58,8 @@ static public function make(Request $request)
protected $request;

private $idLinkVisitAction;
private $idActionName = false;
private $idActionUrl = false;

protected $actionIdsCached = array();

private $actionName;
private $actionType;
Expand All @@ -67,7 +71,6 @@ public function __construct($type, Request $request)
$this->request = $request;
}


/**
* Returns URL of the page currently being tracked, or the file being downloaded, or the outlink being clicked
*
Expand All @@ -88,25 +91,23 @@ public function getActionType()
return $this->actionType;
}

protected function getActionNameType()
{
return ActionInterface::TYPE_PAGE_TITLE;
}

public function getIdActionUrl()
{
$idUrl = $this->idActionUrl;
$idUrl = $this->actionIdsCached['idaction_url'];
// note; idaction_url = 0 is displayed as "Page URL Not Defined"
return (int)$idUrl;
}

public function getIdActionName()
{
return $this->idActionName;
if(!isset($this->actionIdsCached['idaction_name'])) {
return false;
}
return $this->actionIdsCached['idaction_name'];
}

// custom_float column
public function getActionCustomValue()
public function getCustomFloatValue()
{
return false;
}
Expand Down Expand Up @@ -154,6 +155,36 @@ protected function setActionUrl($url)
$this->actionUrl = $url;
}

public function getCustomVariables()
{
$customVariables = $this->request->getCustomVariables($scope = 'page');
return $customVariables;
}

protected function getActionsToLookup()
{
return array(
'idaction_name' => $this->getNameAndType(),
'idaction_url' => $this->getUrlAndType()
);
}

protected function getNameAndType()
{
return array($this->getActionName(), ActionInterface::TYPE_PAGE_TITLE, $prefix = false);
}

protected function getUrlAndType()
{
$url = $this->getActionUrl();
if (!empty($url)) {
// normalize urls by stripping protocol and www
$url = PageUrl::normalizeUrl($url);
return array($url['url'], Tracker\Action::TYPE_PAGE_URL, $url['prefixId']);
}
return false;
}

public static function getTypeAsString($type)
{
$class = new \ReflectionClass("\\Piwik\\Tracker\\ActionInterface");
Expand All @@ -175,37 +206,20 @@ public static function getTypeAsString($type)
* The methods takes care of creating a new record(s) in the action table if the existing
* action name and action url doesn't exist yet.
*/
function loadIdActionNameAndUrl()
public function loadIdsFromLogActionTable()
{
if ($this->idActionUrl !== false
&& $this->idActionName !== false
) {
return;
}
$actions = array();
$actions = $this->getActionsToLookup();
$actions = array_filter($actions, 'count');

$actionNameAndType = $this->getNameAndType();
if($actionNameAndType) {
$actions[] = $actionNameAndType;
if(empty($actions)
|| !empty($this->actionIdsCached)) {
return false;
}

$actionUrlAndType = $this->getUrlAndType();
if($actionUrlAndType) {
$actions[] = $actionUrlAndType;
$loadedActionIds = TableLogAction::loadIdsAction($actions);

}

$loadedActionIds = TableActionIds::loadActionId($actions);

foreach ($loadedActionIds as $loadedActionId) {
var_dump($loadedActionId);
list($name, $type, $prefixId, $actionId) = $loadedActionId;
if ($type == $this->getActionNameType()) {
$this->idActionName = $actionId;
} else {
$this->idActionUrl = $actionId;
}
}
$this->actionIdsCached = $loadedActionIds;
return $this->actionIdsCached;
}

/**
Expand All @@ -220,7 +234,7 @@ function loadIdActionNameAndUrl()
*/
public function record($idVisit, $visitorIdCookie, $idReferrerActionUrl, $idReferrerActionName, $timeSpentReferrerAction)
{
$this->loadIdActionNameAndUrl();
$this->loadIdsFromLogActionTable();

$idActionName = in_array($this->getActionType(), array(Tracker\Action::TYPE_PAGE_TITLE,
Tracker\Action::TYPE_PAGE_URL,
Expand All @@ -241,7 +255,7 @@ public function record($idVisit, $visitorIdCookie, $idReferrerActionUrl, $idRefe
'time_spent_ref_action' => $timeSpentReferrerAction
);

$customValue = $this->getActionCustomValue();
$customValue = $this->getCustomFloatValue();
if (!empty($customValue)) {
$insert[self::DB_COLUMN_CUSTOM_FLOAT] = $customValue;
}
Expand Down Expand Up @@ -280,6 +294,7 @@ public function record($idVisit, $visitorIdCookie, $idReferrerActionUrl, $idRefe
'idReferrerActionName' => $idReferrerActionName,
'timeSpentReferrerAction' => $timeSpentReferrerAction,
);
Common::printDebug("Inserted new action:");
Common::printDebug($insertWithoutNulls);

/**
Expand All @@ -289,31 +304,6 @@ public function record($idVisit, $visitorIdCookie, $idReferrerActionUrl, $idRefe
Piwik::postEvent('Tracker.recordAction', array($trackerAction = $this, $info));
}

public function getCustomVariables()
{
$customVariables = $this->request->getCustomVariables($scope = 'page');
return $customVariables;
}

protected function getNameAndType()
{
$nameType = $this->getActionNameType();
if (!is_null($nameType)) {
return array($this->getActionName(), $nameType, $prefix = false);
}
return false;
}

protected function getUrlAndType()
{
$url = $this->getActionUrl();
if (!empty($url)) {
// normalize urls by stripping protocol and www
$url = PageUrl::normalizeUrl($url);
return array($url['url'], Tracker\Action::TYPE_PAGE_URL, $url['prefixId']);
}
return false;
}
}


Expand Down
10 changes: 8 additions & 2 deletions core/Tracker/ActionClickUrl.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,15 @@ function __construct($type, $url, Request $request)
$this->setActionUrl($url);
}

function getActionNameType()
protected function getActionsToLookup()
{
return null;
$actions = parent::getActionsToLookup();
// set the right type
$actions['idaction_url'][1] = $this->getActionType();

return array(
'idaction_url' => $actions['idaction_url']
);
}

function writeDebugInfo()
Expand Down
7 changes: 6 additions & 1 deletion core/Tracker/ActionPageview.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@

use Piwik\Config;

/**
* This class represents a page view, tracking URL, page title and generation time.
*
* @package Piwik\Tracker
*/
class ActionPageview extends Action
{
protected $timeGeneration = false;
Expand All @@ -32,7 +37,7 @@ function __construct($url, Request $request)
$this->timeGeneration = $this->request->getPageGenerationTime();
}

function getActionCustomValue()
function getCustomFloatValue()
{
return $this->request->getPageGenerationTime();
}
Expand Down
Loading

0 comments on commit 3760ca1

Please sign in to comment.