### Performance concerns The helper methods in this class are instance methods and thus Date instances need to be constructed before they can be used. The memory allocation can result in noticeable performance degradation if you construct thousands of Date instances, say, in a loop. ### Examples **Basic usage** $date = Date::factory('2007-07-24 14:04:24', 'EST'); $date->addHour(5); echo $date->getLocalized("EEE, d. MMM y 'at' HH:mm:ss");
Esempio n. 1
0
 /**
  * @param string $format
  * @param \Piwik\Date $dateStart
  * @param \Piwik\Date $dateEnd
  *
  * @return mixed
  */
 protected static function getTranslatedRange($format, $dateStart, $dateEnd)
 {
     $string = str_replace('From%', '%', $format);
     $string = $dateStart->getLocalized($string);
     $string = str_replace('To%', '%', $string);
     $string = $dateEnd->getLocalized($string);
     return $string;
 }
Esempio n. 2
0
 protected static function getTable(Date $date, $type)
 {
     $tableNamePrefix = "archive_" . $type;
     $tableName = $tableNamePrefix . "_" . $date->toString('Y_m');
     $tableName = Common::prefixTable($tableName);
     self::createArchiveTablesIfAbsent($tableName, $tableNamePrefix);
     return $tableName;
 }
 public function test_deleteAlertsForWebsite()
 {
     $this->createAlert('Initial1', array(), array(2));
     $this->createAlert('Initial2', array(), array(1, 2, 3));
     $this->createAlert('Initial3', array(), array(1));
     $this->createAlert('Initial4', array(), array(1, 3));
     $this->createAlert('Initial5', array(), array(2));
     $this->createAlert('Initial6', array(), array(2));
     $this->model->triggerAlert(1, 1, 99, 48, Date::now()->getDatetime());
     $this->model->triggerAlert(1, 2, 99, 48, Date::now()->getDatetime());
     $this->model->triggerAlert(2, 3, 99, 48, Date::now()->getDatetime());
     $this->model->triggerAlert(3, 2, 99, 48, Date::now()->getDatetime());
     $alerts = $this->model->getTriggeredAlerts(array(1, 2, 3), 'superUserLogin');
     $this->assertCount(4, $alerts);
     $this->plugin->deleteAlertsForSite(2);
     $alerts = $this->model->getAllAlerts();
     $this->assertCount(6, $alerts);
     $this->assertEquals($alerts[0]['id_sites'], array());
     $this->assertEquals($alerts[1]['id_sites'], array(1, 3));
     $this->assertEquals($alerts[2]['id_sites'], array(1));
     $this->assertEquals($alerts[3]['id_sites'], array(1, 3));
     $this->assertEquals($alerts[4]['id_sites'], array());
     $this->assertEquals($alerts[5]['id_sites'], array());
     $alerts = $this->model->getTriggeredAlerts(array(1, 2, 3), 'superUserLogin');
     $this->assertCount(2, $alerts);
 }
Esempio n. 4
0
 /**
  * Controller action that returns HTML displaying annotations for a site and
  * specific date range.
  *
  * Query Param Input:
  *  - idSite: The ID of the site to get annotations for. Only one allowed.
  *  - date: The date to get annotations for. If lastN is not supplied, this is the start date,
  *          otherwise the start date in the last period.
  *  - period: The period type.
  *  - lastN: If supplied, the last N # of periods will be included w/ the range specified
  *           by date + period.
  *
  * Output:
  *  - HTML displaying annotations for a specific range.
  *
  * @param bool $fetch True if the annotation manager should be returned as a string,
  *                    false if it should be echo-ed.
  * @param bool|string $date Override for 'date' query parameter.
  * @param bool|string $period Override for 'period' query parameter.
  * @param bool|string $lastN Override for 'lastN' query parameter.
  * @return string|void
  */
 public function getAnnotationManager($fetch = false, $date = false, $period = false, $lastN = false)
 {
     $idSite = Common::getRequestVar('idSite');
     if ($date === false) {
         $date = Common::getRequestVar('date', false);
     }
     if ($period === false) {
         $period = Common::getRequestVar('period', 'day');
     }
     if ($lastN === false) {
         $lastN = Common::getRequestVar('lastN', false);
     }
     // create & render the view
     $view = new View('@Annotations/getAnnotationManager');
     $allAnnotations = Request::processRequest('Annotations.getAll', array('date' => $date, 'period' => $period, 'lastN' => $lastN));
     $view->annotations = empty($allAnnotations[$idSite]) ? array() : $allAnnotations[$idSite];
     $view->period = $period;
     $view->lastN = $lastN;
     list($startDate, $endDate) = API::getDateRangeForPeriod($date, $period, $lastN);
     $view->startDate = $startDate->toString();
     $view->endDate = $endDate->toString();
     if ($startDate->toString() !== $endDate->toString()) {
         $view->selectedDate = Date::today()->toString();
     } else {
         $view->selectedDate = $endDate->toString();
     }
     $dateFormat = Piwik::translate('CoreHome_ShortDateFormatWithYear');
     $view->startDatePretty = $startDate->getLocalized($dateFormat);
     $view->endDatePretty = $endDate->getLocalized($dateFormat);
     $view->canUserAddNotes = AnnotationList::canUserAddNotesFor($idSite);
     return $view->render();
 }
 public function setUp()
 {
     // add one site
     Fixture::createWebsite($this->date, $ecommerce = 1, $siteName = "Site #0", $siteUrl = "http://whatever.com/");
     // add two goals
     $goals = API::getInstance();
     $goals->addGoal($this->idSite, 'all', 'url', 'http', 'contains', false, 5);
     $goals->addGoal($this->idSite, 'all', 'url', 'thing2', 'contains');
     $start = Date::factory($this->date);
     $dates = array();
     for ($day = 0; $day != 31; ++$day) {
         $dates[] = $start->addDay($day);
     }
     $t = BenchmarkTestCase::getLocalTracker($this->idSite);
     $actionNum = 0;
     foreach ($dates as $date) {
         for ($visitNum = 0; $visitNum != 1000; ++$visitNum) {
             if ($visitNum % 2 == 0) {
                 $url = "http://whatever.com/{$actionNum}/0/1/2/3/4/5/6/7/8/9";
                 $referrerUrl = "http://google.com/?q={$actionNum}";
             } else {
                 $url = "http://whatever.com/thing2/{$actionNum}/0/1/2/3/4/5/6/7/8/9";
                 $referrerUrl = "http://";
             }
             $title = "A page title / {$actionNum} / 0 / 1 / 2 / 3 / 4 / 5 / 6 / 7 / 8 /9";
             $t->setNewVisitorId();
             $t->setForceVisitDateTime($date);
             $t->setUrl($url);
             $t->setUrlReferrer($referrerUrl);
             Fixture::checkResponse($t->doTrackPageView($title));
             ++$actionNum;
         }
     }
 }
