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,13 +9,15 @@
namespace Piwik\Period;
use Exception;
use Piwik\Date;
use Piwik\Period;
use Piwik\Piwik;
/**
*/
class Day extends Period
{
const PERIOD_ID = 1;
protected $label = 'day';
/**
@ -37,8 +39,8 @@ class Day extends Period
public function getLocalizedShortString()
{
//"Mon 15 Aug"
$date = $this->getDateStart();
$out = $date->getLocalized(Piwik::translate('CoreHome_ShortDateFormat'));
$date = $this->getDateStart();
$out = $date->getLocalized(Date::DATE_FORMAT_DAY_MONTH);
return $out;
}
@ -50,9 +52,8 @@ class Day extends Period
public function getLocalizedLongString()
{
//"Mon 15 Aug"
$date = $this->getDateStart();
$template = Piwik::translate('CoreHome_DateFormat');
$out = $date->getLocalized($template);
$date = $this->getDateStart();
$out = $date->getLocalized(Date::DATE_FORMAT_LONG);
return $out;
}
@ -99,4 +100,14 @@ class Day extends Period
{
return $this->toString();
}
public function getImmediateChildPeriodLabel()
{
return null;
}
public function getParentPeriodLabel()
{
return 'week';
}
}

View file

@ -0,0 +1,130 @@
<?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\Period;
use Exception;
use Piwik\Date;
use Piwik\Period;
use Piwik\Piwik;
class Factory
{
/**
* Creates a new Period instance with a period ID and {@link Date} instance.
*
* _Note: This method cannot create {@link Period\Range} periods._
*
* @param string $period `"day"`, `"week"`, `"month"`, `"year"`, `"range"`.
* @param Date|string $date A date within the period or the range of dates.
* @param Date|string $timezone Optional timezone that will be used only when $period is 'range' or $date is 'last|previous'
* @throws Exception If `$strPeriod` is invalid.
* @return \Piwik\Period
*/
public static function build($period, $date, $timezone = 'UTC')
{
self::checkPeriodIsEnabled($period);
if (is_string($date)) {
if (Period::isMultiplePeriod($date, $period)
|| $period == 'range') {
return new Range($period, $date, $timezone);
}
$date = Date::factory($date);
}
switch ($period) {
case 'day':
return new Day($date);
break;
case 'week':
return new Week($date);
break;
case 'month':
return new Month($date);
break;
case 'year':
return new Year($date);
break;
}
}
public static function checkPeriodIsEnabled($period)
{
if (!self::isPeriodEnabledForAPI($period)) {
self::throwExceptionInvalidPeriod($period);
}
}
/**
* @param $strPeriod
* @throws \Exception
*/
private static function throwExceptionInvalidPeriod($strPeriod)
{
$periods = self::getPeriodsEnabledForAPI();
$periods = implode(", ", $periods);
$message = Piwik::translate('General_ExceptionInvalidPeriod', array($strPeriod, $periods));
throw new Exception($message);
}
/**
* Creates a Period instance using a period, date and timezone.
*
* @param string $timezone The timezone of the date. Only used if `$date` is `'now'`, `'today'`,
* `'yesterday'` or `'yesterdaySameTime'`.
* @param string $period The period string: `"day"`, `"week"`, `"month"`, `"year"`, `"range"`.
* @param string $date The date or date range string. Can be a special value including
* `'now'`, `'today'`, `'yesterday'`, `'yesterdaySameTime'`.
* @return \Piwik\Period
*/
public static function makePeriodFromQueryParams($timezone, $period, $date)
{
if (empty($timezone)) {
$timezone = 'UTC';
}
if ($period == 'range') {
self::checkPeriodIsEnabled('range');
$oPeriod = new Range('range', $date, $timezone, Date::factory('today', $timezone));
} else {
if (!($date instanceof Date)) {
if ($date == 'now' || $date == 'today') {
$date = date('Y-m-d', Date::factory('now', $timezone)->getTimestamp());
} elseif ($date == 'yesterday' || $date == 'yesterdaySameTime') {
$date = date('Y-m-d', Date::factory('now', $timezone)->subDay(1)->getTimestamp());
}
$date = Date::factory($date);
}
$oPeriod = Factory::build($period, $date);
}
return $oPeriod;
}
/**
* @param $period
* @return bool
*/
public static function isPeriodEnabledForAPI($period)
{
$periodValidator = new PeriodValidator();
return $periodValidator->isPeriodAllowedForAPI($period);
}
/**
* @return array
*/
public static function getPeriodsEnabledForAPI()
{
$periodValidator = new PeriodValidator();
return $periodValidator->getPeriodsAllowedForAPI();
}
}

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
@ -8,13 +8,15 @@
*/
namespace Piwik\Period;
use Piwik\Date;
use Piwik\Period;
use Piwik\Piwik;
/**
*/
class Month extends Period
{
const PERIOD_ID = 3;
protected $label = 'month';
/**
@ -25,7 +27,7 @@ class Month extends Period
public function getLocalizedShortString()
{
//"Aug 09"
$out = $this->getDateStart()->getLocalized(Piwik::translate('CoreHome_ShortMonthFormat'));
$out = $this->getDateStart()->getLocalized(Date::DATE_FORMAT_MONTH_SHORT);
return $out;
}
@ -37,7 +39,7 @@ class Month extends Period
public function getLocalizedLongString()
{
//"August 2009"
$out = $this->getDateStart()->getLocalized(Piwik::translate('CoreHome_LongMonthFormat'));
$out = $this->getDateStart()->getLocalized(Date::DATE_FORMAT_MONTH_LONG);
return $out;
}
@ -60,15 +62,65 @@ class Month extends Period
if ($this->subperiodsProcessed) {
return;
}
parent::generate();
$date = $this->date;
$startMonth = $date->setDay(1);
$currentDay = clone $startMonth;
while ($currentDay->compareMonth($startMonth) == 0) {
$this->addSubperiod(new Day($currentDay));
$currentDay = $currentDay->addDay(1);
$startMonth = $date->setDay(1)->setTime('00:00:00');
$endMonth = $startMonth->addPeriod(1, 'month')->setDay(1)->subDay(1);
$this->processOptimalSubperiods($startMonth, $endMonth);
}
/**
* Determine which kind of period is best to use.
* See Range.test.php
*
* @param Date $startDate
* @param Date $endDate
*/
protected function processOptimalSubperiods($startDate, $endDate)
{
while ($startDate->isEarlier($endDate)
|| $startDate == $endDate) {
$week = new Week($startDate);
$startOfWeek = $week->getDateStart();
$endOfWeek = $week->getDateEnd();
if ($endOfWeek->isLater($endDate)) {
$this->fillDayPeriods($startDate, $endDate);
} elseif ($startOfWeek == $startDate) {
$this->addSubperiod($week);
} else {
$this->fillDayPeriods($startDate, $endOfWeek);
}
$startDate = $endOfWeek->addDay(1);
}
}
/**
* Fills the periods from startDate to endDate with days
*
* @param Date $startDate
* @param Date $endDate
*/
private function fillDayPeriods($startDate, $endDate)
{
while (($startDate->isEarlier($endDate) || $startDate == $endDate)) {
$this->addSubperiod(new Day($startDate));
$startDate = $startDate->addDay(1);
}
}
public function getImmediateChildPeriodLabel()
{
return 'week';
}
public function getParentPeriodLabel()
{
return 'year';
}
}

