ArchiveInvalidator
6 years ago
ArchiveInvalidator.php
6 years ago
ArchivePurger.php
6 years ago
ArchiveQuery.php
6 years ago
ArchiveQueryFactory.php
6 years ago
Chunk.php
6 years ago
DataCollection.php
6 years ago
DataTableFactory.php
6 years ago
Parameters.php
6 years ago
ArchiveQueryFactory.php
127 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Piwik - free/libre analytics platform |
| 4 | * |
| 5 | * @link https://matomo.org |
| 6 | * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later |
| 7 | * |
| 8 | */ |
| 9 | |
| 10 | namespace Piwik\Archive; |
| 11 | |
| 12 | use Piwik\Archive; |
| 13 | use Piwik\Period; |
| 14 | use Piwik\Segment; |
| 15 | use Piwik\Site; |
| 16 | use Piwik\Period\Factory as PeriodFactory; |
| 17 | |
| 18 | class ArchiveQueryFactory |
| 19 | { |
| 20 | public function __construct() |
| 21 | { |
| 22 | // empty |
| 23 | } |
| 24 | |
| 25 | /** |
| 26 | * @see \Piwik\Archive::build() |
| 27 | */ |
| 28 | public function build($idSites, $strPeriod, $strDate, $strSegment = false, $_restrictSitesToLogin = false) |
| 29 | { |
| 30 | list($websiteIds, $timezone, $idSiteIsAll) = $this->getSiteInfoFromQueryParam($idSites, $_restrictSitesToLogin); |
| 31 | list($allPeriods, $isMultipleDate) = $this->getPeriodInfoFromQueryParam($strDate, $strPeriod, $timezone); |
| 32 | $segment = $this->getSegmentFromQueryParam($strSegment, $websiteIds); |
| 33 | |
| 34 | return $this->factory($segment, $allPeriods, $websiteIds, $idSiteIsAll, $isMultipleDate); |
| 35 | } |
| 36 | |
| 37 | /** |
| 38 | * @see \Piwik\Archive::factory() |
| 39 | */ |
| 40 | public function factory(Segment $segment, array $periods, array $idSites, $idSiteIsAll = false, $isMultipleDate = false) |
| 41 | { |
| 42 | $forceIndexedBySite = false; |
| 43 | $forceIndexedByDate = false; |
| 44 | |
| 45 | if ($idSiteIsAll || count($idSites) > 1) { |
| 46 | $forceIndexedBySite = true; |
| 47 | } |
| 48 | |
| 49 | if (count($periods) > 1 || $isMultipleDate) { |
| 50 | $forceIndexedByDate = true; |
| 51 | } |
| 52 | |
| 53 | $params = new Parameters($idSites, $periods, $segment); |
| 54 | |
| 55 | return $this->newInstance($params, $forceIndexedBySite, $forceIndexedByDate); |
| 56 | } |
| 57 | |
| 58 | public function newInstance(Parameters $params, $forceIndexedBySite, $forceIndexedByDate) |
| 59 | { |
| 60 | return new Archive($params, $forceIndexedBySite, $forceIndexedByDate); |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * Parses the site ID string provided in the 'idSite' query parameter to a list of |
| 65 | * website IDs. |
| 66 | * |
| 67 | * @param string $idSites the value of the 'idSite' query parameter |
| 68 | * @param bool $_restrictSitesToLogin |
| 69 | * @return array an array containing three elements: |
| 70 | * - an array of website IDs |
| 71 | * - string timezone to use (or false to use no timezone) when creating periods. |
| 72 | * - true if the request was for all websites (this forces the archive result to |
| 73 | * be indexed by site, even if there is only one site in Piwik) |
| 74 | */ |
| 75 | protected function getSiteInfoFromQueryParam($idSites, $_restrictSitesToLogin) |
| 76 | { |
| 77 | $websiteIds = Site::getIdSitesFromIdSitesString($idSites, $_restrictSitesToLogin); |
| 78 | |
| 79 | $timezone = false; |
| 80 | if (count($websiteIds) == 1) { |
| 81 | $timezone = Site::getTimezoneFor($websiteIds[0]); |
| 82 | } |
| 83 | |
| 84 | $idSiteIsAll = $idSites == Archive::REQUEST_ALL_WEBSITES_FLAG; |
| 85 | |
| 86 | return [$websiteIds, $timezone, $idSiteIsAll]; |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * Parses the date & period query parameters into a list of periods. |
| 91 | * |
| 92 | * @param string $strDate the value of the 'date' query parameter |
| 93 | * @param string $strPeriod the value of the 'period' query parameter |
| 94 | * @param string $timezone the timezone to use when constructing periods. |
| 95 | * @return array an array containing two elements: |
| 96 | * - the list of period objects to query archive data for |
| 97 | * - true if the request was for multiple periods (ie, two months, two weeks, etc.), false if otherwise. |
| 98 | * (this forces the archive result to be indexed by period, even if the list of periods |
| 99 | * has only one period). |
| 100 | */ |
| 101 | protected function getPeriodInfoFromQueryParam($strDate, $strPeriod, $timezone) |
| 102 | { |
| 103 | if (Period::isMultiplePeriod($strDate, $strPeriod)) { |
| 104 | $oPeriod = PeriodFactory::build($strPeriod, $strDate, $timezone); |
| 105 | $allPeriods = $oPeriod->getSubperiods(); |
| 106 | } else { |
| 107 | $oPeriod = PeriodFactory::makePeriodFromQueryParams($timezone, $strPeriod, $strDate); |
| 108 | $allPeriods = array($oPeriod); |
| 109 | } |
| 110 | |
| 111 | $isMultipleDate = Period::isMultiplePeriod($strDate, $strPeriod); |
| 112 | |
| 113 | return [$allPeriods, $isMultipleDate]; |
| 114 | } |
| 115 | |
| 116 | /** |
| 117 | * Parses the segment query parameter into a Segment object. |
| 118 | * |
| 119 | * @param string $strSegment the value of the 'segment' query parameter. |
| 120 | * @param int[] $websiteIds the list of sites being queried. |
| 121 | * @return Segment |
| 122 | */ |
| 123 | protected function getSegmentFromQueryParam($strSegment, $websiteIds) |
| 124 | { |
| 125 | return new Segment($strSegment, $websiteIds); |
| 126 | } |
| 127 | } |