PDF rausgenommen
This commit is contained in:
56
msd2/tracking/piwik/core/Scheduler/Schedule/Daily.php
Normal file
56
msd2/tracking/piwik/core/Scheduler/Schedule/Daily.php
Normal file
@ -0,0 +1,56 @@
|
||||
<?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\Scheduler\Schedule;
|
||||
|
||||
use Exception;
|
||||
|
||||
/**
|
||||
* Daily class is used to schedule tasks every day.
|
||||
*
|
||||
* @see \Piwik\Scheduler\Task
|
||||
*/
|
||||
class Daily extends Schedule
|
||||
{
|
||||
/**
|
||||
* @see ScheduledTime::getRescheduledTime
|
||||
* @return int
|
||||
*
|
||||
*/
|
||||
public function getRescheduledTime()
|
||||
{
|
||||
$currentTime = $this->getTime();
|
||||
|
||||
// Add one day
|
||||
$rescheduledTime = mktime(date('H', $currentTime),
|
||||
date('i', $currentTime),
|
||||
date('s', $currentTime),
|
||||
date('n', $currentTime),
|
||||
date('j', $currentTime) + 1,
|
||||
date('Y', $currentTime)
|
||||
);
|
||||
|
||||
// Adjusts the scheduled hour
|
||||
$rescheduledTime = $this->adjustHour($rescheduledTime);
|
||||
$rescheduledTime = $this->adjustTimezone($rescheduledTime);
|
||||
|
||||
return $rescheduledTime;
|
||||
}
|
||||
|
||||
/**
|
||||
* @see ScheduledTime::setDay
|
||||
* @param int $_day
|
||||
* @throws \Exception
|
||||
* @ignore
|
||||
*/
|
||||
public function setDay($_day)
|
||||
{
|
||||
throw new Exception("Method not supported");
|
||||
}
|
||||
}
|
61
msd2/tracking/piwik/core/Scheduler/Schedule/Hourly.php
Normal file
61
msd2/tracking/piwik/core/Scheduler/Schedule/Hourly.php
Normal file
@ -0,0 +1,61 @@
|
||||
<?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\Scheduler\Schedule;
|
||||
|
||||
use Exception;
|
||||
|
||||
/**
|
||||
* Hourly class is used to schedule tasks every hour.
|
||||
*
|
||||
* @see \Piwik\Scheduler\Task
|
||||
*/
|
||||
class Hourly extends Schedule
|
||||
{
|
||||
/**
|
||||
* @see ScheduledTime::getRescheduledTime
|
||||
* @return int
|
||||
*/
|
||||
public function getRescheduledTime()
|
||||
{
|
||||
$currentTime = $this->getTime();
|
||||
|
||||
// Adds one hour and reset the number of minutes
|
||||
$rescheduledTime = mktime(date('H', $currentTime) + 1,
|
||||
0,
|
||||
date('s', $currentTime),
|
||||
date('n', $currentTime),
|
||||
date('j', $currentTime),
|
||||
date('Y', $currentTime)
|
||||
);
|
||||
return $rescheduledTime;
|
||||
}
|
||||
|
||||
/**
|
||||
* @see ScheduledTime::setHour
|
||||
* @param int $_hour
|
||||
* @throws \Exception
|
||||
* @return int
|
||||
*/
|
||||
public function setHour($_hour)
|
||||
{
|
||||
throw new Exception("Method not supported");
|
||||
}
|
||||
|
||||
/**
|
||||
* @see ScheduledTime::setDay
|
||||
* @param int $_day
|
||||
* @throws \Exception
|
||||
* @return int
|
||||
*/
|
||||
public function setDay($_day)
|
||||
{
|
||||
throw new Exception("Method not supported");
|
||||
}
|
||||
}
|
144
msd2/tracking/piwik/core/Scheduler/Schedule/Monthly.php
Normal file
144
msd2/tracking/piwik/core/Scheduler/Schedule/Monthly.php
Normal file
@ -0,0 +1,144 @@
|
||||
<?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\Scheduler\Schedule;
|
||||
|
||||
use Exception;
|
||||
|
||||
/**
|
||||
* Monthly class is used to schedule tasks every month.
|
||||
*
|
||||
* @see \Piwik\Scheduler\Task
|
||||
*/
|
||||
class Monthly extends Schedule
|
||||
{
|
||||
/**
|
||||
* List of available week number strings used in setDayOfWeekFromString.
|
||||
*/
|
||||
private static $weekNumberStringToInt = array('first' => 0, 'second' => 1, 'third' => 2, 'fourth' => 3);
|
||||
|
||||
/**
|
||||
* Day of the week for scheduled time.
|
||||
*
|
||||
* @var int
|
||||
*/
|
||||
private $dayOfWeek = null;
|
||||
|
||||
/**
|
||||
* Week number for scheduled time.
|
||||
*
|
||||
* @var int
|
||||
*/
|
||||
private $week = null;
|
||||
|
||||
public function setDayOfWeekFromString($day)
|
||||
{
|
||||
@list($weekNumberString, $dayNumberString) = explode(' ', $day);
|
||||
|
||||
// get day number
|
||||
$day = Weekly::getDayIntFromString($dayNumberString) % 7;
|
||||
|
||||
// get week number
|
||||
$weekNumberString = strtolower($weekNumberString);
|
||||
if (isset(self::$weekNumberStringToInt[$weekNumberString])) {
|
||||
$week = self::$weekNumberStringToInt[$weekNumberString];
|
||||
} else {
|
||||
throw new Exception("Invalid week describer in Schedule\\Monthly::setDayOfWeekFromString: '$weekNumberString'. "
|
||||
. "Supported values are 'first', 'second', 'third', 'fourth'.");
|
||||
}
|
||||
|
||||
$this->setDayOfWeek($day, $week);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return int
|
||||
*/
|
||||
public function getRescheduledTime()
|
||||
{
|
||||
$currentTime = $this->getTime();
|
||||
|
||||
// Adds one month
|
||||
$rescheduledTime = mktime(date('H', $currentTime),
|
||||
date('i', $currentTime),
|
||||
date('s', $currentTime),
|
||||
date('n', $currentTime) + 1,
|
||||
1,
|
||||
date('Y', $currentTime)
|
||||
);
|
||||
|
||||
$nextMonthLength = date('t', $rescheduledTime);
|
||||
|
||||
// Sets scheduled day
|
||||
$scheduledDay = date('j', $currentTime);
|
||||
|
||||
if ($this->day !== null) {
|
||||
$scheduledDay = $this->day;
|
||||
}
|
||||
|
||||
if ($this->dayOfWeek !== null
|
||||
&& $this->week !== null
|
||||
) {
|
||||
$newTime = $rescheduledTime + $this->week * 7 * 86400;
|
||||
while (date("w", $newTime) != $this->dayOfWeek % 7) {
|
||||
// modulus for sanity check
|
||||
|
||||
$newTime += 86400;
|
||||
}
|
||||
$scheduledDay = ($newTime - $rescheduledTime) / 86400 + 1;
|
||||
}
|
||||
|
||||
// Caps scheduled day
|
||||
if ($scheduledDay > $nextMonthLength) {
|
||||
$scheduledDay = $nextMonthLength;
|
||||
}
|
||||
|
||||
// Adjusts the scheduled day
|
||||
$rescheduledTime += ($scheduledDay - 1) * 86400;
|
||||
|
||||
// Adjusts the scheduled hour
|
||||
$rescheduledTime = $this->adjustHour($rescheduledTime);
|
||||
$rescheduledTime = $this->adjustTimezone($rescheduledTime);
|
||||
|
||||
return $rescheduledTime;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $_day the day to set, has to be >= 1 and < 32
|
||||
* @throws Exception if parameter _day is invalid
|
||||
*/
|
||||
public function setDay($_day)
|
||||
{
|
||||
if (!($_day >= 1 && $_day < 32)) {
|
||||
throw new Exception("Invalid day parameter, must be >=1 and < 32");
|
||||
}
|
||||
|
||||
$this->day = $_day;
|
||||
}
|
||||
|
||||
/**
|
||||
* Makes this scheduled time execute on a particular day of the week on each month.
|
||||
*
|
||||
* @param int $_day the day of the week to use, between 0-6 (inclusive). 0 -> Sunday
|
||||
* @param int $_week the week to use, between 0-3 (inclusive)
|
||||
* @throws Exception if either parameter is invalid
|
||||
*/
|
||||
public function setDayOfWeek($_day, $_week)
|
||||
{
|
||||
if (!($_day >= 0 && $_day < 7)) {
|
||||
throw new Exception("Invalid day of week parameter, must be >= 0 & < 7");
|
||||
}
|
||||
|
||||
if (!($_week >= 0 && $_week < 4)) {
|
||||
throw new Exception("Invalid week number, must be >= 1 & < 4");
|
||||
}
|
||||
|
||||
$this->dayOfWeek = $_day;
|
||||
$this->week = $_week;
|
||||
}
|
||||
}
|
224
msd2/tracking/piwik/core/Scheduler/Schedule/Schedule.php
Normal file
224
msd2/tracking/piwik/core/Scheduler/Schedule/Schedule.php
Normal file
@ -0,0 +1,224 @@
|
||||
<?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\Scheduler\Schedule;
|
||||
|
||||
use Exception;
|
||||
use Piwik\Date;
|
||||
|
||||
/**
|
||||
* Describes the interval on which a scheduled task is executed. Use the {@link factory()} method
|
||||
* to create Schedule instances.
|
||||
*
|
||||
* @see \Piwik\Scheduler\Task
|
||||
*/
|
||||
abstract class Schedule
|
||||
{
|
||||
const PERIOD_NEVER = 'never';
|
||||
const PERIOD_DAY = 'day';
|
||||
const PERIOD_WEEK = 'week';
|
||||
const PERIOD_MONTH = 'month';
|
||||
const PERIOD_HOUR = 'hour';
|
||||
const PERIOD_YEAR = 'year';
|
||||
const PERIOD_RANGE = 'range';
|
||||
|
||||
/**
|
||||
* @link http://php.net/manual/en/function.date.php, format string : 'G'
|
||||
* Defaults to midnight
|
||||
* @var integer
|
||||
*/
|
||||
protected $hour = 0;
|
||||
|
||||
/**
|
||||
* For weekly scheduling : http://php.net/manual/en/function.date.php, format string : 'N', defaults to Monday
|
||||
* For monthly scheduling : day of the month (1 to 31) (note: will be capped at the latest day available the
|
||||
* month), defaults to first day of the month
|
||||
* @var integer
|
||||
*/
|
||||
protected $day = 1;
|
||||
|
||||
protected $timezone = null;
|
||||
|
||||
/**
|
||||
* @param $period
|
||||
* @return Daily|Monthly|Weekly
|
||||
* @throws \Exception
|
||||
* @ignore
|
||||
*/
|
||||
public static function getScheduledTimeForPeriod($period)
|
||||
{
|
||||
switch ($period) {
|
||||
case self::PERIOD_MONTH:
|
||||
return new Monthly();
|
||||
case self::PERIOD_WEEK:
|
||||
return new Weekly();
|
||||
case self::PERIOD_DAY:
|
||||
return new Daily();
|
||||
case self::PERIOD_HOUR:
|
||||
return new Hourly();
|
||||
|
||||
default:
|
||||
throw new Exception('period ' . $period . 'is undefined.');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the system time used by subclasses to compute schedulings.
|
||||
* This method has been introduced so unit tests can override the current system time.
|
||||
* @return int
|
||||
*/
|
||||
protected function getTime()
|
||||
{
|
||||
return time();
|
||||
}
|
||||
|
||||
/**
|
||||
* Computes the next scheduled time based on the system time at which the method has been called and
|
||||
* the underlying scheduling interval.
|
||||
*
|
||||
* @abstract
|
||||
* @return integer Returns the rescheduled time measured in the number of seconds since the Unix Epoch
|
||||
* @ignore
|
||||
*/
|
||||
abstract public function getRescheduledTime();
|
||||
|
||||
/**
|
||||
* Sets the day of the period to execute the scheduled task. Not a valid operation for all period types.
|
||||
*
|
||||
* @abstract
|
||||
* @param int $_day a number describing the day to set. Its meaning depends on the Schedule's period type.
|
||||
* @throws Exception if method not supported by subclass or parameter _day is invalid
|
||||
*/
|
||||
abstract public function setDay($_day);
|
||||
|
||||
/**
|
||||
* Sets the hour of the day on which the task should be executed.
|
||||
*
|
||||
* @param int $hour Must be `>= 0` and `< 24`.
|
||||
* @throws Exception If the current scheduled period is **hourly** or if `$hour` is invalid.
|
||||
* @api
|
||||
*/
|
||||
public function setHour($hour)
|
||||
{
|
||||
if (!($hour >= 0 && $hour < 24)) {
|
||||
throw new Exception("Invalid hour parameter, must be >=0 and < 24");
|
||||
}
|
||||
|
||||
$this->hour = $hour;
|
||||
}
|
||||
|
||||
/**
|
||||
* By setting a timezone you make sure the scheduled task will be run at the requested time in the
|
||||
* given timezone. This is useful for instance in case you want to make sure a task runs at midnight in a website's
|
||||
* timezone.
|
||||
*
|
||||
* @param string $timezone
|
||||
*/
|
||||
public function setTimezone($timezone)
|
||||
{
|
||||
$this->timezone = $timezone;
|
||||
}
|
||||
|
||||
protected function adjustTimezone($rescheduledTime)
|
||||
{
|
||||
if (is_null($this->timezone)) {
|
||||
return $rescheduledTime;
|
||||
}
|
||||
|
||||
$arbitraryDateInUTC = Date::factory('2011-01-01');
|
||||
$dateInTimezone = Date::factory($arbitraryDateInUTC, $this->timezone);
|
||||
|
||||
$midnightInTimezone = date('H', $dateInTimezone->getTimestamp());
|
||||
|
||||
if ($arbitraryDateInUTC->isEarlier($dateInTimezone)) {
|
||||
$hoursDifference = 0 - $midnightInTimezone;
|
||||
} else {
|
||||
$hoursDifference = 24 - $midnightInTimezone;
|
||||
}
|
||||
|
||||
$hoursDifference = $hoursDifference % 24;
|
||||
|
||||
$rescheduledTime += (3600 * $hoursDifference);
|
||||
|
||||
if ($this->getTime() > $rescheduledTime) {
|
||||
// make sure the rescheduled date is in the future
|
||||
$rescheduledTime = (24 * 3600) + $rescheduledTime;
|
||||
}
|
||||
|
||||
return $rescheduledTime;
|
||||
}
|
||||
|
||||
/**
|
||||
* Computes the delta in seconds needed to adjust the rescheduled time to the required hour.
|
||||
*
|
||||
* @param int $rescheduledTime The rescheduled time to be adjusted
|
||||
* @return int adjusted rescheduled time
|
||||
*/
|
||||
protected function adjustHour($rescheduledTime)
|
||||
{
|
||||
if ($this->hour !== null) {
|
||||
// Reset the number of minutes and set the scheduled hour to the one specified with setHour()
|
||||
$rescheduledTime = mktime($this->hour,
|
||||
0,
|
||||
date('s', $rescheduledTime),
|
||||
date('n', $rescheduledTime),
|
||||
date('j', $rescheduledTime),
|
||||
date('Y', $rescheduledTime)
|
||||
);
|
||||
}
|
||||
return $rescheduledTime;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a new Schedule instance using a string description of the scheduled period type
|
||||
* and a string description of the day within the period to execute the task on.
|
||||
*
|
||||
* @param string $periodType The scheduled period type. Can be `'hourly'`, `'daily'`, `'weekly'`, or `'monthly'`.
|
||||
* @param bool|false|int|string $periodDay A string describing the day within the scheduled period to execute
|
||||
* the task on. Only valid for week and month periods.
|
||||
*
|
||||
* If `'weekly'` is supplied for `$periodType`, this should be a day
|
||||
* of the week, for example, `'monday'` or `'tuesday'`.
|
||||
*
|
||||
* If `'monthly'` is supplied for `$periodType`, this can be a numeric
|
||||
* day in the month or a day in one week of the month. For example,
|
||||
* `12`, `23`, `'first sunday'` or `'fourth tuesday'`.
|
||||
* @return Hourly|Daily|Weekly|Monthly
|
||||
* @throws Exception
|
||||
* @api
|
||||
*/
|
||||
public static function factory($periodType, $periodDay = false)
|
||||
{
|
||||
switch ($periodType) {
|
||||
case 'hourly':
|
||||
return new Hourly();
|
||||
case 'daily':
|
||||
return new Daily();
|
||||
case 'weekly':
|
||||
$result = new Weekly();
|
||||
if ($periodDay !== false) {
|
||||
$result->setDay($periodDay);
|
||||
}
|
||||
return $result;
|
||||
case 'monthly':
|
||||
$result = new Monthly($periodDay);
|
||||
if ($periodDay !== false) {
|
||||
if (is_int($periodDay)) {
|
||||
$result->setDay($periodDay);
|
||||
} else {
|
||||
$result->setDayOfWeekFromString($periodDay);
|
||||
}
|
||||
}
|
||||
return $result;
|
||||
default:
|
||||
throw new Exception("Unsupported scheduled period type: '$periodType'. Supported values are"
|
||||
. " 'hourly', 'daily', 'weekly' or 'monthly'.");
|
||||
}
|
||||
}
|
||||
}
|
33
msd2/tracking/piwik/core/Scheduler/Schedule/SpecificTime.php
Normal file
33
msd2/tracking/piwik/core/Scheduler/Schedule/SpecificTime.php
Normal file
@ -0,0 +1,33 @@
|
||||
<?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\Scheduler\Schedule;
|
||||
|
||||
class SpecificTime extends Schedule
|
||||
{
|
||||
/**
|
||||
* @var int
|
||||
*/
|
||||
private $scheduledTime;
|
||||
|
||||
public function __construct($scheduledTime)
|
||||
{
|
||||
$this->scheduledTime = $scheduledTime;
|
||||
}
|
||||
|
||||
public function getRescheduledTime()
|
||||
{
|
||||
return $this->scheduledTime;
|
||||
}
|
||||
|
||||
public function setDay($_day)
|
||||
{
|
||||
throw new \Exception('not supported');
|
||||
}
|
||||
}
|
82
msd2/tracking/piwik/core/Scheduler/Schedule/Weekly.php
Normal file
82
msd2/tracking/piwik/core/Scheduler/Schedule/Weekly.php
Normal file
@ -0,0 +1,82 @@
|
||||
<?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\Scheduler\Schedule;
|
||||
|
||||
use Exception;
|
||||
|
||||
/**
|
||||
* Weekly class is used to schedule tasks every week.
|
||||
*
|
||||
* @see \Piwik\Scheduler\Task
|
||||
*/
|
||||
class Weekly extends Schedule
|
||||
{
|
||||
/**
|
||||
* @see ScheduledTime::getRescheduledTime
|
||||
* @return int
|
||||
*/
|
||||
public function getRescheduledTime()
|
||||
{
|
||||
$currentTime = $this->getTime();
|
||||
|
||||
$daysFromNow = 7;
|
||||
|
||||
// Adjusts the scheduled day
|
||||
if ($this->day !== null) {
|
||||
$daysFromNow = ($this->day - date('N', $currentTime) + 7) % 7;
|
||||
|
||||
if ($daysFromNow == 0) {
|
||||
$daysFromNow = 7;
|
||||
}
|
||||
}
|
||||
|
||||
// Adds correct number of days
|
||||
$rescheduledTime = mktime(date('H', $currentTime),
|
||||
date('i', $currentTime),
|
||||
date('s', $currentTime),
|
||||
date('n', $currentTime),
|
||||
date('j', $currentTime) + $daysFromNow,
|
||||
date('Y', $currentTime)
|
||||
);
|
||||
|
||||
// Adjusts the scheduled hour
|
||||
$rescheduledTime = $this->adjustHour($rescheduledTime);
|
||||
$rescheduledTime = $this->adjustTimezone($rescheduledTime);
|
||||
|
||||
return $rescheduledTime;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $day the day to set, has to be >= 1 and < 8
|
||||
* @throws Exception if parameter _day is invalid
|
||||
*/
|
||||
public function setDay($day)
|
||||
{
|
||||
if (!is_int($day)) {
|
||||
$day = self::getDayIntFromString($day);
|
||||
}
|
||||
|
||||
if (!($day >= 1 && $day < 8)) {
|
||||
throw new Exception("Invalid day parameter, must be >=1 and < 8");
|
||||
}
|
||||
|
||||
$this->day = $day;
|
||||
}
|
||||
|
||||
public static function getDayIntFromString($dayString)
|
||||
{
|
||||
$time = strtotime($dayString);
|
||||
if ($time === false) {
|
||||
throw new Exception("Invalid day string '$dayString'. Must be 'monday', 'tuesday', etc.");
|
||||
}
|
||||
|
||||
return date("N", $time);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user