protected function initRequestSet($rawData, $requiresAuth = false, $initToken = null)
 {
     $requestSet = new RequestSet();
     if (!is_null($initToken)) {
         $requestSet->setTokenAuth($initToken);
     }
     $this->injectRawDataToBulk($rawData, $requiresAuth);
     $this->bulk->initRequestSet($requestSet);
     return $requestSet;
 }
Exemplo n.º 2
0
 public function onException(RequestSet $requestSet, Exception $e)
 {
     // todo: how do we want to handle DbException or RedisException?
     $this->hasError = true;
     if ($this->count > 0) {
         // remove the first one that failed and all following (standard bulk tracking behavior)
         $insertedRequests = array_slice($requestSet->getRequests(), 0, $this->count);
         $requestSet->setRequests($insertedRequests);
         $this->requestSetsToRetry[] = $requestSet;
     }
 }
Exemplo n.º 3
0
 /**
  * @return RequestSet[]
  */
 public function getRequestSetsToProcess()
 {
     $values = $this->backend->getFirstXValuesFromList($this->key, $this->numRequestsToProcessInBulk);
     $requests = array();
     foreach ($values as $value) {
         $params = json_decode($value, true);
         $request = new RequestSet();
         $request->restoreState($params);
         $requests[] = $request;
     }
     return $requests;
 }
Exemplo n.º 4
0
 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);
     }
 }
Exemplo n.º 5
0
 public function process(Tracker $tracker, RequestSet $requestSet)
 {
     $invalidRequests = 0;
     foreach ($requestSet->getRequests() as $request) {
         try {
             $tracker->trackRequest($request);
         } catch (UnexpectedWebsiteFoundException $ex) {
             $invalidRequests += 1;
         }
     }
     /** @var Response $response */
     $response = $this->getResponse();
     $response->setInvalidCount($invalidRequests);
 }
Exemplo n.º 6
0
 public function initRequestsAndTokenAuth()
 {
     if ($this->throwExceptionOnInit) {
         throw new Exception('Init requests and token auth exception', 493);
     }
     parent::initRequestsAndTokenAuth();
 }
 protected function assertRequestsAreEqual(PiwikRequestSet $expected, PiwikRequestSet $actual)
 {
     $eState = $expected->getState();
     $aState = $actual->getState();
     $eTime = $eState['time'];
     $aTime = $aState['time'];
     unset($eState['time']);
     unset($aState['time']);
     if (array_key_exists('REQUEST_TIME_FLOAT', $eState['env']['server'])) {
         unset($eState['env']['server']['REQUEST_TIME_FLOAT']);
     }
     if (array_key_exists('REQUEST_TIME_FLOAT', $aState['env']['server'])) {
         unset($aState['env']['server']['REQUEST_TIME_FLOAT']);
     }
     $this->assertGreaterThan(100000, $aTime);
     $this->assertTrue($aTime - 5 < $eTime && $aTime + 5 > $eTime, "{$eTime} is not nearly {$aTime}");
     $this->assertEquals($eState, $aState);
 }
 public function test_numQueueWorkers_WhenChangingAValueItMovesRequestsIntoDifferentQueues()
 {
     $oldNumWorkers = 4;
     $newNumWorkers = 2;
     $this->settings->numQueueWorkers->setValue($oldNumWorkers);
     $manager = Factory::makeQueueManager(Factory::makeBackend());
     $requestSet = new RequestSet();
     $requestSet->setRequests(array('idsite' => '1', '_id' => 1));
     $queues = $manager->getAllQueues();
     foreach ($queues as $queue) {
         $queue->addRequestSet($requestSet);
     }
     $this->assertSame(4, $manager->getNumberOfRequestSetsInAllQueues());
     $this->assertSame(1, $queues[0]->getNumberOfRequestSetsInQueue());
     $this->assertSame(1, $queues[1]->getNumberOfRequestSetsInQueue());
     $this->assertSame(1, $queues[2]->getNumberOfRequestSetsInQueue());
     $this->assertSame(1, $queues[3]->getNumberOfRequestSetsInQueue());
     $this->settings->numQueueWorkers->setValue($newNumWorkers);
     $this->assertSame(4, $manager->getNumberOfRequestSetsInAllQueues());
     $this->assertGreaterThanOrEqual(1, $queues[0]->getNumberOfRequestSetsInQueue());
     $this->assertGreaterThanOrEqual(1, $queues[1]->getNumberOfRequestSetsInQueue());
     $this->assertSame(0, $queues[2]->getNumberOfRequestSetsInQueue());
     $this->assertSame(0, $queues[3]->getNumberOfRequestSetsInQueue());
 }
Exemplo n.º 9
0
 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;
 }
Exemplo n.º 10
0
 public function getEnvironment()
 {
     return parent::getEnvironment();
 }
