public static function getArchiveIdAndVisits(Site $site, Period $period, Segment $segment, $minDatetimeArchiveProcessedUTC, $requestedPlugin) { $dateStart = $period->getDateStart(); $bindSQL = array($site->getId(), $dateStart->toString('Y-m-d'), $period->getDateEnd()->toString('Y-m-d'), $period->getId()); $timeStampWhere = ''; if ($minDatetimeArchiveProcessedUTC) { $timeStampWhere = " AND ts_archived >= ? "; $bindSQL[] = Date::factory($minDatetimeArchiveProcessedUTC)->getDatetime(); } $pluginOrVisitsSummary = array("VisitsSummary", $requestedPlugin); $pluginOrVisitsSummary = array_unique($pluginOrVisitsSummary); $sqlWhereArchiveName = self::getNameCondition($pluginOrVisitsSummary, $segment); $sqlQuery = "\tSELECT idarchive, value, name, date1 as startDate\n\t\t\t\t\t\tFROM " . ArchiveTableCreator::getNumericTable($dateStart) . "``\n\t\t\t\t\t\tWHERE idsite = ?\n\t\t\t\t\t\t\tAND date1 = ?\n\t\t\t\t\t\t\tAND date2 = ?\n\t\t\t\t\t\t\tAND period = ?\n\t\t\t\t\t\t\tAND ( ({$sqlWhereArchiveName})\n\t\t\t\t\t\t\t\t OR name = '" . self::NB_VISITS_RECORD_LOOKED_UP . "'\n\t\t\t\t\t\t\t\t OR name = '" . self::NB_VISITS_CONVERTED_RECORD_LOOKED_UP . "')\n\t\t\t\t\t\t\t{$timeStampWhere}\n\t\t\t\t\t\tORDER BY idarchive DESC"; $results = Db::fetchAll($sqlQuery, $bindSQL); 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 ($visits === false && $idArchive === false) { return false; } return array($idArchive, $visits, $visitsConverted); }
/** * 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 getRowEvolution($idSite, $period, $date, $apiModule, $apiAction, $label = false, $segment = false, $column = false, $language = false, $idGoal = false, $legendAppendMetric = true, $labelUseAbsoluteUrl = true) { // validation of requested $period & $date if ($period == 'range') { // load days in the range $period = 'day'; } if (!Period::isMultiplePeriod($date, $period)) { throw new Exception("Row evolutions can not be processed with this combination of \\'date\\' and \\'period\\' parameters."); } $label = ResponseBuilder::unsanitizeLabelParameter($label); $labels = Piwik::getArrayFromApiParameter($label); $metadata = $this->getRowEvolutionMetaData($idSite, $period, $date, $apiModule, $apiAction, $language, $idGoal); $dataTable = $this->loadRowEvolutionDataFromAPI($metadata, $idSite, $period, $date, $apiModule, $apiAction, $labels, $segment, $idGoal); if (empty($labels)) { $labels = $this->getLabelsFromDataTable($dataTable, $labels); $dataTable = $this->enrichRowAddMetadataLabelIndex($labels, $dataTable); } if (count($labels) != 1) { $data = $this->getMultiRowEvolution($dataTable, $metadata, $apiModule, $apiAction, $labels, $column, $legendAppendMetric, $labelUseAbsoluteUrl); } else { $data = $this->getSingleRowEvolution($idSite, $dataTable, $metadata, $apiModule, $apiAction, $labels[0], $labelUseAbsoluteUrl); } return $data; }
/** * Generates the subperiods (one for each day in the month) */ protected function generate() { if ($this->subperiodsProcessed) { return; } parent::generate(); $date = $this->date; $startMonth = $date->setDay(1)->setTime('00:00:00'); $endMonth = $startMonth->addPeriod(1, 'month')->setDay(1)->subDay(1); $this->processOptimalSubperiods($startMonth, $endMonth); }
/** * Generates the subperiods (one for each month of the year) */ protected function generate() { if ($this->subperiodsProcessed) { return; } parent::generate(); $year = $this->date->toString("Y"); for ($i = 1; $i <= 12; $i++) { $this->addSubperiod(new Month(Date::factory("{$year}-{$i}-01"))); } }
/** * Generates the subperiods (one for each day in the month) */ protected function generate() { if ($this->subperiodsProcessed) { return; } parent::generate(); $date = $this->date; $startMonth = $date->setDay(1); $currentDay = clone $startMonth; while ($currentDay->compareMonth($startMonth) == 0) { $this->addSubperiod(new Day($currentDay)); $currentDay = $currentDay->addDay(1); } }
/** * Generates the subperiods - one for each day in the week */ protected function generate() { if ($this->subperiodsProcessed) { return; } parent::generate(); $date = $this->date; if ($date->toString('N') > 1) { $date = $date->subDay($date->toString('N') - 1); } $startWeek = $date; $currentDay = clone $startWeek; while ($currentDay->compareWeek($startWeek) == 0) { $this->addSubperiod(new Day($currentDay)); $currentDay = $currentDay->addDay(1); } }
/** * General method to get transitions for an action * * @param $actionName * @param $actionType "url"|"title" * @param $idSite * @param $period * @param $date * @param bool $segment * @param bool $limitBeforeGrouping * @param string $parts * @return array * @throws Exception */ public function getTransitionsForAction($actionName, $actionType, $idSite, $period, $date, $segment = false, $limitBeforeGrouping = false, $parts = 'all') { Piwik::checkUserHasViewAccess($idSite); // get idaction of the requested action $idaction = $this->deriveIdAction($actionName, $actionType); if ($idaction < 0) { throw new Exception('NoDataForAction'); } // prepare log aggregator $segment = new Segment($segment, $idSite); $site = new Site($idSite); $period = Period::factory($period, $date); $params = new ArchiveProcessor\Parameters($site, $period, $segment); $logAggregator = new LogAggregator($params); // prepare the report $report = array('date' => Period::factory($period->getLabel(), $date)->getLocalizedShortString()); $partsArray = explode(',', $parts); if ($parts == 'all' || in_array('internalReferrers', $partsArray)) { $this->addInternalReferrers($logAggregator, $report, $idaction, $actionType, $limitBeforeGrouping); } if ($parts == 'all' || in_array('followingActions', $partsArray)) { $includeLoops = $parts != 'all' && !in_array('internalReferrers', $partsArray); $this->addFollowingActions($logAggregator, $report, $idaction, $actionType, $limitBeforeGrouping, $includeLoops); } if ($parts == 'all' || in_array('externalReferrers', $partsArray)) { $this->addExternalReferrers($logAggregator, $report, $idaction, $actionType, $limitBeforeGrouping); } // derive the number of exits from the other metrics if ($parts == 'all') { $report['pageMetrics']['exits'] = $report['pageMetrics']['pageviews'] - $this->getTotalTransitionsToFollowingActions() - $report['pageMetrics']['loops']; } // replace column names in the data tables $reportNames = array('previousPages' => true, 'previousSiteSearches' => false, 'followingPages' => true, 'followingSiteSearches' => false, 'outlinks' => true, 'downloads' => true); foreach ($reportNames as $reportName => $replaceLabel) { if (isset($report[$reportName])) { $columnNames = array(Metrics::INDEX_NB_ACTIONS => 'referrals'); if ($replaceLabel) { $columnNames[Metrics::INDEX_NB_ACTIONS] = 'referrals'; } $report[$reportName]->filter('ReplaceColumnNames', array($columnNames)); } } return $report; }
/** * Returns datatable describing the number of visits for each day of the week. * * @param string $idSite The site ID. Cannot refer to multiple sites. * @param string $period The period type: day, week, year, range... * @param string $date The start date of the period. Cannot refer to multiple dates. * @param bool|string $segment The segment. * @throws Exception * @return DataTable */ public function getByDayOfWeek($idSite, $period, $date, $segment = false) { Piwik::checkUserHasViewAccess($idSite); // metrics to query $metrics = Metrics::getVisitsMetricNames(); unset($metrics[Metrics::INDEX_MAX_ACTIONS]); // disabled for multiple dates if (Period::isMultiplePeriod($date, $period)) { throw new Exception("VisitTime.getByDayOfWeek does not support multiple dates."); } // get metric data for every day within the supplied period $oPeriod = Period\Factory::makePeriodFromQueryParams(Site::getTimezoneFor($idSite), $period, $date); $dateRange = $oPeriod->getDateStart()->toString() . ',' . $oPeriod->getDateEnd()->toString(); $archive = Archive::build($idSite, 'day', $dateRange, $segment); // disabled for multiple sites if (count($archive->getParams()->getIdSites()) > 1) { throw new Exception("VisitTime.getByDayOfWeek does not support multiple sites."); } $dataTable = $archive->getDataTableFromNumeric($metrics)->mergeChildren(); // if there's no data for this report, don't bother w/ anything else if ($dataTable->getRowsCount() == 0) { return $dataTable; } // group by the day of the week (see below for dayOfWeekFromDate function) $dataTable->filter('GroupBy', array('label', __NAMESPACE__ . '\\dayOfWeekFromDate')); // create new datatable w/ empty rows, then add calculated datatable $rows = array(); foreach (array(1, 2, 3, 4, 5, 6, 7) as $day) { $rows[] = array('label' => $day, 'nb_visits' => 0); } $result = new DataTable(); $result->addRowsFromSimpleArray($rows); $result->addDataTable($dataTable); // set day of week integer as metadata $result->filter('ColumnCallbackAddMetadata', array('label', 'day_of_week')); // translate labels $result->filter('ColumnCallbackReplace', array('label', __NAMESPACE__ . '\\translateDayOfWeek')); // set datatable metadata for period start & finish $result->setMetadata('date_start', $oPeriod->getDateStart()); $result->setMetadata('date_end', $oPeriod->getDateEnd()); return $result; }
/** * Creates a new Period instance with a period ID and {@link Date} instance. * * _Note: This method cannot create {@link Period\Range} periods._ * * @param string $period `"day"`, `"week"`, `"month"`, `"year"`, `"range"`. * @param Date|string $date A date within the period or the range of dates. * @param Date|string $timezone Optional timezone that will be used only when $period is 'range' or $date is 'last|previous' * @throws Exception If `$strPeriod` is invalid. * @return \Piwik\Period */ public static function build($period, $date, $timezone = 'UTC') { self::checkPeriodIsEnabled($period); if (is_string($date)) { if (Period::isMultiplePeriod($date, $period) || $period == 'range') { return new Range($period, $date, $timezone); } $date = Date::factory($date); } switch ($period) { case 'day': return new Day($date); break; case 'week': return new Week($date); break; case 'month': return new Month($date); break; case 'year': return new Year($date); break; } }
/** * Adds new subperiods * * @param Date $startDate * @param Date $endDate * @param string $period */ protected function fillArraySubPeriods($startDate, $endDate, $period) { $arrayPeriods = array(); $endSubperiod = Period::factory($period, $endDate); $arrayPeriods[] = $endSubperiod; // set end date to start of end period since we're comparing against start date. $endDate = $endSubperiod->getDateStart(); while ($endDate->isLater($startDate)) { $endDate = $endDate->addPeriod(-1, $period); $subPeriod = Period::factory($period, $endDate); $arrayPeriods[] = $subPeriod; } $arrayPeriods = array_reverse($arrayPeriods); foreach ($arrayPeriods as $period) { $this->addSubperiod($period); } }
/** * Returns `true` if this instance will request a single DataTable, `false` if requesting * more than one. * * @return bool */ public function isRequestingSingleDataTable() { $requestArray = $this->request->getRequestArray() + $_GET + $_POST; $date = Common::getRequestVar('date', null, 'string', $requestArray); $period = Common::getRequestVar('period', null, 'string', $requestArray); $idSite = Common::getRequestVar('idSite', null, 'string', $requestArray); if (Period::isMultiplePeriod($date, $period) || strpos($idSite, ',') !== false || $idSite == 'all') { return false; } return true; }
/** * @expectedException \Exception * @expectedExceptionMessage General_ExceptionInvalidDateFormat * @dataProvider getInvalidDateFormats */ public function testValidate_InvalidDates($invalidDateFormat) { Period::checkDateFormat($invalidDateFormat); }
/** * @param $archiveGroups * @param $site * @param $period */ private function prepareArchive(array $archiveGroups, Site $site, Period $period) { $parameters = new ArchiveProcessor\Parameters($site, $period, $this->params->getSegment(), $this->params->isSkipAggregationOfSubTables()); $archiveLoader = new ArchiveProcessor\Loader($parameters); $periodString = $period->getRangeString(); // process for each plugin as well foreach ($archiveGroups as $plugin) { $doneFlag = $this->getDoneStringForPlugin($plugin); $this->initializeArchiveIdCache($doneFlag); $idArchive = $archiveLoader->prepareArchive($plugin); if ($idArchive) { $this->idarchives[$doneFlag][$periodString][] = $idArchive; } } }
private function makeSureToWorkOnFirstLevelDataTable($table) { if (!array_key_exists('idSubtable', $this->request)) { return $table; } $firstLevelReport = $this->findFirstLevelReport(); if (empty($firstLevelReport)) { // it is not a subtable report $module = $this->apiModule; $action = $this->apiMethod; } else { $module = $firstLevelReport->getModule(); $action = $firstLevelReport->getAction(); } $request = $this->request; /** @var \Piwik\Period $period */ $period = $table->getMetadata('period'); if (!empty($period)) { // we want a dataTable, not a dataTable\map if (Period::isMultiplePeriod($request['date'], $request['period']) || 'range' == $period->getLabel()) { $request['date'] = $period->getRangeString(); $request['period'] = 'range'; } else { $request['date'] = $period->getDateStart()->toString(); $request['period'] = $period->getLabel(); } } $table = $this->callApiAndReturnDataTable($module, $action, $request); if ($table instanceof DataTable\Map) { $table = $table->mergeChildren(); } return $table; }
/** * Returns start & end dates for the range described by a period and optional lastN * argument. * * @param string|bool $date The start date of the period (or the date range of a range * period). * @param string $period The period type ('day', 'week', 'month', 'year' or 'range'). * @param bool|int $lastN Whether to include the last N periods in the range or not. * Ignored if period == range. * * @return Date[] array of Date objects or array(false, false) * @ignore */ public static function getDateRangeForPeriod($date, $period, $lastN = false) { if ($date === false) { return array(false, false); } // if the range is just a normal period (or the period is a range in which case lastN is ignored) if ($lastN === false || $period == 'range') { if ($period == 'range') { $oPeriod = new Range('day', $date); } else { $oPeriod = Period::factory($period, Date::factory($date)); } $startDate = $oPeriod->getDateStart(); $endDate = $oPeriod->getDateEnd(); } else { list($date, $lastN) = EvolutionViz::getDateRangeAndLastN($period, $date, $lastN); list($startDate, $endDate) = explode(',', $date); $startDate = Date::factory($startDate); $endDate = Date::factory($endDate); } return array($startDate, $endDate); }
/** * Returns a prettified date string for use in period selector widget. * * @param Period $period The period to return a pretty string for. * @return string * @api */ public static function getCalendarPrettyDate($period) { if ($period instanceof Month) { // show month name when period is for a month return $period->getLocalizedLongString(); } else { return $period->getPrettyString(); } }
/** * Returns a prettified date string for use in period selector widget. * * @param Period $period The period to return a pretty string for. * @return string * @api */ public static function getCalendarPrettyDate($period) { if ($period instanceof Month) { return $period->getLocalizedLongString(); } else { return $period->getPrettyString(); } }
public function getProcessedReport($idSite, $period, $date, $apiModule, $apiAction, $segment = false, $apiParameters = false, $idGoal = false, $language = false, $showTimer = true, $hideMetricsDoc = false, $idSubtable = false, $showRawMetrics = false) { $timer = new Timer(); if (empty($apiParameters)) { $apiParameters = array(); } if (!empty($idGoal) && empty($apiParameters['idGoal'])) { $apiParameters['idGoal'] = $idGoal; } // Is this report found in the Metadata available reports? $reportMetadata = $this->getMetadata($idSite, $apiModule, $apiAction, $apiParameters, $language, $period, $date, $hideMetricsDoc, $showSubtableReports = true); if (empty($reportMetadata)) { throw new Exception("Requested report {$apiModule}.{$apiAction} for Website id={$idSite} not found in the list of available reports. \n"); } $reportMetadata = reset($reportMetadata); // Generate Api call URL passing custom parameters $parameters = array_merge($apiParameters, array('method' => $apiModule . '.' . $apiAction, 'idSite' => $idSite, 'period' => $period, 'date' => $date, 'format' => 'original', 'serialize' => '0', 'language' => $language, 'idSubtable' => $idSubtable)); if (!empty($segment)) { $parameters['segment'] = $segment; } $url = Url::getQueryStringFromParameters($parameters); $request = new Request($url); try { /** @var DataTable */ $dataTable = $request->process(); } catch (Exception $e) { throw new Exception("API returned an error: " . $e->getMessage() . " at " . basename($e->getFile()) . ":" . $e->getLine() . "\n"); } list($newReport, $columns, $rowsMetadata, $totals) = $this->handleTableReport($idSite, $dataTable, $reportMetadata, $showRawMetrics); foreach ($columns as $columnId => &$name) { $name = ucfirst($name); } $website = new Site($idSite); $period = Period::factory($period, $date); $period = $period->getLocalizedLongString(); $return = array('website' => $website->getName(), 'prettyDate' => $period, 'metadata' => $reportMetadata, 'columns' => $columns, 'reportData' => $newReport, 'reportMetadata' => $rowsMetadata, 'reportTotal' => $totals); if ($showTimer) { $return['timerMillis'] = $timer->getTimeMs(0); } return $return; }
/** * Sends the http headers for csv file */ protected function renderHeader() { $fileName = 'Piwik ' . Piwik::translate('General_Export'); $period = Common::getRequestVar('period', false); $date = Common::getRequestVar('date', false); if ($period || $date) { if ($period == 'range') { $period = new Range($period, $date); } else { if (strpos($date, ',') !== false) { $period = new Range('range', $date); } else { $period = Period::factory($period, Date::factory($date)); } } $prettyDate = $period->getLocalizedLongString(); $meta = $this->getApiMetaData(); $fileName .= ' _ ' . $meta['name'] . ' _ ' . $prettyDate . '.csv'; } // silent fail otherwise unit tests fail @header('Content-Type: application/vnd.ms-excel'); @header('Content-Disposition: attachment; filename="' . $fileName . '"'); ProxyHttp::overrideCacheControlHeaders(); }
/** * @param array $reports * @param array $info * @return mixed */ public function getReportMetadata(&$reports, $info) { $idSites = $info['idSites']; // If only one website is selected, we add the Graph URL if (count($idSites) != 1) { return; } $idSite = reset($idSites); // in case API.getReportMetadata was not called with date/period we use sane defaults if (empty($info['period'])) { $info['period'] = 'day'; } if (empty($info['date'])) { $info['date'] = 'today'; } // need two sets of period & date, one for single period graphs, one for multiple periods graphs if (Period::isMultiplePeriod($info['date'], $info['period'])) { $periodForMultiplePeriodGraph = $info['period']; $dateForMultiplePeriodGraph = $info['date']; $periodForSinglePeriodGraph = 'range'; $dateForSinglePeriodGraph = $info['date']; } else { $periodForSinglePeriodGraph = $info['period']; $dateForSinglePeriodGraph = $info['date']; $piwikSite = new Site($idSite); if ($periodForSinglePeriodGraph == 'range') { $periodForMultiplePeriodGraph = Config::getInstance()->General['graphs_default_period_to_plot_when_period_range']; $dateForMultiplePeriodGraph = $dateForSinglePeriodGraph; } else { $periodForMultiplePeriodGraph = $periodForSinglePeriodGraph; $dateForMultiplePeriodGraph = Range::getRelativeToEndDate($periodForSinglePeriodGraph, 'last' . self::GRAPH_EVOLUTION_LAST_PERIODS, $dateForSinglePeriodGraph, $piwikSite); } } $token_auth = Common::getRequestVar('token_auth', false); $urlPrefix = "index.php?"; foreach ($reports as &$report) { $reportModule = $report['module']; $reportAction = $report['action']; $reportUniqueId = $reportModule . '_' . $reportAction; $parameters = array(); $parameters['module'] = 'API'; $parameters['method'] = 'ImageGraph.get'; $parameters['idSite'] = $idSite; $parameters['apiModule'] = $reportModule; $parameters['apiAction'] = $reportAction; if (!empty($token_auth)) { $parameters['token_auth'] = $token_auth; } // Forward custom Report parameters to the graph URL if (!empty($report['parameters'])) { $parameters = array_merge($parameters, $report['parameters']); } if (empty($report['dimension'])) { $parameters['period'] = $periodForMultiplePeriodGraph; $parameters['date'] = $dateForMultiplePeriodGraph; } else { $parameters['period'] = $periodForSinglePeriodGraph; $parameters['date'] = $dateForSinglePeriodGraph; } // add the idSubtable if it exists $idSubtable = Common::getRequestVar('idSubtable', false); if ($idSubtable !== false) { $parameters['idSubtable'] = $idSubtable; } if (!empty($_GET['_restrictSitesToLogin']) && TaskScheduler::isTaskBeingExecuted()) { $parameters['_restrictSitesToLogin'] = $_GET['_restrictSitesToLogin']; } $report['imageGraphUrl'] = $urlPrefix . Url::getQueryStringFromParameters($parameters); // thanks to API.getRowEvolution, reports with dimensions can now be plotted using an evolution graph // however, most reports with a fixed set of dimension values are excluded // this is done so Piwik Mobile and Scheduled Reports do not display them $reportWithDimensionsSupportsEvolution = empty($report['constantRowsCount']) || in_array($reportUniqueId, self::$CONSTANT_ROW_COUNT_REPORT_EXCEPTIONS); $reportSupportsEvolution = !in_array($reportUniqueId, self::$REPORTS_DISABLED_EVOLUTION_GRAPH); if ($reportSupportsEvolution && $reportWithDimensionsSupportsEvolution) { $parameters['period'] = $periodForMultiplePeriodGraph; $parameters['date'] = $dateForMultiplePeriodGraph; $report['imageGraphEvolutionUrl'] = $urlPrefix . Url::getQueryStringFromParameters($parameters); } } }
public function get($idSite, $period, $date, $apiModule, $apiAction, $graphType = false, $outputType = API::GRAPH_OUTPUT_INLINE, $columns = false, $labels = false, $showLegend = true, $width = false, $height = false, $fontSize = API::DEFAULT_FONT_SIZE, $legendFontSize = false, $aliasedGraph = true, $idGoal = false, $colors = false, $textColor = API::DEFAULT_TEXT_COLOR, $backgroundColor = API::DEFAULT_BACKGROUND_COLOR, $gridColor = API::DEFAULT_GRID_COLOR, $idSubtable = false, $legendAppendMetric = true, $segment = false) { Piwik::checkUserHasViewAccess($idSite); // Health check - should we also test for GD2 only? if (!SettingsServer::isGdExtensionEnabled()) { throw new Exception('Error: To create graphs in Piwik, please enable GD php extension (with Freetype support) in php.ini, and restart your web server.'); } $useUnicodeFont = array('am', 'ar', 'el', 'fa', 'fi', 'he', 'ja', 'ka', 'ko', 'te', 'th', 'zh-cn', 'zh-tw'); $languageLoaded = Translate::getLanguageLoaded(); $font = self::getFontPath(self::DEFAULT_FONT); if (in_array($languageLoaded, $useUnicodeFont)) { $unicodeFontPath = self::getFontPath(self::UNICODE_FONT); $font = file_exists($unicodeFontPath) ? $unicodeFontPath : $font; } // save original GET to reset after processing. Important for API-in-API-call $savedGET = $_GET; try { $apiParameters = array(); if (!empty($idGoal)) { $apiParameters = array('idGoal' => $idGoal); } // Fetch the metadata for given api-action $metadata = APIMetadata::getInstance()->getMetadata($idSite, $apiModule, $apiAction, $apiParameters, $languageLoaded, $period, $date, $hideMetricsDoc = false, $showSubtableReports = true); if (!$metadata) { throw new Exception('Invalid API Module and/or API Action'); } $metadata = $metadata[0]; $reportHasDimension = !empty($metadata['dimension']); $constantRowsCount = !empty($metadata['constantRowsCount']); $isMultiplePeriod = Period::isMultiplePeriod($date, $period); if (!$reportHasDimension && !$isMultiplePeriod) { throw new Exception('The graph cannot be drawn for this combination of \'date\' and \'period\' parameters.'); } if (empty($legendFontSize)) { $legendFontSize = (int) $fontSize + self::DEFAULT_LEGEND_FONT_SIZE_OFFSET; } if (empty($graphType)) { if ($isMultiplePeriod) { $graphType = StaticGraph::GRAPH_TYPE_BASIC_LINE; } else { if ($constantRowsCount) { $graphType = StaticGraph::GRAPH_TYPE_VERTICAL_BAR; } else { $graphType = StaticGraph::GRAPH_TYPE_HORIZONTAL_BAR; } } $reportUniqueId = $metadata['uniqueId']; if (isset(self::$DEFAULT_GRAPH_TYPE_OVERRIDE[$reportUniqueId][$isMultiplePeriod])) { $graphType = self::$DEFAULT_GRAPH_TYPE_OVERRIDE[$reportUniqueId][$isMultiplePeriod]; } } else { $availableGraphTypes = StaticGraph::getAvailableStaticGraphTypes(); if (!in_array($graphType, $availableGraphTypes)) { throw new Exception(Piwik::translate('General_ExceptionInvalidStaticGraphType', array($graphType, implode(', ', $availableGraphTypes)))); } } $width = (int) $width; $height = (int) $height; if (empty($width)) { $width = self::$DEFAULT_PARAMETERS[$graphType][self::WIDTH_KEY]; } if (empty($height)) { $height = self::$DEFAULT_PARAMETERS[$graphType][self::HEIGHT_KEY]; } // Cap width and height to a safe amount $width = min($width, self::MAX_WIDTH); $height = min($height, self::MAX_HEIGHT); $reportColumns = array_merge(!empty($metadata['metrics']) ? $metadata['metrics'] : array(), !empty($metadata['processedMetrics']) ? $metadata['processedMetrics'] : array(), !empty($metadata['metricsGoal']) ? $metadata['metricsGoal'] : array(), !empty($metadata['processedMetricsGoal']) ? $metadata['processedMetricsGoal'] : array()); $ordinateColumns = array(); if (empty($columns)) { $ordinateColumns[] = empty($reportColumns[self::DEFAULT_ORDINATE_METRIC]) ? key($metadata['metrics']) : self::DEFAULT_ORDINATE_METRIC; } else { $ordinateColumns = explode(',', $columns); foreach ($ordinateColumns as $column) { if (empty($reportColumns[$column])) { throw new Exception(Piwik::translate('ImageGraph_ColumnOrdinateMissing', array($column, implode(',', array_keys($reportColumns))))); } } } $ordinateLabels = array(); foreach ($ordinateColumns as $column) { $ordinateLabels[$column] = $reportColumns[$column]; } // sort and truncate filters $defaultFilterTruncate = self::$DEFAULT_PARAMETERS[$graphType][self::TRUNCATE_KEY]; switch ($graphType) { case StaticGraph::GRAPH_TYPE_3D_PIE: case StaticGraph::GRAPH_TYPE_BASIC_PIE: if (count($ordinateColumns) > 1) { // pChart doesn't support multiple series on pie charts throw new Exception("Pie charts do not currently support multiple series"); } $_GET['filter_sort_column'] = reset($ordinateColumns); $this->setFilterTruncate($defaultFilterTruncate); break; case StaticGraph::GRAPH_TYPE_VERTICAL_BAR: case StaticGraph::GRAPH_TYPE_BASIC_LINE: if (!$isMultiplePeriod && !$constantRowsCount) { $this->setFilterTruncate($defaultFilterTruncate); } break; } $ordinateLogos = array(); // row evolutions if ($isMultiplePeriod && $reportHasDimension) { $plottedMetric = reset($ordinateColumns); // when no labels are specified, getRowEvolution returns the top N=filter_limit row evolutions // rows are sorted using filter_sort_column (see DataTableGenericFilter for more info) if (!$labels) { $savedFilterSortColumnValue = Common::getRequestVar('filter_sort_column', ''); $_GET['filter_sort_column'] = $plottedMetric; $savedFilterLimitValue = Common::getRequestVar('filter_limit', -1, 'int'); if ($savedFilterLimitValue == -1 || $savedFilterLimitValue > self::MAX_NB_ROW_LABELS) { $_GET['filter_limit'] = self::DEFAULT_NB_ROW_EVOLUTIONS; } } $processedReport = APIMetadata::getInstance()->getRowEvolution($idSite, $period, $date, $apiModule, $apiAction, $labels, $segment, $plottedMetric, $languageLoaded, $idGoal, $legendAppendMetric, $labelUseAbsoluteUrl = false); //@review this test will need to be updated after evaluating the @review comment in API/API.php if (!$processedReport) { throw new Exception(Piwik::translate('General_NoDataForGraph')); } // restoring generic filter parameters if (!$labels) { $_GET['filter_sort_column'] = $savedFilterSortColumnValue; if ($savedFilterLimitValue != -1) { $_GET['filter_limit'] = $savedFilterLimitValue; } } // retrieve metric names & labels $metrics = $processedReport['metadata']['metrics']; $ordinateLabels = array(); // getRowEvolution returned more than one label if (!array_key_exists($plottedMetric, $metrics)) { $ordinateColumns = array(); $i = 0; foreach ($metrics as $metric => $info) { $ordinateColumn = $plottedMetric . '_' . $i++; $ordinateColumns[] = $metric; $ordinateLabels[$ordinateColumn] = $info['name']; if (isset($info['logo'])) { $ordinateLogo = $info['logo']; // @review pChart does not support gifs in graph legends, would it be possible to convert all plugin pictures (cookie.gif, flash.gif, ..) to png files? if (!strstr($ordinateLogo, '.gif')) { $absoluteLogoPath = self::getAbsoluteLogoPath($ordinateLogo); if (file_exists($absoluteLogoPath)) { $ordinateLogos[$ordinateColumn] = $absoluteLogoPath; } } } } } else { $ordinateLabels[$plottedMetric] = $processedReport['label'] . ' (' . $metrics[$plottedMetric]['name'] . ')'; } } else { $processedReport = APIMetadata::getInstance()->getProcessedReport($idSite, $period, $date, $apiModule, $apiAction, $segment, $apiParameters = false, $idGoal, $languageLoaded, $showTimer = true, $hideMetricsDoc = false, $idSubtable, $showRawMetrics = false); } // prepare abscissa and ordinate series $abscissaSeries = array(); $abscissaLogos = array(); $ordinateSeries = array(); /** @var \Piwik\DataTable\Simple|\Piwik\DataTable\Map $reportData */ $reportData = $processedReport['reportData']; $hasData = false; $hasNonZeroValue = false; if (!$isMultiplePeriod) { $reportMetadata = $processedReport['reportMetadata']->getRows(); $i = 0; // $reportData instanceof DataTable foreach ($reportData->getRows() as $row) { // $row instanceof Row $rowData = $row->getColumns(); // Associative Array $abscissaSeries[] = Common::unsanitizeInputValue($rowData['label']); foreach ($ordinateColumns as $column) { $parsedOrdinateValue = $this->parseOrdinateValue($rowData[$column]); $hasData = true; if ($parsedOrdinateValue != 0) { $hasNonZeroValue = true; } $ordinateSeries[$column][] = $parsedOrdinateValue; } if (isset($reportMetadata[$i])) { $rowMetadata = $reportMetadata[$i]->getColumns(); if (isset($rowMetadata['logo'])) { $absoluteLogoPath = self::getAbsoluteLogoPath($rowMetadata['logo']); if (file_exists($absoluteLogoPath)) { $abscissaLogos[$i] = $absoluteLogoPath; } } } $i++; } } else { // $periodsData instanceof Simple[] $periodsData = array_values($reportData->getDataTables()); $periodsCount = count($periodsData); for ($i = 0; $i < $periodsCount; $i++) { // $periodsData[$i] instanceof Simple // $rows instanceof Row[] if (empty($periodsData[$i])) { continue; } $rows = $periodsData[$i]->getRows(); if (array_key_exists(0, $rows)) { $rowData = $rows[0]->getColumns(); // associative Array foreach ($ordinateColumns as $column) { $ordinateValue = $rowData[$column]; $parsedOrdinateValue = $this->parseOrdinateValue($ordinateValue); $hasData = true; if (!empty($parsedOrdinateValue)) { $hasNonZeroValue = true; } $ordinateSeries[$column][] = $parsedOrdinateValue; } } else { foreach ($ordinateColumns as $column) { $ordinateSeries[$column][] = 0; } } $rowId = $periodsData[$i]->getMetadata(DataTableFactory::TABLE_METADATA_PERIOD_INDEX)->getLocalizedShortString(); $abscissaSeries[] = Common::unsanitizeInputValue($rowId); } } if (!$hasData || !$hasNonZeroValue) { throw new Exception(Piwik::translate('General_NoDataForGraph')); } //Setup the graph $graph = StaticGraph::factory($graphType); $graph->setWidth($width); $graph->setHeight($height); $graph->setFont($font); $graph->setFontSize($fontSize); $graph->setLegendFontSize($legendFontSize); $graph->setOrdinateLabels($ordinateLabels); $graph->setShowLegend($showLegend); $graph->setAliasedGraph($aliasedGraph); $graph->setAbscissaSeries($abscissaSeries); $graph->setAbscissaLogos($abscissaLogos); $graph->setOrdinateSeries($ordinateSeries); $graph->setOrdinateLogos($ordinateLogos); $graph->setColors(!empty($colors) ? explode(',', $colors) : array()); $graph->setTextColor($textColor); $graph->setBackgroundColor($backgroundColor); $graph->setGridColor($gridColor); // when requested period is day, x-axis unit is time and all date labels can not be displayed // within requested width, force labels to be skipped every 6 days to delimit weeks if ($period == 'day' && $isMultiplePeriod) { $graph->setForceSkippedLabels(6); } // render graph $graph->renderGraph(); } catch (\Exception $e) { $graph = new \Piwik\Plugins\ImageGraph\StaticGraph\Exception(); $graph->setWidth($width); $graph->setHeight($height); $graph->setFont($font); $graph->setFontSize($fontSize); $graph->setBackgroundColor($backgroundColor); $graph->setTextColor($textColor); $graph->setException($e); $graph->renderGraph(); } // restoring get parameters $_GET = $savedGET; switch ($outputType) { case self::GRAPH_OUTPUT_FILE: if ($idGoal != '') { $idGoal = '_' . $idGoal; } $fileName = self::$DEFAULT_PARAMETERS[$graphType][self::FILENAME_KEY] . '_' . $apiModule . '_' . $apiAction . $idGoal . ' ' . str_replace(',', '-', $date) . ' ' . $idSite . '.png'; $fileName = str_replace(array(' ', '/'), '_', $fileName); if (!Filesystem::isValidFilename($fileName)) { throw new Exception('Error: Image graph filename ' . $fileName . ' is not valid.'); } return $graph->sendToDisk($fileName); case self::GRAPH_OUTPUT_PHP: return $graph->getRenderedImage(); case self::GRAPH_OUTPUT_INLINE: default: $graph->sendToBrowser(); exit; } }
public function getSitesInfo($isWidgetized = false) { Piwik::checkUserHasSomeViewAccess(); $displayRevenueColumn = Common::isGoalPluginEnabled(); $date = Common::getRequestVar('date', 'today'); $period = Common::getRequestVar('period', 'day'); $siteIds = APISitesManager::getInstance()->getSitesIdWithAtLeastViewAccess(); list($minDate, $maxDate) = Site::getMinMaxDateAcrossWebsites($siteIds); // overwrites the default Date set in the parent controller // Instead of the default current website's local date, // we set "today" or "yesterday" based on the default Piwik timezone $piwikDefaultTimezone = APISitesManager::getInstance()->getDefaultTimezone(); if ($period != 'range') { $date = $this->getDateParameterInTimezone($date, $piwikDefaultTimezone); $this->setDate($date); $date = $date->toString(); } $dataTable = APIMultiSites::getInstance()->getAll($period, $date, $segment = false); // put data into a form the template will understand better $digestableData = array(); foreach ($siteIds as $idSite) { $isEcommerceEnabled = Site::isEcommerceEnabledFor($idSite); $digestableData[$idSite] = array('idsite' => $idSite, 'main_url' => Site::getMainUrlFor($idSite), 'name' => Site::getNameFor($idSite), 'visits' => 0, 'pageviews' => 0); if ($period != 'range') { $digestableData[$idSite]['visits_evolution'] = 0; $digestableData[$idSite]['pageviews_evolution'] = 0; } if ($displayRevenueColumn) { $revenueDefault = $isEcommerceEnabled ? 0 : "'-'"; if ($period != 'range') { $digestableData[$idSite]['revenue_evolution'] = $revenueDefault; } } } foreach ($dataTable->getRows() as $row) { $idsite = (int) $row->getMetadata('idsite'); $site =& $digestableData[$idsite]; $site['visits'] = (int) $row->getColumn('nb_visits'); $site['pageviews'] = (int) $row->getColumn('nb_pageviews'); if ($displayRevenueColumn) { if ($row->getColumn('revenue') !== false) { $site['revenue'] = $row->getColumn('revenue'); } } if ($period != 'range') { $site['visits_evolution'] = $row->getColumn('visits_evolution'); $site['pageviews_evolution'] = $row->getColumn('pageviews_evolution'); if ($displayRevenueColumn) { $site['revenue_evolution'] = $row->getColumn('revenue_evolution'); } } } $this->applyPrettyMoney($digestableData); $view = new View("@MultiSites/getSitesInfo"); $view->isWidgetized = $isWidgetized; $view->sitesData = array_values($digestableData); $view->evolutionBy = $this->evolutionBy; $view->period = $period; $view->page = $this->page; $view->limit = $this->limit; $view->orderBy = $this->orderBy; $view->order = $this->order; $view->totalVisits = $dataTable->getMetadata('total_nb_visits'); $view->totalRevenue = $dataTable->getMetadata('total_revenue'); $view->displayRevenueColumn = $displayRevenueColumn; $view->totalPageviews = $dataTable->getMetadata('total_nb_pageviews'); $view->pastTotalVisits = $dataTable->getMetadata('last_period_total_nb_visits'); $view->totalVisitsEvolution = $dataTable->getMetadata('total_visits_evolution'); if ($view->totalVisitsEvolution > 0) { $view->totalVisitsEvolution = '+' . $view->totalVisitsEvolution; } if ($period != 'range') { $lastPeriod = Period::factory($period, $dataTable->getMetadata('last_period_date')); $view->pastPeriodPretty = self::getCalendarPrettyDate($lastPeriod); } $params = $this->getGraphParamsModified(); $view->dateSparkline = $period == 'range' ? $date : $params['date']; $view->autoRefreshTodayReport = false; // if the current date is today, or yesterday, // in case the website is set to UTC-12), or today in UTC+14, we refresh the page every 5min if (in_array($date, array('today', date('Y-m-d'), 'yesterday', Date::factory('yesterday')->toString('Y-m-d'), Date::factory('now', 'UTC+14')->toString('Y-m-d')))) { $view->autoRefreshTodayReport = Config::getInstance()->General['multisites_refresh_after_seconds']; } $this->setGeneralVariablesView($view); $this->setMinDateView($minDate, $view); $this->setMaxDateView($maxDate, $view); $view->show_sparklines = Config::getInstance()->General['show_multisites_sparklines']; return $view->render(); }
/** * Returns the end date of the period. * * @return null|Date */ public function getDateEnd() { if (!is_null($this->endDate)) { return $this->endDate; } return parent::getDateEnd(); }
private function markReportAsSent($report, Period $period) { $key = self::OPTION_KEY_LAST_SENT_DATERANGE . $report['idreport']; Option::set($key, $period->getRangeString()); }
protected static function makeLockName($idsite, Period $period, Segment $segment) { $config = Config::getInstance(); $lockName = 'piwik.' . $config->database['dbname'] . '.' . $config->database['tables_prefix'] . '/' . $idsite . '/' . (!$segment->isEmpty() ? $segment->getHash() . '/' : '') . $period->getId() . '/' . $period->getDateStart()->toString('Y-m-d') . ',' . $period->getDateEnd()->toString('Y-m-d'); return $lockName . '/' . md5($lockName . SettingsPiwik::getSalt()); }
private static function getDateRangeForFooterMessage() { // get query params $idSite = Common::getRequestVar('idSite', false); $date = Common::getRequestVar('date', false); $period = Common::getRequestVar('period', false); // create a period instance try { $oPeriod = Period::makePeriodFromQueryParams(Site::getTimezoneFor($idSite), $period, $date); } catch (Exception $ex) { return ''; // if query params are incorrect, forget about the footer message } // set the footer message using the period start & end date $start = $oPeriod->getDateStart()->toString(); $end = $oPeriod->getDateEnd()->toString(); if ($start == $end) { $dateRange = $start; } else { $dateRange = $start . " – " . $end; } return $dateRange; }
/** * @param array $reports * @param array $info * @return mixed */ public function getReportMetadata(&$reports, $info) { $idSites = $info['idSites']; // If only one website is selected, we add the Graph URL if (count($idSites) != 1) { return; } $idSite = reset($idSites); // in case API.getReportMetadata was not called with date/period we use sane defaults if (empty($info['period'])) { $info['period'] = 'day'; } if (empty($info['date'])) { $info['date'] = 'today'; } // need two sets of period & date, one for single period graphs, one for multiple periods graphs if (Period::isMultiplePeriod($info['date'], $info['period'])) { $periodForMultiplePeriodGraph = $info['period']; $dateForMultiplePeriodGraph = $info['date']; $periodForSinglePeriodGraph = 'range'; $dateForSinglePeriodGraph = $info['date']; } else { $periodForSinglePeriodGraph = $info['period']; $dateForSinglePeriodGraph = $info['date']; $piwikSite = new Site($idSite); if ($periodForSinglePeriodGraph == 'range') { // for period=range, show the configured sub-periods $periodForMultiplePeriodGraph = Config::getInstance()->General['graphs_default_period_to_plot_when_period_range']; $dateForMultiplePeriodGraph = $dateForSinglePeriodGraph; } else { if ($info['period'] == 'day' || !Config::getInstance()->General['graphs_show_evolution_within_selected_period']) { // for period=day, always show the last n days // if graphs_show_evolution_within_selected_period=false, show the last n periods $periodForMultiplePeriodGraph = $periodForSinglePeriodGraph; $dateForMultiplePeriodGraph = Range::getRelativeToEndDate($periodForSinglePeriodGraph, 'last' . self::GRAPH_EVOLUTION_LAST_PERIODS, $dateForSinglePeriodGraph, $piwikSite); } else { // if graphs_show_evolution_within_selected_period=true, show the days within the period // (except if the period is day, see above) $periodForMultiplePeriodGraph = 'day'; $period = PeriodFactory::build($info['period'], $info['date']); $start = $period->getDateStart()->toString(); $end = $period->getDateEnd()->toString(); $dateForMultiplePeriodGraph = $start . ',' . $end; } } } $token_auth = Common::getRequestVar('token_auth', false); $segment = Request::getRawSegmentFromRequest(); /** @var Scheduler $scheduler */ $scheduler = StaticContainer::getContainer()->get('Piwik\\Scheduler\\Scheduler'); $isRunningTask = $scheduler->isRunningTask(); // add the idSubtable if it exists $idSubtable = Common::getRequestVar('idSubtable', false); $urlPrefix = "index.php?"; foreach ($reports as &$report) { $reportModule = $report['module']; $reportAction = $report['action']; $reportUniqueId = $reportModule . '_' . $reportAction; $parameters = array(); $parameters['module'] = 'API'; $parameters['method'] = 'ImageGraph.get'; $parameters['idSite'] = $idSite; $parameters['apiModule'] = $reportModule; $parameters['apiAction'] = $reportAction; if (!empty($token_auth)) { $parameters['token_auth'] = $token_auth; } // Forward custom Report parameters to the graph URL if (!empty($report['parameters'])) { $parameters = array_merge($parameters, $report['parameters']); } if (empty($report['dimension'])) { $parameters['period'] = $periodForMultiplePeriodGraph; $parameters['date'] = $dateForMultiplePeriodGraph; } else { $parameters['period'] = $periodForSinglePeriodGraph; $parameters['date'] = $dateForSinglePeriodGraph; } if ($idSubtable !== false) { $parameters['idSubtable'] = $idSubtable; } if (!empty($_GET['_restrictSitesToLogin']) && $isRunningTask) { $parameters['_restrictSitesToLogin'] = $_GET['_restrictSitesToLogin']; } if (!empty($segment)) { $parameters['segment'] = $segment; } $report['imageGraphUrl'] = $urlPrefix . Url::getQueryStringFromParameters($parameters); // thanks to API.getRowEvolution, reports with dimensions can now be plotted using an evolution graph // however, most reports with a fixed set of dimension values are excluded // this is done so Piwik Mobile and Scheduled Reports do not display them $reportWithDimensionsSupportsEvolution = empty($report['constantRowsCount']) || in_array($reportUniqueId, self::$CONSTANT_ROW_COUNT_REPORT_EXCEPTIONS); $reportSupportsEvolution = !in_array($reportUniqueId, self::$REPORTS_DISABLED_EVOLUTION_GRAPH); if ($reportSupportsEvolution && $reportWithDimensionsSupportsEvolution) { $parameters['period'] = $periodForMultiplePeriodGraph; $parameters['date'] = $dateForMultiplePeriodGraph; $report['imageGraphEvolutionUrl'] = $urlPrefix . Url::getQueryStringFromParameters($parameters); } } }
/** * Creates a Period instance using a period, date and timezone. * * @param string $timezone The timezone of the date. Only used if `$date` is `'now'`, `'today'`, * `'yesterday'` or `'yesterdaySameTime'`. * @param string $period The period string: `"day"`, `"week"`, `"month"`, `"year"`, `"range"`. * @param string $date The date or date range string. Can be a special value including * `'now'`, `'today'`, `'yesterday'`, `'yesterdaySameTime'`. * @return \Piwik\Period */ public static function makePeriodFromQueryParams($timezone, $period, $date) { if (empty($timezone)) { $timezone = 'UTC'; } if ($period == 'range') { $oPeriod = new Period\Range('range', $date, $timezone, Date::factory('today', $timezone)); } else { if (!$date instanceof Date) { if ($date == 'now' || $date == 'today') { $date = date('Y-m-d', Date::factory('now', $timezone)->getTimestamp()); } elseif ($date == 'yesterday' || $date == 'yesterdaySameTime') { $date = date('Y-m-d', Date::factory('now', $timezone)->subDay(1)->getTimestamp()); } $date = Date::factory($date); } $oPeriod = Period::factory($period, $date); } return $oPeriod; }
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; }