2010-04-17 03:13:30 -04:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @package phpBB3
|
|
|
|
* @copyright (c) 2010 phpBB Group
|
|
|
|
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @ignore
|
|
|
|
*/
|
|
|
|
if (!defined('IN_PHPBB'))
|
|
|
|
{
|
|
|
|
exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Cron manager class.
|
|
|
|
*
|
|
|
|
* Finds installed cron tasks, stores task objects, provides task selection.
|
|
|
|
*
|
|
|
|
* @package phpBB3
|
|
|
|
*/
|
2010-10-28 22:26:49 +02:00
|
|
|
class phpbb_cron_manager
|
2010-04-17 03:13:30 -04:00
|
|
|
{
|
2010-12-17 01:15:57 +01:00
|
|
|
/**
|
|
|
|
* Set of phpbb_cron_task_wrapper objects.
|
|
|
|
* Array holding all tasks that have been found.
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
2010-04-17 03:13:30 -04:00
|
|
|
private $tasks = array();
|
|
|
|
|
2010-12-17 01:15:57 +01:00
|
|
|
/**
|
|
|
|
* Constructor. Loads all available tasks.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2010-04-17 03:13:30 -04:00
|
|
|
public function __construct()
|
|
|
|
{
|
2010-10-28 22:15:30 +02:00
|
|
|
$task_names = $this->find_cron_task_names();
|
|
|
|
$this->load_tasks($task_names);
|
2010-04-17 03:13:30 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2010-10-29 11:50:39 +02:00
|
|
|
* Finds cron task names.
|
2010-04-17 03:13:30 -04:00
|
|
|
*
|
|
|
|
* A cron task file must follow the naming convention:
|
2010-10-28 22:15:30 +02:00
|
|
|
* includes/cron/task/$mod/$name.php.
|
2010-04-17 03:13:30 -04:00
|
|
|
* $mod is core for tasks that are part of phpbb.
|
|
|
|
* Modifications should use their name as $mod.
|
|
|
|
* $name is the name of the cron task.
|
2010-10-29 11:50:39 +02:00
|
|
|
* Cron task is expected to be a class named phpbb_cron_task_${mod}_${name}.
|
2010-04-17 03:13:30 -04:00
|
|
|
*
|
|
|
|
* Todo: consider caching found task file list in global cache.
|
2010-12-17 01:15:57 +01:00
|
|
|
*
|
|
|
|
* @return array Array of strings
|
2010-04-17 03:13:30 -04:00
|
|
|
*/
|
2010-10-28 22:15:30 +02:00
|
|
|
public function find_cron_task_names()
|
2010-04-17 03:13:30 -04:00
|
|
|
{
|
|
|
|
global $phpbb_root_path, $phpEx;
|
|
|
|
|
2010-10-28 22:15:30 +02:00
|
|
|
$tasks_root_path = $phpbb_root_path . 'includes/cron/task/';
|
|
|
|
|
|
|
|
$task_names = array();
|
|
|
|
$ext = '.' . $phpEx;
|
|
|
|
$ext_length = strlen($ext);
|
|
|
|
|
|
|
|
$dh = opendir($tasks_root_path);
|
|
|
|
while (($mod = readdir($dh)) !== false)
|
2010-04-17 03:13:30 -04:00
|
|
|
{
|
|
|
|
// ignore ., .. and dot directories
|
|
|
|
// todo: change is_dir to account for symlinks
|
2010-10-28 22:15:30 +02:00
|
|
|
if ($mod[0] == '.' || !is_dir($tasks_root_path . $mod))
|
2010-04-17 03:13:30 -04:00
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2010-10-28 22:15:30 +02:00
|
|
|
$dh2 = opendir($tasks_root_path . $mod);
|
|
|
|
while (($file = readdir($dh2)) !== false)
|
2010-04-17 03:13:30 -04:00
|
|
|
{
|
2010-10-28 22:15:30 +02:00
|
|
|
$task_name = substr($file, 0, -$ext_length);
|
|
|
|
if (substr($file, -$ext_length) == $ext && $this->is_valid_name($mod) && $this->is_valid_name($task_name))
|
2010-04-18 13:48:32 -04:00
|
|
|
{
|
2010-10-28 22:15:30 +02:00
|
|
|
$full_task_name = $mod . '_' . $task_name;
|
|
|
|
$task_names[] = $full_task_name;
|
2010-04-18 13:48:32 -04:00
|
|
|
}
|
2010-04-17 03:13:30 -04:00
|
|
|
}
|
2010-10-28 22:15:30 +02:00
|
|
|
closedir($dh2);
|
2010-04-17 03:13:30 -04:00
|
|
|
}
|
2010-10-28 22:15:30 +02:00
|
|
|
closedir($dh);
|
|
|
|
|
|
|
|
return $task_names;
|
2010-04-17 03:13:30 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2010-12-17 01:15:57 +01:00
|
|
|
* Checks whether $name is a valid identifier, and
|
|
|
|
* therefore part of valid cron task class name.
|
|
|
|
*
|
|
|
|
* @param string $name Name to check
|
|
|
|
*
|
2010-12-17 01:20:23 +01:00
|
|
|
* @return bool
|
2010-04-17 03:13:30 -04:00
|
|
|
*/
|
|
|
|
public function is_valid_name($name)
|
|
|
|
{
|
2010-12-17 01:20:23 +01:00
|
|
|
return (bool) preg_match('/^[a-zA-Z][a-zA-Z0-9_]*$/', $name);
|
2010-04-17 03:13:30 -04:00
|
|
|
}
|
|
|
|
|
2010-12-17 01:15:57 +01:00
|
|
|
/**
|
|
|
|
* Loads tasks given by name, wraps them
|
|
|
|
* and puts them into $this->tasks.
|
|
|
|
*
|
|
|
|
* @param array $task_names Array of strings
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2010-10-28 22:15:30 +02:00
|
|
|
public function load_tasks($task_names)
|
2010-04-17 03:13:30 -04:00
|
|
|
{
|
2010-10-28 22:15:30 +02:00
|
|
|
foreach ($task_names as $task_name)
|
2010-04-17 03:13:30 -04:00
|
|
|
{
|
2010-12-14 13:26:42 +01:00
|
|
|
$class = "phpbb_cron_task_$task_name";
|
|
|
|
$task = new $class();
|
|
|
|
$wrapper = new phpbb_cron_task_wrapper($task);
|
|
|
|
$this->tasks[] = $wrapper;
|
2010-04-17 03:13:30 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Finds a task that is ready to run.
|
|
|
|
* If several tasks are ready, any one of them could be returned.
|
2010-12-17 01:15:57 +01:00
|
|
|
*
|
|
|
|
* @return phpbb_cron_task_wrapper|null
|
2010-04-17 03:13:30 -04:00
|
|
|
*/
|
2010-08-22 21:49:12 -04:00
|
|
|
public function find_one_ready_task()
|
2010-04-17 03:13:30 -04:00
|
|
|
{
|
|
|
|
foreach ($this->tasks as $task)
|
|
|
|
{
|
|
|
|
if ($task->is_ready())
|
|
|
|
{
|
|
|
|
return $task;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Finds all tasks that are ready to run.
|
2010-12-17 01:15:57 +01:00
|
|
|
*
|
|
|
|
* @return array Array of phpbb_cron_task_wrapper
|
2010-04-17 03:13:30 -04:00
|
|
|
*/
|
2010-08-22 21:49:12 -04:00
|
|
|
public function find_all_ready_tasks()
|
2010-04-17 03:13:30 -04:00
|
|
|
{
|
|
|
|
$tasks = array();
|
|
|
|
foreach ($this->tasks as $task)
|
|
|
|
{
|
|
|
|
if ($task->is_ready())
|
|
|
|
{
|
|
|
|
$tasks[] = $task;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $tasks;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Finds a task by name.
|
|
|
|
* Web runner uses this method to resolve names to tasks.
|
2010-12-17 01:15:57 +01:00
|
|
|
*
|
|
|
|
* @return array|null Array of phpbb_cron_task_wrapper
|
2010-04-17 03:13:30 -04:00
|
|
|
*/
|
2010-08-22 21:49:12 -04:00
|
|
|
public function find_task($name)
|
2010-04-17 03:13:30 -04:00
|
|
|
{
|
|
|
|
foreach ($this->tasks as $task)
|
|
|
|
{
|
|
|
|
if ($task->get_name() == $name)
|
|
|
|
{
|
|
|
|
return $task;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2010-05-09 16:47:52 -04:00
|
|
|
/**
|
|
|
|
* Creates an instance of parametrized cron task $name with args $args.
|
|
|
|
* The constructed task is wrapped with cron task wrapper before being returned.
|
2010-12-17 01:15:57 +01:00
|
|
|
*
|
|
|
|
* @param string $name The task name, which is the same as cron task class name.
|
|
|
|
* @param array $args Will be passed to the task class's constructor.
|
|
|
|
*
|
|
|
|
* @return phpbb_cron_task_wrapper|null
|
2010-05-09 16:47:52 -04:00
|
|
|
*/
|
2010-08-22 21:49:12 -04:00
|
|
|
public function instantiate_task($name, $args)
|
2010-04-17 03:13:30 -04:00
|
|
|
{
|
|
|
|
$task = $this->find_task($name);
|
|
|
|
if ($task)
|
|
|
|
{
|
2010-05-09 16:47:52 -04:00
|
|
|
// task here is actually an instance of cron task wrapper
|
|
|
|
$class = $task->get_name();
|
2010-04-17 03:13:30 -04:00
|
|
|
$task = new $class($args);
|
2010-05-09 16:47:52 -04:00
|
|
|
// need to wrap the new task too
|
2010-10-28 22:26:49 +02:00
|
|
|
$task = new phpbb_cron_task_wrapper($task);
|
2010-04-17 03:13:30 -04:00
|
|
|
}
|
|
|
|
return $task;
|
|
|
|
}
|
|
|
|
}
|