Exemplo n.º 11
0
 protected function sendRequest($url, $method = 'GET', $data = null, $force = false)
 {
     if ($this->DEBUG_APPEND_URL) {
         $url .= $this->DEBUG_APPEND_URL;
     }
     // if doing a bulk request, store the url
     if ($this->doBulkRequests && !$force) {
         $this->storedTrackingActions[] = $url;
         return true;
     }
     if ($method == 'POST') {
         $requests = array();
         foreach ($this->storedTrackingActions as $action) {
             $requests[] = $this->parseUrl($action);
         }
         $testEnvironmentArgs = array();
     } else {
         $testEnvironmentArgs = $this->parseUrl($url);
         $requests = array($testEnvironmentArgs);
     }
     // unset cached values
     Cache::$cache = null;
     Tracker\Visit::$dimensions = null;
     // save some values
     $plugins = Config::getInstance()->Plugins['Plugins'];
     $oldTrackerConfig = Config::getInstance()->Tracker;
     \Piwik\Plugin\Manager::getInstance()->unloadPlugins();
     // modify config
     \Piwik\SettingsServer::setIsTrackerApiRequest();
     $GLOBALS['PIWIK_TRACKER_LOCAL_TRACKING'] = true;
     Tracker::$initTrackerMode = false;
     Tracker::setTestEnvironment($testEnvironmentArgs, $method);
     // set language
     $oldLang = isset($_SERVER['HTTP_ACCEPT_LANGUAGE']) ? $_SERVER['HTTP_ACCEPT_LANGUAGE'] : '';
     $_SERVER['HTTP_ACCEPT_LANGUAGE'] = $this->acceptLanguage;
     // set user agent
     $oldUserAgent = isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : '';
     $_SERVER['HTTP_USER_AGENT'] = $this->userAgent;
     // set cookie
     $oldCookie = $_COOKIE;
     //        parse_str(parse_url($this->requestCookie, PHP_URL_QUERY), $_COOKIE);
     // do tracking and capture output
     ob_start();
     $localTracker = new Tracker();
     $request = new Tracker\RequestSet();
     $request->setRequests($requests);
     \Piwik\Plugin\Manager::getInstance()->loadTrackerPlugins();
     $handler = Tracker\Handler\Factory::make();
     $response = $localTracker->main($handler, $request);
     if (!is_null($response)) {
         echo $response;
     }
     $output = ob_get_contents();
     ob_end_clean();
     // restore vars
     Config::getInstance()->Tracker = $oldTrackerConfig;
     $_SERVER['HTTP_ACCEPT_LANGUAGE'] = $oldLang;
     $_SERVER['HTTP_USER_AGENT'] = $oldUserAgent;
     $_COOKIE = $oldCookie;
     $GLOBALS['PIWIK_TRACKER_LOCAL_TRACKING'] = false;
     \Piwik\SettingsServer::setIsNotTrackerApiRequest();
     unset($_GET['bots']);
     // reload plugins
     \Piwik\Plugin\Manager::getInstance()->loadPlugins($plugins);
     return $output;
 }
 private function buildRequestSetWithIdSite($numRequests, $additionalParams = array())
 {
     $req = new RequestSet();
     $requests = array();
     for ($index = 1; $index <= $numRequests; $index++) {
         $requests[] = array_merge(array('idsite' => 1, 'cip' => '192.168.33.11', 'token_auth' => Fixture::getTokenAuth()), $additionalParams);
     }
     $req->setRequests($requests);
     $req->rememberEnvironment();
     return $req;
 }
Exemplo n.º 13
0
 public function addRequestSetToQueues(RequestSet $requestSet)
 {
     /** @var RequestSet[][] $queues */
     $queues = array();
     // make sure the requests within a bulk request go into the correct queue
     foreach ($requestSet->getRequests() as $request) {
         $visitorId = $this->getVisitorIdFromRequest($request);
         $queueId = $this->getQueueIdForVisitor($visitorId);
         if (!isset($queues[$queueId])) {
             $queues[$queueId] = array();
         }
         $queues[$queueId][] = $request;
     }
     foreach ($queues as $queueId => $requests) {
         $requestSet->setRequests($requests);
         $queue = $this->createQueue($queueId);
         $queue->addRequestSet($requestSet);
     }
 }
Exemplo n.º 14
0
 private function buildRequestSetWithIdSite($numRequests, $idSite = null)
 {
     $req = new RequestSet();
     $requests = array();
     for ($index = 1; $index <= $numRequests; $index++) {
         $requests[] = array('idsite' => $idSite ?: $index);
     }
     $req->setRequests($requests);
     $req->rememberEnvironment();
     return $req;
 }
Exemplo n.º 15
0
 /**
  * @param RequestSet $requestSet
  * @return bool true if we still have the lock and if expire was set successfully
  */
 private function extendLockExpireToMakeSureWeCanProcessARequestSet(RequestSet $requestSet)
 {
     // 2 seconds per tracking request should give it enough time to process it
     $ttl = $requestSet->getNumberOfRequests() * 2;
     $ttl = max($ttl, 20);
     // lock for at least 20 seconds
     return $this->queueManager->expireLock($ttl);
 }
Exemplo n.º 16
0
 public function track(Handler $handler, RequestSet $requestSet)
 {
     if (!$this->shouldRecordStatistics()) {
         return;
     }
     $requestSet->initRequestsAndTokenAuth();
     if ($requestSet->hasRequests()) {
         $handler->onStartTrackRequests($this, $requestSet);
         $handler->process($this, $requestSet);
         $handler->onAllRequestsTracked($this, $requestSet);
     }
 }
Exemplo n.º 17
0
 public function test_registerEvents_shouldListenToInitRequestSetEventAndInit_IfBulkRequest()
 {
     $this->injectRawDataToBulk($this->getDummyRequest());
     $requestSet = new RequestSet();
     $requestSet->initRequestsAndTokenAuth();
     $this->assertCount(2, $requestSet->getRequests());
 }
Exemplo n.º 18
0
 protected function redirectIfNeeded(RequestSet $requestSet)
 {
     $redirectUrl = $requestSet->shouldPerformRedirectToUrl();
     if (!empty($redirectUrl)) {
         Url::redirectToUrl($redirectUrl);
     }
 }