public function test_getUsedTypeIds_shouldReturnAnotherType_IfDifferentOnesAreUsed() { for ($i = 0; $i < 9; $i++) { $this->createMeasurable('website'); $this->createMeasurable('universal'); $this->createMeasurable('mobileapp'); } $this->assertSame(array('website', 'universal', 'mobileapp'), $this->model->getUsedTypeIds()); }
public function getAllSiteUrls() { $model = new Model(); $siteIds = $model->getSitesId(); $siteUrls = array(); if (empty($siteIds)) { return array(); } foreach ($siteIds as $siteId) { $siteId = (int) $siteId; $siteUrls[$siteId] = $model->getSiteUrlsFromId($siteId); } return $siteUrls; }
private function getFirstTypeIfOnlyOneIsInUse() { $types = $this->typeManager->getAllTypes(); if (count($types) === 1) { // only one type is in use, use this one for the wording return reset($types); } else { // multiple types are activated, check whether only one is actually in use $model = new Model(); $typeIds = $model->getUsedTypeIds(); if (count($typeIds) === 1) { $typeManager = new TypeManager(); return $typeManager->getType(reset($typeIds)); } } }
public function test_getAllKnownUrlsForAllSites_shouldReturnAllUrls() { $idSite = $this->createMeasurable('website', 'http://apache.piwik'); $this->model->insertSiteUrl($idSite, 'http://example.apache.piwik'); $this->model->insertSiteUrl($idSite, 'http://example.org'); $idSite2 = $this->createMeasurable('website'); $this->model->insertSiteUrl($idSite2, 'http://example.org'); $this->model->insertSiteUrl($idSite2, 'http://example.com'); $idSite3 = $this->createMeasurable('website', 'http://example.pro'); $expected = array(array('idsite' => $idSite, 'url' => 'http://apache.piwik'), array('idsite' => $idSite2, 'url' => 'http://piwik.net'), array('idsite' => $idSite3, 'url' => 'http://example.pro'), array('idsite' => $idSite, 'url' => 'http://example.apache.piwik'), array('idsite' => $idSite, 'url' => 'http://example.org'), array('idsite' => $idSite2, 'url' => 'http://example.com'), array('idsite' => $idSite2, 'url' => 'http://example.org')); $this->assertEquals($expected, $this->model->getAllKnownUrlsForAllSites()); }
private function assertHasNotSite($idSite) { $model = new Model(); $siteInfo = $model->getSiteFromId($idSite); $this->assertEmpty($siteInfo); }
public function getAllSiteUrls() { $model = new Model(); $siteUrls = $model->getAllKnownUrlsForAllSites(); if (empty($siteUrls)) { return array(); } $urls = array(); foreach ($siteUrls as $siteUrl) { $siteId = (int) $siteUrl['idsite']; if (!isset($urls[$siteId])) { $urls[$siteId] = array(); } $urls[$siteId][] = $siteUrl['url']; } return $urls; }
/** * Fetches the list of sites which names match the string pattern * * @param string $pattern * @param bool $_restrictSitesToLogin * @return array|string */ private function getSitesIdFromPattern($pattern, $_restrictSitesToLogin) { // First clear cache Site::clearCache(); if (empty($pattern)) { /** @var Scheduler $scheduler */ $scheduler = StaticContainer::getContainer()->get('Piwik\\Scheduler\\Scheduler'); // Then, warm the cache with only the data we should have access to if (Piwik::hasUserSuperUserAccess() && !$scheduler->isRunningTask()) { APISitesManager::getInstance()->getAllSites(); } else { APISitesManager::getInstance()->getSitesWithAtLeastViewAccess($limit = false, $_restrictSitesToLogin); } } else { $sites = Request::processRequest('SitesManager.getPatternMatchSites', array('pattern' => $pattern, 'showColumns' => '', 'hideColumns' => '', 'serialize' => 0, 'format' => 'original')); if (!empty($sites)) { $idSites = array(); foreach ($sites as $site) { $idSites[] = $site['idsite']; } $model = new ModelSitesManager(); $sites = $model->getSitesFromIds($idSites); // getPatternMatchSites does not return all sites information... Site::setSitesFromArray($sites); } } // Both calls above have called Site::setSitesFromArray. We now get these sites: $sitesToProblablyAdd = Site::getSites(); return $sitesToProblablyAdd; }
private function updateSiteCreatedTime($idSites, Date $minDate) { $idSites = Site::getIdSitesFromIdSitesString($idSites); $minDateSql = $minDate->subDay(1)->getDatetime(); $model = new SitesManagerModel(); $model->updateSiteCreatedTime($idSites, $minDateSql); }
private function performRedirectToUrlIfSet() { if (!$this->hasRedirectUrl()) { return; } if (empty($this->requests)) { return; } $redirectUrl = $this->getRedirectUrl(); $host = Url::getHostFromUrl($redirectUrl); if (empty($host)) { return; } $siteIds = array(); foreach ($this->requests as $request) { $siteIds[] = (int) $request['idsite']; } $siteIds = array_unique($siteIds); $model = new Model(); foreach ($siteIds as $siteId) { $siteUrls = $model->getSiteUrlsFromId($siteId); if (Url::isHostInUrls($host, $siteUrls)) { Url::redirectToUrl($redirectUrl); } } }
private function buildDataTable($idSitesOrIdSite, $period, $date, $segment, $_restrictSitesToLogin, $enhanced, $multipleWebsitesRequested) { $allWebsitesRequested = $idSitesOrIdSite == 'all'; if ($allWebsitesRequested) { // First clear cache Site::clearCache(); // Then, warm the cache with only the data we should have access to if (Piwik::hasUserSuperUserAccess() && !TaskScheduler::isTaskBeingExecuted()) { APISitesManager::getInstance()->getAllSites(); } else { APISitesManager::getInstance()->getSitesWithAtLeastViewAccess($limit = false, $_restrictSitesToLogin); } // Both calls above have called Site::setSitesFromArray. We now get these sites: $sitesToProblablyAdd = Site::getSites(); } else { if (is_array($idSitesOrIdSite)) { $model = new ModelSitesManager(); $sitesToProblablyAdd = $model->getSitesFromIds($idSitesOrIdSite); } else { $sitesToProblablyAdd = array(APISitesManager::getInstance()->getSiteFromId($idSitesOrIdSite)); } } // build the archive type used to query archive data $archive = Archive::build($idSitesOrIdSite, $period, $date, $segment, $_restrictSitesToLogin); // determine what data will be displayed $fieldsToGet = array(); $columnNameRewrites = array(); $apiECommerceMetrics = array(); $apiMetrics = API::getApiMetrics($enhanced); foreach ($apiMetrics as $metricName => $metricSettings) { $fieldsToGet[] = $metricSettings[self::METRIC_RECORD_NAME_KEY]; $columnNameRewrites[$metricSettings[self::METRIC_RECORD_NAME_KEY]] = $metricName; if ($metricSettings[self::METRIC_IS_ECOMMERCE_KEY]) { $apiECommerceMetrics[$metricName] = $metricSettings; } } // get the data // $dataTable instanceOf Set $dataTable = $archive->getDataTableFromNumeric($fieldsToGet); $dataTable = $this->mergeDataTableMapAndPopulateLabel($idSitesOrIdSite, $multipleWebsitesRequested, $dataTable); if ($dataTable instanceof DataTable\Map) { foreach ($dataTable->getDataTables() as $table) { $this->addMissingWebsites($table, $fieldsToGet, $sitesToProblablyAdd); } } else { $this->addMissingWebsites($dataTable, $fieldsToGet, $sitesToProblablyAdd); } // calculate total visits/actions/revenue $this->setMetricsTotalsMetadata($dataTable, $apiMetrics); // if the period isn't a range & a lastN/previousN date isn't used, we get the same // data for the last period to show the evolution of visits/actions/revenue list($strLastDate, $lastPeriod) = Range::getLastDate($date, $period); if ($strLastDate !== false) { if ($lastPeriod !== false) { // NOTE: no easy way to set last period date metadata when range of dates is requested. // will be easier if DataTable\Map::metadata is removed, and metadata that is // put there is put directly in DataTable::metadata. $dataTable->setMetadata(self::getLastPeriodMetadataName('date'), $lastPeriod); } $pastArchive = Archive::build($idSitesOrIdSite, $period, $strLastDate, $segment, $_restrictSitesToLogin); $pastData = $pastArchive->getDataTableFromNumeric($fieldsToGet); $pastData = $this->mergeDataTableMapAndPopulateLabel($idSitesOrIdSite, $multipleWebsitesRequested, $pastData); // use past data to calculate evolution percentages $this->calculateEvolutionPercentages($dataTable, $pastData, $apiMetrics); Common::destroy($pastData); } // remove eCommerce related metrics on non eCommerce Piwik sites // note: this is not optimal in terms of performance: those metrics should not be retrieved in the first place if ($enhanced) { if ($dataTable instanceof DataTable\Map) { foreach ($dataTable->getDataTables() as $table) { $this->removeEcommerceRelatedMetricsOnNonEcommercePiwikSites($table, $apiECommerceMetrics); } } else { $this->removeEcommerceRelatedMetricsOnNonEcommercePiwikSites($dataTable, $apiECommerceMetrics); } } // move the site id to a metadata column $dataTable->filter('ColumnCallbackAddMetadata', array('label', 'group', array('\\Piwik\\Site', 'getGroupFor'), array())); $dataTable->filter('ColumnCallbackAddMetadata', array('label', 'main_url', array('\\Piwik\\Site', 'getMainUrlFor'), array())); $dataTable->filter('ColumnCallbackAddMetadata', array('label', 'idsite')); // set the label of each row to the site name if ($multipleWebsitesRequested) { $dataTable->filter('ColumnCallbackReplace', array('label', '\\Piwik\\Site::getNameFor')); } else { $dataTable->filter('ColumnDelete', array('label')); } Site::clearCache(); // replace record names with user friendly metric names $dataTable->filter('ReplaceColumnNames', array($columnNameRewrites)); // Ensures data set sorted, for Metadata output $dataTable->filter('Sort', array(self::NB_VISITS_METRIC, 'desc', $naturalSort = false)); // filter rows without visits // note: if only one website is queried and there are no visits, we can not remove the row otherwise // ResponseBuilder throws 'Call to a member function getColumns() on a non-object' if ($multipleWebsitesRequested && !$enhanced) { $dataTable->filter('ColumnCallbackDeleteRow', array(self::NB_VISITS_METRIC, function ($value) { return $value == 0; })); } if ($multipleWebsitesRequested && $dataTable->getRowsCount() === 1 && $dataTable instanceof DataTable\Simple) { $simpleTable = $dataTable; $dataTable = $simpleTable->getEmptyClone(); $dataTable->addRow($simpleTable->getFirstRow()); unset($simpleTable); } return $dataTable; }
private function updateSiteCreatedTime($idSite, Date $timestamp) { $this->sitesManagerModel->updateSiteCreatedTime(array($idSite), $timestamp->getDatetime()); Cache::deleteCacheWebsiteAttributes($idSite); }