update Piwik to version 2.16 (fixes #91)

This commit is contained in:
oliver 2016-04-10 18:55:57 +02:00
commit d885a4baa9
5833 changed files with 418860 additions and 226988 deletions

View file

@ -1,6 +1,6 @@
<?php
/**
* Piwik - Open source web analytics
* Piwik - free/libre analytics platform
*
* @link http://piwik.org
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
@ -9,10 +9,11 @@
namespace Piwik\Period;
use Exception;
use Piwik\Cache;
use Piwik\Common;
use Piwik\Container\StaticContainer;
use Piwik\Date;
use Piwik\Period;
use Piwik\Piwik;
/**
* Arbitrary date range representation.
@ -23,15 +24,22 @@ use Piwik\Piwik;
* date=2007-07-24,2013-11-15).
*
* The range period differs from other periods mainly in that since it is arbitrary,
* range periods are not pre-archived by the **archive.php** cron script.
* range periods are not pre-archived by the cron core:archive command.
*
* @api
*/
class Range extends Period
{
const PERIOD_ID = 5;
protected $label = 'range';
protected $today;
/**
* @var null|Date
*/
protected $defaultEndDate;
/**
* Constructor.
*
@ -45,13 +53,52 @@ class Range extends Period
public function __construct($strPeriod, $strDate, $timezone = 'UTC', $today = false)
{
$this->strPeriod = $strPeriod;
$this->strDate = $strDate;
$this->strDate = $strDate;
$this->timezone = $timezone;
$this->defaultEndDate = null;
$this->timezone = $timezone;
if ($today === false) {
$today = Date::factory('now', $this->timezone);
}
$this->today = $today;
$this->translator = StaticContainer::get('Piwik\Translation\Translator');
}
private function getCache()
{
return Cache::getTransientCache();
}
private function getCacheId()
{
$end = '';
if ($this->defaultEndDate) {
$end = $this->defaultEndDate->getTimestamp();
}
$today = $this->today->getTimestamp();
return 'range' . $this->strPeriod . $this->strDate . $this->timezone . $end . $today;
}
private function loadAllFromCache()
{
$range = $this->getCache()->fetch($this->getCacheId());
if (!empty($range)) {
foreach ($range as $key => $val) {
$this->$key = $val;
}
}
}
private function cacheAll()
{
$props = get_object_vars($this);
$this->getCache()->save($this->getCacheId(), $props);
}
/**
@ -61,14 +108,7 @@ class Range extends Period
*/
public function getLocalizedShortString()
{
//"30 Dec 08 - 26 Feb 09"
$dateStart = $this->getDateStart();
$dateEnd = $this->getDateEnd();
$template = Piwik::translate('CoreHome_ShortDateFormatWithYear');
$shortDateStart = $dateStart->getLocalized($template);
$shortDateEnd = $dateEnd->getLocalized($template);
$out = "$shortDateStart - $shortDateEnd";
return $out;
return $this->getTranslatedRange($this->getRangeFormat(true));
}
/**
@ -78,7 +118,7 @@ class Range extends Period
*/
public function getLocalizedLongString()
{
return $this->getLocalizedShortString();
return $this->getTranslatedRange($this->getRangeFormat());
}
/**
@ -90,9 +130,11 @@ class Range extends Period
public function getDateStart()
{
$dateStart = parent::getDateStart();
if (empty($dateStart)) {
throw new Exception("Specified date range is invalid.");
}
return $dateStart;
}
@ -103,7 +145,7 @@ class Range extends Period
*/
public function getPrettyString()
{
$out = Piwik::translate('General_DateRangeFromTo', array($this->getDateStart()->toString(), $this->getDateEnd()->toString()));
$out = $this->translator->translate('General_DateRangeFromTo', array($this->getDateStart()->toString(), $this->getDateEnd()->toString()));
return $out;
}
@ -150,6 +192,12 @@ class Range extends Period
return;
}
$this->loadAllFromCache();
if ($this->subperiodsProcessed) {
return;
}
parent::generate();
if (preg_match('/(last|previous)([0-9]*)/', $this->strDate, $regs)) {
@ -180,20 +228,16 @@ class Range extends Period
// last1 means only one result ; last2 means 2 results so we remove only 1 to the days/weeks/etc
$lastN--;
$lastN = abs($lastN);
if ($lastN < 0) {
$lastN = 0;
}
$startDate = $endDate->addPeriod(-1 * $lastN, $period);
} elseif ($dateRange = Range::parseDateRange($this->strDate)) {
$strDateStart = $dateRange[1];
$strDateEnd = $dateRange[2];
$startDate = Date::factory($strDateStart);
if ($strDateEnd == 'today') {
$strDateEnd = 'now';
} elseif ($strDateEnd == 'yesterday') {
$strDateEnd = 'yesterdaySameTime';
}
// we set the timezone in the Date object only if the date is relative eg. 'today', 'yesterday', 'now'
$timezone = null;
if (strpos($strDateEnd, '-') === false) {
@ -201,16 +245,20 @@ class Range extends Period
}
$endDate = Date::factory($strDateEnd, $timezone);
} else {
throw new Exception(Piwik::translate('General_ExceptionInvalidDateRange', array($this->strDate, ' \'lastN\', \'previousN\', \'YYYY-MM-DD,YYYY-MM-DD\'')));
throw new Exception($this->translator->translate('General_ExceptionInvalidDateRange', array($this->strDate, ' \'lastN\', \'previousN\', \'YYYY-MM-DD,YYYY-MM-DD\'')));
}
if ($this->strPeriod != 'range') {
$this->fillArraySubPeriods($startDate, $endDate, $this->strPeriod);
$this->cacheAll();
return;
}
$this->processOptimalSubperiods($startDate, $endDate);
// When period=range, we want End Date to be the actual specified end date,
// rather than the end of the month / week / whatever is used for processing this range
$this->endDate = $endDate;
$this->cacheAll();
}
/**
@ -220,12 +268,14 @@ class Range extends Period
* @param string $dateString
* @return mixed array(1 => dateStartString, 2 => dateEndString) or `false` if the input was not a date range.
*/
static public function parseDateRange($dateString)
public static function parseDateRange($dateString)
{
$matched = preg_match('/^([0-9]{4}-[0-9]{1,2}-[0-9]{1,2}),(([0-9]{4}-[0-9]{1,2}-[0-9]{1,2})|today|now|yesterday)$/D', trim($dateString), $regs);
if (empty($matched)) {
return false;
}
return $regs;
}
@ -241,6 +291,7 @@ class Range extends Period
if (!is_null($this->endDate)) {
return $this->endDate;
}
return parent::getDateEnd();
}
@ -278,7 +329,7 @@ class Range extends Period
) {
$this->addSubperiod($year);
$endOfPeriod = $endOfYear;
} else if ($startDate == $startOfMonth
} elseif ($startDate == $startOfMonth
&& ($endOfMonth->isEarlier($endDate)
|| $endOfMonth == $endDate
|| $endOfMonth->isLater($this->today)
@ -338,14 +389,14 @@ class Range extends Period
protected function fillArraySubPeriods($startDate, $endDate, $period)
{
$arrayPeriods = array();
$endSubperiod = Period::factory($period, $endDate);
$endSubperiod = Period\Factory::build($period, $endDate);
$arrayPeriods[] = $endSubperiod;
// set end date to start of end period since we're comparing against start date.
$endDate = $endSubperiod->getDateStart();
while ($endDate->isLater($startDate)) {
$endDate = $endDate->addPeriod(-1, $period);
$subPeriod = Period::factory($period, $endDate);
$subPeriod = Period\Factory::build($period, $endDate);
$arrayPeriods[] = $subPeriod;
}
$arrayPeriods = array_reverse($arrayPeriods);
@ -400,8 +451,9 @@ class Range extends Period
$strLastDate = false;
$lastPeriod = false;
if ($period != 'range' && !preg_match('/(last|previous)([0-9]*)/', $date, $regs)) {
if (strpos($date, ',')) // date in the form of 2011-01-01,2011-02-02
{
if (strpos($date, ',')) {
// date in the form of 2011-01-01,2011-02-02
$rangePeriod = new Range($period, $date);
$lastStartDate = $rangePeriod->getDateStart()->subPeriod($subXPeriods, $period);
@ -425,7 +477,7 @@ class Range extends Period
* @param int $lastN The number of periods of type `$period` that the result range should
* span.
* @param string $endDate The desired end date of the range.
* @param Site $site The site whose timezone should be used.
* @param \Piwik\Site $site The site whose timezone should be used.
* @return string The date range string, eg, `'2012-01-02,2013-01-02'`.
* @api
*/
@ -434,6 +486,7 @@ class Range extends Period
$last30Relative = new Range($period, $lastN, $site->getTimezone());
$last30Relative->setDefaultEndDate(Date::factory($endDate));
$date = $last30Relative->getDateStart()->toString() . "," . $last30Relative->getDateEnd()->toString();
return $date;
}
@ -448,4 +501,28 @@ class Range extends Period
return $isEndOfWeekLaterThanEndDate;
}
/**
* Returns the date range string comprising two dates
*
* @return string eg, `'2012-01-01,2012-01-31'`.
*/
public function getRangeString()
{
$dateStart = $this->getDateStart();
$dateEnd = $this->getDateEnd();
return $dateStart->toString("Y-m-d") . "," . $dateEnd->toString("Y-m-d");
}
public function getImmediateChildPeriodLabel()
{
$subperiods = $this->getSubperiods();
return reset($subperiods)->getImmediateChildPeriodLabel();
}
public function getParentPeriodLabel()
{
return null;
}
}