diff --git a/core/ArchiveProcessing.php b/core/ArchiveProcessing.php index a997e3714b0a28f3958415a0e19e241738adcb67..7436d6e3b70ddf3a3ae44c2f974011ffeaaafdb7 100644 --- a/core/ArchiveProcessing.php +++ b/core/ArchiveProcessing.php @@ -322,22 +322,47 @@ abstract class Piwik_ArchiveProcessing $this->idsite = $this->site->getId(); $this->periodId = $this->period->getId(); - $dateStartLocalTimezone = $this->period->getDateStart(); - $dateEndLocalTimezone = $this->period->getDateEnd(); + $this->initDates(); $this->tableArchiveNumeric = self::makeNumericArchiveTable($this->period); $this->tableArchiveBlob = self::makeBlobArchiveTable($this->period); + $this->minDatetimeArchiveProcessedUTC = $this->getMinTimeArchivedProcessed(); + $db = Zend_Registry::get('db'); + $this->compressBlob = $db->hasBlobDataType(); + } + + /** + * The archive processing classes have features that might be useful for live querying; + * In particular, Piwik_ArchiveProcessing_Day::query*. In order to reuse those methods + * outside the actual archiving or to reuse archiving code for live querying, an instance + * of archive processing has to be faked. + * + * For example, this code can be used in an API method: + * $archiveProcessing = new Piwik_ArchiveProcessing_Day(); + * $archiveProcessing->setSite(new Piwik_Site($idSite)); + * $archiveProcessing->setPeriod(Piwik_Period::advancedFactory($period, $date)); + * $archiveProcessing->setSegment(new Piwik_Segment($segment, $idSite)); + * $archiveProcessing->initForLiveUsage(); + * Then, either use $archiveProcessing->query* or pass the instance to the archiving + * code of the plugin. Note that even though we use Piwik_ArchiveProcessing_Day, this + * works for any $period and $date that has been passed to the API. + */ + public function initForLiveUsage() { + $this->idsite = $this->site->getId(); + $this->initDates(); + } + + private function initDates() { + $dateStartLocalTimezone = $this->period->getDateStart(); + $dateEndLocalTimezone = $this->period->getDateEnd(); + $dateStartUTC = $dateStartLocalTimezone->setTimezone($this->site->getTimezone()); $dateEndUTC = $dateEndLocalTimezone->setTimezone($this->site->getTimezone()); $this->startDatetimeUTC = $dateStartUTC->getDateStartUTC(); $this->endDatetimeUTC = $dateEndUTC->getDateEndUTC(); $this->startTimestampUTC = $dateStartUTC->getTimestamp(); - $this->endTimestampUTC = strtotime($this->endDatetimeUTC); - - $this->minDatetimeArchiveProcessedUTC = $this->getMinTimeArchivedProcessed(); - $db = Zend_Registry::get('db'); - $this->compressBlob = $db->hasBlobDataType(); + $this->endTimestampUTC = strtotime($this->endDatetimeUTC); } /** diff --git a/core/Period.php b/core/Period.php index dd91877d4466c323c7fa472e11ad4f9e9bcd4143..0e23b4782b198307c22cfcfcf6ad777755194c93 100644 --- a/core/Period.php +++ b/core/Period.php @@ -72,6 +72,25 @@ abstract class Piwik_Period break; } } + + /** + * The advanced factory method is easier to use from the API than the factory + * method above. It doesn't require an instance of Piwik_Date and works for + * period=range. Generally speaking, anything that can be passed as period + * and range to the API methods can directly be forwarded to this factory + * method in order to get a suitable instance of Piwik_Period. + * + * @param string $strPeriod "day", "week", "month", "year", "range" + * @param string $strDate + * @return Piwik_Period + */ + static public function advancedFactory($strPeriod, $strDate) { + if (Piwik_Archive::isMultiplePeriod($strDate, $strPeriod) || $strPeriod == 'range') + { + return new Piwik_Period_Range($strPeriod, $strDate); + } + return self::factory($strPeriod, Piwik_Date::factory($strDate)); + } /** diff --git a/plugins/API/API.php b/plugins/API/API.php index fe28e96be8500318151480aa1d6ac16d2cc818a1..ddd726b84d4b55e5d9eb60f11cc946ae1598dfa3 100644 --- a/plugins/API/API.php +++ b/plugins/API/API.php @@ -716,23 +716,8 @@ class Piwik_API_API } $website = new Piwik_Site($idSite); // $segment = new Piwik_Segment($segment, $idSite); - - if(Piwik_Archive::isMultiplePeriod($date, $period)) - { - $period = new Piwik_Period_Range($period, $date); - } - else - { - if($period == 'range') - { - $period = new Piwik_Period_Range($period, $date); - } - else - { - $period = Piwik_Period::factory($period, Piwik_Date::factory($date)); - } - } - + + $period = Piwik_Period::advancedFactory($period, $date); $period = $period->getLocalizedLongString(); $return = array(