Esempio n. 6
0
 /**
  * Computes the output for the given data table
  *
  * @param DataTable $table
  * @return string
  * @throws Exception
  */
 protected function renderTable($table)
 {
     if (!$table instanceof DataTable\Map || $table->getKeyName() != 'date') {
         throw new Exception("RSS feeds can be generated for one specific website &idSite=X." . "\nPlease specify only one idSite or consider using &format=XML instead.");
     }
     $idSite = Common::getRequestVar('idSite', 1, 'int');
     $period = Common::getRequestVar('period');
     $piwikUrl = SettingsPiwik::getPiwikUrl() . "?module=CoreHome&action=index&idSite=" . $idSite . "&period=" . $period;
     $out = "";
     $moreRecentFirst = array_reverse($table->getDataTables(), true);
     foreach ($moreRecentFirst as $date => $subtable) {
         /** @var DataTable $subtable */
         $timestamp = $subtable->getMetadata(Archive\DataTableFactory::TABLE_METADATA_PERIOD_INDEX)->getDateStart()->getTimestamp();
         $site = $subtable->getMetadata(Archive\DataTableFactory::TABLE_METADATA_SITE_INDEX);
         $pudDate = date('r', $timestamp);
         $dateInSiteTimezone = Date::factory($timestamp);
         if ($site) {
             $dateInSiteTimezone = $dateInSiteTimezone->setTimezone($site->getTimezone());
         }
         $dateInSiteTimezone = $dateInSiteTimezone->toString('Y-m-d');
         $thisPiwikUrl = Common::sanitizeInputValue($piwikUrl . "&date={$dateInSiteTimezone}");
         $siteName = $site ? $site->getName() : '';
         $title = $siteName . " on " . $date;
         $out .= "\t<item>\n\t\t<pubDate>{$pudDate}</pubDate>\n\t\t<guid>{$thisPiwikUrl}</guid>\n\t\t<link>{$thisPiwikUrl}</link>\n\t\t<title>{$title}</title>\n\t\t<author>http://piwik.org</author>\n\t\t<description>";
         $out .= Common::sanitizeInputValue($this->renderDataTable($subtable));
         $out .= "</description>\n\t</item>\n";
     }
     $header = $this->getRssHeader();
     $footer = $this->getRssFooter();
     return $header . $out . $footer;
 }
 private function trackVisits()
 {
     $dateTimes = $this->dateTimes;
     $days = 0;
     $ridx = 0;
     foreach ($dateTimes as $dateTime) {
         $days++;
         // Fake the visit count cookie
         $debugStringAppend = "&_idvc={$days}";
         $visitor = $this->makeTracker($this->idSite, $dateTime, $debugStringAppend);
         // FIRST VISIT THIS DAY
         $visitor->setForceVisitDateTime(Date::factory($dateTime)->addHour(0.1)->getDatetime());
         $visitor->setUrl('http://example.org/homepage');
         $visitor->setUrlReferrer($this->referrerUrls[$ridx++]);
         self::checkResponse($visitor->doTrackPageView('ou pas'));
         // Test change the IP, the visit should not be split but recorded to the same idvisitor
         $visitor->setIp('200.1.15.22');
         $visitor->setForceVisitDateTime(Date::factory($dateTime)->addHour(0.2)->getDatetime());
         $urlWithThreeSubdirectories = 'http://example.org/sub1/sub2/sub3/news';
         $visitor->setUrl($urlWithThreeSubdirectories);
         self::checkResponse($visitor->doTrackPageView('ou pas'));
         // SECOND VISIT THIS DAY
         $visitor->setForceVisitDateTime(Date::factory($dateTime)->addHour(1)->getDatetime());
         $visitor->setUrl('http://example.org/news');
         $visitor->setUrlReferrer($this->referrerUrls[$ridx++]);
         self::checkResponse($visitor->doTrackPageView('ou pas'));
         if ($days <= 3) {
             $visitor = $this->makeTracker($this->idSite2, $dateTime);
             $visitor->setForceVisitDateTime(Date::factory($dateTime)->addHour(0.1)->getDatetime());
             $visitor->setUrl('http://example.org/homepage');
             $visitor->setUrlReferrer($this->referrerUrls[$ridx - 1]);
             self::checkResponse($visitor->doTrackPageView('Second website'));
         }
     }
 }
 private function trackVisits()
 {
     $dateTime = $this->dateTime;
     $idSite = $this->idSite;
     // two visits to site 1 & 3
     $visitor1 = self::getTracker($idSite, $dateTime, $defaultInit = true);
     $visitor1->setForceVisitDateTime(Date::factory($this->dateTime)->getDatetime());
     $visitor1->setUrl('http://helios.org/alpha');
     $visitor1->doTrackPageView("page title");
     $visitor1->setIdSite($this->idSite2);
     $visitor1->setForceVisitDateTime(Date::factory($this->dateTime)->addHour(1)->getDatetime());
     $visitor1->setUrl('http://taura.org/');
     $visitor1->doTrackPageView("page title");
     // one visit to site 1
     $visitor2 = self::getTracker($idSite, $dateTime, $defaultInit = true);
     $visitor2->setForceVisitDateTime(Date::factory($this->dateTime)->addHour(2)->getDatetime());
     $visitor2->setUrl('http://helios.org/beta');
     $visitor2->doTrackPageView("page title 2");
     // two visits to site 2 and 3
     $visitor3 = self::getTracker($this->idSite1, $dateTime, $defaultInit = true);
     $visitor3->setForceVisitDateTime(Date::factory($this->dateTime)->addHour(3)->getDatetime());
     $visitor3->setUrl('http://virgon.org/');
     $visitor3->doTrackPageView("page title 2");
     $visitor3->setIdSite($this->idSite2);
     $visitor3->setForceVisitDateTime(Date::factory($this->dateTime)->addHour(4)->getDatetime());
     $visitor3->setUrl('http://taura.org/');
     $visitor3->doTrackPageView("page title");
 }
 private function trackVisits()
 {
     $dateTime = $this->dateTime;
     $idSite = 1;
     $idGoal_OneConversionPerVisit = $this->idGoal_OneConversionPerVisit;
     $idGoal_MultipleConversionPerVisit = $this->idGoal_MultipleConversionPerVisit;
     $t = self::getTracker($idSite, $dateTime, $defaultInit = true);
     // Record 1st goal, should only have 1 conversion
     $t->setUrl('http://example.org/index.htm');
     $t->setForceVisitDateTime(Date::factory($dateTime)->addHour(0.3)->getDatetime());
     self::checkResponse($t->doTrackPageView('Thank you mate'));
     $t->setForceVisitDateTime(Date::factory($dateTime)->addHour(0.4)->getDatetime());
     self::checkResponse($t->doTrackGoal($idGoal_OneConversionPerVisit, $revenue = 10000000));
     // Record 2nd goal, should record both conversions
     $t->setForceVisitDateTime(Date::factory($dateTime)->addHour(0.5)->getDatetime());
     self::checkResponse($t->doTrackGoal($idGoal_MultipleConversionPerVisit, $revenue = 300));
     $t->setForceVisitDateTime(Date::factory($dateTime)->addHour(0.6)->getDatetime());
     self::checkResponse($t->doTrackGoal($idGoal_MultipleConversionPerVisit, $revenue = 366));
     // Update & set to not allow multiple
     $goals = API::getInstance()->getGoals($idSite);
     $goal = $goals[$idGoal_OneConversionPerVisit];
     self::assertTrue($goal['allow_multiple'] == 0);
     API::getInstance()->updateGoal($idSite, $idGoal_OneConversionPerVisit, $goal['name'], @$goal['match_attribute'], @$goal['pattern'], @$goal['pattern_type'], @$goal['case_sensitive'], $goal['revenue'], $goal['allow_multiple'] = 1);
     self::assertTrue($goal['allow_multiple'] == 1);
     // 1st goal should Now be tracked
     $t->setForceVisitDateTime(Date::factory($dateTime)->addHour(0.61)->getDatetime());
     self::checkResponse($t->doTrackGoal($idGoal_OneConversionPerVisit, $revenue = 656));
     // few minutes later, create a new_visit
     $t->setForceVisitDateTime(Date::factory($dateTime)->addHour(0.7)->getDatetime());
     $t->setTokenAuth($this->getTokenAuth());
     $t->setForceNewVisit();
     $t->doTrackPageView('This is tracked in a new visit.');
 }
 public function index()
 {
     Piwik::checkUserHasSuperUserAccess();
     $view = new View('@TasksTimetable/index.twig');
     $this->setGeneralVariablesView($view);
     $tasks = Option::get('TaskScheduler.timetable');
     if (!empty($tasks)) {
         $tasks = unserialize($tasks);
     }
     if (empty($tasks)) {
         $tasks = array();
     } else {
         asort($tasks);
     }
     $tsNow = Date::now()->getTimestamp();
     $dateFormat = Piwik::translate(Date::DATE_FORMAT_LONG) . ' h:mm:ss';
     $formatter = new Formatter();
     $tasksFormatted = array();
     foreach ($tasks as $name => $timestamp) {
         $tasksFormatted[] = array('name' => $name, 'executionDate' => Date::factory($timestamp)->getLocalized($dateFormat), 'ts_difference' => $formatter->getPrettyTimeFromSeconds($timestamp - $tsNow));
     }
     $view->currentTime = Date::now()->getLocalized($dateFormat);
     $view->tasks = $tasksFormatted;
     return $view->render();
 }
 private function trackVisits()
 {
     $dateTime = $this->dateTime;
     $idSite = $this->idSite;
     $t = self::getTracker($idSite, $dateTime, $defaultInit = true);
     $t->setUrlReferrer('http://www.google.com/search?q=piwik');
     $t->setUrl('http://example.org/foo/bar.html');
     self::checkResponse($t->doTrackPageView('http://incredible.title/'));
     $t->setUrl('https://example.org/foo/bar.html');
     $t->setForceVisitDateTime(Date::factory($dateTime)->addHour(0.2)->getDatetime());
     self::checkResponse($t->doTrackPageView('https://incredible.title/'));
     $t->setUrl('https://wWw.example.org/foo/bar2.html');
     $t->setForceVisitDateTime(Date::factory($dateTime)->addHour(0.3)->getDatetime());
     self::checkResponse($t->doTrackPageView('http://www.incredible.title/'));
     $t->setUrl('http://WwW.example.org/foo/bar2.html');
     $t->setForceVisitDateTime(Date::factory($dateTime)->addHour(0.4)->getDatetime());
     self::checkResponse($t->doTrackPageView('https://www.incredible.title/'));
     $t->setUrl('http://www.example.org/foo/bar3.html');
     $t->setForceVisitDateTime(Date::factory($dateTime)->addHour(0.5)->getDatetime());
     self::checkResponse($t->doTrackPageView('incredible.title/'));
     $t->setUrl('http://www.my.url/ꟽ碌㒧䊶亄ﶆⅅขκもኸόσशμεޖृ');
     $t->setForceVisitDateTime(Date::factory($dateTime)->addHour(0.7)->getDatetime());
     self::checkResponse($t->doTrackPageView('Valid URL, although strange looking'));
     $t->setUrl('https://make.wordpress.org/?emoji=😎l&param=test');
     $t->setForceVisitDateTime(Date::factory($dateTime)->addHour(0.8)->getDatetime());
     self::checkResponse($t->doTrackPageView('Emoji here: %F0%9F%98%8E'));
     // this pageview should be last
     $t->setUrl('https://example.org/foo/bar4.html');
     $t->setForceVisitDateTime(Date::factory($dateTime)->addHour(0.6)->getDatetime());
     self::checkResponse($t->doTrackPageView('incredible.title/'));
 }
 public function setUp()
 {
     parent::setUp();
     $this->archiveTableDao = self::$fixture->piwikEnvironment->getContainer()->get('Piwik\\DataAccess\\ArchiveTableDao');
     ArchiveTableCreator::getBlobTable(Date::factory('2015-01-01'));
     ArchiveTableCreator::getNumericTable(Date::factory('2015-01-01'));
 }
 private function trackVisits()
 {
     $dateTime = $this->dateTime;
     $idSite = $this->idSite;
     $t = self::getTracker($idSite, $dateTime, $defaultInit = true, $useThirdPartyCookie = 1);
     $t->setUrlReferrer('http://www.google.com.vn/url?sa=t&rct=j&q=%3C%3E%26%5C%22the%20pdo%20extension%20is%20required%20for%20this%20adapter%20but%20the%20extension%20is%20not%20loaded&source=web&cd=4&ved=0FjAD&url=http%3A%2F%2Fforum.piwik.org%2Fread.php%3F2%2C1011&ei=y-HHAQ&usg=AFQjCN2-nt5_GgDeg&cad=rja');
     $t->setUrl('http://example.org/%C3%A9%C3%A9%C3%A9%22%27...%20%3Cthis%20is%20cool%3E!');
     $t->setGenerationTime(523);
     self::checkResponse($t->doTrackPageView('incredible title! <>,;'));
     $t->setUrl('http://example.org/dir/file.php?foo=bar&foo2=bar');
     $t->setForceVisitDateTime(Date::factory($dateTime)->addHour(0.2)->getDatetime());
     $t->setGenerationTime(123);
     self::checkResponse($t->doTrackPageView('incredible title! <>,;'));
     $t->setUrl('http://example.org/dir/file.php?foo=bar&foo2=bar2');
     $t->setForceVisitDateTime(Date::factory($dateTime)->addHour(0.3)->getDatetime());
     $t->setGenerationTime(153);
     self::checkResponse($t->doTrackPageView('incredible parent title! <>,; / subtitle <>,;'));
     $t->setUrl('http://example.org/dir/file.php?foo=bar&foo2=bar2');
     $t->setForceVisitDateTime(Date::factory($dateTime)->addHour(0.31)->getDatetime());
     $t->setGenerationTime(153);
     self::checkResponse($t->doTrackEvent('Category', 'Action', 'Name', 11111));
     $t->setUrl('http://example.org/dir2/file.php?foo=bar&foo2=bar');
     $t->setForceVisitDateTime(Date::factory($dateTime)->addHour(0.4)->getDatetime());
     $t->setGenerationTime(1233);
     self::checkResponse($t->doTrackPageView('incredible title! <>,;'));
     $t->setUrl('http://example.org/dir2/sub/0/file.php');
     $t->setForceVisitDateTime(Date::factory($dateTime)->addHour(0.4)->getDatetime());
     // Very high Generation time should be ignored
     $t->setGenerationTime(6350000);
     self::checkResponse($t->doTrackPageView('incredible title! <>,;'));
     $t->setUrl('http://example.org/0');
     $t->setForceVisitDateTime(Date::factory($dateTime)->addHour(0.4)->getDatetime());
     $t->setGenerationTime(635);
     self::checkResponse($t->doTrackPageView('I am URL zero!'));
 }
