PDF rausgenommen
This commit is contained in:
84
msd2/tracking/piwik/plugins/BulkTracking/BulkTracking.php
Normal file
84
msd2/tracking/piwik/plugins/BulkTracking/BulkTracking.php
Normal file
@ -0,0 +1,84 @@
|
||||
<?php
|
||||
/**
|
||||
* Piwik - free/libre analytics platform
|
||||
*
|
||||
* @link http://piwik.org
|
||||
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
|
||||
*
|
||||
*/
|
||||
namespace Piwik\Plugins\BulkTracking;
|
||||
use Piwik\Plugins\BulkTracking\Tracker\Handler;
|
||||
use Piwik\Plugins\BulkTracking\Tracker\Requests;
|
||||
use Piwik\Tracker\RequestSet;
|
||||
|
||||
class BulkTracking extends \Piwik\Plugin
|
||||
{
|
||||
/**
|
||||
* @var Requests
|
||||
*/
|
||||
private $requests;
|
||||
|
||||
/**
|
||||
* @see Piwik\Plugin::registerEvents
|
||||
*/
|
||||
public function registerEvents()
|
||||
{
|
||||
return array(
|
||||
'Tracker.newHandler' => 'setHandlerIfBulkRequest',
|
||||
'Tracker.initRequestSet' => 'initRequestSet',
|
||||
);
|
||||
}
|
||||
|
||||
public function setRequests(Requests $requests)
|
||||
{
|
||||
$this->requests = $requests;
|
||||
}
|
||||
|
||||
public function initRequestSet(RequestSet $requestSet)
|
||||
{
|
||||
if ($this->isUsingBulkRequest()) {
|
||||
|
||||
$bulk = $this->buildBulkRequests();
|
||||
|
||||
list($requests, $token) = $bulk->initRequestsAndTokenAuth($bulk->getRawBulkRequest());
|
||||
|
||||
if ($bulk->requiresAuthentication()) {
|
||||
$bulk->authenticateRequests($requests);
|
||||
}
|
||||
|
||||
if (!$requestSet->getTokenAuth()) {
|
||||
$requestSet->setTokenAuth($token);
|
||||
}
|
||||
|
||||
$requestSet->setRequests($requests);
|
||||
}
|
||||
}
|
||||
|
||||
public function setHandlerIfBulkRequest(&$handler)
|
||||
{
|
||||
if (!is_null($handler)) {
|
||||
return;
|
||||
}
|
||||
|
||||
if ($this->isUsingBulkRequest()) {
|
||||
$handler = new Handler();
|
||||
}
|
||||
}
|
||||
|
||||
private function isUsingBulkRequest()
|
||||
{
|
||||
$requests = $this->buildBulkRequests();
|
||||
$rawData = $requests->getRawBulkRequest();
|
||||
|
||||
return $requests->isUsingBulkRequest($rawData);
|
||||
}
|
||||
|
||||
private function buildBulkRequests()
|
||||
{
|
||||
if (!is_null($this->requests)) {
|
||||
return $this->requests;
|
||||
}
|
||||
|
||||
return new Requests();
|
||||
}
|
||||
}
|
128
msd2/tracking/piwik/plugins/BulkTracking/Tracker/Handler.php
Normal file
128
msd2/tracking/piwik/plugins/BulkTracking/Tracker/Handler.php
Normal file
@ -0,0 +1,128 @@
|
||||
<?php
|
||||
/**
|
||||
* Piwik - free/libre analytics platform
|
||||
*
|
||||
* @link http://piwik.org
|
||||
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
|
||||
*
|
||||
*/
|
||||
|
||||
namespace Piwik\Plugins\BulkTracking\Tracker;
|
||||
|
||||
use Piwik\AuthResult;
|
||||
use Piwik\Container\StaticContainer;
|
||||
use Piwik\Exception\InvalidRequestParameterException;
|
||||
use Piwik\Exception\UnexpectedWebsiteFoundException;
|
||||
use Piwik\Piwik;
|
||||
use Piwik\Tracker;
|
||||
use Piwik\Tracker\RequestSet;
|
||||
use Piwik\Tracker\TrackerConfig;
|
||||
use Exception;
|
||||
|
||||
class Handler extends Tracker\Handler
|
||||
{
|
||||
private $transactionId = null;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->setResponse(new Response());
|
||||
}
|
||||
|
||||
public function onStartTrackRequests(Tracker $tracker, RequestSet $requestSet)
|
||||
{
|
||||
if ($this->isTransactionSupported()) {
|
||||
$this->beginTransaction();
|
||||
}
|
||||
}
|
||||
|
||||
public function onAllRequestsTracked(Tracker $tracker, RequestSet $requestSet)
|
||||
{
|
||||
$this->commitTransaction();
|
||||
|
||||
// Do not run schedule task if we are importing logs or doing custom tracking (as it could slow down)
|
||||
}
|
||||
|
||||
public function process(Tracker $tracker, RequestSet $requestSet)
|
||||
{
|
||||
$isAuthenticated = $this->isBulkTrackingRequestAuthenticated($requestSet);
|
||||
|
||||
/** @var Response $response */
|
||||
$response = $this->getResponse();
|
||||
$response->setIsAuthenticated($isAuthenticated);
|
||||
|
||||
$invalidRequests = array();
|
||||
foreach ($requestSet->getRequests() as $index => $request) {
|
||||
try {
|
||||
$tracker->trackRequest($request);
|
||||
} catch (UnexpectedWebsiteFoundException $ex) {
|
||||
$invalidRequests[] = $index;
|
||||
} catch (InvalidRequestParameterException $ex) {
|
||||
$invalidRequests[] = $index;
|
||||
}
|
||||
}
|
||||
|
||||
$response->setInvalidRequests($invalidRequests);
|
||||
}
|
||||
|
||||
public function onException(Tracker $tracker, RequestSet $requestSet, Exception $e)
|
||||
{
|
||||
$this->rollbackTransaction();
|
||||
parent::onException($tracker, $requestSet, $e);
|
||||
}
|
||||
|
||||
private function beginTransaction()
|
||||
{
|
||||
if (empty($this->transactionId)) {
|
||||
$this->transactionId = $this->getDb()->beginTransaction();
|
||||
}
|
||||
}
|
||||
|
||||
private function commitTransaction()
|
||||
{
|
||||
if (!empty($this->transactionId)) {
|
||||
$this->getDb()->commit($this->transactionId);
|
||||
$this->transactionId = null;
|
||||
}
|
||||
}
|
||||
|
||||
private function rollbackTransaction()
|
||||
{
|
||||
if (!empty($this->transactionId)) {
|
||||
$this->getDb()->rollback($this->transactionId);
|
||||
$this->transactionId = null;
|
||||
}
|
||||
}
|
||||
|
||||
private function getDb()
|
||||
{
|
||||
return Tracker::getDatabase();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
private function isTransactionSupported()
|
||||
{
|
||||
return (bool) TrackerConfig::getConfigValue('bulk_requests_use_transaction');
|
||||
}
|
||||
|
||||
private function isBulkTrackingRequestAuthenticated(RequestSet $requestSet)
|
||||
{
|
||||
$tokenAuth = $requestSet->getTokenAuth();
|
||||
if (empty($tokenAuth)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
Piwik::postEvent('Request.initAuthenticationObject');
|
||||
|
||||
/** @var \Piwik\Auth $auth */
|
||||
$auth = StaticContainer::get('Piwik\Auth');
|
||||
$auth->setTokenAuth($tokenAuth);
|
||||
$auth->setLogin(null);
|
||||
$auth->setPassword(null);
|
||||
$auth->setPasswordHash(null);
|
||||
$access = $auth->authenticate();
|
||||
|
||||
return $access->getCode() == AuthResult::SUCCESS_SUPERUSER_AUTH_CODE;
|
||||
}
|
||||
}
|
111
msd2/tracking/piwik/plugins/BulkTracking/Tracker/Requests.php
Normal file
111
msd2/tracking/piwik/plugins/BulkTracking/Tracker/Requests.php
Normal file
@ -0,0 +1,111 @@
|
||||
<?php
|
||||
/**
|
||||
* Piwik - free/libre analytics platform
|
||||
*
|
||||
* @link http://piwik.org
|
||||
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
|
||||
*
|
||||
*/
|
||||
namespace Piwik\Plugins\BulkTracking\Tracker;
|
||||
|
||||
use Exception;
|
||||
use Piwik\Common;
|
||||
use Piwik\Tracker\Request;
|
||||
use Piwik\Tracker\TrackerConfig;
|
||||
|
||||
class Requests
|
||||
{
|
||||
|
||||
public function requiresAuthentication()
|
||||
{
|
||||
$requiresAuth = TrackerConfig::getConfigValue('bulk_requests_require_authentication');
|
||||
|
||||
return !empty($requiresAuth);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Request[] $requests
|
||||
* @throws Exception
|
||||
*/
|
||||
public function authenticateRequests($requests)
|
||||
{
|
||||
foreach ($requests as $request) {
|
||||
$this->checkTokenAuthNotEmpty($request->getTokenAuth());
|
||||
|
||||
if (!$request->isAuthenticated()) {
|
||||
$msg = sprintf("token_auth specified does not have Admin permission for idsite=%s", $request->getIdSite());
|
||||
throw new Exception($msg);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private function checkTokenAuthNotEmpty($token)
|
||||
{
|
||||
if (empty($token)) {
|
||||
throw new Exception("token_auth must be specified when using Bulk Tracking Import. "
|
||||
. " See <a href='https://developer.piwik.org/api-reference/tracking-api'>Tracking Doc</a>");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getRawBulkRequest()
|
||||
{
|
||||
return file_get_contents("php://input");
|
||||
}
|
||||
|
||||
public function isUsingBulkRequest($rawData)
|
||||
{
|
||||
if (!empty($rawData)) {
|
||||
return strpos($rawData, '"requests"') || strpos($rawData, "'requests'");
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public function getRequestsArrayFromBulkRequest($rawData)
|
||||
{
|
||||
$rawData = trim($rawData);
|
||||
$rawData = Common::sanitizeLineBreaks($rawData);
|
||||
|
||||
// POST data can be array of string URLs or array of arrays w/ visit info
|
||||
$jsonData = json_decode($rawData, $assoc = true);
|
||||
|
||||
$tokenAuth = Common::getRequestVar('token_auth', false, 'string', $jsonData);
|
||||
|
||||
$requests = array();
|
||||
if (isset($jsonData['requests'])) {
|
||||
$requests = $jsonData['requests'];
|
||||
}
|
||||
|
||||
return array($requests, $tokenAuth);
|
||||
}
|
||||
|
||||
public function initRequestsAndTokenAuth($rawData)
|
||||
{
|
||||
list($requests, $tokenAuth) = $this->getRequestsArrayFromBulkRequest($rawData);
|
||||
|
||||
$validRequests = array();
|
||||
|
||||
if (!empty($requests)) {
|
||||
|
||||
foreach ($requests as $index => $request) {
|
||||
// if a string is sent, we assume its a URL and try to parse it
|
||||
if (is_string($request)) {
|
||||
$params = array();
|
||||
|
||||
$url = @parse_url($request);
|
||||
if (!empty($url['query'])) {
|
||||
@parse_str($url['query'], $params);
|
||||
$validRequests[] = new Request($params, $tokenAuth);
|
||||
}
|
||||
} else {
|
||||
$validRequests[] = new Request($request, $tokenAuth);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return array($validRequests, $tokenAuth);
|
||||
}
|
||||
}
|
111
msd2/tracking/piwik/plugins/BulkTracking/Tracker/Response.php
Normal file
111
msd2/tracking/piwik/plugins/BulkTracking/Tracker/Response.php
Normal file
@ -0,0 +1,111 @@
|
||||
<?php
|
||||
/**
|
||||
* Piwik - free/libre analytics platform
|
||||
*
|
||||
* @link http://piwik.org
|
||||
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
|
||||
*
|
||||
*/
|
||||
namespace Piwik\Plugins\BulkTracking\Tracker;
|
||||
|
||||
use Exception;
|
||||
use Piwik\Common;
|
||||
use Piwik\Tracker;
|
||||
|
||||
class Response extends Tracker\Response
|
||||
{
|
||||
/**
|
||||
* @var int[]
|
||||
*/
|
||||
private $invalidRequests = array();
|
||||
|
||||
/**
|
||||
* @var bool
|
||||
*/
|
||||
private $isAuthenticated = false;
|
||||
|
||||
/**
|
||||
* Echos an error message & other information, then exits.
|
||||
*
|
||||
* @param Tracker $tracker
|
||||
* @param Exception $e
|
||||
* @param int $statusCode eg 500
|
||||
*/
|
||||
public function outputException(Tracker $tracker, Exception $e, $statusCode)
|
||||
{
|
||||
Common::sendResponseCode($statusCode);
|
||||
|
||||
$this->logExceptionToErrorLog($e);
|
||||
|
||||
$result = $this->formatException($tracker, $e);
|
||||
|
||||
echo json_encode($result);
|
||||
}
|
||||
|
||||
public function outputResponse(Tracker $tracker)
|
||||
{
|
||||
if ($this->hasAlreadyPrintedOutput()) {
|
||||
return;
|
||||
}
|
||||
|
||||
$result = $this->formatResponse($tracker);
|
||||
|
||||
echo json_encode($result);
|
||||
}
|
||||
|
||||
public function getOutput()
|
||||
{
|
||||
Common::sendHeader('Content-Type: application/json');
|
||||
|
||||
return parent::getOutput();
|
||||
}
|
||||
|
||||
private function formatException(Tracker $tracker, Exception $e)
|
||||
{
|
||||
// when doing bulk tracking we return JSON so the caller will know how many succeeded
|
||||
$result = array(
|
||||
'status' => 'error',
|
||||
'tracked' => $tracker->getCountOfLoggedRequests(),
|
||||
'invalid' => count($this->invalidRequests),
|
||||
);
|
||||
|
||||
$this->addInvalidIndicesIfAuthenticated($result);
|
||||
|
||||
// send error when in debug mode
|
||||
if ($tracker->isDebugModeEnabled()) {
|
||||
$result['message'] = $this->getMessageFromException($e);
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
private function formatResponse(Tracker $tracker)
|
||||
{
|
||||
$result = array(
|
||||
'status' => 'success',
|
||||
'tracked' => $tracker->getCountOfLoggedRequests(),
|
||||
'invalid' => count($this->invalidRequests),
|
||||
);
|
||||
|
||||
$this->addInvalidIndicesIfAuthenticated($result);
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
public function setInvalidRequests($invalidRequests)
|
||||
{
|
||||
$this->invalidRequests = $invalidRequests;
|
||||
}
|
||||
|
||||
public function setIsAuthenticated($isAuthenticated)
|
||||
{
|
||||
$this->isAuthenticated = $isAuthenticated;
|
||||
}
|
||||
|
||||
private function addInvalidIndicesIfAuthenticated(&$result)
|
||||
{
|
||||
if ($this->isAuthenticated) {
|
||||
$result['invalid_indices'] = $this->invalidRequests;
|
||||
}
|
||||
}
|
||||
}
|
4
msd2/tracking/piwik/plugins/BulkTracking/plugin.json
Normal file
4
msd2/tracking/piwik/plugins/BulkTracking/plugin.json
Normal file
@ -0,0 +1,4 @@
|
||||
{
|
||||
"name": "BulkTracking",
|
||||
"description": "Provides ability to send several Tracking API requests in one bulk request. Makes importing a lot of data in Matomo faster."
|
||||
}
|
Reference in New Issue
Block a user