diff options
author | Benaka <diosmosis@users.noreply.github.com> | 2017-09-11 16:08:42 +0300 |
---|---|---|
committer | Matthieu Aubry <mattab@users.noreply.github.com> | 2017-09-11 16:08:42 +0300 |
commit | f0ddb70c85a6211540797665f17d36b6d79128a4 (patch) | |
tree | 605ef83a15a39b1d30d6641234b83aee58ec8b16 /core/Archive | |
parent | 1092e4d7290333591f0f9beead6580a1424bc99f (diff) |
Changes to support custom periods (#11837)
* Separate Archive query creation responsibility from Archive class.
* Add ability for plugins to define custom period types.
* Make period responsible for determining start/end time of periods, not LogAggregator.
* Allow specifying custom archive writer in PluginsArchiver.
Diffstat (limited to 'core/Archive')
-rw-r--r-- | core/Archive/ArchiveQuery.php | 49 | ||||
-rw-r--r-- | core/Archive/ArchiveQueryFactory.php | 127 |
2 files changed, 176 insertions, 0 deletions
diff --git a/core/Archive/ArchiveQuery.php b/core/Archive/ArchiveQuery.php new file mode 100644 index 0000000000..acd6bbf29e --- /dev/null +++ b/core/Archive/ArchiveQuery.php @@ -0,0 +1,49 @@ +<?php +/** + * Piwik - free/libre analytics platform + * + * @link http://piwik.org + * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later + * + */ +namespace Piwik\Archive; + + +use Piwik\DataTable; + +interface ArchiveQuery +{ + /** + * @param string|string[] $names + * @return false|number|array + */ + public function getNumeric($names); + + /** + * @param string|string[] $names + * @return DataTable|DataTable\Map + */ + public function getDataTableFromNumeric($names); + + /** + * @param $names + * @return mixed + */ + public function getDataTableFromNumericAndMergeChildren($names); + + /** + * @param string $name + * @param int|string|null $idSubtable + * @return DataTable|DataTable\Map + */ + public function getDataTable($name, $idSubtable = null); + + /** + * @param string $name + * @param int|string|null $idSubtable + * @param int|null $depth + * @param bool $addMetadataSubtableId + * @return DataTable|DataTable\Map + */ + public function getDataTableExpanded($name, $idSubtable = null, $depth = null, $addMetadataSubtableId = true); +}
\ No newline at end of file diff --git a/core/Archive/ArchiveQueryFactory.php b/core/Archive/ArchiveQueryFactory.php new file mode 100644 index 0000000000..ddf3db6b87 --- /dev/null +++ b/core/Archive/ArchiveQueryFactory.php @@ -0,0 +1,127 @@ +<?php +/** + * Piwik - free/libre analytics platform + * + * @link http://piwik.org + * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later + * + */ + +namespace Piwik\Archive; + +use Piwik\Archive; +use Piwik\Period; +use Piwik\Segment; +use Piwik\Site; +use Piwik\Period\Factory as PeriodFactory; + +class ArchiveQueryFactory +{ + public function __construct() + { + // empty + } + + /** + * @see \Piwik\Archive::build() + */ + public function build($idSites, $strPeriod, $strDate, $strSegment = false, $_restrictSitesToLogin = false) + { + list($websiteIds, $timezone, $idSiteIsAll) = $this->getSiteInfoFromQueryParam($idSites, $_restrictSitesToLogin); + list($allPeriods, $isMultipleDate) = $this->getPeriodInfoFromQueryParam($strDate, $strPeriod, $timezone); + $segment = $this->getSegmentFromQueryParam($strSegment, $websiteIds); + + return $this->factory($segment, $allPeriods, $websiteIds, $idSiteIsAll, $isMultipleDate); + } + + /** + * @see \Piwik\Archive::factory() + */ + public function factory(Segment $segment, array $periods, array $idSites, $idSiteIsAll = false, $isMultipleDate = false) + { + $forceIndexedBySite = false; + $forceIndexedByDate = false; + + if ($idSiteIsAll || count($idSites) > 1) { + $forceIndexedBySite = true; + } + + if (count($periods) > 1 || $isMultipleDate) { + $forceIndexedByDate = true; + } + + $params = new Parameters($idSites, $periods, $segment); + + return $this->newInstance($params, $forceIndexedBySite, $forceIndexedByDate); + } + + public function newInstance(Parameters $params, $forceIndexedBySite, $forceIndexedByDate) + { + return new Archive($params, $forceIndexedBySite, $forceIndexedByDate); + } + + /** + * Parses the site ID string provided in the 'idSite' query parameter to a list of + * website IDs. + * + * @param string $idSites the value of the 'idSite' query parameter + * @param bool $_restrictSitesToLogin + * @return array an array containing three elements: + * - an array of website IDs + * - string timezone to use (or false to use no timezone) when creating periods. + * - true if the request was for all websites (this forces the archive result to + * be indexed by site, even if there is only one site in Piwik) + */ + protected function getSiteInfoFromQueryParam($idSites, $_restrictSitesToLogin) + { + $websiteIds = Site::getIdSitesFromIdSitesString($idSites, $_restrictSitesToLogin); + + $timezone = false; + if (count($websiteIds) == 1) { + $timezone = Site::getTimezoneFor($websiteIds[0]); + } + + $idSiteIsAll = $idSites == Archive::REQUEST_ALL_WEBSITES_FLAG; + + return [$websiteIds, $timezone, $idSiteIsAll]; + } + + /** + * Parses the date & period query parameters into a list of periods. + * + * @param string $strDate the value of the 'date' query parameter + * @param string $strPeriod the value of the 'period' query parameter + * @param string $timezone the timezone to use when constructing periods. + * @return array an array containing two elements: + * - the list of period objects to query archive data for + * - true if the request was for multiple periods (ie, two months, two weeks, etc.), false if otherwise. + * (this forces the archive result to be indexed by period, even if the list of periods + * has only one period). + */ + protected function getPeriodInfoFromQueryParam($strDate, $strPeriod, $timezone) + { + if (Period::isMultiplePeriod($strDate, $strPeriod)) { + $oPeriod = PeriodFactory::build($strPeriod, $strDate, $timezone); + $allPeriods = $oPeriod->getSubperiods(); + } else { + $oPeriod = PeriodFactory::makePeriodFromQueryParams($timezone, $strPeriod, $strDate); + $allPeriods = array($oPeriod); + } + + $isMultipleDate = Period::isMultiplePeriod($strDate, $strPeriod); + + return [$allPeriods, $isMultipleDate]; + } + + /** + * Parses the segment query parameter into a Segment object. + * + * @param string $strSegment the value of the 'segment' query parameter. + * @param int[] $websiteIds the list of sites being queried. + * @return Segment + */ + protected function getSegmentFromQueryParam($strSegment, $websiteIds) + { + return new Segment($strSegment, $websiteIds); + } +}
\ No newline at end of file |