Esempio n. 14
0
 /**
  * @return bool `true` if the purge was executed, `false` if it was skipped.
  * @throws \Exception
  */
 public function purgeOutdatedArchives()
 {
     if ($this->willPurgingCausePotentialProblemInUI()) {
         $this->logger->info("Purging temporary archives: skipped (browser triggered archiving not enabled & not running after core:archive)");
         return false;
     }
     $archiveTables = ArchiveTableCreator::getTablesArchivesInstalled();
     $this->logger->info("Purging archives in {tableCount} archive tables.", array('tableCount' => count($archiveTables)));
     // keep track of dates we purge for, since getTablesArchivesInstalled() will return numeric & blob
     // tables (so dates will appear two times, and we should only purge once per date)
     $datesPurged = array();
     foreach ($archiveTables as $table) {
         $date = ArchiveTableCreator::getDateFromTableName($table);
         list($year, $month) = explode('_', $date);
         // Somehow we may have archive tables created with older dates, prevent exception from being thrown
         if ($year > 1990) {
             if (empty($datesPurged[$date])) {
                 $dateObj = Date::factory("{$year}-{$month}-15");
                 $this->archivePurger->purgeOutdatedArchives($dateObj);
                 $this->archivePurger->purgeArchivesWithPeriodRange($dateObj);
                 $datesPurged[$date] = true;
             } else {
                 $this->logger->debug("Date {date} already purged.", array('date' => $date));
             }
         } else {
             $this->logger->info("Skipping purging of archive tables *_{year}_{month}, year <= 1990.", array('year' => $year, 'month' => $month));
         }
     }
     return true;
 }
 private function trackVisits()
 {
     // tests run in UTC, the Tracker in UTC
     $dateTime = $this->dateTime;
     $idSite = $this->idSite;
     $t = self::getTracker($idSite, $dateTime, $defaultInit = true);
     // Also testing to record this as a bot while specifically allowing bots
     $t->setUserAgent('Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)');
     $t->DEBUG_APPEND_URL .= '&bots=1';
     $t->DEBUG_APPEND_URL .= '&forceIpAnonymization=1';
     // VISIT 1 = Referrer is "Keyword not defined"
     // Alsotrigger goal to check that attribution goes to this keyword
     $t->setUrlReferrer('http://www.google.com/url?sa=t&rct=j&q=&esrc=s&source=web&cd=1&ved=0CC&url=http%3A%2F%2Fpiwik.org%2F&ei=&usg=');
     $t->setUrl('http://example.org/this%20is%20cool!?filter=<script>alert(1);</script>{"place":{"place":"0c5b2444-70a0-4932-980c-b4dc0d3f02b5"}}');
     self::checkResponse($t->doTrackPageView('incredible title! (Page URL contains a HTML entity)'));
     $idGoal = 1;
     if (!self::goalExists($idSite, $idGoal)) {
         $idGoal = API::getInstance()->addGoal($idSite, 'triggered js', 'manually', '', '');
     }
     $t->setForceVisitDateTime(Date::factory($dateTime)->addHour(0.3)->getDatetime());
     self::checkResponse($t->doTrackGoal($idGoal, $revenue = 42));
     // VISIT 2 = Referrer has keyword, but the URL should be rewritten
     // in Live Output to point to google search result page
     $t->setForceVisitDateTime(Date::factory($dateTime)->addHour(2)->getDatetime());
     $t->setUrlReferrer('http://www.google.com.vn/url?sa=t&rct=j&q=%3C%3E%26%5C%22the%20pdo%20extension%20is%20required%20for%20this%20adapter%20but%20the%20extension%20is%20not%20loaded&source=web&cd=4&ved=0FjAD&url=http%3A%2F%2Fforum.piwik.org%2Fread.php%3F2%2C1011&ei=y-HHAQ&usg=AFQjCN2-nt5_GgDeg&cad=rja');
     // Test with empty title, that the output of Live is valid
     self::checkResponse($t->doTrackPageView(''));
 }
