update Piwik to version 2.16 (fixes #91)

This commit is contained in:
oliver 2016-04-10 18:55:57 +02:00
commit d885a4baa9
5833 changed files with 418860 additions and 226988 deletions

View file

@ -1,6 +1,6 @@
<?php
/**
* Piwik - Open source web analytics
* Piwik - free/libre analytics platform
*
* @link http://piwik.org
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
@ -10,9 +10,9 @@
namespace Piwik\Tracker;
use Piwik\Common;
use Piwik\SegmentExpression;
use Piwik\Tracker;
use Piwik\Config;
use Piwik\Container\StaticContainer;
use Piwik\Segment\SegmentExpression;
/**
* This class is used to query Action IDs from the log_action table.
@ -36,35 +36,22 @@ class TableLogAction
public static function loadIdsAction($actionsNameAndType)
{
// Add url prefix if not set
foreach($actionsNameAndType as &$action) {
if(count($action) == 2) {
foreach ($actionsNameAndType as &$action) {
if (2 == count($action)) {
$action[] = null;
}
}
$actionIds = self::queryIdsAction($actionsNameAndType);
list($queriedIds, $fieldNamesToInsert) = self::processIdsToInsert($actionsNameAndType, $actionIds);
$insertedIds = self::insertNewIdsAction($actionsNameAndType, $fieldNamesToInsert);
$queriedIds = $queriedIds + $insertedIds;
$queriedIds = $queriedIds + $insertedIds;
return $queriedIds;
}
/**
* @param $name
* @param $type
* @return string
*/
private static function getIdActionMatchingNameAndType($name, $type)
{
$sql = TableLogAction::getSqlSelectActionId();
$bind = array($name, $name, $type);
$idAction = \Piwik\Db::fetchOne($sql, $bind);
return $idAction;
}
/**
* @param $matchType
* @param $actionType
@ -76,73 +63,66 @@ class TableLogAction
// now, we handle the cases =@ (contains) and !@ (does not contain)
// build the expression based on the match type
$sql = 'SELECT idaction FROM ' . Common::prefixTable('log_action') . ' WHERE %s AND type = ' . $actionType . ' )';
switch ($matchType) {
case '=@':
case SegmentExpression::MATCH_CONTAINS:
// use concat to make sure, no %s occurs because some plugins use %s in their sql
$where = '( name LIKE CONCAT(\'%\', ?, \'%\') ';
break;
case '!@':
case SegmentExpression::MATCH_DOES_NOT_CONTAIN:
$where = '( name NOT LIKE CONCAT(\'%\', ?, \'%\') ';
break;
case SegmentExpression::MATCH_STARTS_WITH:
// use concat to make sure, no %s occurs because some plugins use %s in their sql
$where = '( name LIKE CONCAT(?, \'%\') ';
break;
case SegmentExpression::MATCH_ENDS_WITH:
// use concat to make sure, no %s occurs because some plugins use %s in their sql
$where = '( name LIKE CONCAT(\'%\', ?) ';
break;
default:
throw new \Exception("This match type $matchType is not available for action-segments.");
break;
}
$sql = sprintf($sql, $where);
return $sql;
}
private static function getSqlSelectActionId()
{
$sql = "SELECT idaction, type, name
FROM " . Common::prefixTable('log_action')
. " WHERE "
. " ( hash = CRC32(?) AND name = ? AND type = ? ) ";
$sql = sprintf($sql, $where);
return $sql;
}
private static function insertNewIdsAction($actionsNameAndType, $fieldNamesToInsert)
{
$sql = "INSERT INTO " . Common::prefixTable('log_action') .
"( name, hash, type, url_prefix ) VALUES (?,CRC32(?),?,?)";
// Then, we insert all new actions in the lookup table
$inserted = array();
foreach ($fieldNamesToInsert as $fieldName) {
list($name, $type, $urlPrefix) = $actionsNameAndType[$fieldName];
Tracker::getDatabase()->query($sql, array($name, $name, $type, $urlPrefix));
$actionId = Tracker::getDatabase()->lastInsertId();
$inserted[$fieldName] = $actionId;
$actionId = self::getModel()->createNewIdAction($name, $type, $urlPrefix);
Common::printDebug("Recorded a new action (" . Action::getTypeAsString($type) . ") in the lookup table: " . $name . " (idaction = " . $actionId . ")");
$inserted[$fieldName] = $actionId;
}
return $inserted;
}
private static function getModel()
{
return new Model();
}
private static function queryIdsAction($actionsNameAndType)
{
$sql = TableLogAction::getSqlSelectActionId();
$bind = array();
$i = 0;
$toQuery = array();
foreach ($actionsNameAndType as &$actionNameType) {
list($name, $type, $urlPrefix) = $actionNameType;
if (empty($name)) {
continue;
}
if ($i > 0) {
$sql .= " OR ( hash = CRC32(?) AND name = ? AND type = ? ) ";
}
$bind[] = $name;
$bind[] = $name;
$bind[] = $type;
$i++;
$toQuery[] = array('name' => $name, 'type' => $type);
}
// Case URL & Title are empty
if (empty($bind)) {
return false;
}
$actionIds = Tracker::getDatabase()->fetchAll($sql, $bind);
$actionIds = self::getModel()->getIdsAction($toQuery);
return $actionIds;
}
@ -151,6 +131,7 @@ class TableLogAction
// For the Actions found in the lookup table, add the idaction in the array,
// If not found in lookup table, queue for INSERT
$fieldNamesToInsert = $fieldNameToActionId = array();
foreach ($actionsNameAndType as $fieldName => &$actionNameType) {
@list($name, $type, $urlPrefix) = $actionNameType;
if (empty($name)) {
@ -173,10 +154,10 @@ class TableLogAction
$fieldNamesToInsert[] = $fieldName;
}
}
return array($fieldNameToActionId, $fieldNamesToInsert);
}
/**
* Convert segment expression to an action ID or an SQL expression.
*
@ -193,34 +174,37 @@ class TableLogAction
*/
public static function getIdActionFromSegment($valueToMatch, $sqlField, $matchType, $segmentName)
{
$actionType = self::guessActionTypeFromSegment($segmentName);
if ($actionType == Action::TYPE_PAGE_URL) {
// for urls trim protocol and www because it is not recorded in the db
$valueToMatch = preg_replace('@^http[s]?://(www\.)?@i', '', $valueToMatch);
}
$valueToMatch = Common::sanitizeInputValue(Common::unsanitizeInputValue($valueToMatch));
if ($matchType == SegmentExpression::MATCH_EQUAL
|| $matchType == SegmentExpression::MATCH_NOT_EQUAL
) {
$idAction = self::getIdActionMatchingNameAndType($valueToMatch, $actionType);
// if the action is not found, we hack -100 to ensure it tries to match against an integer
// otherwise binding idaction_name to "false" returns some rows for some reasons (in case &segment=pageTitle==Větrnásssssss)
if (empty($idAction)) {
$idAction = -100;
if ($segmentName === 'actionType') {
$actionType = (int) $valueToMatch;
$valueToMatch = array();
$sql = 'SELECT idaction FROM ' . Common::prefixTable('log_action') . ' WHERE type = ' . $actionType . ' )';
} else {
$actionType = self::guessActionTypeFromSegment($segmentName);
if ($actionType == Action::TYPE_PAGE_URL) {
// for urls trim protocol and www because it is not recorded in the db
$valueToMatch = preg_replace('@^http[s]?://(www\.)?@i', '', $valueToMatch);
}
return $idAction;
$valueToMatch = self::normaliseActionString($actionType, $valueToMatch);
if ($matchType == SegmentExpression::MATCH_EQUAL
|| $matchType == SegmentExpression::MATCH_NOT_EQUAL
) {
$idAction = self::getModel()->getIdActionMatchingNameAndType($valueToMatch, $actionType);
// Action is not found (eg. &segment=pageTitle==Větrnásssssss)
if (empty($idAction)) {
$idAction = null;
}
return $idAction;
}
// "name contains $string" match can match several idaction so we cannot return yet an idaction
// special case
$sql = self::getSelectQueryWhereNameContains($matchType, $actionType);
}
// "name contains $string" match can match several idaction so we cannot return yet an idaction
// special case
$sql = TableLogAction::getSelectQueryWhereNameContains($matchType, $actionType);
return array(
// mark that the returned value is an sql-expression instead of a literal value
'SQL' => $sql,
'bind' => $valueToMatch,
);
$cache = StaticContainer::get('Piwik\Tracker\TableLogAction\Cache');
return $cache->getIdActionFromSegment($valueToMatch, $sql);
}
/**
@ -231,11 +215,18 @@ class TableLogAction
private static function guessActionTypeFromSegment($segmentName)
{
$exactMatch = array(
'eventAction' => Action::TYPE_EVENT_ACTION,
'eventCategory' => Action::TYPE_EVENT_CATEGORY,
'eventName' => Action::TYPE_EVENT_NAME,
'outlinkUrl' => Action::TYPE_OUTLINK,
'downloadUrl' => Action::TYPE_DOWNLOAD,
'eventAction' => Action::TYPE_EVENT_ACTION,
'eventCategory' => Action::TYPE_EVENT_CATEGORY,
'eventName' => Action::TYPE_EVENT_NAME,
'contentPiece' => Action::TYPE_CONTENT_PIECE,
'contentTarget' => Action::TYPE_CONTENT_TARGET,
'contentName' => Action::TYPE_CONTENT_NAME,
'contentInteraction' => Action::TYPE_CONTENT_INTERACTION,
);
if(!empty($exactMatch[$segmentName])) {
if (!empty($exactMatch[$segmentName])) {
return $exactMatch[$segmentName];
}
@ -253,5 +244,40 @@ class TableLogAction
}
}
}
/**
* This function will sanitize or not if it's needed for the specified action type
*
* URLs (Download URL, Outlink URL) are stored raw (unsanitized)
* while other action types are stored Sanitized
*
* @param $actionType
* @param $actionString
* @return string
*/
private static function normaliseActionString($actionType, $actionString)
{
$actionString = Common::unsanitizeInputValue($actionString);
if (self::isActionTypeStoredUnsanitized($actionType)) {
return $actionString;
}
return Common::sanitizeInputValue($actionString);
}
/**
* @param $actionType
* @return bool
*/
private static function isActionTypeStoredUnsanitized($actionType)
{
$actionsTypesStoredUnsanitized = array(
$actionType == Action::TYPE_DOWNLOAD,
$actionType == Action::TYPE_OUTLINK,
$actionType == Action::TYPE_PAGE_URL,
$actionType == Action::TYPE_CONTENT,
);
return in_array($actionType, $actionsTypesStoredUnsanitized);
}
}