/** * Returns true if it is likely that the data for this report has been purged and if the * user should be told about that. * * In order for this function to return true, the following must also be true: * - The data table for this report must either be empty or not have been fetched. * - The period of this report is not a multiple period. * - The date of this report must be older than the delete_reports_older_than config option. * @param DataTableInterface $dataTable * @return bool */ public static function hasReportBeenPurged($dataTable) { $strPeriod = Common::getRequestVar('period', false); $strDate = Common::getRequestVar('date', false); if (false !== $strPeriod && false !== $strDate && (is_null($dataTable) || !empty($dataTable) && $dataTable->getRowsCount() == 0)) { // if range, only look at the first date if ($strPeriod == 'range') { $idSite = Common::getRequestVar('idSite', ''); if (intval($idSite) != 0) { $site = new Site($idSite); $timezone = $site->getTimezone(); } else { $timezone = 'UTC'; } $period = new Range('range', $strDate, $timezone); $reportDate = $period->getDateStart(); } elseif (Period::isMultiplePeriod($strDate, $strPeriod)) { // if a multiple period, this function is irrelevant return false; } else { // otherwise, use the date as given $reportDate = Date::factory($strDate); } $reportYear = $reportDate->toString('Y'); $reportMonth = $reportDate->toString('m'); if (static::shouldReportBePurged($reportYear, $reportMonth)) { return true; } } return false; }
public function testSetDefaultTimezoneAndCurrencyAndExcludedQueryParametersAndExcludedIps() { // test that they return default values $defaultTimezone = API::getInstance()->getDefaultTimezone(); $this->assertEquals('UTC', $defaultTimezone); $defaultCurrency = API::getInstance()->getDefaultCurrency(); $this->assertEquals('USD', $defaultCurrency); $excludedIps = API::getInstance()->getExcludedIpsGlobal(); $this->assertEquals('', $excludedIps); $excludedQueryParameters = API::getInstance()->getExcludedQueryParametersGlobal(); $this->assertEquals('', $excludedQueryParameters); // test that when not specified, defaults are set as expected $idsite = API::getInstance()->addSite("site1", array('http://example.org')); $site = new Site($idsite); $this->assertEquals('UTC', $site->getTimezone()); $this->assertEquals('USD', $site->getCurrency()); $this->assertEquals('', $site->getExcludedQueryParameters()); $this->assertEquals('', $site->getExcludedIps()); $this->assertEquals(false, $site->isEcommerceEnabled()); // set the global timezone and get it $newDefaultTimezone = 'UTC+5.5'; API::getInstance()->setDefaultTimezone($newDefaultTimezone); $defaultTimezone = API::getInstance()->getDefaultTimezone(); $this->assertEquals($newDefaultTimezone, $defaultTimezone); // set the default currency and get it $newDefaultCurrency = 'EUR'; API::getInstance()->setDefaultCurrency($newDefaultCurrency); $defaultCurrency = API::getInstance()->getDefaultCurrency(); $this->assertEquals($newDefaultCurrency, $defaultCurrency); // set the global IPs to exclude and get it $newGlobalExcludedIps = '1.1.1.*,1.1.*.*,150.1.1.1'; API::getInstance()->setGlobalExcludedIps($newGlobalExcludedIps); $globalExcludedIps = API::getInstance()->getExcludedIpsGlobal(); $this->assertEquals($newGlobalExcludedIps, $globalExcludedIps); // set the global URL query params to exclude and get it $newGlobalExcludedQueryParameters = 'PHPSESSID,blabla, TesT'; // removed the space $expectedGlobalExcludedQueryParameters = 'PHPSESSID,blabla,TesT'; API::getInstance()->setGlobalExcludedQueryParameters($newGlobalExcludedQueryParameters); $globalExcludedQueryParameters = API::getInstance()->getExcludedQueryParametersGlobal(); $this->assertEquals($expectedGlobalExcludedQueryParameters, $globalExcludedQueryParameters); // create a website and check that default currency and default timezone are set // however, excluded IPs and excluded query Params are not returned $idsite = API::getInstance()->addSite("site1", array('http://example.org'), $ecommerce = 0, $siteSearch = 0, $searchKeywordParameters = 'test1,test2', $searchCategoryParameters = 'test2,test1', '', '', $newDefaultTimezone); $site = new Site($idsite); $this->assertEquals($newDefaultTimezone, $site->getTimezone()); $this->assertEquals(date('Y-m-d'), $site->getCreationDate()->toString()); $this->assertEquals($newDefaultCurrency, $site->getCurrency()); $this->assertEquals('', $site->getExcludedIps()); $this->assertEquals('', $site->getExcludedQueryParameters()); $this->assertEquals('test1,test2', $site->getSearchKeywordParameters()); $this->assertEquals('test2,test1', $site->getSearchCategoryParameters()); $this->assertFalse($site->isSiteSearchEnabled()); $this->assertFalse(Site::isSiteSearchEnabledFor($idsite)); $this->assertFalse($site->isEcommerceEnabled()); $this->assertFalse(Site::isEcommerceEnabledFor($idsite)); }
/** * Assigns variables to {@link Piwik\View} instances that display an entire page. * * The following variables assigned: * * **date** - The value of the **date** query parameter. * **idSite** - The value of the **idSite** query parameter. * **rawDate** - The value of the **date** query parameter. * **prettyDate** - A pretty string description of the current period. * **siteName** - The current site's name. * **siteMainUrl** - The URL of the current site. * **startDate** - The start date of the current period. A {@link Piwik\Date} instance. * **endDate** - The end date of the current period. A {@link Piwik\Date} instance. * **language** - The current language's language code. * **config_action_url_category_delimiter** - The value of the `[General] action_url_category_delimiter` * INI config option. * **topMenu** - The result of `MenuTop::getInstance()->getMenu()`. * * As well as the variables set by {@link setPeriodVariablesView()}. * * Will exit on error. * * @param View $view * @return void * @api */ protected function setGeneralVariablesView($view) { $view->date = $this->strDate; try { $view->idSite = $this->idSite; if (empty($this->site) || empty($this->idSite)) { throw new Exception("The requested website idSite is not found in the request, or is invalid.\n\t\t\t\tPlease check that you are logged in Piwik and have permission to access the specified website."); } $this->setPeriodVariablesView($view); $rawDate = Common::getRequestVar('date'); $periodStr = Common::getRequestVar('period'); if ($periodStr != 'range') { $date = Date::factory($this->strDate); $period = Period\Factory::build($periodStr, $date); } else { $period = new Range($periodStr, $rawDate, $this->site->getTimezone()); } $view->rawDate = $rawDate; $view->prettyDate = self::getCalendarPrettyDate($period); $view->siteName = $this->site->getName(); $view->siteMainUrl = $this->site->getMainUrl(); $datetimeMinDate = $this->site->getCreationDate()->getDatetime(); $minDate = Date::factory($datetimeMinDate, $this->site->getTimezone()); $this->setMinDateView($minDate, $view); $maxDate = Date::factory('now', $this->site->getTimezone()); $this->setMaxDateView($maxDate, $view); // Setting current period start & end dates, for pre-setting the calendar when "Date Range" is selected $dateStart = $period->getDateStart(); if ($dateStart->isEarlier($minDate)) { $dateStart = $minDate; } $dateEnd = $period->getDateEnd(); if ($dateEnd->isLater($maxDate)) { $dateEnd = $maxDate; } $view->startDate = $dateStart; $view->endDate = $dateEnd; $language = LanguagesManager::getLanguageForSession(); $view->language = !empty($language) ? $language : LanguagesManager::getLanguageCodeForCurrentUser(); $this->setBasicVariablesView($view); $view->topMenu = MenuTop::getInstance()->getMenu(); $view->userMenu = MenuUser::getInstance()->getMenu(); $notifications = $view->notifications; if (empty($notifications)) { $view->notifications = NotificationManager::getAllNotificationsToDisplay(); NotificationManager::cancelAllNonPersistent(); } } catch (Exception $e) { Piwik_ExitWithMessage($e->getMessage(), $e->getTraceAsString()); } }
/** * @param DataTable $table * @param int $idSite * @param string $period * @param string $date * @return mixed */ protected function removeHoursInFuture($table, $idSite, $period, $date) { $site = new Site($idSite); if ($period == 'day' && ($date == 'today' || $date == Date::factory('now', $site->getTimezone())->toString())) { $currentHour = Date::factory('now', $site->getTimezone())->toString('G'); // If no data for today, this is an exception to the API output rule, as we normally return nothing: // we shall return all hours of the day, with nb_visits = 0 if ($table->getRowsCount() == 0) { for ($hour = 0; $hour <= $currentHour; $hour++) { $table->addRowFromSimpleArray(array('label' => $hour, 'nb_visits' => 0)); } return $table; } $idsToDelete = array(); foreach ($table->getRows() as $id => $row) { $hour = $row->getColumn('label'); if ($hour > $currentHour) { $idsToDelete[] = $id; } } $table->deleteRows($idsToDelete); } return $table; }
/** * For an array of visits, query the list of pages for this visit * as well as make the data human readable * @param DataTable $dataTable * @param int $idSite * @param bool $flat whether to flatten the array (eg. 'customVariables' names/values will appear in the root array rather than in 'customVariables' key * @param bool $doNotFetchActions If set to true, we only fetch visit info and not actions (much faster) * @param bool $filterNow If true, the visitors will be cleaned immediately */ private function addFilterToCleanVisitors(DataTable $dataTable, $idSite, $flat = false, $doNotFetchActions = false, $filterNow = false) { $filter = 'queueFilter'; if ($filterNow) { $filter = 'filter'; } $dataTable->{$filter}(function ($table) use($idSite, $flat, $doNotFetchActions) { /** @var DataTable $table */ $actionsLimit = (int) Config::getInstance()->General['visitor_log_maximum_actions_per_visit']; $visitorFactory = new VisitorFactory(); $website = new Site($idSite); $timezone = $website->getTimezone(); $currency = $website->getCurrency(); $currencies = APISitesManager::getInstance()->getCurrencySymbols(); // live api is not summable, prevents errors like "Unexpected ECommerce status value" $table->deleteRow(DataTable::ID_SUMMARY_ROW); foreach ($table->getRows() as $visitorDetailRow) { $visitorDetailsArray = Visitor::cleanVisitorDetails($visitorDetailRow->getColumns()); $visitor = $visitorFactory->create($visitorDetailsArray); $visitorDetailsArray = $visitor->getAllVisitorDetails(); $visitorDetailsArray['siteCurrency'] = $currency; $visitorDetailsArray['siteCurrencySymbol'] = @$currencies[$visitorDetailsArray['siteCurrency']]; $visitorDetailsArray['serverTimestamp'] = $visitorDetailsArray['lastActionTimestamp']; $dateTimeVisit = Date::factory($visitorDetailsArray['lastActionTimestamp'], $timezone); if ($dateTimeVisit) { $visitorDetailsArray['serverTimePretty'] = $dateTimeVisit->getLocalized(Date::TIME_FORMAT); $visitorDetailsArray['serverDatePretty'] = $dateTimeVisit->getLocalized(Date::DATE_FORMAT_LONG); } $dateTimeVisitFirstAction = Date::factory($visitorDetailsArray['firstActionTimestamp'], $timezone); $visitorDetailsArray['serverDatePrettyFirstAction'] = $dateTimeVisitFirstAction->getLocalized(Date::DATE_FORMAT_LONG); $visitorDetailsArray['serverTimePrettyFirstAction'] = $dateTimeVisitFirstAction->getLocalized(Date::TIME_FORMAT); $visitorDetailsArray['actionDetails'] = array(); if (!$doNotFetchActions) { $visitorDetailsArray = Visitor::enrichVisitorArrayWithActions($visitorDetailsArray, $actionsLimit, $timezone); } if ($flat) { $visitorDetailsArray = Visitor::flattenVisitorDetailsArray($visitorDetailsArray); } $visitorDetailRow->setColumns($visitorDetailsArray); } }); }
public static function getMinTimeProcessedForTemporaryArchive(Date $dateStart, \Piwik\Period $period, Segment $segment, Site $site) { $now = time(); $minimumArchiveTime = $now - Rules::getTodayArchiveTimeToLive(); $idSites = array($site->getId()); $isArchivingDisabled = Rules::isArchivingDisabledFor($idSites, $segment, $period->getLabel()); if ($isArchivingDisabled) { if ($period->getNumberOfSubperiods() == 0 && $dateStart->getTimestamp() <= $now) { // Today: accept any recent enough archive $minimumArchiveTime = false; } else { // This week, this month, this year: // accept any archive that was processed today after 00:00:01 this morning $timezone = $site->getTimezone(); $minimumArchiveTime = Date::factory(Date::factory('now', $timezone)->getDateStartUTC())->setTimezone($timezone)->getTimestamp(); } } return $minimumArchiveTime; }
protected function setDateTodayIfWebsiteCreatedToday() { $date = Common::getRequestVar('date', false); if ($date == 'today' || Common::getRequestVar('period', false) == 'range') { return; } $websiteId = Common::getRequestVar('idSite', false, 'int'); if ($websiteId) { $website = new Site($websiteId); $datetimeCreationDate = $website->getCreationDate()->getDatetime(); $creationDateLocalTimezone = Date::factory($datetimeCreationDate, $website->getTimezone())->toString('Y-m-d'); $todayLocalTimezone = Date::factory('now', $website->getTimezone())->toString('Y-m-d'); if ($creationDateLocalTimezone == $todayLocalTimezone) { Piwik::redirectToModule('CoreHome', 'index', array('date' => 'today', 'idSite' => $websiteId, 'period' => Common::getRequestVar('period'))); } } }
/** * Returns a date range string given a period type, end date and number of periods * the range spans over. * * @param string $period The sub period type, `'day'`, `'week'`, `'month'` and `'year'`. * @param int $lastN The number of periods of type `$period` that the result range should * span. * @param string $endDate The desired end date of the range. * @param \Piwik\Site $site The site whose timezone should be used. * @return string The date range string, eg, `'2012-01-02,2013-01-02'`. * @api */ public static function getRelativeToEndDate($period, $lastN, $endDate, $site) { $last30Relative = new Range($period, $lastN, $site->getTimezone()); $last30Relative->setDefaultEndDate(Date::factory($endDate)); $date = $last30Relative->getDateStart()->toString() . "," . $last30Relative->getDateEnd()->toString(); return $date; }
private function loadLastVisitorDetailsFromDatabase($idSite, $period, $date, $segment = false, $countVisitorsToFetch = 100, $visitorId = false, $minTimestamp = false, $filterSortOrder = false) { $where = $whereBind = array(); list($whereClause, $idSites) = $this->getIdSitesWhereClause($idSite); $where[] = $whereClause; $whereBind = $idSites; if (strtolower($filterSortOrder) !== 'asc') { $filterSortOrder = 'DESC'; } $orderBy = "idsite, visit_last_action_time " . $filterSortOrder; $orderByParent = "sub.visit_last_action_time " . $filterSortOrder; if (!empty($visitorId)) { $where[] = "log_visit.idvisitor = ? "; $whereBind[] = @Common::hex2bin($visitorId); } if (!empty($minTimestamp)) { $where[] = "log_visit.visit_last_action_time > ? "; $whereBind[] = date("Y-m-d H:i:s", $minTimestamp); } // If no other filter, only look at the last 24 hours of stats if (empty($visitorId) && empty($countVisitorsToFetch) && empty($period) && empty($date)) { $period = 'day'; $date = 'yesterdaySameTime'; } // SQL Filter with provided period if (!empty($period) && !empty($date)) { $currentSite = new Site($idSite); $currentTimezone = $currentSite->getTimezone(); $dateString = $date; if ($period == 'range') { $processedPeriod = new Range('range', $date); if ($parsedDate = Range::parseDateRange($date)) { $dateString = $parsedDate[2]; } } else { $processedDate = Date::factory($date); if ($date == 'today' || $date == 'now' || $processedDate->toString() == Date::factory('now', $currentTimezone)->toString()) { $processedDate = $processedDate->subDay(1); } $processedPeriod = Period\Factory::build($period, $processedDate); } $dateStart = $processedPeriod->getDateStart()->setTimezone($currentTimezone); $where[] = "log_visit.visit_last_action_time >= ?"; $whereBind[] = $dateStart->toString('Y-m-d H:i:s'); if (!in_array($date, array('now', 'today', 'yesterdaySameTime')) && strpos($date, 'last') === false && strpos($date, 'previous') === false && Date::factory($dateString)->toString('Y-m-d') != Date::factory('now', $currentTimezone)->toString()) { $dateEnd = $processedPeriod->getDateEnd()->setTimezone($currentTimezone); $where[] = " log_visit.visit_last_action_time <= ?"; $dateEndString = $dateEnd->addDay(1)->toString('Y-m-d H:i:s'); $whereBind[] = $dateEndString; } } if (count($where) > 0) { $where = join("\n\t\t\t\tAND ", $where); } else { $where = false; } $segment = new Segment($segment, $idSite); // Subquery to use the indexes for ORDER BY $select = "log_visit.*"; $from = "log_visit"; $subQuery = $segment->getSelectQuery($select, $from, $where, $whereBind, $orderBy); $sqlLimit = $countVisitorsToFetch >= 1 ? " LIMIT 0, " . (int) $countVisitorsToFetch : ""; // Group by idvisit so that a visitor converting 2 goals only appears once $sql = "\n\t\t\tSELECT sub.* FROM (\n\t\t\t\t" . $subQuery['sql'] . "\n\t\t\t\t{$sqlLimit}\n\t\t\t) AS sub\n\t\t\tGROUP BY sub.idvisit\n\t\t\tORDER BY {$orderByParent}\n\t\t"; try { $data = Db::fetchAll($sql, $subQuery['bind']); } catch (Exception $e) { echo $e->getMessage(); exit; } $dataTable = new DataTable(); $dataTable->addRowsFromSimpleArray($data); // $dataTable->disableFilter('Truncate'); if (!empty($data[0])) { $columnsToNotAggregate = array_map(function () { return 'skip'; }, $data[0]); $dataTable->setMetadata(DataTable::COLUMN_AGGREGATION_OPS_METADATA_NAME, $columnsToNotAggregate); } return $dataTable; }
/** * Assigns variables to {@link Piwik\View} instances that display an entire page. * * The following variables assigned: * * **date** - The value of the **date** query parameter. * **idSite** - The value of the **idSite** query parameter. * **rawDate** - The value of the **date** query parameter. * **prettyDate** - A pretty string description of the current period. * **siteName** - The current site's name. * **siteMainUrl** - The URL of the current site. * **startDate** - The start date of the current period. A {@link Piwik\Date} instance. * **endDate** - The end date of the current period. A {@link Piwik\Date} instance. * **language** - The current language's language code. * **config_action_url_category_delimiter** - The value of the `[General] action_url_category_delimiter` * INI config option. * **topMenu** - The result of `MenuTop::getInstance()->getMenu()`. * * As well as the variables set by {@link setPeriodVariablesView()}. * * Will exit on error. * * @param View $view * @return void * @api */ protected function setGeneralVariablesView($view) { $view->idSite = $this->idSite; $this->checkSitePermission(); $this->setPeriodVariablesView($view); $view->siteName = $this->site->getName(); $view->siteMainUrl = $this->site->getMainUrl(); $siteTimezone = $this->site->getTimezone(); $datetimeMinDate = $this->site->getCreationDate()->getDatetime(); $minDate = Date::factory($datetimeMinDate, $siteTimezone); $this->setMinDateView($minDate, $view); $maxDate = Date::factory('now', $siteTimezone); $this->setMaxDateView($maxDate, $view); $rawDate = Common::getRequestVar('date'); Period::checkDateFormat($rawDate); $periodStr = Common::getRequestVar('period'); if ($periodStr != 'range') { $date = Date::factory($this->strDate); $validDate = $this->getValidDate($date, $minDate, $maxDate); $period = Period\Factory::build($periodStr, $validDate); if ($date->toString() !== $validDate->toString()) { // we to not always change date since it could convert a strDate "today" to "YYYY-MM-DD" // only change $this->strDate if it was not valid before $this->setDate($validDate); } } else { $period = new Range($periodStr, $rawDate, $siteTimezone); } // Setting current period start & end dates, for pre-setting the calendar when "Date Range" is selected $dateStart = $period->getDateStart(); $dateStart = $this->getValidDate($dateStart, $minDate, $maxDate); $dateEnd = $period->getDateEnd(); $dateEnd = $this->getValidDate($dateEnd, $minDate, $maxDate); if ($periodStr == 'range') { // make sure we actually display the correct calendar pretty date $newRawDate = $dateStart->toString() . ',' . $dateEnd->toString(); $period = new Range($periodStr, $newRawDate, $siteTimezone); } $view->date = $this->strDate; $view->prettyDate = self::getCalendarPrettyDate($period); $view->prettyDateLong = $period->getLocalizedLongString(); $view->rawDate = $rawDate; $view->startDate = $dateStart; $view->endDate = $dateEnd; $language = LanguagesManager::getLanguageForSession(); $view->language = !empty($language) ? $language : LanguagesManager::getLanguageCodeForCurrentUser(); $this->setBasicVariablesView($view); $view->topMenu = MenuTop::getInstance()->getMenu(); $view->adminMenu = MenuAdmin::getInstance()->getMenu(); $notifications = $view->notifications; if (empty($notifications)) { $view->notifications = NotificationManager::getAllNotificationsToDisplay(); NotificationManager::cancelAllNonPersistent(); } }