Esempio n. 16
0
 /**
  * The constructor
  * Initialize some local variables from the request
  * @param int $idSite
  * @param Date $date ($this->date from controller)
  * @param null|string $graphType
  * @throws Exception
  */
 public function __construct($idSite, $date, $graphType = null)
 {
     $this->apiMethod = Common::getRequestVar('apiMethod', '', 'string');
     if (empty($this->apiMethod)) {
         throw new Exception("Parameter apiMethod not set.");
     }
     $this->label = ResponseBuilder::getLabelFromRequest($_GET);
     $this->label = $this->label[0];
     if ($this->label === '') {
         throw new Exception("Parameter label not set.");
     }
     $this->period = Common::getRequestVar('period', '', 'string');
     if (empty($this->period)) {
         throw new Exception("Parameter period not set.");
     }
     $this->idSite = $idSite;
     $this->graphType = $graphType;
     if ($this->period != 'range') {
         // handle day, week, month and year: display last X periods
         $end = $date->toString();
         list($this->date, $lastN) = EvolutionViz::getDateRangeAndLastN($this->period, $end);
     }
     $this->segment = \Piwik\API\Request::getRawSegmentFromRequest();
     $this->loadEvolutionReport();
 }
Esempio n. 17
0
 /**
  * The constructor
  * Initialize some local variables from the request
  * @param int $idSite
  * @param Date $date ($this->date from controller)
  * @param null|string $graphType
  * @throws Exception
  */
 public function __construct($idSite, $date, $graphType = 'graphEvolution')
 {
     $this->apiMethod = Common::getRequestVar('apiMethod', '', 'string');
     if (empty($this->apiMethod)) {
         throw new Exception("Parameter apiMethod not set.");
     }
     $this->label = DataTablePostProcessor::getLabelFromRequest($_GET);
     if (!is_array($this->label)) {
         throw new Exception("Expected label to be an array, got instead: " . $this->label);
     }
     $this->label = $this->label[0];
     if ($this->label === '') {
         throw new Exception("Parameter label not set.");
     }
     $this->period = Common::getRequestVar('period', '', 'string');
     PeriodFactory::checkPeriodIsEnabled($this->period);
     $this->idSite = $idSite;
     $this->graphType = $graphType;
     if ($this->period != 'range') {
         // handle day, week, month and year: display last X periods
         $end = $date->toString();
         list($this->date, $lastN) = EvolutionViz::getDateRangeAndLastN($this->period, $end);
     }
     $this->segment = \Piwik\API\Request::getRawSegmentFromRequest();
     $this->loadEvolutionReport();
 }
