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,66 +9,57 @@
namespace Piwik;
use Exception;
use Piwik\Container\StaticContainer;
use Piwik\Scheduler\Scheduler;
use Piwik\Scheduler\Task;
// When set to true, all scheduled tasks will be triggered in all requests (careful!)
define('DEBUG_FORCE_SCHEDULED_TASKS', false);
//define('DEBUG_FORCE_SCHEDULED_TASKS', true);
/**
* Manages scheduled task execution.
*
*
* A scheduled task is a callback that should be executed every so often (such as daily,
* weekly, monthly, etc.). They are registered with **TaskScheduler** through the
* {@hook TaskScheduler.getScheduledTasks} event.
*
* Tasks are executed when the cron archive.php script is executed.
*
* weekly, monthly, etc.). They are registered by extending {@link \Piwik\Plugin\Tasks}.
*
* Tasks are executed when the `core:archive` command is executed.
*
* ### Examples
*
*
* **Scheduling a task**
*
* // event handler for TaskScheduler.getScheduledTasks event
* public function getScheduledTasks(&$tasks)
*
* class Tasks extends \Piwik\Plugin\Tasks
* {
* $tasks[] = new ScheduledTask(
* 'Piwik\Plugins\CorePluginsAdmin\MarketplaceApiClient',
* 'clearAllCacheEntries',
* null,
* ScheduledTime::factory('daily'),
* ScheduledTask::LOWEST_PRIORITY
* );
* public function schedule()
* {
* $this->hourly('myTask'); // myTask() will be executed once every hour
* }
* public function myTask()
* {
* // do something
* }
* }
*
*
* **Executing all pending tasks**
*
*
* $results = TaskScheduler::runTasks();
* $task1Result = $results[0];
* $task1Name = $task1Result['task'];
* $task1Output = $task1Result['output'];
*
*
* echo "Executed task '$task1Name'. Task output:\n$task1Output";
*
* @method static \Piwik\TaskScheduler getInstance()
* @deprecated Use Piwik\Scheduler\Scheduler instead
* @see \Piwik\Scheduler\Scheduler
*/
class TaskScheduler extends Singleton
class TaskScheduler
{
const GET_TASKS_EVENT = "TaskScheduler.getScheduledTasks";
private $isRunning = false;
private $timetable = null;
public function __construct()
{
$this->timetable = new ScheduledTaskTimetable();
}
/**
* Executes tasks that are scheduled to run, then reschedules them.
*
* @return array An array describing the results of scheduled task execution. Each element
* in the array will have the following format:
*
*
* ```
* array(
* 'task' => 'task name',
@ -76,97 +67,33 @@ class TaskScheduler extends Singleton
* )
* ```
*/
static public function runTasks()
public static function runTasks()
{
return self::getInstance()->doRunTasks();
}
private function doRunTasks()
{
// collect tasks
$tasks = array();
/**
* Triggered during scheduled task execution. Collects all the tasks to run.
*
* Subscribe to this event to schedule code execution on an hourly, daily, weekly or monthly
* basis.
*
* **Example**
*
* public function getScheduledTasks(&$tasks)
* {
* $tasks[] = new ScheduledTask(
* 'Piwik\Plugins\CorePluginsAdmin\MarketplaceApiClient',
* 'clearAllCacheEntries',
* null,
* ScheduledTime::factory('daily'),
* ScheduledTask::LOWEST_PRIORITY
* );
* }
*
* @param ScheduledTask[] &$tasks List of tasks to run periodically.
*/
Piwik::postEvent(self::GET_TASKS_EVENT, array(&$tasks));
/** @var ScheduledTask[] $tasks */
// remove from timetable tasks that are not active anymore
$this->timetable->removeInactiveTasks($tasks);
// for every priority level, starting with the highest and concluding with the lowest
$executionResults = array();
for ($priority = ScheduledTask::HIGHEST_PRIORITY;
$priority <= ScheduledTask::LOWEST_PRIORITY;
++$priority) {
// loop through each task
foreach ($tasks as $task) {
// if the task does not have the current priority level, don't execute it yet
if ($task->getPriority() != $priority) {
continue;
}
$taskName = $task->getName();
$shouldExecuteTask = $this->timetable->shouldExecuteTask($taskName);
if ($this->timetable->taskShouldBeRescheduled($taskName)) {
$this->timetable->rescheduleTask($task);
}
if ($shouldExecuteTask) {
$this->isRunning = true;
$message = self::executeTask($task);
$this->isRunning = false;
$executionResults[] = array('task' => $taskName, 'output' => $message);
}
}
}
return $executionResults;
return self::getInstance()->run();
}
/**
* Determines a task's scheduled time and persists it, overwriting the previous scheduled time.
*
*
* Call this method if your task's scheduled time has changed due to, for example, an option that
* was changed.
*
* @param ScheduledTask $task Describes the scheduled task being rescheduled.
*
* @param Task $task Describes the scheduled task being rescheduled.
* @api
*/
static public function rescheduleTask(ScheduledTask $task)
public static function rescheduleTask(Task $task)
{
self::getInstance()->timetable->rescheduleTask($task);
self::getInstance()->rescheduleTask($task);
}
/**
* Returns true if the TaskScheduler is currently running a scheduled task.
*
*
* @return bool
*/
static public function isTaskBeingExecuted()
public static function isTaskBeingExecuted()
{
return self::getInstance()->isRunning;
return self::getInstance()->isRunningTask();
}
/**
@ -178,27 +105,16 @@ class TaskScheduler extends Singleton
* @return mixed int|bool The time in miliseconds when the scheduled task will be executed
* next or false if it is not scheduled to run.
*/
static public function getScheduledTimeForMethod($className, $methodName, $methodParameter = null)
public static function getScheduledTimeForMethod($className, $methodName, $methodParameter = null)
{
return self::getInstance()->timetable->getScheduledTimeForMethod($className, $methodName, $methodParameter);
return self::getInstance()->getScheduledTimeForMethod($className, $methodName, $methodParameter);
}
/**
* Executes the given taks
*
* @param ScheduledTask $task
* @return string
* @return Scheduler
*/
static private function executeTask($task)
private static function getInstance()
{
try {
$timer = new Timer();
call_user_func(array($task->getObjectInstance(), $task->getMethodName()), $task->getMethodParameter());
$message = $timer->__toString();
} catch (Exception $e) {
$message = 'ERROR: ' . $e->getMessage();
}
return $message;
return StaticContainer::getContainer()->get('Piwik\Scheduler\Scheduler');
}
}