From 1bfbcb8b7e91f4b36b703eeee7e6eeb99fbca3a0 Mon Sep 17 00:00:00 2001 From: Igor Wiedler Date: Thu, 28 Oct 2010 21:38:50 +0200 Subject: [feature/system-cron] remove cron_ prefix from files PHPBB3-9596 --- phpBB/includes/cron/cron_lock.php | 80 ------------- phpBB/includes/cron/cron_manager.php | 191 ------------------------------ phpBB/includes/cron/cron_task.php | 80 ------------- phpBB/includes/cron/cron_task_base.php | 64 ---------- phpBB/includes/cron/cron_task_wrapper.php | 103 ---------------- phpBB/includes/cron/lock.php | 80 +++++++++++++ phpBB/includes/cron/manager.php | 191 ++++++++++++++++++++++++++++++ phpBB/includes/cron/task.php | 80 +++++++++++++ phpBB/includes/cron/task_base.php | 64 ++++++++++ phpBB/includes/cron/task_wrapper.php | 103 ++++++++++++++++ 10 files changed, 518 insertions(+), 518 deletions(-) delete mode 100644 phpBB/includes/cron/cron_lock.php delete mode 100644 phpBB/includes/cron/cron_manager.php delete mode 100644 phpBB/includes/cron/cron_task.php delete mode 100644 phpBB/includes/cron/cron_task_base.php delete mode 100644 phpBB/includes/cron/cron_task_wrapper.php create mode 100644 phpBB/includes/cron/lock.php create mode 100644 phpBB/includes/cron/manager.php create mode 100644 phpBB/includes/cron/task.php create mode 100644 phpBB/includes/cron/task_base.php create mode 100644 phpBB/includes/cron/task_wrapper.php (limited to 'phpBB') diff --git a/phpBB/includes/cron/cron_lock.php b/phpBB/includes/cron/cron_lock.php deleted file mode 100644 index 64b82128ce..0000000000 --- a/phpBB/includes/cron/cron_lock.php +++ /dev/null @@ -1,80 +0,0 @@ -= time()) - { - return false; - } - } - - $this->cron_id = time() . ' ' . unique_id(); - - $sql = 'UPDATE ' . CONFIG_TABLE . " - SET config_value = '" . $db->sql_escape($this->cron_id) . "' - WHERE config_name = 'cron_lock' AND config_value = '" . $db->sql_escape($config['cron_lock']) . "'"; - $db->sql_query($sql); - - // another cron process altered the table between script start and UPDATE query so exit - if ($db->sql_affectedrows() != 1) - { - return false; - } - - return true; - } - - /** - * Releases cron lock. - * - * Attempting to release a cron lock that is already released is harmless. - */ - public function unlock() - { - global $db; - - $sql = 'UPDATE ' . CONFIG_TABLE . " - SET config_value = '0' - WHERE config_name = 'cron_lock' AND config_value = '" . $db->sql_escape($this->cron_id) . "'"; - $db->sql_query($sql); - } -} diff --git a/phpBB/includes/cron/cron_manager.php b/phpBB/includes/cron/cron_manager.php deleted file mode 100644 index 58333b0b66..0000000000 --- a/phpBB/includes/cron/cron_manager.php +++ /dev/null @@ -1,191 +0,0 @@ -find_cron_task_files(); - $this->load_tasks($task_files); - } - - /** - * Finds cron task files. - * - * A cron task file must follow the naming convention: - * includes/cron/tasks/$mod/$name.php. - * $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. - * Cron task is expected to be a class named cron_task_${mod}_${name}. - * - * Todo: consider caching found task file list in global cache. - */ - public function find_cron_task_files() - { - global $phpbb_root_path, $phpEx; - - $tasks_root_path = $phpbb_root_path . 'includes/cron/tasks'; - $dir = opendir($tasks_root_path); - $task_dirs = array(); - while (($entry = readdir($dir)) !== false) - { - // ignore ., .. and dot directories - // todo: change is_dir to account for symlinks - if ($entry[0] == '.' || !is_dir("$tasks_root_path/$entry")) - { - continue; - } - $task_dirs[] = $entry; - } - closedir($dir); - - $ext = '.' . $phpEx; - $ext_length = strlen($ext); - $task_files = array(); - foreach ($task_dirs as $task_dir) - { - $path = $phpbb_root_path . 'includes/cron/tasks/' . $task_dir; - $dir = opendir($path); - while (($entry = readdir($dir)) !== false) - { - if (substr($entry, -$ext_length) == $ext) - { - $task_file = substr($entry, 0, -$ext_length); - $task_files[] = array($task_dir, $task_file); - } - } - closedir($dir); - } - return $task_files; - } - - /** - * Checks whether $name is a valid identifier, and therefore part of valid cron task class name. - */ - public function is_valid_name($name) - { - return preg_match('/^[a-zA-Z][a-zA-Z0-9_]*$/', $name); - } - - public function load_tasks($task_files) - { - global $phpbb_root_path, $phpEx; - - foreach ($task_files as $task_file) - { - list($mod, $filename) = $task_file; - if ($this->is_valid_name($mod) && $this->is_valid_name($filename)) - { - $class = "cron_task_${mod}_${filename}"; - if (!class_exists($class)) - { - include($phpbb_root_path . "includes/cron/tasks/$mod/$filename.$phpEx"); - } - $object = new $class; - $wrapper = new cron_task_wrapper($object); - $this->tasks[] = $wrapper; - } - } - } - - /** - * Finds a task that is ready to run. - * - * If several tasks are ready, any one of them could be returned. - */ - 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. - */ - 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. - * - * Web runner uses this method to resolve names to tasks. - */ - 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. - * - * $name is the task name, which is the same as cron task class name. - * $args will be passed to the task class's constructor. - * The constructed task is wrapped with cron task wrapper before being returned. - */ - public function instantiate_task($name, $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 cron_task_wrapper($task); - } - return $task; - } -} diff --git a/phpBB/includes/cron/cron_task.php b/phpBB/includes/cron/cron_task.php deleted file mode 100644 index 46eacff517..0000000000 --- a/phpBB/includes/cron/cron_task.php +++ /dev/null @@ -1,80 +0,0 @@ -task = $task; - } - - /** - * Returns whether this task is parametrized. - * - * Parametrized tasks accept parameters during initialization and must - * normally be scheduled with parameters. - */ - public function is_parametrized() - { - return $this->task instanceof parametrized_cron_task; - } - - /** - * 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. - */ - 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. - */ - public function get_name() - { - return get_class($this->task); - } - - /** - * Returns a url through which this task may be invoked via web. - */ - 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. - */ - public function __call($name, $args) - { - return call_user_func_array(array($this->task, $name), $args); - } -} diff --git a/phpBB/includes/cron/lock.php b/phpBB/includes/cron/lock.php new file mode 100644 index 0000000000..64b82128ce --- /dev/null +++ b/phpBB/includes/cron/lock.php @@ -0,0 +1,80 @@ += time()) + { + return false; + } + } + + $this->cron_id = time() . ' ' . unique_id(); + + $sql = 'UPDATE ' . CONFIG_TABLE . " + SET config_value = '" . $db->sql_escape($this->cron_id) . "' + WHERE config_name = 'cron_lock' AND config_value = '" . $db->sql_escape($config['cron_lock']) . "'"; + $db->sql_query($sql); + + // another cron process altered the table between script start and UPDATE query so exit + if ($db->sql_affectedrows() != 1) + { + return false; + } + + return true; + } + + /** + * Releases cron lock. + * + * Attempting to release a cron lock that is already released is harmless. + */ + public function unlock() + { + global $db; + + $sql = 'UPDATE ' . CONFIG_TABLE . " + SET config_value = '0' + WHERE config_name = 'cron_lock' AND config_value = '" . $db->sql_escape($this->cron_id) . "'"; + $db->sql_query($sql); + } +} diff --git a/phpBB/includes/cron/manager.php b/phpBB/includes/cron/manager.php new file mode 100644 index 0000000000..58333b0b66 --- /dev/null +++ b/phpBB/includes/cron/manager.php @@ -0,0 +1,191 @@ +find_cron_task_files(); + $this->load_tasks($task_files); + } + + /** + * Finds cron task files. + * + * A cron task file must follow the naming convention: + * includes/cron/tasks/$mod/$name.php. + * $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. + * Cron task is expected to be a class named cron_task_${mod}_${name}. + * + * Todo: consider caching found task file list in global cache. + */ + public function find_cron_task_files() + { + global $phpbb_root_path, $phpEx; + + $tasks_root_path = $phpbb_root_path . 'includes/cron/tasks'; + $dir = opendir($tasks_root_path); + $task_dirs = array(); + while (($entry = readdir($dir)) !== false) + { + // ignore ., .. and dot directories + // todo: change is_dir to account for symlinks + if ($entry[0] == '.' || !is_dir("$tasks_root_path/$entry")) + { + continue; + } + $task_dirs[] = $entry; + } + closedir($dir); + + $ext = '.' . $phpEx; + $ext_length = strlen($ext); + $task_files = array(); + foreach ($task_dirs as $task_dir) + { + $path = $phpbb_root_path . 'includes/cron/tasks/' . $task_dir; + $dir = opendir($path); + while (($entry = readdir($dir)) !== false) + { + if (substr($entry, -$ext_length) == $ext) + { + $task_file = substr($entry, 0, -$ext_length); + $task_files[] = array($task_dir, $task_file); + } + } + closedir($dir); + } + return $task_files; + } + + /** + * Checks whether $name is a valid identifier, and therefore part of valid cron task class name. + */ + public function is_valid_name($name) + { + return preg_match('/^[a-zA-Z][a-zA-Z0-9_]*$/', $name); + } + + public function load_tasks($task_files) + { + global $phpbb_root_path, $phpEx; + + foreach ($task_files as $task_file) + { + list($mod, $filename) = $task_file; + if ($this->is_valid_name($mod) && $this->is_valid_name($filename)) + { + $class = "cron_task_${mod}_${filename}"; + if (!class_exists($class)) + { + include($phpbb_root_path . "includes/cron/tasks/$mod/$filename.$phpEx"); + } + $object = new $class; + $wrapper = new cron_task_wrapper($object); + $this->tasks[] = $wrapper; + } + } + } + + /** + * Finds a task that is ready to run. + * + * If several tasks are ready, any one of them could be returned. + */ + 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. + */ + 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. + * + * Web runner uses this method to resolve names to tasks. + */ + 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. + * + * $name is the task name, which is the same as cron task class name. + * $args will be passed to the task class's constructor. + * The constructed task is wrapped with cron task wrapper before being returned. + */ + public function instantiate_task($name, $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 cron_task_wrapper($task); + } + return $task; + } +} diff --git a/phpBB/includes/cron/task.php b/phpBB/includes/cron/task.php new file mode 100644 index 0000000000..46eacff517 --- /dev/null +++ b/phpBB/includes/cron/task.php @@ -0,0 +1,80 @@ +task = $task; + } + + /** + * Returns whether this task is parametrized. + * + * Parametrized tasks accept parameters during initialization and must + * normally be scheduled with parameters. + */ + public function is_parametrized() + { + return $this->task instanceof parametrized_cron_task; + } + + /** + * 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. + */ + 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. + */ + public function get_name() + { + return get_class($this->task); + } + + /** + * Returns a url through which this task may be invoked via web. + */ + 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. + */ + public function __call($name, $args) + { + return call_user_func_array(array($this->task, $name), $args); + } +} -- cgit v1.2.1