Esempio n. 18
0
 /**
  * Constructor.
  */
 public function __construct()
 {
     $date = $this->month . '-01';
     $classes = get_declared_classes();
     sort($classes);
     foreach ($classes as $className) {
         if (is_subclass_of($className, 'Fixture') && !is_subclass_of($className, __CLASS__) && $className != __CLASS__ && $className != "Piwik_Test_Fixture_SqlDump" && $className != "Piwik\\Tests\\Fixtures\\UpdaterTestFixture") {
             $klassReflect = new ReflectionClass($className);
             if (!strpos($klassReflect->getFilename(), "tests/PHPUnit/Fixtures") && $className != "Test_Piwik_Fixture_CustomAlerts" && $className != "Piwik\\Plugins\\Insights\\tests\\Fixtures\\SomeVisitsDifferentPathsOnTwoDays") {
                 continue;
             }
             $fixture = new $className();
             if (!property_exists($fixture, 'dateTime')) {
                 continue;
             }
             $fixture->dateTime = $this->adjustDateTime($fixture->dateTime, $date);
             $this->fixtures[$className] = $fixture;
             $date = Date::factory($date)->addDay(1)->toString();
         }
     }
     $this->now = $this->fixtures['Test_Piwik_Fixture_ManySitesImportedLogsWithXssAttempts']->now;
     // make sure Test_Piwik_Fixture_ManySitesImportedLogsWithXssAttempts is the first fixture
     $fixture = $this->fixtures['Test_Piwik_Fixture_ManySitesImportedLogsWithXssAttempts'];
     unset($this->fixtures['Test_Piwik_Fixture_ManySitesImportedLogsWithXssAttempts']);
     $this->fixtures = array_merge(array('Test_Piwik_Fixture_ManySitesImportedLogsWithXssAttempts' => $fixture), $this->fixtures);
 }
