diff options
Diffstat (limited to 'phpBB/includes/cron')
| -rw-r--r-- | phpBB/includes/cron/manager.php | 146 | ||||
| -rw-r--r-- | phpBB/includes/cron/task/base.php | 54 | ||||
| -rw-r--r-- | phpBB/includes/cron/task/core/prune_all_forums.php | 75 | ||||
| -rw-r--r-- | phpBB/includes/cron/task/core/prune_forum.php | 153 | ||||
| -rw-r--r-- | phpBB/includes/cron/task/core/queue.php | 67 | ||||
| -rw-r--r-- | phpBB/includes/cron/task/core/tidy_cache.php | 64 | ||||
| -rw-r--r-- | phpBB/includes/cron/task/core/tidy_database.php | 54 | ||||
| -rw-r--r-- | phpBB/includes/cron/task/core/tidy_search.php | 87 | ||||
| -rw-r--r-- | phpBB/includes/cron/task/core/tidy_sessions.php | 50 | ||||
| -rw-r--r-- | phpBB/includes/cron/task/core/tidy_warnings.php | 69 | ||||
| -rw-r--r-- | phpBB/includes/cron/task/parametrized.php | 52 | ||||
| -rw-r--r-- | phpBB/includes/cron/task/provider.php | 48 | ||||
| -rw-r--r-- | phpBB/includes/cron/task/task.php | 48 | ||||
| -rw-r--r-- | phpBB/includes/cron/task/wrapper.php | 114 | 
14 files changed, 1081 insertions, 0 deletions
| diff --git a/phpBB/includes/cron/manager.php b/phpBB/includes/cron/manager.php new file mode 100644 index 0000000000..7a78a1b054 --- /dev/null +++ b/phpBB/includes/cron/manager.php @@ -0,0 +1,146 @@ +<?php +/** +* +* @package phpBB3 +* @copyright (c) 2010 phpBB Group +* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2 +* +*/ + +/** +* @ignore +*/ +if (!defined('IN_PHPBB')) +{ +	exit; +} + +/** +* Cron manager class. +* +* Finds installed cron tasks, stores task objects, provides task selection. +* +* @package phpBB3 +*/ +class phpbb_cron_manager +{ +	/** +	* Set of phpbb_cron_task_wrapper objects. +	* Array holding all tasks that have been found. +	* +	* @var array +	*/ +	protected $tasks = array(); + +	/** +	* Constructor. Loads all available tasks. +	* +	* @param array|Traversable $task_names Provides an iterable set of task names +	*/ +	public function __construct($task_names) +	{ +		$this->load_tasks($task_names); +	} + +	/** +	* Loads tasks given by name, wraps them +	* and puts them into $this->tasks. +	* +	* @param array|Traversable $task_names		Array of strings +	* +	* @return void +	*/ +	public function load_tasks($task_names) +	{ +		foreach ($task_names as $task_name) +		{ +			$task = new $task_name(); +			$wrapper = new phpbb_cron_task_wrapper($task); +			$this->tasks[] = $wrapper; +		} +	} + +	/** +	* Finds a task that is ready to run. +	* +	* If several tasks are ready, any one of them could be returned. +	* +	* If no tasks are ready, null is returned. +	* +	* @return phpbb_cron_task_wrapper|null +	*/ +	public function find_one_ready_task() +	{ +		foreach ($this->tasks as $task) +		{ +			if ($task->is_ready()) +			{ +				return $task; +			} +		} +		return null; +	} + +	/** +	* Finds all tasks that are ready to run. +	* +	* @return array		List of tasks which are ready to run (wrapped in phpbb_cron_task_wrapper). +	*/ +	public function find_all_ready_tasks() +	{ +		$tasks = array(); +		foreach ($this->tasks as $task) +		{ +			if ($task->is_ready()) +			{ +				$tasks[] = $task; +			} +		} +		return $tasks; +	} + +	/** +	* Finds a task by name. +	* +	* If there is no task with the specified name, null is returned. +	* +	* Web runner uses this method to resolve names to tasks. +	* +	* @param string				$name Name of the task to look up. +	* @return phpbb_cron_task	A task corresponding to the given name, or null. +	*/ +	public function find_task($name) +	{ +		foreach ($this->tasks as $task) +		{ +			if ($task->get_name() == $name) +			{ +				return $task; +			} +		} +		return null; +	} + +	/** +	* Creates an instance of parametrized cron task $name with args $args. +	* The constructed task is wrapped with cron task wrapper before being returned. +	* +	* @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 +	*/ +	public function instantiate_task($name, array $args) +	{ +		$task = $this->find_task($name); +		if ($task) +		{ +			// task here is actually an instance of cron task wrapper +			$class = $task->get_name(); +			$task = new $class($args); +			// need to wrap the new task too +			$task = new phpbb_cron_task_wrapper($task); +		} +		return $task; +	} +} diff --git a/phpBB/includes/cron/task/base.php b/phpBB/includes/cron/task/base.php new file mode 100644 index 0000000000..c05fb9a87c --- /dev/null +++ b/phpBB/includes/cron/task/base.php @@ -0,0 +1,54 @@ +<?php +/** +* +* @package phpBB3 +* @copyright (c) 2010 phpBB Group +* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2 +* +*/ + +/** +* @ignore +*/ +if (!defined('IN_PHPBB')) +{ +	exit; +} + +/** +* Cron task base class. Provides sensible defaults for cron tasks +* and partially implements cron task interface, making writing cron tasks easier. +* +* At a minimum, subclasses must override the run() method. +* +* Cron tasks need not inherit from this base class. If desired, +* they may implement cron task interface directly. +* +* @package phpBB3 +*/ +abstract class phpbb_cron_task_base implements phpbb_cron_task +{ +	/** +	* Returns whether this cron task can run, given current board configuration. +	* +	* For example, a cron task that prunes forums can only run when +	* forum pruning is enabled. +	* +	* @return bool +	*/ +	public function is_runnable() +	{ +		return true; +	} + +	/** +	* Returns whether this cron task should run now, because enough time +	* has passed since it was last run. +	* +	* @return bool +	*/ +	public function should_run() +	{ +		return true; +	} +} diff --git a/phpBB/includes/cron/task/core/prune_all_forums.php b/phpBB/includes/cron/task/core/prune_all_forums.php new file mode 100644 index 0000000000..15b93a9ca6 --- /dev/null +++ b/phpBB/includes/cron/task/core/prune_all_forums.php @@ -0,0 +1,75 @@ +<?php +/** +* +* @package phpBB3 +* @copyright (c) 2010 phpBB Group +* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2 +* +*/ + +/** +* @ignore +*/ +if (!defined('IN_PHPBB')) +{ +	exit; +} + +/** +* Prune all forums cron task. +* +* It is intended to be invoked from system cron. +* This task will find all forums for which pruning is enabled, and will +* prune all forums as necessary. +* +* @package phpBB3 +*/ +class phpbb_cron_task_core_prune_all_forums extends phpbb_cron_task_base +{ +	/** +	* Runs this cron task. +	* +	* @return void +	*/ +	public function run() +	{ +		global $phpbb_root_path, $phpEx, $db; + +		if (!function_exists('auto_prune')) +		{ +			include($phpbb_root_path . 'includes/functions_admin.' . $phpEx); +		} + +		$sql = 'SELECT forum_id, prune_next, enable_prune, prune_days, prune_viewed, forum_flags, prune_freq +			FROM ' . FORUMS_TABLE . " +			WHERE enable_prune = 1  +				AND prune_next < " . time(); +		$result = $db->sql_query($sql); +		while ($row = $db->sql_fetchrow($result)) +		{ +			if ($row['prune_days']) +			{ +				auto_prune($row['forum_id'], 'posted', $row['forum_flags'], $row['prune_days'], $row['prune_freq']); +			} + +			if ($row['prune_viewed']) +			{ +				auto_prune($row['forum_id'], 'viewed', $row['forum_flags'], $row['prune_viewed'], $row['prune_freq']); +			} +		} +		$db->sql_freeresult($result); +	} + +	/** +	* Returns whether this cron task can run, given current board configuration. +	* +	* This cron task will only run when system cron is utilised. +	* +	* @return bool +	*/ +	public function is_runnable() +	{ +		global $config; +		return (bool) $config['use_system_cron']; +	} +} diff --git a/phpBB/includes/cron/task/core/prune_forum.php b/phpBB/includes/cron/task/core/prune_forum.php new file mode 100644 index 0000000000..7686fd4281 --- /dev/null +++ b/phpBB/includes/cron/task/core/prune_forum.php @@ -0,0 +1,153 @@ +<?php +/** +* +* @package phpBB3 +* @copyright (c) 2010 phpBB Group +* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2 +* +*/ + +/** +* @ignore +*/ +if (!defined('IN_PHPBB')) +{ +	exit; +} + +/** +* Prune one forum cron task. +* +* It is intended to be used when cron is invoked via web. +* This task can decide whether it should be run using data obtained by viewforum +* code, without making additional database queries. +* +* @package phpBB3 +*/ +class phpbb_cron_task_core_prune_forum extends phpbb_cron_task_base implements phpbb_cron_task_parametrized +{ +	private $forum_data; + +	/** +	* Constructor. +	* +	* If $forum_data is given, it is assumed to contain necessary information +	* about a single forum that is to be pruned. +	* +	* If $forum_data is not given, forum id will be retrieved via request_var +	* and a database query will be performed to load the necessary information +	* about the forum. +	* +	* @param array $forum_data Information about a forum to be pruned. +	*/ +	public function __construct($forum_data = null) +	{ +		global $db; +		if ($forum_data) +		{ +			$this->forum_data = $forum_data; +		} +		else +		{ +			$this->forum_data = null; +		} +	} + +	/** +	* Runs this cron task. +	* +	* @return void +	*/ +	public function run() +	{ +		global $phpbb_root_path, $phpEx; +		if (!function_exists('auto_prune')) +		{ +			include($phpbb_root_path . 'includes/functions_admin.' . $phpEx); +		} + +		if ($this->forum_data['prune_days']) +		{ +			auto_prune($this->forum_data['forum_id'], 'posted', $this->forum_data['forum_flags'], $this->forum_data['prune_days'], $this->forum_data['prune_freq']); +		} + +		if ($this->forum_data['prune_viewed']) +		{ +			auto_prune($this->forum_data['forum_id'], 'viewed', $this->forum_data['forum_flags'], $this->forum_data['prune_viewed'], $this->forum_data['prune_freq']); +		} +	} + +	/** +	* Returns whether this cron task can run, given current board configuration. +	* +	* This cron task will not run when system cron is utilised, as in +	* such cases prune_all_forums task would run instead. +	* +	* Additionally, this task must be given the forum data, either via +	* the constructor or parse_parameters method. +	* +	* @return bool +	*/ +	public function is_runnable() +	{ +		global $config; +		return !$config['use_system_cron'] && $this->forum_data; +	} + +	/** +	* Returns whether this cron task should run now, because enough time +	* has passed since it was last run. +	* +	* Forum pruning interval is specified in the forum data. +	* +	* @return bool +	*/ +	public function should_run() +	{ +		return $this->forum_data['enable_prune'] && $this->forum_data['prune_next'] < time(); +	} + +	/** +	* Returns parameters of this cron task as an array. +	* The array has one key, f, whose value is id of the forum to be pruned. +	* +	* @return array +	*/ +	public function get_parameters() +	{ +		return array('f' => $this->forum_data['forum_id']); +	} + +	/** +	* Parses parameters found in $request, which is an instance of +	* phpbb_request_interface. +	* +	* It is expected to have a key f whose value is id of the forum to be pruned. +	* +	* @param phpbb_request_interface $request Request object. +	* +	* @return void +	*/ +	public function parse_parameters(phpbb_request_interface $request) +	{ +		global $db; + +		$this->forum_data = null; +		if ($request->is_set('f')) +		{ +			$forum_id = $request->variable('f', 0); + +			$sql = 'SELECT forum_id, prune_next, enable_prune, prune_days, prune_viewed, forum_flags, prune_freq +				FROM ' . FORUMS_TABLE . " +				WHERE forum_id = $forum_id"; +			$result = $db->sql_query($sql); +			$row = $db->sql_fetchrow($result); +			$db->sql_freeresult($result); + +			if ($row) +			{ +				$this->forum_data = $row; +			} +		} +	} +} diff --git a/phpBB/includes/cron/task/core/queue.php b/phpBB/includes/cron/task/core/queue.php new file mode 100644 index 0000000000..1c72eec7c7 --- /dev/null +++ b/phpBB/includes/cron/task/core/queue.php @@ -0,0 +1,67 @@ +<?php +/** +* +* @package phpBB3 +* @copyright (c) 2010 phpBB Group +* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2 +* +*/ + +/** +* @ignore +*/ +if (!defined('IN_PHPBB')) +{ +	exit; +} + +/** +* Queue cron task. Sends email and jabber messages queued by other scripts. +* +* @package phpBB3 +*/ +class phpbb_cron_task_core_queue extends phpbb_cron_task_base +{ +	/** +	* Runs this cron task. +	* +	* @return void +	*/ +	public function run() +	{ +		global $phpbb_root_path, $phpEx; +		if (!class_exists('queue')) +		{ +			include($phpbb_root_path . 'includes/functions_messenger.' . $phpEx); +		} +		$queue = new queue(); +		$queue->process(); +	} + +	/** +	* Returns whether this cron task can run, given current board configuration. +	* +	* Queue task is only run if the email queue (file) exists. +	* +	* @return bool +	*/ +	public function is_runnable() +	{ +		global $phpbb_root_path, $phpEx; +		return file_exists($phpbb_root_path . 'cache/queue.' . $phpEx); +	} + +	/** +	* Returns whether this cron task should run now, because enough time +	* has passed since it was last run. +	* +	* The interval between queue runs is specified in board configuration. +	* +	* @return bool +	*/ +	public function should_run() +	{ +		global $config; +		return $config['last_queue_run'] < time() - $config['queue_interval_config']; +	} +} diff --git a/phpBB/includes/cron/task/core/tidy_cache.php b/phpBB/includes/cron/task/core/tidy_cache.php new file mode 100644 index 0000000000..c9dc0bd9ae --- /dev/null +++ b/phpBB/includes/cron/task/core/tidy_cache.php @@ -0,0 +1,64 @@ +<?php +/** +* +* @package phpBB3 +* @copyright (c) 2010 phpBB Group +* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2 +* +*/ + +/** +* @ignore +*/ +if (!defined('IN_PHPBB')) +{ +	exit; +} + +/** +* Tidy cache cron task. +* +* @package phpBB3 +*/ +class phpbb_cron_task_core_tidy_cache extends phpbb_cron_task_base +{ +	/** +	* Runs this cron task. +	* +	* @return void +	*/ +	public function run() +	{ +		global $cache; +		$cache->tidy(); +	} + +	/** +	* Returns whether this cron task can run, given current board configuration. +	* +	* Tidy cache cron task runs if the cache implementation in use +	* supports tidying. +	* +	* @return bool +	*/ +	public function is_runnable() +	{ +		global $cache; +		return method_exists($cache, 'tidy'); +	} + +	/** +	* Returns whether this cron task should run now, because enough time +	* has passed since it was last run. +	* +	* The interval between cache tidying is specified in board +	* configuration. +	* +	* @return bool +	*/ +	public function should_run() +	{ +		global $config; +		return $config['cache_last_gc'] < time() - $config['cache_gc']; +	} +} diff --git a/phpBB/includes/cron/task/core/tidy_database.php b/phpBB/includes/cron/task/core/tidy_database.php new file mode 100644 index 0000000000..80a1901b1e --- /dev/null +++ b/phpBB/includes/cron/task/core/tidy_database.php @@ -0,0 +1,54 @@ +<?php +/** +* +* @package phpBB3 +* @copyright (c) 2010 phpBB Group +* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2 +* +*/ + +/** +* @ignore +*/ +if (!defined('IN_PHPBB')) +{ +	exit; +} + +/** +* Tidy database cron task. +* +* @package phpBB3 +*/ +class phpbb_cron_task_core_tidy_database extends phpbb_cron_task_base +{ +	/** +	* Runs this cron task. +	* +	* @return void +	*/ +	public function run() +	{ +		global $phpbb_root_path, $phpEx; +		if (!function_exists('tidy_database')) +		{ +			include($phpbb_root_path . 'includes/functions_admin.' . $phpEx); +		} +		tidy_database(); +	} + +	/** +	* Returns whether this cron task should run now, because enough time +	* has passed since it was last run. +	* +	* The interval between database tidying is specified in board +	* configuration. +	* +	* @return bool +	*/ +	public function should_run() +	{ +		global $config; +		return $config['database_last_gc'] < time() - $config['database_gc']; +	} +} diff --git a/phpBB/includes/cron/task/core/tidy_search.php b/phpBB/includes/cron/task/core/tidy_search.php new file mode 100644 index 0000000000..8a0b1b690a --- /dev/null +++ b/phpBB/includes/cron/task/core/tidy_search.php @@ -0,0 +1,87 @@ +<?php +/** +* +* @package phpBB3 +* @copyright (c) 2010 phpBB Group +* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2 +* +*/ + +/** +* @ignore +*/ +if (!defined('IN_PHPBB')) +{ +	exit; +} + +/** +* Tidy search cron task. +* +* Will only run when the currently selected search backend supports tidying. +* +* @package phpBB3 +*/ +class phpbb_cron_task_core_tidy_search extends phpbb_cron_task_base +{ +	/** +	* Runs this cron task. +	* +	* @return void +	*/ +	public function run() +	{ +		global $phpbb_root_path, $phpEx, $config, $error; + +		// Select the search method +		$search_type = basename($config['search_type']); + +		if (!class_exists($search_type)) +		{ +			include("{$phpbb_root_path}includes/search/$search_type.$phpEx"); +		} + +		// We do some additional checks in the module to ensure it can actually be utilised +		$error = false; +		$search = new $search_type($error); + +		if (!$error) +		{ +			$search->tidy(); +		} +	} + +	/** +	* Returns whether this cron task can run, given current board configuration. +	* +	* Search cron task is runnable in all normal use. It may not be +	* runnable if the search backend implementation selected in board +	* configuration does not exist. +	* +	* @return bool +	*/ +	public function is_runnable() +	{ +		global $phpbb_root_path, $phpEx, $config; + +		// Select the search method +		$search_type = basename($config['search_type']); + +		return file_exists($phpbb_root_path . 'includes/search/' . $search_type . '.' . $phpEx); +	} + +	/** +	* Returns whether this cron task should run now, because enough time +	* has passed since it was last run. +	* +	* The interval between search tidying is specified in board +	* configuration. +	* +	* @return bool +	*/ +	public function should_run() +	{ +		global $config; +		return $config['search_last_gc'] < time() - $config['search_gc']; +	} +} diff --git a/phpBB/includes/cron/task/core/tidy_sessions.php b/phpBB/includes/cron/task/core/tidy_sessions.php new file mode 100644 index 0000000000..ae7bb242b8 --- /dev/null +++ b/phpBB/includes/cron/task/core/tidy_sessions.php @@ -0,0 +1,50 @@ +<?php +/** +* +* @package phpBB3 +* @copyright (c) 2010 phpBB Group +* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2 +* +*/ + +/** +* @ignore +*/ +if (!defined('IN_PHPBB')) +{ +	exit; +} + +/** +* Tidy sessions cron task. +* +* @package phpBB3 +*/ +class phpbb_cron_task_core_tidy_sessions extends phpbb_cron_task_base +{ +	/** +	* Runs this cron task. +	* +	* @return void +	*/ +	public function run() +	{ +		global $user; +		$user->session_gc(); +	} + +	/** +	* Returns whether this cron task should run now, because enough time +	* has passed since it was last run. +	* +	* The interval between session tidying is specified in board +	* configuration. +	* +	* @return bool +	*/ +	public function should_run() +	{ +		global $config; +		return $config['session_last_gc'] < time() - $config['session_gc']; +	} +} diff --git a/phpBB/includes/cron/task/core/tidy_warnings.php b/phpBB/includes/cron/task/core/tidy_warnings.php new file mode 100644 index 0000000000..e1434e7087 --- /dev/null +++ b/phpBB/includes/cron/task/core/tidy_warnings.php @@ -0,0 +1,69 @@ +<?php +/** +* +* @package phpBB3 +* @copyright (c) 2010 phpBB Group +* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2 +* +*/ + +/** +* @ignore +*/ +if (!defined('IN_PHPBB')) +{ +	exit; +} + +/** +* Tidy warnings cron task. +* +* Will only run when warnings are configured to expire. +* +* @package phpBB3 +*/ +class phpbb_cron_task_core_tidy_warnings extends phpbb_cron_task_base +{ +	/** +	* Runs this cron task. +	* +	* @return void +	*/ +	public function run() +	{ +		global $phpbb_root_path, $phpEx; +		if (!function_exists('tidy_warnings')) +		{ +			include($phpbb_root_path . 'includes/functions_admin.' . $phpEx); +		} +		tidy_warnings(); +	} + +	/** +	* Returns whether this cron task can run, given current board configuration. +	* +	* If warnings are set to never expire, this cron task will not run. +	* +	* @return bool +	*/ +	public function is_runnable() +	{ +		global $config; +		return (bool) $config['warnings_expire_days']; +	} + +	/** +	* Returns whether this cron task should run now, because enough time +	* has passed since it was last run. +	* +	* The interval between warnings tidying is specified in board +	* configuration. +	* +	* @return bool +	*/ +	public function should_run() +	{ +		global $config; +		return $config['warnings_last_gc'] < time() - $config['warnings_gc']; +	} +} diff --git a/phpBB/includes/cron/task/parametrized.php b/phpBB/includes/cron/task/parametrized.php new file mode 100644 index 0000000000..0714b2e701 --- /dev/null +++ b/phpBB/includes/cron/task/parametrized.php @@ -0,0 +1,52 @@ +<?php +/** +* +* @package phpBB3 +* @copyright (c) 2010 phpBB Group +* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2 +* +*/ + +/** +* @ignore +*/ +if (!defined('IN_PHPBB')) +{ +	exit; +} + +/** +* Parametrized cron task interface. +* +* Parametrized cron tasks are somewhat of a cross between regular cron tasks and +* delayed jobs. Whereas regular cron tasks perform some action globally, +* parametrized cron tasks perform actions on a particular object (or objects). +* Parametrized cron tasks do not make sense and are not usable without +* specifying these objects. +* +* @package phpBB3 +*/ +interface phpbb_cron_task_parametrized extends phpbb_cron_task +{ +	/** +	* Returns parameters of this cron task as an array. +	* +	* The array must map string keys to string values. +	* +	* @return array +	*/ +	public function get_parameters(); + +	/** +	* Parses parameters found in $request, which is an instance of +	* phpbb_request_interface. +	* +	* $request contains user input and must not be trusted. +	* Cron task must validate all data before using it. +	* +	* @param phpbb_request_interface $request Request object. +	* +	* @return void +	*/ +	public function parse_parameters(phpbb_request_interface $request); +} diff --git a/phpBB/includes/cron/task/provider.php b/phpBB/includes/cron/task/provider.php new file mode 100644 index 0000000000..1482051699 --- /dev/null +++ b/phpBB/includes/cron/task/provider.php @@ -0,0 +1,48 @@ +<?php +/** +* +* @package phpBB3 +* @copyright (c) 2011 phpBB Group +* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2 +* +*/ + +/** +* @ignore +*/ +if (!defined('IN_PHPBB')) +{ +	exit; +} + +/** +* Provides cron manager with tasks +* +* Finds installed cron tasks and makes them available to the cron manager. +* +* @package phpBB3 +*/ +class phpbb_cron_task_provider extends phpbb_extension_provider +{ +	/** +	* Finds cron task names using the extension manager. +	* +	* All PHP files in includes/cron/task/core/ are considered tasks. Tasks +	* in extensions have to be located in a directory called cron or a subdir +	* of a directory called cron. The class and filename must end in a _task +	* suffix. Additionally all PHP files in includes/cron/task/core/ are +	* tasks. +	* +	* @return array     List of task names +	*/ +	protected function find() +	{ +		$finder = $this->extension_manager->get_finder(); + +		return $finder +			->extension_suffix('_task') +			->extension_directory('/cron') +			->core_path('includes/cron/task/core/') +			->get_classes(); +	} +} diff --git a/phpBB/includes/cron/task/task.php b/phpBB/includes/cron/task/task.php new file mode 100644 index 0000000000..2f2a9e51f9 --- /dev/null +++ b/phpBB/includes/cron/task/task.php @@ -0,0 +1,48 @@ +<?php +/** +* +* @package phpBB3 +* @copyright (c) 2010 phpBB Group +* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2 +* +*/ + +/** +* @ignore +*/ +if (!defined('IN_PHPBB')) +{ +	exit; +} + +/** +* Cron task interface +* @package phpBB3 +*/ +interface phpbb_cron_task +{ +	/** +	* Runs this cron task. +	* +	* @return void +	*/ +	public function run(); + +	/** +	* Returns whether this cron task can run, given current board configuration. +	* +	* For example, a cron task that prunes forums can only run when +	* forum pruning is enabled. +	* +	* @return bool +	*/ +	public function is_runnable(); + +	/** +	* Returns whether this cron task should run now, because enough time +	* has passed since it was last run. +	* +	* @return bool +	*/ +	public function should_run(); +} diff --git a/phpBB/includes/cron/task/wrapper.php b/phpBB/includes/cron/task/wrapper.php new file mode 100644 index 0000000000..66c45189e5 --- /dev/null +++ b/phpBB/includes/cron/task/wrapper.php @@ -0,0 +1,114 @@ +<?php +/** +* +* @package phpBB3 +* @copyright (c) 2010 phpBB Group +* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2 +* +*/ + +/** +* @ignore +*/ +if (!defined('IN_PHPBB')) +{ +	exit; +} + +/** +* Cron task wrapper class. +* Enhances cron tasks with convenience methods that work identically for all tasks. +* +* @package phpBB3 +*/ +class phpbb_cron_task_wrapper +{ +	/** +	* Constructor. +	* +	* Wraps a task $task, which must implement cron_task interface. +	* +	* @param phpbb_cron_task $task The cron task to wrap. +	*/ +	public function __construct(phpbb_cron_task $task) +	{ +		$this->task = $task; +	} + +	/** +	* Returns whether the wrapped task is parametrised. +	* +	* Parametrized tasks accept parameters during initialization and must +	* normally be scheduled with parameters. +	* +	* @return bool		Whether or not this task is parametrized. +	*/ +	public function is_parametrized() +	{ +		return $this->task instanceof phpbb_cron_task_parametrized; +	} + +	/** +	* Returns whether the wrapped task is ready to run. +	* +	* A task is ready to run when it is runnable according to current configuration +	* and enough time has passed since it was last run. +	* +	* @return bool		Whether the wrapped task is ready to run. +	*/ +	public function is_ready() +	{ +		return $this->task->is_runnable() && $this->task->should_run(); +	} + +	/** +	* Returns the name of wrapped task. It is the same as the wrapped class's class name. +	* +	* @return string		Class name of wrapped task. +	*/ +	public function get_name() +	{ +		return get_class($this->task); +	} + +	/** +	* Returns a url through which this task may be invoked via web. +	* +	* When system cron is not in use, running a cron task is accomplished +	* by outputting an image with the url returned by this function as +	* source. +	* +	* @return string		URL through which this task may be invoked. +	*/ +	public function get_url() +	{ +		global $phpbb_root_path, $phpEx; + +		$name = $this->get_name(); +		if ($this->is_parametrized()) +		{ +			$params = $this->task->get_parameters(); +			$extra = ''; +			foreach ($params as $key => $value) +			{ +				$extra .= '&' . $key . '=' . urlencode($value); +			} +		} +		else +		{ +			$extra = ''; +		} +		$url = append_sid($phpbb_root_path . 'cron.' . $phpEx, 'cron_type=' . $name . $extra); +		return $url; +	} + +	/** +	* Forwards all other method calls to the wrapped task implementation. +	* +	* @return mixed +	*/ +	public function __call($name, $args) +	{ +		return call_user_func_array(array($this->task, $name), $args); +	} +} | 
