diff options
author | mattab <matthieu.aubry@gmail.com> | 2015-03-19 06:49:41 +0300 |
---|---|---|
committer | mattab <matthieu.aubry@gmail.com> | 2015-03-19 06:49:41 +0300 |
commit | 8a1ed4d3670b07049131d60f3ae369cf8d353e76 (patch) | |
tree | 8483485a4ad2fe618ee023bf75c57110046313df | |
parent | 82865a9f6e132ecf780a9726b7562118dd80276d (diff) |
Simplifies object scope to only return modified date parameter
-rw-r--r-- | core/CronArchive.php | 37 | ||||
-rw-r--r-- | core/CronArchive/SegmentArchivingRequestUrlProvider.php | 17 | ||||
-rw-r--r-- | tests/PHPUnit/Unit/CronArchive/SegmentArchivingRequestUrlProviderTest.php | 28 |
3 files changed, 40 insertions, 42 deletions
diff --git a/core/CronArchive.php b/core/CronArchive.php index 7a4c0bf972..c21a0205e7 100644 --- a/core/CronArchive.php +++ b/core/CronArchive.php @@ -236,7 +236,7 @@ class CronArchive $this->initTokenAuth(); $processNewSegmentsFrom = $processNewSegmentsFrom ?: StaticContainer::get('ini.General.process_new_segments_from'); - $this->segmentArchivingRequestUrlProvider = new SegmentArchivingRequestUrlProvider($this->piwikUrl, $this->token_auth, $processNewSegmentsFrom); + $this->segmentArchivingRequestUrlProvider = new SegmentArchivingRequestUrlProvider($processNewSegmentsFrom); } /** @@ -629,9 +629,13 @@ class CronArchive /** * Returns base URL to process reports for the $idSite on a given $period */ - private function getVisitsRequestUrl($idSite, $period, $date) + private function getVisitsRequestUrl($idSite, $period, $date, $segment = false) { - return "?module=API&method=API.get&idSite=$idSite&period=$period&date=" . $date . "&format=php&token_auth=" . $this->token_auth; + $request = "?module=API&method=API.get&idSite=$idSite&period=$period&date=" . $date . "&format=php&token_auth=" . $this->token_auth; + if($segment) { + $request .= '&segment=' . urlencode($segment);; + } + return $request; } private function initSegmentsToArchive() @@ -764,11 +768,8 @@ class CronArchive { $timer = new Timer(); - $url = $this->piwikUrl; - - $url .= $this->getVisitsRequestUrl($idSite, $period, $date); - - $url .= self::APPEND_TO_API_REQUEST; + $url = $this->getVisitsRequestUrl($idSite, $period, $date, $segment = false); + $url = $this->makeRequestUrl($url); $visitsInLastPeriods = $visitsLastPeriod = 0; $success = true; @@ -783,8 +784,10 @@ class CronArchive } foreach ($this->getSegmentsForSite($idSite) as $segment) { - $urlWithSegment = $this->segmentArchivingRequestUrlProvider->getUrlToArchiveSegment($idSite, $period, $date, $segment); - $urlWithSegment .= self::APPEND_TO_API_REQUEST; + $dateParamForSegment = $this->segmentArchivingRequestUrlProvider->getUrlParameterDateString($idSite, $period, $date, $segment); + + $urlWithSegment = $this->getVisitsRequestUrl($idSite, $period, $dateParamForSegment, $segment); + $urlWithSegment = $this->makeRequestUrl($urlWithSegment); $urls[] = $urlWithSegment; $this->requests++; @@ -866,11 +869,12 @@ class CronArchive } /** - * Issues a request to $url + * Issues a request to $url eg. "?module=API&method=API.getDefaultMetricTranslations&format=original&serialize=1" + * */ private function request($url) { - $url = $this->piwikUrl . $url . self::APPEND_TO_API_REQUEST; + $url = $this->makeRequestUrl($url); if ($this->shouldStartProfiler) { $url .= "&xhprof=2"; @@ -1545,4 +1549,13 @@ class CronArchive return $customDateRangesToProcessForSites; } + + /** + * @param $url + * @return string + */ + private function makeRequestUrl($url) + { + return $this->piwikUrl . $url . self::APPEND_TO_API_REQUEST; + } }
\ No newline at end of file diff --git a/core/CronArchive/SegmentArchivingRequestUrlProvider.php b/core/CronArchive/SegmentArchivingRequestUrlProvider.php index 6b3828af06..332d5f5cee 100644 --- a/core/CronArchive/SegmentArchivingRequestUrlProvider.php +++ b/core/CronArchive/SegmentArchivingRequestUrlProvider.php @@ -39,30 +39,17 @@ class SegmentArchivingRequestUrlProvider */ private $now; - private $baseUrl; - private $tokenAuth; private $processNewSegmentsFrom; - public function __construct($baseUrl, $tokenAuth, $processNewSegmentsFrom, Model $segmentEditorModel = null, Cache $segmentListCache = null, Date $now = null) + public function __construct($processNewSegmentsFrom, Model $segmentEditorModel = null, Cache $segmentListCache = null, Date $now = null) { - $this->baseUrl = $baseUrl; - $this->tokenAuth = $tokenAuth; $this->processNewSegmentsFrom = $processNewSegmentsFrom; $this->segmentEditorModel = $segmentEditorModel ?: new Model(); $this->segmentListCache = $segmentListCache ?: new Transient(); $this->now = $now ?: Date::factory('now'); } - public function getUrlToArchiveSegment($idSite, $period, $date, $segment) - { - $date = $this->getModifiedSegmentArchvingDateRange($idSite, $period, $date, $segment); - - return $this->baseUrl - . "?module=API&method=API.get&idSite=$idSite&period=$period&date=" . $date . "&format=php&token_auth=" . $this->tokenAuth - . '&segment=' . urlencode($segment); - } - - private function getModifiedSegmentArchvingDateRange($idSite, $period, $date, $segment) + public function getUrlParameterDateString($idSite, $period, $date, $segment) { $segmentCreatedTime = $this->getCreatedTimeOfSegment($idSite, $segment); if (empty($segmentCreatedTime)) { diff --git a/tests/PHPUnit/Unit/CronArchive/SegmentArchivingRequestUrlProviderTest.php b/tests/PHPUnit/Unit/CronArchive/SegmentArchivingRequestUrlProviderTest.php index 7d50d3f17f..20d4faca20 100644 --- a/tests/PHPUnit/Unit/CronArchive/SegmentArchivingRequestUrlProviderTest.php +++ b/tests/PHPUnit/Unit/CronArchive/SegmentArchivingRequestUrlProviderTest.php @@ -13,10 +13,8 @@ use Piwik\CronArchive\SegmentArchivingRequestUrlProvider; /** * @group Core */ -class SegmentArchivingRequestUrlProviderTest extends \PHPUnit_Framework_TestCase +class test extends \PHPUnit_Framework_TestCase { - const BASE_URL = 'http://base/'; - const TOKEN_AUTH = 'tokenauth'; const TEST_NOW = '2015-03-01'; private $mockSegmentEntries; @@ -69,13 +67,14 @@ class SegmentArchivingRequestUrlProviderTest extends \PHPUnit_Framework_TestCase { $urlProvider = $this->createUrlProviderToTest($processNewSegmentsFrom); - $actual = $urlProvider->getUrlToArchiveSegment($idSite, $period, $date, $segment); + $actual = $urlProvider->getUrlParameterDateString($idSite, $period, $date, $segment); $this->assertEquals($expected, $actual); } public function getUrlToArchiveSegmentTestData() { $dateRange = '2010-02-01,' . self::TEST_NOW; + return array( array( // test beginning_of_time does not modify date 'beginning_of_time', @@ -83,7 +82,7 @@ class SegmentArchivingRequestUrlProviderTest extends \PHPUnit_Framework_TestCase $dateRange, 'week', 'browserName==FF', - "http://base/?module=API&method=API.get&idSite=1&period=week&date=$dateRange&format=php&token_auth=tokenauth&segment=" . urlencode('browserName==FF') + $dateRange ), array( // test garbage string does not modify date @@ -92,7 +91,7 @@ class SegmentArchivingRequestUrlProviderTest extends \PHPUnit_Framework_TestCase $dateRange, 'week', 'browserName==FF', - "http://base/?module=API&method=API.get&idSite=1&period=week&date=$dateRange&format=php&token_auth=tokenauth&segment=" . urlencode('browserName==FF') + $dateRange ), array( // test creation_time uses creation time of segment @@ -101,7 +100,7 @@ class SegmentArchivingRequestUrlProviderTest extends \PHPUnit_Framework_TestCase $dateRange, 'week', 'browserName==FF', - 'http://base/?module=API&method=API.get&idSite=1&period=week&date=2014-01-01,2015-03-01&format=php&token_auth=tokenauth&segment=' . urlencode('browserName==FF') + "2014-01-01,2015-03-01" ), array( // test creation_time uses earliest time of segment if multiple match (multiple for site) @@ -110,7 +109,7 @@ class SegmentArchivingRequestUrlProviderTest extends \PHPUnit_Framework_TestCase $dateRange, 'week', 'countryCode==us', - 'http://base/?module=API&method=API.get&idSite=1&period=week&date=2012-01-01,2015-03-01&format=php&token_auth=tokenauth&segment=' . urlencode('countryCode==us') + '2012-01-01,2015-03-01' ), array( // test creation_time uses earliest time of segment if multiple match (multiple for site + one for all) @@ -119,7 +118,7 @@ class SegmentArchivingRequestUrlProviderTest extends \PHPUnit_Framework_TestCase $dateRange, 'week', 'countryCode==ca', - 'http://base/?module=API&method=API.get&idSite=2&period=week&date=2011-01-01,2015-03-01&format=php&token_auth=tokenauth&segment=' . urlencode('countryCode==ca') + '2011-01-01,2015-03-01' ), array( // test 'now' is used if no site matches (testing w/o any segments) @@ -128,7 +127,7 @@ class SegmentArchivingRequestUrlProviderTest extends \PHPUnit_Framework_TestCase $dateRange, 'week', 'pageTitle==abc', - 'http://base/?module=API&method=API.get&idSite=1&period=week&date=2015-03-01,2015-03-01&format=php&token_auth=tokenauth&segment=' . urlencode('pageTitle==abc') + "2015-03-01,2015-03-01" ), array( // test 'now' is used if no site matches (testing w/ segment for another site) @@ -137,7 +136,7 @@ class SegmentArchivingRequestUrlProviderTest extends \PHPUnit_Framework_TestCase $dateRange, 'week', 'countryCode==us', - 'http://base/?module=API&method=API.get&idSite=3&period=week&date=2015-03-01,2015-03-01&format=php&token_auth=tokenauth&segment=' . urlencode('countryCode==us') + "2015-03-01,2015-03-01" ), array( // test lastN rewinds created date by N days @@ -146,7 +145,7 @@ class SegmentArchivingRequestUrlProviderTest extends \PHPUnit_Framework_TestCase $dateRange, 'week', 'countryCode==us', - 'http://base/?module=API&method=API.get&idSite=1&period=week&date=2011-12-22,2015-03-01&format=php&token_auth=tokenauth&segment=' . urlencode('countryCode==us') + "2011-12-22,2015-03-01" ), array( // test lastN rewinds now by N days (testing w/ no found segment) @@ -155,7 +154,7 @@ class SegmentArchivingRequestUrlProviderTest extends \PHPUnit_Framework_TestCase $dateRange, 'week', 'countryCode==us', - 'http://base/?module=API&method=API.get&idSite=3&period=week&date=2015-02-19,2015-03-01&format=php&token_auth=tokenauth&segment=' . urlencode('countryCode==us') + "2015-02-19,2015-03-01" ), ); } @@ -165,7 +164,6 @@ class SegmentArchivingRequestUrlProviderTest extends \PHPUnit_Framework_TestCase $mockSegmentEditorModel = $this->getMock('Piwik\Plugins\SegmentEditor\Model', array('getAllSegmentsAndIgnoreVisibility')); $mockSegmentEditorModel->expects($this->any())->method('getAllSegmentsAndIgnoreVisibility')->will($this->returnValue($this->mockSegmentEntries)); - return new SegmentArchivingRequestUrlProvider(self::BASE_URL, self::TOKEN_AUTH, $processNewSegmentsFrom, - $mockSegmentEditorModel, null, Date::factory(self::TEST_NOW)); + return new SegmentArchivingRequestUrlProvider($processNewSegmentsFrom, $mockSegmentEditorModel, null, Date::factory(self::TEST_NOW)); } }
\ No newline at end of file |