Esempio n. 19
0
 protected static function beforeTableDataCached()
 {
     $date = Date::factory('2010-03-01');
     $archiveTableCreator = new ArchiveTableCreator();
     $archiveTableCreator->getBlobTable($date);
     $archiveTableCreator->getNumericTable($date);
 }
Esempio n. 20
0
 private function addAnnotations()
 {
     // create fake access for fake username
     $access = new FakeAccess();
     FakeAccess::$superUser = true;
     Access::setSingletonInstance($access);
     // add two annotations per week for three months, starring every third annotation
     // first month in 2011, second two in 2012
     $count = 0;
     $dateStart = Date::factory('2011-12-01');
     $dateEnd = Date::factory('2012-03-01');
     while ($dateStart->getTimestamp() < $dateEnd->getTimestamp()) {
         $starred = $count % 3 == 0 ? 1 : 0;
         $site1Text = "{$count}: Site 1 annotation for " . $dateStart->toString();
         $site2Text = "{$count}: Site 2 annotation for " . $dateStart->toString();
         API::getInstance()->add($this->idSite1, $dateStart->toString(), $site1Text, $starred);
         API::getInstance()->add($this->idSite2, $dateStart->toString(), $site2Text, $starred);
         $nextDay = $dateStart->addDay(1);
         ++$count;
         $starred = $count % 3 == 0 ? 1 : 0;
         $site1Text = "{$count}: Site 1 annotation for " . $nextDay->toString();
         $site2Text = "{$count}: Site 2 annotation for " . $nextDay->toString();
         API::getInstance()->add($this->idSite1, $nextDay->toString(), $site1Text, $starred);
         API::getInstance()->add($this->idSite2, $nextDay->toString(), $site2Text, $starred);
         $dateStart = $dateStart->addPeriod(1, 'WEEK');
         ++$count;
     }
 }
 private function trackVisits()
 {
     $dateTime = $this->dateTime;
     $idSite = $this->idSite;
     for ($referrerSite = 1; $referrerSite < 4; $referrerSite++) {
         for ($referrerPage = 1; $referrerPage < 3; $referrerPage++) {
             $offset = $referrerSite * 3 + $referrerPage;
             $t = self::getTracker($idSite, Date::factory($dateTime)->addHour($offset)->getDatetime());
             $t->setUrlReferrer('http://www.referrer' . $referrerSite . '.com/sub/dir/page' . $referrerPage . '.html');
             $t->setCustomVariable(1, 'CustomVarVisit', 'CustomVarValue' . $referrerPage, 'visit');
             for ($page = 0; $page < 3; $page++) {
                 $t->setUrl('http://example.org/dir' . $referrerSite . '/sub/dir/page' . $page . '.html');
                 $t->setCustomVariable(1, 'CustomVarPage', 'CustomVarValue' . $page, 'page');
                 $t->setGenerationTime($referrerPage * $referrerSite * ($page + 1) * 100);
                 self::checkResponse($t->doTrackPageView('title'));
             }
         }
     }
     $t = self::getTracker($idSite, Date::factory($dateTime)->addHour(24)->getDatetime());
     $t->setCustomVariable(1, 'CustomVarVisit', 'CustomVarValue1', 'visit');
     $t->setUrl('http://example.org/sub/dir/dir1/page1.html');
     $t->setCustomVariable(1, 'CustomVarPage', 'CustomVarValue1', 'page');
     self::checkResponse($t->doTrackPageView('title'));
     $t = self::getTracker($idSite, Date::factory($dateTime)->addHour(24)->getDatetime());
     $t->setUrl('http://example.org/page1.html');
     self::checkResponse($t->doTrackPageView('title'));
 }
 public function getApiForTesting()
 {
     $idSite = self::$fixture->idSite;
     $dateTime = self::$fixture->dateTime;
     $dateString = Date::factory($dateTime)->toString();
     // Note: we must set  'UserCountry.getLocationFromIP' since it's "excluded" by default in setApiNotToCall
     $apiToCall = array('UserCountry');
     $apiToTest = array(array($apiToCall, array('idSite' => $idSite, 'date' => $dateTime, 'periods' => array('month'))), array($apiToCall, array('idSite' => $idSite, 'date' => $dateTime, 'periods' => array('month'), 'testSuffix' => '_segment_region', 'segment' => 'regionCode==P3;countryCode==gb')), array($apiToCall, array('idSite' => $idSite, 'date' => $dateTime, 'periods' => array('month'), 'testSuffix' => '_segment_city', 'segment' => 'city==Stratford-upon-Avon;regionCode==P3;countryCode==gb')), array($apiToCall, array('idSite' => $idSite, 'date' => $dateTime, 'periods' => array('month'), 'testSuffix' => '_segment_lat_long', 'segment' => 'latitude>45;latitude<49.3;longitude>-125;longitude<-122')), array('UserCountry.getCountry', array('idSite' => $idSite, 'date' => $dateTime, 'periods' => array('month'), 'testSuffix' => '_segment_continent', 'segment' => 'continentCode==eur')), array('API.getProcessedReport', array('idSite' => $idSite, 'date' => $dateTime, 'periods' => 'day', 'apiModule' => 'Actions', 'apiAction' => 'getPageUrls', 'testSuffix' => '_sortByProcessedMetric', 'otherRequestParameters' => array('filter_sort_column' => 'nb_actions_per_visit'))), array('API.getProcessedReport', array('idSite' => $idSite, 'date' => $dateTime, 'periods' => 'day', 'apiModule' => 'VisitTime', 'apiAction' => 'getVisitInformationPerServerTime', 'testSuffix' => '_sortByProcessedMetric_constantRowsCountShouldKeepEmptyRows', 'otherRequestParameters' => array('filter_sort_column' => 'nb_actions_per_visit'))), array(array('UserCountry.getLocationFromIP', 'Live.getLastVisitsDetails'), array('idSite' => $idSite, 'date' => $dateTime, 'periods' => array('month'), 'otherRequestParameters' => array('ip' => '194.57.91.215'))), array(array('UserCountry.getLocationFromIP'), array('otherRequestParameters' => array('ip' => '194.57.91.215'))), array(array('UserCountry.getLocationFromIP'), array('testSuffix' => '_IPv6', 'otherRequestParameters' => array('ip' => '2001:db8:85a3:0:0:8a2e:370:7334'))));
     // Randomly fails on 5.3
     if (!self::isPhpVersion53()) {
         $apiToTest[] = array('Live.getLastVisitsDetails', array('idSite' => $idSite, 'date' => $dateString, 'periods' => 'month', 'testSuffix' => '_Live.getLastVisitsDetails_sortDesc', 'otherRequestParameters' => array('filter_sort_order' => 'desc', 'filter_limit' => 7)));
         // #5950
         $apiToTest[] = array('Live.getLastVisitsDetails', array('idSite' => $idSite, 'date' => $dateString, 'periods' => 'month', 'testSuffix' => '_Live.getLastVisitsDetails_sortByIdVisit', 'otherRequestParameters' => array('filter_sort_order' => 'desc', 'filter_sort_column' => 'idVisit', 'filter_limit' => 7)));
         // #7458
         $apiToTest[] = array('Live.getLastVisitsDetails', array('idSite' => $idSite, 'date' => $dateString, 'periods' => 'month', 'testSuffix' => '_Live.getLastVisitsDetails_offsetAndLimit_1', 'otherRequestParameters' => array('filter_offset' => '1', 'filter_limit' => 3)));
         $apiToTest[] = array('Live.getLastVisitsDetails', array('idSite' => $idSite, 'date' => $dateString, 'periods' => 'month', 'testSuffix' => '_Live.getLastVisitsDetails_offsetAndLimit_2', 'otherRequestParameters' => array('filter_offset' => '4', 'filter_limit' => 3)));
     }
     // this also fails on all PHP versions, it seems randomly.
     //            $apiToTest[] = array('Live.getLastVisitsDetails', array(
     //                'idSite'                 => $idSite,
     //                'date'                   => $dateString,
     //                'periods'                => 'month',
     //                'testSuffix'             => '_Live.getLastVisitsDetails_sortAsc',
     //                'otherRequestParameters' => array('filter_sort_order' => 'asc', 'filter_limit' => 7)
     //            ));
     return $apiToTest;
 }