View file

@ -0,0 +1,52 @@
<?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\Period;
use Piwik\Config;
class PeriodValidator
{
/**
* @param string $period
* @return bool
*/
public function isPeriodAllowedForUI($period)
{
return in_array($period, $this->getPeriodsAllowedForUI());
}
/**
* @param string $period
* @return bool
*/
public function isPeriodAllowedForAPI($period)
{
return in_array($period, $this->getPeriodsAllowedForAPI());
}
/**
* @return string[]
*/
public function getPeriodsAllowedForUI()
{
$periodsAllowed = Config::getInstance()->General['enabled_periods_UI'];
return array_map('trim', explode(',', $periodsAllowed));
}
/**
* @return string[]
*/
public function getPeriodsAllowedForAPI()
{
$periodsAllowed = Config::getInstance()->General['enabled_periods_API'];
return array_map('trim', explode(',', $periodsAllowed));
}
}

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;
}
}

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
@ -8,14 +8,14 @@
*/
namespace Piwik\Period;
use Piwik\Period;
use Piwik\Piwik;
/**
*/
class Week extends Period
{
const PERIOD_ID = 2;
protected $label = 'week';
/**
@ -25,13 +25,7 @@ class Week extends Period
*/
public function getLocalizedShortString()
{
//"30 Dec - 6 Jan 09"
$dateStart = $this->getDateStart();
$dateEnd = $this->getDateEnd();
$string = Piwik::translate('CoreHome_ShortWeekFormat');
$string = self::getTranslatedRange($string, $dateStart, $dateEnd);
return $string;
return $this->getTranslatedRange($this->getRangeFormat(true));
}
/**
@ -41,25 +35,8 @@ class Week extends Period
*/
public function getLocalizedLongString()
{
$format = Piwik::translate('CoreHome_LongWeekFormat');
$string = self::getTranslatedRange($format, $this->getDateStart(), $this->getDateEnd());
return Piwik::translate('CoreHome_PeriodWeek') . " " . $string;
}
/**
* @param string $format
* @param \Piwik\Date $dateStart
* @param \Piwik\Date $dateEnd
*
* @return mixed
*/
static protected function getTranslatedRange($format, $dateStart, $dateEnd)
{
$string = str_replace('From%', '%', $format);
$string = $dateStart->getLocalized($string);
$string = str_replace('To%', '%', $string);
$string = $dateEnd->getLocalized($string);
return $string;
$string = $this->getTranslatedRange($this->getRangeFormat());
return $this->translator->translate('Intl_PeriodWeek') . " " . $string;
}
/**
@ -69,10 +46,11 @@ class Week extends Period
*/
public function getPrettyString()
{
$out = Piwik::translate('General_DateRangeFromTo',
array($this->getDateStart()->toString(),
$this->getDateEnd()->toString())
);
$dateStart = $this->getDateStart();
$dateEnd = $this->getDateEnd();
$out = $this->translator->translate('General_DateRangeFromTo', array($dateStart->toString(), $dateEnd->toString()));
return $out;
}
@ -84,6 +62,7 @@ class Week extends Period
if ($this->subperiodsProcessed) {
return;
}
parent::generate();
$date = $this->date;
@ -99,4 +78,14 @@ class Week extends Period
$currentDay = $currentDay->addDay(1);
}
}
public function getImmediateChildPeriodLabel()
{
return 'day';
}
public function getParentPeriodLabel()
{
return 'month';
}
}

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
@ -15,6 +15,8 @@ use Piwik\Period;
*/
class Year extends Period
{
const PERIOD_ID = 4;
protected $label = 'year';
/**
@ -35,7 +37,7 @@ class Year extends Period
public function getLocalizedLongString()
{
//"2009"
$out = $this->getDateStart()->getLocalized("%longYear%");
$out = $this->getDateStart()->getLocalized(Date::DATE_FORMAT_YEAR);
return $out;
}
@ -58,6 +60,7 @@ class Year extends Period
if ($this->subperiodsProcessed) {
return;
}
parent::generate();
$year = $this->date->toString("Y");
@ -75,13 +78,25 @@ class Year extends Period
* @param string $format
* @return array
*/
function toString($format = 'ignored')
public function toString($format = 'ignored')
{
$this->generate();
$stringMonth = array();
foreach ($this->subperiods as $month) {
$stringMonth[] = $month->getDateStart()->toString("Y") . "-" . $month->getDateStart()->toString("m") . "-01";
}
return $stringMonth;
}
public function getImmediateChildPeriodLabel()
{
return 'month';
}
public function getParentPeriodLabel()
{
return null;
}
}