Esempio n. 23
0
 private static function migrateConfigSuperUserToDb()
 {
     $config = Config::getInstance();
     if (!$config->existsLocalConfig()) {
         return;
     }
     try {
         $superUser = $config->superuser;
     } catch (\Exception $e) {
         $superUser = null;
     }
     if (!empty($superUser['bridge']) || empty($superUser) || empty($superUser['login'])) {
         // there is a super user which is not from the config but from the bridge, that means we already have
         // a super user in the database
         return;
     }
     $userApi = UsersManagerApi::getInstance();
     try {
         Db::get()->insert(Common::prefixTable('user'), array('login' => $superUser['login'], 'password' => $superUser['password'], 'alias' => $superUser['login'], 'email' => $superUser['email'], 'token_auth' => $userApi->getTokenAuth($superUser['login'], $superUser['password']), 'date_registered' => Date::now()->getDatetime(), 'superuser_access' => 1));
     } catch (\Exception $e) {
         echo "There was an issue, but we proceed: " . $e->getMessage();
     }
     if (array_key_exists('salt', $superUser)) {
         $salt = $superUser['salt'];
     } else {
         $salt = Common::generateUniqId();
     }
     $config->General['salt'] = $salt;
     $config->superuser = array();
     $config->forceSave();
 }
Esempio n. 24
0
 /**
  * @group Core
  */
 public function testGetPrettyString()
 {
     Translate::loadEnglishTranslation();
     $year = new Year(Date::factory('2024-10-09'));
     $shouldBe = '2024';
     $this->assertEquals($shouldBe, $year->getPrettyString());
 }
Esempio n. 25
0
 public static function getArchiveIdAndVisits(ArchiveProcessor\Parameters $params, $minDatetimeArchiveProcessedUTC)
 {
     $idSite = $params->getSite()->getId();
     $period = $params->getPeriod()->getId();
     $dateStart = $params->getPeriod()->getDateStart();
     $dateStartIso = $dateStart->toString('Y-m-d');
     $dateEndIso = $params->getPeriod()->getDateEnd()->toString('Y-m-d');
     $numericTable = ArchiveTableCreator::getNumericTable($dateStart);
     $minDatetimeIsoArchiveProcessedUTC = null;
     if ($minDatetimeArchiveProcessedUTC) {
         $minDatetimeIsoArchiveProcessedUTC = Date::factory($minDatetimeArchiveProcessedUTC)->getDatetime();
     }
     $requestedPlugin = $params->getRequestedPlugin();
     $segment = $params->getSegment();
     $plugins = array("VisitsSummary", $requestedPlugin);
     $doneFlags = Rules::getDoneFlags($plugins, $segment);
     $doneFlagValues = Rules::getSelectableDoneFlagValues();
     $results = self::getModel()->getArchiveIdAndVisits($numericTable, $idSite, $period, $dateStartIso, $dateEndIso, $minDatetimeIsoArchiveProcessedUTC, $doneFlags, $doneFlagValues);
     if (empty($results)) {
         return false;
     }
     $idArchive = self::getMostRecentIdArchiveFromResults($segment, $requestedPlugin, $results);
     $idArchiveVisitsSummary = self::getMostRecentIdArchiveFromResults($segment, "VisitsSummary", $results);
     list($visits, $visitsConverted) = self::getVisitsMetricsFromResults($idArchive, $idArchiveVisitsSummary, $results);
     if (false === $visits && false === $idArchive) {
         return false;
     }
     return array($idArchive, $visits, $visitsConverted);
 }
 public function setUp()
 {
     // add one thousand sites
     $allIdSites = array();
     for ($i = 0; $i < 1000; ++$i) {
         $allIdSites[] = Fixture::createWebsite($this->date, $ecommerce = 1, $siteName = "Site #{$i}");
     }
     $urls = array();
     for ($i = 0; $i != 3; ++$i) {
         $url = "http://whatever.com/" . ($i - 1) . "/" . ($i + 1);
         $title = "page view " . ($i - 1) . " / " . ($i + 1);
         $urls[$url] = $title;
     }
     $visitTimes = array();
     $date = Date::factory($this->date);
     for ($i = 0; $i != 4; ++$i) {
         $visitTimes[] = $date->addHour($i)->getDatetime();
     }
     // add 12000 visits (3 visitors with 4 visits each for each site) w/ 3 pageviews each on one day
     foreach ($visitTimes as $visitTime) {
         foreach ($allIdSites as $idSite) {
             for ($visitor = 0; $visitor != 3; ++$visitor) {
                 $t = BenchmarkTestCase::getLocalTracker($idSite);
                 $ip = "157.5.6." . ($visitor + 1);
                 $t->setIp($ip);
                 $t->setNewVisitorId();
                 $t->setForceVisitDateTime($visitTime);
                 foreach ($urls as $url => $title) {
                     $t->setUrl($url);
                     $t->doTrackPageView($title);
                 }
             }
         }
     }
 }
 public function test_filterDataTable_MatchesExactlyIntegration()
 {
     $date = Date::today()->addHour(10);
     $t = Fixture::getTracker($this->idSite, $date->getDatetime(), $defaultInit = true);
     $t->setUrlReferrer('http://www.google.com.vn/url?sa=t&rct=j&q=%3C%3E%26%5C%22the%20pdo%20extension%20is%20required%20for%20this%20adapter%20but%20the%20extension%20is%20not%20loaded&source=web&cd=4&ved=0FjAD&url=http%3A%2F%2Fforum.piwik.org%2Fread.php%3F2%2C1011&ei=y-HHAQ&usg=AFQjCN2-nt5_GgDeg&cad=rja');
     $t->setUrl('http://example.org/%C3%A9%C3%A9%C3%A9%22%27...%20%3Cthis%20is%20cool%3E!');
     $t->setGenerationTime(523);
     $t->doTrackPageView('incredible title! <>,;');
     $t->setForceVisitDateTime($date->addHour(0.1)->getDatetime());
     $t->setUrl('http://example.org/dir/file.php?foo=bar&foo2=bar');
     $t->setGenerationTime(123);
     $t->doTrackPageView('incredible title! <>,;');
     $t->setForceVisitDateTime($date->addHour(0.2)->getDatetime());
     $t->setUrl('http://example.org/dir/file/xyz.php?foo=bar&foo2=bar');
     $t->setGenerationTime(231);
     $t->doTrackPageView('incredible title! <>,;');
     $t->setForceVisitDateTime($date->addHour(0.2)->getDatetime());
     $t->setUrl('http://example.org/what-is-piwik');
     $t->setGenerationTime(231);
     $t->doTrackPageView('incredible title! <>,;');
     $t->setForceVisitDateTime($date->addHour(0.3)->getDatetime());
     $t->setUrl('http://example.org/dir/file.php?foo=bar&foo2=bar');
     $t->setGenerationTime(147);
     $t->doTrackPageView('incredible title! <>,;');
     // for some reasons @dataProvider results in an "Mysql::getProfiler() undefined method" error
     $assertions = array(array('nb_hits', 'what-is-piwik', 1), array('nb_hits', '/what-is-piwik', null), array('nb_hits', 'foo', 3), array('nb_visits', 'foo', 2), array('nb_hits', 'i', 5), array('nb_hits', 'foo2=bar', 3), array('nb_hits', '/', 3), array('nb_hits', 'foo=bar&foo2=bar', 3), array('nb_hits', 'php?foo=bar&foo2=bar', 3), array('nb_hits', 'file.php?foo=bar&foo2=bar', 2), array('nb_hits', 'dir/file.php?foo=bar&foo2=bar', 2), array('nb_hits', 'dir', 3), array('avg_time_generation', 'dir/file.php?foo=bar&foo2=bar', 0.135), array('bounce_rate', 'php?foo=bar', 0));
     foreach ($assertions as $assert) {
         $alert = array('report' => 'Actions_getPageUrls', 'metric' => $assert[0], 'period' => 'day', 'report_condition' => 'contains', 'report_matched' => Common::sanitizeInputValue($assert[1]));
         $value = $this->processor->getValueForAlertInPast($alert, $this->idSite, 0);
         $this->assertEquals($assert[2], $value, $assert[0] . ':' . $assert[1] . ' should return value ' . $assert[2] . ' but returns ' . $value);
     }
 }
Esempio n. 28
0
 /**
  * Caches the intermediate DataTables used in the getIndividualReportsSummary and
  * getIndividualMetricsSummary reports in the option table.
  */
 public function cacheDataByArchiveNameReports()
 {
     $api = API::getInstance();
     $api->getIndividualReportsSummary(true);
     $api->getIndividualMetricsSummary(true);
     $now = Date::now()->getLocalized(Date::DATE_FORMAT_SHORT);
     Option::set(DBStats::TIME_OF_LAST_TASK_RUN_OPTION, $now);
 }
 private function buildTracker()
 {
     $t = Fixture::getTracker($this->idSite, Date::factory('2014-01-05 00:01:01')->getDatetime());
     $t->setDebugStringAppend('debug=1');
     $t->setTokenAuth(Fixture::getTokenAuth());
     $t->setUrl('http://example.org/index1.htm');
     return $t;
 }
Esempio n. 30
0
 /**
  * Caches the intermediate DataTables used in the getIndividualReportsSummary and
  * getIndividualMetricsSummary reports in the option table.
  */
 public function cacheDataByArchiveNameReports()
 {
     $api = API::getInstance();
     $api->getIndividualReportsSummary(true);
     $api->getIndividualMetricsSummary(true);
     $now = Date::now()->getLocalized("%longYear%, %shortMonth% %day%");
     Option::set(DBStats::TIME_OF_LAST_TASK_RUN_OPTION, $now);
 }