From 77d7238eef84f498fc024fa8b9e06f187dd0f2a6 Mon Sep 17 00:00:00 2001 From: Oleg Pudeyev Date: Wed, 14 Apr 2010 16:14:32 -0400 Subject: [PATCH 01/67] [feature/system-cron] WIP on making cron tasks runnable via system cron PHPBB3-9596 --- phpBB/common.php | 6 + phpBB/cron.php | 293 +++++-------------------------- phpBB/includes/cron.php | 135 ++++++++++++++ phpBB/includes/cron/standard.php | 163 +++++++++++++++++ phpBB/includes/cron_lock.php | 71 ++++++++ phpBB/includes/functions.php | 36 +--- phpBB/viewforum.php | 4 +- 7 files changed, 421 insertions(+), 287 deletions(-) create mode 100644 phpBB/includes/cron.php create mode 100644 phpBB/includes/cron/standard.php create mode 100644 phpBB/includes/cron_lock.php diff --git a/phpBB/common.php b/phpBB/common.php index 0ac7cbbd86..3586031c36 100644 --- a/phpBB/common.php +++ b/phpBB/common.php @@ -239,3 +239,9 @@ foreach ($cache->obtain_hooks() as $hook) { @include($phpbb_root_path . 'includes/hooks/' . $hook . '.' . $phpEx); } + +if (!$config['use_system_cron']) +{ + require($phpbb_root_path . 'includes/cron.' . $phpEx); + $cron = new cron(); +} diff --git a/phpBB/cron.php b/phpBB/cron.php index 4462f52e93..1dbe1768c1 100644 --- a/phpBB/cron.php +++ b/phpBB/cron.php @@ -15,271 +15,58 @@ define('IN_CRON', true); $phpbb_root_path = (defined('PHPBB_ROOT_PATH')) ? PHPBB_ROOT_PATH : './'; $phpEx = substr(strrchr(__FILE__, '.'), 1); include($phpbb_root_path . 'common.' . $phpEx); +include($phpbb_root_path . 'includes/cron_lock.' . $phpEx); // Do not update users last page entry $user->session_begin(false); $auth->acl($user->data); -$cron_type = request_var('cron_type', ''); -$use_shutdown_function = (@function_exists('register_shutdown_function')) ? true : false; +function output_image() { + // Output transparent gif + header('Cache-Control: no-cache'); + header('Content-type: image/gif'); + header('Content-length: 43'); -// Output transparent gif -header('Cache-Control: no-cache'); -header('Content-type: image/gif'); -header('Content-length: 43'); + echo base64_decode('R0lGODlhAQABAIAAAP///wAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw=='); -echo base64_decode('R0lGODlhAQABAIAAAP///wAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw=='); - -// test without flush ;) -// flush(); - -// -if (!isset($config['cron_lock'])) -{ - set_config('cron_lock', '0', true); + // test without flush ;) + // flush(); } -// make sure cron doesn't run multiple times in parallel -if ($config['cron_lock']) -{ - // if the other process is running more than an hour already we have to assume it - // aborted without cleaning the lock - $time = explode(' ', $config['cron_lock']); - $time = $time[0]; - - if ($time + 3600 >= time()) - { - exit; +function do_cron($run_tasks) { + global $cron_lock; + + foreach ($run_tasks as $cron_type) { + $cron->run_task($cron_type); } -} - -define('CRON_ID', time() . ' ' . unique_id()); - -$sql = 'UPDATE ' . CONFIG_TABLE . " - SET config_value = '" . $db->sql_escape(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) -{ - exit; -} - -/** -* Run cron-like action -* Real cron-based layer will be introduced in 3.2 -*/ -switch ($cron_type) -{ - case 'queue': - - if (time() - $config['queue_interval'] <= $config['last_queue_run'] || !file_exists($phpbb_root_path . 'cache/queue.' . $phpEx)) - { - break; - } - - // A user reported using the mail() function while using shutdown does not work. We do not want to risk that. - if ($use_shutdown_function && !$config['smtp_delivery']) - { - $use_shutdown_function = false; - } - - include_once($phpbb_root_path . 'includes/functions_messenger.' . $phpEx); - $queue = new queue(); - - if ($use_shutdown_function) - { - register_shutdown_function(array(&$queue, 'process')); - } - else - { - $queue->process(); - } - - break; - - case 'tidy_cache': - - if (time() - $config['cache_gc'] <= $config['cache_last_gc'] || !method_exists($cache, 'tidy')) - { - break; - } - - if ($use_shutdown_function) - { - register_shutdown_function(array(&$cache, 'tidy')); - } - else - { - $cache->tidy(); - } - - break; - - case 'tidy_search': - - // Select the search method - $search_type = basename($config['search_type']); - - if (time() - $config['search_gc'] <= $config['search_last_gc'] || !file_exists($phpbb_root_path . 'includes/search/' . $search_type . '.' . $phpEx)) - { - break; - } - - include_once("{$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) - { - break; - } - - if ($use_shutdown_function) - { - register_shutdown_function(array(&$search, 'tidy')); - } - else - { - $search->tidy(); - } - - break; - - case 'tidy_warnings': - - if (time() - $config['warnings_gc'] <= $config['warnings_last_gc']) - { - break; - } - - include_once($phpbb_root_path . 'includes/functions_admin.' . $phpEx); - - if ($use_shutdown_function) - { - register_shutdown_function('tidy_warnings'); - } - else - { - tidy_warnings(); - } - - break; - - case 'tidy_database': - - if (time() - $config['database_gc'] <= $config['database_last_gc']) - { - break; - } - - include_once($phpbb_root_path . 'includes/functions_admin.' . $phpEx); - - if ($use_shutdown_function) - { - register_shutdown_function('tidy_database'); - } - else - { - tidy_database(); - } - - break; - - case 'tidy_sessions': - - if (time() - $config['session_gc'] <= $config['session_last_gc']) - { - break; - } - - if ($use_shutdown_function) - { - register_shutdown_function(array(&$user, 'session_gc')); - } - else - { - $user->session_gc(); - } - - break; - - case 'prune_forum': - - $forum_id = request_var('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) - { - break; - } - - // Do the forum Prune thang - if ($row['prune_next'] < time() && $row['enable_prune']) - { - include_once($phpbb_root_path . 'includes/functions_admin.' . $phpEx); - - if ($row['prune_days']) - { - if ($use_shutdown_function) - { - register_shutdown_function('auto_prune', $row['forum_id'], 'posted', $row['forum_flags'], $row['prune_days'], $row['prune_freq']); - } - else - { - auto_prune($row['forum_id'], 'posted', $row['forum_flags'], $row['prune_days'], $row['prune_freq']); - } - } - - if ($row['prune_viewed']) - { - if ($use_shutdown_function) - { - register_shutdown_function('auto_prune', $row['forum_id'], 'viewed', $row['forum_flags'], $row['prune_viewed'], $row['prune_freq']); - } - else - { - auto_prune($row['forum_id'], 'viewed', $row['forum_flags'], $row['prune_viewed'], $row['prune_freq']); - } - } - } - - break; -} - -// Unloading cache and closing db after having done the dirty work. -if ($use_shutdown_function) -{ - register_shutdown_function('unlock_cron'); - register_shutdown_function('garbage_collection'); -} -else -{ - unlock_cron(); + + // Unloading cache and closing db after having done the dirty work. + $cron_lock->unlock(); garbage_collection(); } -exit; +if ($cron_lock->lock()) { + if ($config['use_system_cron']) { + $use_shutdown_function = false; + + $run_tasks = $cron->find_all_runnable_tasks(); + } else { + $cron_type = request_var('cron_type', ''); + $use_shutdown_function = (@function_exists('register_shutdown_function')) ? true : false; + + output_image(); - -/** -* Unlock cron script -*/ -function unlock_cron() -{ - global $db; - - $sql = 'UPDATE ' . CONFIG_TABLE . " - SET config_value = '0' - WHERE config_name = 'cron_lock' AND config_value = '" . $db->sql_escape(CRON_ID) . "'"; - $db->sql_query($sql); + if ($cron->is_valid_task($cron_type) && $cron->is_task_runnable($cron_type)) + { + if ($use_shutdown_function && !$cron->is_task_shutdown_function_compatible($cron_type)) { + $use_shutdown_function = false; + } + $run_tasks = array($cron_type); + } + } + if ($use_shutdown_function) { + register_shutdown_function('do_cron', $run_tasks); + } else { + do_cron($run_tasks); + } } diff --git a/phpBB/includes/cron.php b/phpBB/includes/cron.php new file mode 100644 index 0000000000..2aa22858b7 --- /dev/null +++ b/phpBB/includes/cron.php @@ -0,0 +1,135 @@ +tasks as $cron_type => $params) { + $params['object'] = $object; + $this->tasks[$cron_type] = $params; + } + } + } + } + + function is_valid_task($cron_type) { + return isset($this->tasks[$cron_type]); + } + + function is_task_runnable($cron_type, $args=null) { + global $config; + $time_now = time(); + $cron_params = $this->tasks[$cron_type]; + if ($cron_params['enable_config'] && !$config[$cron_params['enable_config']]) { + return false; + } + if ($cron_param['custom_condition']) { + $callable = array($cron_params['object'], $cron_type . '_condition'); + if ($args) { + $answer = call_user_func_array($callable, $args); + } else { + $answer = call_user_func($callable); + } + if (!$answer) { + return false; + } + } + if ($time_now - $config[$cron_params['interval_config']] > $config[$cron_params['last_run_config']]) { + return true; + } + return false; + } + + function is_task_shutdown_function_compatible($cron_type) { + $cron_params = $this->tasks[$cron_type]; + if (isset($cron_params['shutdown_function_condition'])) { + return call_user_func(array($cron_params->object, $cron_type . '_shutdown_function_condition')); + } else { + return true; + } + } + + function determine_cron_mode_param() { + global $config; + if ($config['use_system_cron']) { + $mode = 'run_from_system'; + } else { + $mode_param = 'run_from_phpbb'; + } + return $mode_param; + } + + function find_one_runnable_task() { + $mode_param = $this->determine_cron_mode_param(); + foreach ($this->tasks as $cron_type => $cron_params) { + if ($cron_params[$mode_param] && $this->is_task_runnable($cron_type)) { + return $cron_type; + } + } + return null; + } + + function find_all_runnable_tasks() { + $mode_param = $this->determine_cron_mode_param(); + $tasks = array(); + foreach ($this->tasks as $cron_type => $cron_params) { + if ($cron_params[$mode_param] && $this->is_task_runnable($cron_type)) { + $tasks[] = $cron_type; + } + } + return $tasks; + } + + function generate_task_code($cron_type, $args=array()) { + $cron_params = $this->tasks[$cron_type]; + if ($cron_params['custom_code']) { + $code = call_user_func_array(array($cron_params['object'], $cron_type . '_code'), $args); + } else { + $code = $this->generate_generic_task_code($cron_type); + } + return $code; + } + + function generate_generic_task_code($cron_type) { + global $phpbb_root_path, $phpEx; + return 'cron'; + } + + function run_task($cron_type) { + call_user_func(array($this->tasks[$cron_type]['object'], 'run_' . $cron_type)); + } +} diff --git a/phpBB/includes/cron/standard.php b/phpBB/includes/cron/standard.php new file mode 100644 index 0000000000..1cb8738f17 --- /dev/null +++ b/phpBB/includes/cron/standard.php @@ -0,0 +1,163 @@ + array( + 'custom_condition' => true, + 'run_from_system' => true, + ), + 'prune_forum' => array( + 'custom_condition' => true, + 'custom_code' => true, + ), + 'queue' => array( + 'custom_condition' => true, + 'interval_config' => 'queue_interval_config', + 'last_run_config' => 'last_queue_run', + 'run_from_phpbb' => true, + 'run_from_system' => true, + 'shutdown_function_condition' => true, + ), + 'tidy_cache' => array( + 'custom_condition' => true, + 'interval_config' => 'cache_gc', + 'last_run_config' => 'cache_last_gc', + 'run_from_phpbb' => true, + 'run_from_system' => true, + ), + 'tidy_database' => array( + 'interval_config' => 'database_gc', + 'last_run_config' => 'database_last_gc', + 'run_from_phpbb' => true, + 'run_from_system' => true, + ), + 'tidy_search' => array( + 'interval_config' => 'search_gc', + 'last_run_config' => 'search_last_gc', + 'run_from_phpbb' => true, + 'run_from_system' => true, + ), + 'tidy_sessions' => array( + 'interval_config' => 'session_gc', + 'last_run_config' => 'session_last_gc', + 'run_from_phpbb' => true, + 'run_from_system' => true, + ), + 'tidy_warnings' => array( + 'enable_config' => 'warnings_expire_days', + 'interval_config' => 'warnings_gc', + 'last_run_config' => 'warnings_last_gc', + 'run_from_phpbb' => true, + 'run_from_system' => true, + ), + ); + + function prune_forum_condition($forum_data) { + return $forum_data['enable_prune'] && $forum_data['prune_next'] < time(); + } + + function prune_forum_code($forum_id) { + global $phpbb_root_path, $phpEx; + return 'cron'; + } + + function run_prune_forum() { + } + + function queue_condition() { + global $phpbb_root_path, $phpEx; + return file_exists($phpbb_root_path . 'cache/queue.' . $phpEx); + } + + function queue_shutdown_function_condition() { + global $config; + return !$config['smtp_delivery']; + } + + function run_queue() { + global $phpbb_root_path, $phpEx; + include_once($phpbb_root_path . 'includes/functions_messenger.' . $phpEx); + $queue = new queue(); + $queue->process(); + } + + function tidy_cache_condition() { + global $cache; + return method_exists($cache, 'tidy'); + } + + function run_tidy_cache() { + global $cache; + $cache->tidy(); + } + + function run_tidy_database() { + include_once($phpbb_root_path . 'includes/functions_admin.' . $phpEx); + tidy_database(); + } + + function tidy_search_condition() { + 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); + } + + function run_tidy_search() { + global $phpbb_root_path, $phpEx, $config, $error; + + // Select the search method + $search_type = basename($config['search_type']); + + include_once("{$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(); + } + } + + function run_tidy_sessions() { + global $user; + $user->session_gc(); + } + + function run_tidy_warnings() { + include_once($phpbb_root_path . 'includes/functions_admin.' . $phpEx); + tidy_warnings(); + } +} diff --git a/phpBB/includes/cron_lock.php b/phpBB/includes/cron_lock.php new file mode 100644 index 0000000000..2a09590772 --- /dev/null +++ b/phpBB/includes/cron_lock.php @@ -0,0 +1,71 @@ += time()) + { + return false; + } + } + + define('CRON_ID', time() . ' ' . unique_id()); + + $sql = 'UPDATE ' . CONFIG_TABLE . " + SET config_value = '" . $db->sql_escape(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; + } + + function unlock() { + global $db; + + $sql = 'UPDATE ' . CONFIG_TABLE . " + SET config_value = '0' + WHERE config_name = 'cron_lock' AND config_value = '" . $db->sql_escape(CRON_ID) . "'"; + $db->sql_query($sql); + } +} diff --git a/phpBB/includes/functions.php b/phpBB/includes/functions.php index 056d578e75..eb787bfc62 100644 --- a/phpBB/includes/functions.php +++ b/phpBB/includes/functions.php @@ -4595,7 +4595,7 @@ function page_footer($run_cron = true) // Call cron-type script $call_cron = false; - if (!defined('IN_CRON') && $run_cron && !$config['board_disable']) + if (!defined('IN_CRON') && !$config['use_system_cron'] && $run_cron && !$config['board_disable']) { $call_cron = true; $time_now = (!empty($user->time_now) && is_int($user->time_now)) ? $user->time_now : time(); @@ -4616,40 +4616,12 @@ function page_footer($run_cron = true) // Call cron job? if ($call_cron) { - $cron_type = ''; - - if ($time_now - $config['queue_interval'] > $config['last_queue_run'] && !defined('IN_ADMIN') && file_exists($phpbb_root_path . 'cache/queue.' . $phpEx)) - { - // Process email queue - $cron_type = 'queue'; - } - else if (method_exists($cache, 'tidy') && $time_now - $config['cache_gc'] > $config['cache_last_gc']) - { - // Tidy the cache - $cron_type = 'tidy_cache'; - } - else if ($config['warnings_expire_days'] && ($time_now - $config['warnings_gc'] > $config['warnings_last_gc'])) - { - $cron_type = 'tidy_warnings'; - } - else if ($time_now - $config['database_gc'] > $config['database_last_gc']) - { - // Tidy the database - $cron_type = 'tidy_database'; - } - else if ($time_now - $config['search_gc'] > $config['search_last_gc']) - { - // Tidy the search - $cron_type = 'tidy_search'; - } - else if ($time_now - $config['session_gc'] > $config['session_last_gc']) - { - $cron_type = 'tidy_sessions'; - } + global $cron; + $cron_type = $cron->find_one_runnable_task(); if ($cron_type) { - $template->assign_var('RUN_CRON_TASK', 'cron'); + $template->assign_var('RUN_CRON_TASK', $cron->generate_task_code($cron_type)); } } diff --git a/phpBB/viewforum.php b/phpBB/viewforum.php index 47d71849cb..8a06e28394 100644 --- a/phpBB/viewforum.php +++ b/phpBB/viewforum.php @@ -193,9 +193,9 @@ if ($forum_data['forum_topics_per_page']) } // Do the forum Prune thang - cron type job ... -if ($forum_data['prune_next'] < time() && $forum_data['enable_prune']) +if (!$config['use_system_cron'] && $cron->is_task_runnable('prune_forum', array($forum_data))) { - $template->assign_var('RUN_CRON_TASK', 'cron'); + $template->assign_var('RUN_CRON_TASK', $cron->generate_task_code('prune_forum', array($forum_id))); } // Forum rules and subscription info From 9be4f438eb1915e0b8b005abe42e3796867cac29 Mon Sep 17 00:00:00 2001 From: Oleg Pudeyev Date: Wed, 14 Apr 2010 20:43:24 -0400 Subject: [PATCH 02/67] [feature/system-cron] Private instance variable as cron id Changed cron lock to use a private instance variable for cron id instead of a constant. PHPBB3-9596 --- phpBB/includes/cron_lock.php | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/phpBB/includes/cron_lock.php b/phpBB/includes/cron_lock.php index 2a09590772..22c052f5ca 100644 --- a/phpBB/includes/cron_lock.php +++ b/phpBB/includes/cron_lock.php @@ -22,6 +22,8 @@ if (!defined('IN_PHPBB')) */ class cron_lock { + private $cron_id; + function lock() { global $config, $db; @@ -44,10 +46,10 @@ class cron_lock } } - define('CRON_ID', time() . ' ' . unique_id()); + $this->cron_id = time() . ' ' . unique_id(); $sql = 'UPDATE ' . CONFIG_TABLE . " - SET config_value = '" . $db->sql_escape(CRON_ID) . "' + 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); @@ -65,7 +67,7 @@ class cron_lock $sql = 'UPDATE ' . CONFIG_TABLE . " SET config_value = '0' - WHERE config_name = 'cron_lock' AND config_value = '" . $db->sql_escape(CRON_ID) . "'"; + WHERE config_name = 'cron_lock' AND config_value = '" . $db->sql_escape($this->cron_id) . "'"; $db->sql_query($sql); } } From 0532048292e056d62b7bafa5dda3d53b297bce94 Mon Sep 17 00:00:00 2001 From: Oleg Pudeyev Date: Thu, 15 Apr 2010 03:28:59 -0400 Subject: [PATCH 03/67] [feature/system-cron] WIP on reorganizing cron tasks into classes. PHPBB3-9596 --- phpBB/includes/cron_task.php | 60 ++++++++++++++++ phpBB/includes/cron_task_base.php | 64 +++++++++++++++++ .../cron_tasks/standard/prune_all_forums.php | 45 ++++++++++++ .../cron_tasks/standard/prune_forum.php | 67 +++++++++++++++++ phpBB/includes/cron_tasks/standard/queue.php | 64 +++++++++++++++++ .../cron_tasks/standard/tidy_cache.php | 53 ++++++++++++++ .../cron_tasks/standard/tidy_database.php | 44 ++++++++++++ .../cron_tasks/standard/tidy_search.php | 72 +++++++++++++++++++ .../cron_tasks/standard/tidy_sessions.php | 44 ++++++++++++ .../cron_tasks/standard/tidy_warnings.php | 56 +++++++++++++++ 10 files changed, 569 insertions(+) create mode 100644 phpBB/includes/cron_task.php create mode 100644 phpBB/includes/cron_task_base.php create mode 100644 phpBB/includes/cron_tasks/standard/prune_all_forums.php create mode 100644 phpBB/includes/cron_tasks/standard/prune_forum.php create mode 100644 phpBB/includes/cron_tasks/standard/queue.php create mode 100644 phpBB/includes/cron_tasks/standard/tidy_cache.php create mode 100644 phpBB/includes/cron_tasks/standard/tidy_database.php create mode 100644 phpBB/includes/cron_tasks/standard/tidy_search.php create mode 100644 phpBB/includes/cron_tasks/standard/tidy_sessions.php create mode 100644 phpBB/includes/cron_tasks/standard/tidy_warnings.php diff --git a/phpBB/includes/cron_task.php b/phpBB/includes/cron_task.php new file mode 100644 index 0000000000..8b9ffacae6 --- /dev/null +++ b/phpBB/includes/cron_task.php @@ -0,0 +1,60 @@ +forum_data = $forum_data; + } + + /** + * Runs this cron task. + */ + public function run() + { + } + + /** + * Returns whether this cron task can run, given current board configuration. + */ + public function is_runnable() + { + global $config; + return !$config['use_system_cron']; + } + + /** + * Returns whether this cron task should run now, because enough time + * has passed since it was last run. + */ + public function should_run() + { + return $this->forum_data['enable_prune'] && $this->forum_data['prune_next'] < time(); + } + + /** + * Returns parameters of this cron task as a query string. + */ + public function get_url_query_string() + { + return 'f=' . $this->forum_data['forum_id']; + } +} diff --git a/phpBB/includes/cron_tasks/standard/queue.php b/phpBB/includes/cron_tasks/standard/queue.php new file mode 100644 index 0000000000..6a69799ef4 --- /dev/null +++ b/phpBB/includes/cron_tasks/standard/queue.php @@ -0,0 +1,64 @@ +process(); + } + + /** + * Returns whether this cron task can run, given current board configuration. + */ + 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. + */ + public function should_run() + { + global $config; + return $config['last_queue_run'] < time() - $config['queue_interval_config']; + } + + /** + * Returns whether this cron task can be run in shutdown function. + */ + public function is_shutdown_function_safe() + { + global $config; + return !$config['smtp_delivery']; + } +} diff --git a/phpBB/includes/cron_tasks/standard/tidy_cache.php b/phpBB/includes/cron_tasks/standard/tidy_cache.php new file mode 100644 index 0000000000..7c47be06c1 --- /dev/null +++ b/phpBB/includes/cron_tasks/standard/tidy_cache.php @@ -0,0 +1,53 @@ +tidy(); + } + + /** + * Returns whether this cron task can run, given current board configuration. + */ + 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. + */ + public function should_run() + { + global $config; + return $config['cache_last_gc'] < time() - $config['cache_gc']; + } +} diff --git a/phpBB/includes/cron_tasks/standard/tidy_database.php b/phpBB/includes/cron_tasks/standard/tidy_database.php new file mode 100644 index 0000000000..16a17b3538 --- /dev/null +++ b/phpBB/includes/cron_tasks/standard/tidy_database.php @@ -0,0 +1,44 @@ +tidy(); + } + } + + /** + * Returns whether this cron task can run, given current board configuration. + */ + 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. + */ + public function should_run() + { + global $config; + return $config['search_last_gc'] < time() - $config['search_gc']; + } +} diff --git a/phpBB/includes/cron_tasks/standard/tidy_sessions.php b/phpBB/includes/cron_tasks/standard/tidy_sessions.php new file mode 100644 index 0000000000..6ff2dee14b --- /dev/null +++ b/phpBB/includes/cron_tasks/standard/tidy_sessions.php @@ -0,0 +1,44 @@ +session_gc(); + } + + /** + * Returns whether this cron task should run now, because enough time + * has passed since it was last run. + */ + public function should_run() + { + global $config; + return $config['session_last_gc'] < time() - $config['session_gc']; + } +} diff --git a/phpBB/includes/cron_tasks/standard/tidy_warnings.php b/phpBB/includes/cron_tasks/standard/tidy_warnings.php new file mode 100644 index 0000000000..059125b18d --- /dev/null +++ b/phpBB/includes/cron_tasks/standard/tidy_warnings.php @@ -0,0 +1,56 @@ + Date: Thu, 15 Apr 2010 10:05:47 -0400 Subject: [PATCH 04/67] [feature/system-cron] Added implementations for run methods in prune cron tasks. PHPBB3-9596 --- .../cron_tasks/standard/prune_all_forums.php | 18 +++++++ .../cron_tasks/standard/prune_forum.php | 49 ++++++++++++++++++- 2 files changed, 65 insertions(+), 2 deletions(-) diff --git a/phpBB/includes/cron_tasks/standard/prune_all_forums.php b/phpBB/includes/cron_tasks/standard/prune_all_forums.php index f54f5d9ed9..569752dcb0 100644 --- a/phpBB/includes/cron_tasks/standard/prune_all_forums.php +++ b/phpBB/includes/cron_tasks/standard/prune_all_forums.php @@ -32,6 +32,24 @@ class prune_all_forums_cron_task extends cron_task_base */ public function run() { + global $db; + $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); } /** diff --git a/phpBB/includes/cron_tasks/standard/prune_forum.php b/phpBB/includes/cron_tasks/standard/prune_forum.php index 5ae1083677..f4ef2ea6dd 100644 --- a/phpBB/includes/cron_tasks/standard/prune_forum.php +++ b/phpBB/includes/cron_tasks/standard/prune_forum.php @@ -27,9 +27,42 @@ if (!defined('IN_PHPBB')) */ class prune_forum_cron_task extends cron_task_base implements parametrized_cron_task { - public function __construct($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. + */ + public function __construct($forum_data=null) { - $this->forum_data = $forum_data; + global $db; + if ($forum_data) + { + $this->forum_data = $forum_data; + } + else + { + $forum_id = request_var('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) + { + // FIXME what to do? + break; + } + + $this->forum_data = $row; + } } /** @@ -37,6 +70,18 @@ class prune_forum_cron_task extends cron_task_base implements parametrized_cron_ */ public function run() { + global $phpbb_root_path, $phpEx; + include_once($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']); + } } /** From 61e0285da86ffaf825cfcd486b5372b9566e51d7 Mon Sep 17 00:00:00 2001 From: Oleg Pudeyev Date: Thu, 15 Apr 2010 10:11:40 -0400 Subject: [PATCH 05/67] [feature/system-cron] Reformatted cron, includes/cron and includes/cron_lock. PHPBB3-9596 --- phpBB/cron.php | 37 +++++++----- phpBB/includes/cron.php | 107 ++++++++++++++++++++++------------- phpBB/includes/cron_lock.php | 12 ++-- 3 files changed, 99 insertions(+), 57 deletions(-) diff --git a/phpBB/cron.php b/phpBB/cron.php index 1dbe1768c1..5f21e105d3 100644 --- a/phpBB/cron.php +++ b/phpBB/cron.php @@ -21,7 +21,8 @@ include($phpbb_root_path . 'includes/cron_lock.' . $phpEx); $user->session_begin(false); $auth->acl($user->data); -function output_image() { +function output_image() +{ // Output transparent gif header('Cache-Control: no-cache'); header('Content-type: image/gif'); @@ -33,40 +34,50 @@ function output_image() { // flush(); } -function do_cron($run_tasks) { +function do_cron($run_tasks) +{ global $cron_lock; - - foreach ($run_tasks as $cron_type) { + + foreach ($run_tasks as $cron_type) + { $cron->run_task($cron_type); } - + // Unloading cache and closing db after having done the dirty work. $cron_lock->unlock(); garbage_collection(); } -if ($cron_lock->lock()) { - if ($config['use_system_cron']) { +if ($cron_lock->lock()) +{ + if ($config['use_system_cron']) + { $use_shutdown_function = false; - + $run_tasks = $cron->find_all_runnable_tasks(); - } else { + } + else + { $cron_type = request_var('cron_type', ''); $use_shutdown_function = (@function_exists('register_shutdown_function')) ? true : false; - + output_image(); if ($cron->is_valid_task($cron_type) && $cron->is_task_runnable($cron_type)) { - if ($use_shutdown_function && !$cron->is_task_shutdown_function_compatible($cron_type)) { + if ($use_shutdown_function && !$cron->is_task_shutdown_function_compatible($cron_type)) + { $use_shutdown_function = false; } $run_tasks = array($cron_type); } } - if ($use_shutdown_function) { + if ($use_shutdown_function) + { register_shutdown_function('do_cron', $run_tasks); - } else { + } + else + { do_cron($run_tasks); } } diff --git a/phpBB/includes/cron.php b/phpBB/includes/cron.php index 2aa22858b7..b9a1bb778a 100644 --- a/phpBB/includes/cron.php +++ b/phpBB/includes/cron.php @@ -23,113 +23,142 @@ if (!defined('IN_PHPBB')) class cron { var $tasks = array(); - - function cron() { + + function cron() + { global $config, $phpbb_root_path, $phpEx; $modules = $config['cron_modules']; $modules = explode(',', $modules); - foreach ($modules as $module) { + foreach ($modules as $module) + { // explode will return array("") when exploding an empty string; // users may also specify something like foo,,bar. // Account for module being possibly empty - if (!empty($module)) { + if (!empty($module)) + { // Misspelling or specifying nonexistent modules here may make the board // unusable due to error messages screwing up header output include_once($phpbb_root_path . "includes/cron/$module.$phpEx"); $cron_class = "cron_tasks_$module"; $object = new $cron_class; - foreach ($object->tasks as $cron_type => $params) { + foreach ($object->tasks as $cron_type => $params) + { $params['object'] = $object; $this->tasks[$cron_type] = $params; } } } } - - function is_valid_task($cron_type) { + + function is_valid_task($cron_type) + { return isset($this->tasks[$cron_type]); } - - function is_task_runnable($cron_type, $args=null) { + + function is_task_runnable($cron_type, $args=null) + { global $config; $time_now = time(); $cron_params = $this->tasks[$cron_type]; - if ($cron_params['enable_config'] && !$config[$cron_params['enable_config']]) { + if ($cron_params['enable_config'] && !$config[$cron_params['enable_config']]) + { return false; } - if ($cron_param['custom_condition']) { + if ($cron_param['custom_condition']) + { $callable = array($cron_params['object'], $cron_type . '_condition'); - if ($args) { + if ($args) + { $answer = call_user_func_array($callable, $args); - } else { + } else + { $answer = call_user_func($callable); } - if (!$answer) { + if (!$answer) + { return false; } } - if ($time_now - $config[$cron_params['interval_config']] > $config[$cron_params['last_run_config']]) { + if ($time_now - $config[$cron_params['interval_config']] > $config[$cron_params['last_run_config']]) + { return true; } return false; } - - function is_task_shutdown_function_compatible($cron_type) { + + function is_task_shutdown_function_compatible($cron_type) + { $cron_params = $this->tasks[$cron_type]; - if (isset($cron_params['shutdown_function_condition'])) { + if (isset($cron_params['shutdown_function_condition'])) + { return call_user_func(array($cron_params->object, $cron_type . '_shutdown_function_condition')); - } else { + } else + { return true; } } - - function determine_cron_mode_param() { + + function determine_cron_mode_param() + { global $config; - if ($config['use_system_cron']) { + if ($config['use_system_cron']) + { $mode = 'run_from_system'; - } else { + } else + { $mode_param = 'run_from_phpbb'; } return $mode_param; } - - function find_one_runnable_task() { + + function find_one_runnable_task() + { $mode_param = $this->determine_cron_mode_param(); - foreach ($this->tasks as $cron_type => $cron_params) { - if ($cron_params[$mode_param] && $this->is_task_runnable($cron_type)) { + foreach ($this->tasks as $cron_type => $cron_params) + { + if ($cron_params[$mode_param] && $this->is_task_runnable($cron_type)) + { return $cron_type; } } return null; } - - function find_all_runnable_tasks() { + + function find_all_runnable_tasks() + { $mode_param = $this->determine_cron_mode_param(); $tasks = array(); - foreach ($this->tasks as $cron_type => $cron_params) { - if ($cron_params[$mode_param] && $this->is_task_runnable($cron_type)) { + foreach ($this->tasks as $cron_type => $cron_params) + { + if ($cron_params[$mode_param] && $this->is_task_runnable($cron_type)) + { $tasks[] = $cron_type; } } return $tasks; } - - function generate_task_code($cron_type, $args=array()) { + + function generate_task_code($cron_type, $args=array()) + { $cron_params = $this->tasks[$cron_type]; - if ($cron_params['custom_code']) { + if ($cron_params['custom_code']) + { $code = call_user_func_array(array($cron_params['object'], $cron_type . '_code'), $args); - } else { + } else + { $code = $this->generate_generic_task_code($cron_type); } return $code; } - - function generate_generic_task_code($cron_type) { + + function generate_generic_task_code($cron_type) + { global $phpbb_root_path, $phpEx; return 'cron'; } - - function run_task($cron_type) { + + function run_task($cron_type) + { call_user_func(array($this->tasks[$cron_type]['object'], 'run_' . $cron_type)); } } diff --git a/phpBB/includes/cron_lock.php b/phpBB/includes/cron_lock.php index 22c052f5ca..1046d62da4 100644 --- a/phpBB/includes/cron_lock.php +++ b/phpBB/includes/cron_lock.php @@ -24,9 +24,10 @@ class cron_lock { private $cron_id; - function lock() { + function lock() + { global $config, $db; - + if (!isset($config['cron_lock'])) { set_config('cron_lock', '0', true); @@ -58,11 +59,12 @@ class cron_lock { return false; } - + return true; } - - function unlock() { + + function unlock() + { global $db; $sql = 'UPDATE ' . CONFIG_TABLE . " From 0f9b3bcc27e7daf7d605a7a38310a8f62b9a76e8 Mon Sep 17 00:00:00 2001 From: Oleg Pudeyev Date: Sat, 17 Apr 2010 03:13:30 -0400 Subject: [PATCH 06/67] [feature/system-cron] Refactored cron task naming, loading and running. PHPBB3-9596 --- phpBB/includes/cron.php | 164 ---------------- phpBB/includes/{ => cron}/cron_lock.php | 0 phpBB/includes/cron/cron_manager.php | 175 ++++++++++++++++++ phpBB/includes/{ => cron}/cron_task.php | 0 phpBB/includes/{ => cron}/cron_task_base.php | 0 phpBB/includes/cron/cron_task_wrapper.php | 68 +++++++ phpBB/includes/cron/standard.php | 163 ---------------- .../tasks/core}/prune_all_forums.php | 2 +- .../tasks/core}/prune_forum.php | 2 +- .../standard => cron/tasks/core}/queue.php | 2 +- .../tasks/core}/tidy_cache.php | 2 +- .../tasks/core}/tidy_database.php | 2 +- .../tasks/core}/tidy_search.php | 2 +- .../tasks/core}/tidy_sessions.php | 2 +- .../tasks/core}/tidy_warnings.php | 2 +- phpBB/includes/functions.php | 7 +- phpBB/viewforum.php | 8 +- 17 files changed, 261 insertions(+), 340 deletions(-) delete mode 100644 phpBB/includes/cron.php rename phpBB/includes/{ => cron}/cron_lock.php (100%) create mode 100644 phpBB/includes/cron/cron_manager.php rename phpBB/includes/{ => cron}/cron_task.php (100%) rename phpBB/includes/{ => cron}/cron_task_base.php (100%) create mode 100644 phpBB/includes/cron/cron_task_wrapper.php delete mode 100644 phpBB/includes/cron/standard.php rename phpBB/includes/{cron_tasks/standard => cron/tasks/core}/prune_all_forums.php (95%) rename phpBB/includes/{cron_tasks/standard => cron/tasks/core}/prune_forum.php (96%) rename phpBB/includes/{cron_tasks/standard => cron/tasks/core}/queue.php (96%) rename phpBB/includes/{cron_tasks/standard => cron/tasks/core}/tidy_cache.php (93%) rename phpBB/includes/{cron_tasks/standard => cron/tasks/core}/tidy_database.php (92%) rename phpBB/includes/{cron_tasks/standard => cron/tasks/core}/tidy_search.php (96%) rename phpBB/includes/{cron_tasks/standard => cron/tasks/core}/tidy_sessions.php (91%) rename phpBB/includes/{cron_tasks/standard => cron/tasks/core}/tidy_warnings.php (94%) diff --git a/phpBB/includes/cron.php b/phpBB/includes/cron.php deleted file mode 100644 index b9a1bb778a..0000000000 --- a/phpBB/includes/cron.php +++ /dev/null @@ -1,164 +0,0 @@ -tasks as $cron_type => $params) - { - $params['object'] = $object; - $this->tasks[$cron_type] = $params; - } - } - } - } - - function is_valid_task($cron_type) - { - return isset($this->tasks[$cron_type]); - } - - function is_task_runnable($cron_type, $args=null) - { - global $config; - $time_now = time(); - $cron_params = $this->tasks[$cron_type]; - if ($cron_params['enable_config'] && !$config[$cron_params['enable_config']]) - { - return false; - } - if ($cron_param['custom_condition']) - { - $callable = array($cron_params['object'], $cron_type . '_condition'); - if ($args) - { - $answer = call_user_func_array($callable, $args); - } else - { - $answer = call_user_func($callable); - } - if (!$answer) - { - return false; - } - } - if ($time_now - $config[$cron_params['interval_config']] > $config[$cron_params['last_run_config']]) - { - return true; - } - return false; - } - - function is_task_shutdown_function_compatible($cron_type) - { - $cron_params = $this->tasks[$cron_type]; - if (isset($cron_params['shutdown_function_condition'])) - { - return call_user_func(array($cron_params->object, $cron_type . '_shutdown_function_condition')); - } else - { - return true; - } - } - - function determine_cron_mode_param() - { - global $config; - if ($config['use_system_cron']) - { - $mode = 'run_from_system'; - } else - { - $mode_param = 'run_from_phpbb'; - } - return $mode_param; - } - - function find_one_runnable_task() - { - $mode_param = $this->determine_cron_mode_param(); - foreach ($this->tasks as $cron_type => $cron_params) - { - if ($cron_params[$mode_param] && $this->is_task_runnable($cron_type)) - { - return $cron_type; - } - } - return null; - } - - function find_all_runnable_tasks() - { - $mode_param = $this->determine_cron_mode_param(); - $tasks = array(); - foreach ($this->tasks as $cron_type => $cron_params) - { - if ($cron_params[$mode_param] && $this->is_task_runnable($cron_type)) - { - $tasks[] = $cron_type; - } - } - return $tasks; - } - - function generate_task_code($cron_type, $args=array()) - { - $cron_params = $this->tasks[$cron_type]; - if ($cron_params['custom_code']) - { - $code = call_user_func_array(array($cron_params['object'], $cron_type . '_code'), $args); - } else - { - $code = $this->generate_generic_task_code($cron_type); - } - return $code; - } - - function generate_generic_task_code($cron_type) - { - global $phpbb_root_path, $phpEx; - return 'cron'; - } - - function run_task($cron_type) - { - call_user_func(array($this->tasks[$cron_type]['object'], 'run_' . $cron_type)); - } -} diff --git a/phpBB/includes/cron_lock.php b/phpBB/includes/cron/cron_lock.php similarity index 100% rename from phpBB/includes/cron_lock.php rename to phpBB/includes/cron/cron_lock.php diff --git a/phpBB/includes/cron/cron_manager.php b/phpBB/includes/cron/cron_manager.php new file mode 100644 index 0000000000..5ee06ac102 --- /dev/null +++ b/phpBB/includes/cron/cron_manager.php @@ -0,0 +1,175 @@ +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($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 && 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)) + { + include_once($phpbb_root_path . "includes/cron/$mod/$filename.$phpEx"); + $class = "cron_task_${mod}_${filename}"; + $object = new $class; + $this->tasks[] = $object; + } + } + } + + /** + * Finds a task that is ready to run. + * + * If several tasks are ready, any one of them could be returned. + */ + 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. + */ + 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. + */ + function find_task($name) + { + foreach ($this->tasks as $task) + { + if ($task->get_name() == $name) + { + return $task; + } + } + return null; + } + + function instantiate_task($name, $args) + { + $task = $this->find_task($name); + if ($task) + { + $class = get_class($task); + $task = new $class($args); + } + return $task; + } + + function generate_generic_task_code($cron_type) + { + global $phpbb_root_path, $phpEx; + return 'cron'; + } +} diff --git a/phpBB/includes/cron_task.php b/phpBB/includes/cron/cron_task.php similarity index 100% rename from phpBB/includes/cron_task.php rename to phpBB/includes/cron/cron_task.php diff --git a/phpBB/includes/cron_task_base.php b/phpBB/includes/cron/cron_task_base.php similarity index 100% rename from phpBB/includes/cron_task_base.php rename to phpBB/includes/cron/cron_task_base.php diff --git a/phpBB/includes/cron/cron_task_wrapper.php b/phpBB/includes/cron/cron_task_wrapper.php new file mode 100644 index 0000000000..3919e4f049 --- /dev/null +++ b/phpBB/includes/cron/cron_task_wrapper.php @@ -0,0 +1,68 @@ +task = $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. + */ + public function get_name() + { + $class = get_class($this->task); + return preg_replace('/^cron_task_/', '', $class); + } + + public function get_url() + { + global $phpbb_root_path, $phpEx; + + $name = $this->get_name(); + $url = append_sid($phpbb_root_path . 'cron.' . $phpEx, 'cron_type=' . $name); + 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/standard.php b/phpBB/includes/cron/standard.php deleted file mode 100644 index 1cb8738f17..0000000000 --- a/phpBB/includes/cron/standard.php +++ /dev/null @@ -1,163 +0,0 @@ - array( - 'custom_condition' => true, - 'run_from_system' => true, - ), - 'prune_forum' => array( - 'custom_condition' => true, - 'custom_code' => true, - ), - 'queue' => array( - 'custom_condition' => true, - 'interval_config' => 'queue_interval_config', - 'last_run_config' => 'last_queue_run', - 'run_from_phpbb' => true, - 'run_from_system' => true, - 'shutdown_function_condition' => true, - ), - 'tidy_cache' => array( - 'custom_condition' => true, - 'interval_config' => 'cache_gc', - 'last_run_config' => 'cache_last_gc', - 'run_from_phpbb' => true, - 'run_from_system' => true, - ), - 'tidy_database' => array( - 'interval_config' => 'database_gc', - 'last_run_config' => 'database_last_gc', - 'run_from_phpbb' => true, - 'run_from_system' => true, - ), - 'tidy_search' => array( - 'interval_config' => 'search_gc', - 'last_run_config' => 'search_last_gc', - 'run_from_phpbb' => true, - 'run_from_system' => true, - ), - 'tidy_sessions' => array( - 'interval_config' => 'session_gc', - 'last_run_config' => 'session_last_gc', - 'run_from_phpbb' => true, - 'run_from_system' => true, - ), - 'tidy_warnings' => array( - 'enable_config' => 'warnings_expire_days', - 'interval_config' => 'warnings_gc', - 'last_run_config' => 'warnings_last_gc', - 'run_from_phpbb' => true, - 'run_from_system' => true, - ), - ); - - function prune_forum_condition($forum_data) { - return $forum_data['enable_prune'] && $forum_data['prune_next'] < time(); - } - - function prune_forum_code($forum_id) { - global $phpbb_root_path, $phpEx; - return 'cron'; - } - - function run_prune_forum() { - } - - function queue_condition() { - global $phpbb_root_path, $phpEx; - return file_exists($phpbb_root_path . 'cache/queue.' . $phpEx); - } - - function queue_shutdown_function_condition() { - global $config; - return !$config['smtp_delivery']; - } - - function run_queue() { - global $phpbb_root_path, $phpEx; - include_once($phpbb_root_path . 'includes/functions_messenger.' . $phpEx); - $queue = new queue(); - $queue->process(); - } - - function tidy_cache_condition() { - global $cache; - return method_exists($cache, 'tidy'); - } - - function run_tidy_cache() { - global $cache; - $cache->tidy(); - } - - function run_tidy_database() { - include_once($phpbb_root_path . 'includes/functions_admin.' . $phpEx); - tidy_database(); - } - - function tidy_search_condition() { - 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); - } - - function run_tidy_search() { - global $phpbb_root_path, $phpEx, $config, $error; - - // Select the search method - $search_type = basename($config['search_type']); - - include_once("{$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(); - } - } - - function run_tidy_sessions() { - global $user; - $user->session_gc(); - } - - function run_tidy_warnings() { - include_once($phpbb_root_path . 'includes/functions_admin.' . $phpEx); - tidy_warnings(); - } -} diff --git a/phpBB/includes/cron_tasks/standard/prune_all_forums.php b/phpBB/includes/cron/tasks/core/prune_all_forums.php similarity index 95% rename from phpBB/includes/cron_tasks/standard/prune_all_forums.php rename to phpBB/includes/cron/tasks/core/prune_all_forums.php index 569752dcb0..13286de2b0 100644 --- a/phpBB/includes/cron_tasks/standard/prune_all_forums.php +++ b/phpBB/includes/cron/tasks/core/prune_all_forums.php @@ -25,7 +25,7 @@ if (!defined('IN_PHPBB')) * * @package phpBB3 */ -class prune_all_forums_cron_task extends cron_task_base +class cron_task_core_prune_all_forums extends cron_task_base { /** * Runs this cron task. diff --git a/phpBB/includes/cron_tasks/standard/prune_forum.php b/phpBB/includes/cron/tasks/core/prune_forum.php similarity index 96% rename from phpBB/includes/cron_tasks/standard/prune_forum.php rename to phpBB/includes/cron/tasks/core/prune_forum.php index f4ef2ea6dd..4925447162 100644 --- a/phpBB/includes/cron_tasks/standard/prune_forum.php +++ b/phpBB/includes/cron/tasks/core/prune_forum.php @@ -25,7 +25,7 @@ if (!defined('IN_PHPBB')) * * @package phpBB3 */ -class prune_forum_cron_task extends cron_task_base implements parametrized_cron_task +class cron_task_core_prune_forum extends cron_task_base implements parametrized_cron_task { /** * Constructor. diff --git a/phpBB/includes/cron_tasks/standard/queue.php b/phpBB/includes/cron/tasks/core/queue.php similarity index 96% rename from phpBB/includes/cron_tasks/standard/queue.php rename to phpBB/includes/cron/tasks/core/queue.php index 6a69799ef4..d7dfeb9319 100644 --- a/phpBB/includes/cron_tasks/standard/queue.php +++ b/phpBB/includes/cron/tasks/core/queue.php @@ -21,7 +21,7 @@ if (!defined('IN_PHPBB')) * * @package phpBB3 */ -class queue_cron_task extends cron_task_base +class cron_task_core_queue extends cron_task_base { /** * Runs this cron task. diff --git a/phpBB/includes/cron_tasks/standard/tidy_cache.php b/phpBB/includes/cron/tasks/core/tidy_cache.php similarity index 93% rename from phpBB/includes/cron_tasks/standard/tidy_cache.php rename to phpBB/includes/cron/tasks/core/tidy_cache.php index 7c47be06c1..69038a8a5a 100644 --- a/phpBB/includes/cron_tasks/standard/tidy_cache.php +++ b/phpBB/includes/cron/tasks/core/tidy_cache.php @@ -21,7 +21,7 @@ if (!defined('IN_PHPBB')) * * @package phpBB3 */ -class tidy_cache_cron_task extends cron_task_base +class cron_task_core_tidy_cache extends cron_task_base { /** * Runs this cron task. diff --git a/phpBB/includes/cron_tasks/standard/tidy_database.php b/phpBB/includes/cron/tasks/core/tidy_database.php similarity index 92% rename from phpBB/includes/cron_tasks/standard/tidy_database.php rename to phpBB/includes/cron/tasks/core/tidy_database.php index 16a17b3538..c6c2a60445 100644 --- a/phpBB/includes/cron_tasks/standard/tidy_database.php +++ b/phpBB/includes/cron/tasks/core/tidy_database.php @@ -21,7 +21,7 @@ if (!defined('IN_PHPBB')) * * @package phpBB3 */ -class tidy_database_cron_task extends cron_task_base +class cron_task_core_tidy_database extends cron_task_base { /** * Runs this cron task. diff --git a/phpBB/includes/cron_tasks/standard/tidy_search.php b/phpBB/includes/cron/tasks/core/tidy_search.php similarity index 96% rename from phpBB/includes/cron_tasks/standard/tidy_search.php rename to phpBB/includes/cron/tasks/core/tidy_search.php index ac42c26689..0f409d114a 100644 --- a/phpBB/includes/cron_tasks/standard/tidy_search.php +++ b/phpBB/includes/cron/tasks/core/tidy_search.php @@ -23,7 +23,7 @@ if (!defined('IN_PHPBB')) * * @package phpBB3 */ -class tidy_sessions_cron_task extends cron_task_base +class cron_task_core_tidy_sessions extends cron_task_base { /** * Runs this cron task. diff --git a/phpBB/includes/cron_tasks/standard/tidy_sessions.php b/phpBB/includes/cron/tasks/core/tidy_sessions.php similarity index 91% rename from phpBB/includes/cron_tasks/standard/tidy_sessions.php rename to phpBB/includes/cron/tasks/core/tidy_sessions.php index 6ff2dee14b..ea6aa70699 100644 --- a/phpBB/includes/cron_tasks/standard/tidy_sessions.php +++ b/phpBB/includes/cron/tasks/core/tidy_sessions.php @@ -21,7 +21,7 @@ if (!defined('IN_PHPBB')) * * @package phpBB3 */ -class tidy_sessions_cron_task extends cron_task_base +class cron_task_core_tidy_sessions extends cron_task_base { /** * Runs this cron task. diff --git a/phpBB/includes/cron_tasks/standard/tidy_warnings.php b/phpBB/includes/cron/tasks/core/tidy_warnings.php similarity index 94% rename from phpBB/includes/cron_tasks/standard/tidy_warnings.php rename to phpBB/includes/cron/tasks/core/tidy_warnings.php index 059125b18d..c1ab14d788 100644 --- a/phpBB/includes/cron_tasks/standard/tidy_warnings.php +++ b/phpBB/includes/cron/tasks/core/tidy_warnings.php @@ -23,7 +23,7 @@ if (!defined('IN_PHPBB')) * * @package phpBB3 */ -class tidy_warnings_cron_task extends cron_task_base +class cron_task_core_tidy_warnings extends cron_task_base { /** * Runs this cron task. diff --git a/phpBB/includes/functions.php b/phpBB/includes/functions.php index eb787bfc62..418e8dc51d 100644 --- a/phpBB/includes/functions.php +++ b/phpBB/includes/functions.php @@ -4617,11 +4617,12 @@ function page_footer($run_cron = true) if ($call_cron) { global $cron; - $cron_type = $cron->find_one_runnable_task(); + $task = $cron->find_one_ready_task(); - if ($cron_type) + if ($task) { - $template->assign_var('RUN_CRON_TASK', $cron->generate_task_code($cron_type)); + $url = $task->get_url(); + $template->assign_var('RUN_CRON_TASK', 'cron'); } } diff --git a/phpBB/viewforum.php b/phpBB/viewforum.php index 8a06e28394..a3fd9fa6fb 100644 --- a/phpBB/viewforum.php +++ b/phpBB/viewforum.php @@ -193,9 +193,13 @@ if ($forum_data['forum_topics_per_page']) } // Do the forum Prune thang - cron type job ... -if (!$config['use_system_cron'] && $cron->is_task_runnable('prune_forum', array($forum_data))) +if (!$config['use_system_cron']) { - $template->assign_var('RUN_CRON_TASK', $cron->generate_task_code('prune_forum', array($forum_id))); + $task = $cron->instantiate_task('core_prune_forum', $forum_data); + if ($task && $task->is_ready()) { + $url = $task->get_url(); + $template->assign_var('RUN_CRON_TASK', 'cron'); + } } // Forum rules and subscription info From 0cfbdcc7449f1cc17b819ffe49aec88c274dd090 Mon Sep 17 00:00:00 2001 From: Oleg Pudeyev Date: Sat, 17 Apr 2010 06:32:15 -0400 Subject: [PATCH 07/67] [feature/system-cron] Reorganized cron task parametrization. PHPBB3-9596 --- phpBB/cron.php | 24 ++++---- phpBB/includes/cron/cron_task.php | 26 +++++++- phpBB/includes/cron/cron_task_wrapper.php | 32 +++++++++- .../includes/cron/tasks/core/prune_forum.php | 59 ++++++++++++------- 4 files changed, 107 insertions(+), 34 deletions(-) diff --git a/phpBB/cron.php b/phpBB/cron.php index 5f21e105d3..2d9e6afe5d 100644 --- a/phpBB/cron.php +++ b/phpBB/cron.php @@ -38,9 +38,9 @@ function do_cron($run_tasks) { global $cron_lock; - foreach ($run_tasks as $cron_type) + foreach ($run_tasks as $task) { - $cron->run_task($cron_type); + $task->run(); } // Unloading cache and closing db after having done the dirty work. @@ -54,7 +54,7 @@ if ($cron_lock->lock()) { $use_shutdown_function = false; - $run_tasks = $cron->find_all_runnable_tasks(); + $run_tasks = $cron->find_all_ready_tasks(); } else { @@ -62,14 +62,18 @@ if ($cron_lock->lock()) $use_shutdown_function = (@function_exists('register_shutdown_function')) ? true : false; output_image(); - - if ($cron->is_valid_task($cron_type) && $cron->is_task_runnable($cron_type)) - { - if ($use_shutdown_function && !$cron->is_task_shutdown_function_compatible($cron_type)) - { - $use_shutdown_function = false; + + $task = $cron->find_task($cron_type); + if ($task) { + if ($task->is_parametrized()) { + $task->parse_parameters($_GET); + } + if ($task->is_ready()) { + if ($use_shutdown_function && !$task->is_shutdown_function_safe()) { + $use_shutdown_function = false; + } + $run_tasks = array($task); } - $run_tasks = array($cron_type); } } if ($use_shutdown_function) diff --git a/phpBB/includes/cron/cron_task.php b/phpBB/includes/cron/cron_task.php index 8b9ffacae6..46eacff517 100644 --- a/phpBB/includes/cron/cron_task.php +++ b/phpBB/includes/cron/cron_task.php @@ -48,13 +48,33 @@ interface cron_task } /** -* Parametrized cron task interface +* 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 parametrized_cron_task extends cron_task { /** - * Returns parameters of this cron task as a query string. + * Returns parameters of this cron task as an array. + * + * The array must map string keys to string values. */ - public function get_url_query_string(); + public function get_parameters(); + + /** + * Parses parameters found in $params, which is an array. + * + * $params contains user input and must not be trusted. + * In normal operation $params contains the same data that was returned by + * get_parameters method. However, a malicious user can supply arbitrary + * data in $params. + * Cron task must validate all keys and values in $params before using them. + */ + public function parse_parameters($params); } diff --git a/phpBB/includes/cron/cron_task_wrapper.php b/phpBB/includes/cron/cron_task_wrapper.php index 3919e4f049..0e63000846 100644 --- a/phpBB/includes/cron/cron_task_wrapper.php +++ b/phpBB/includes/cron/cron_task_wrapper.php @@ -24,11 +24,25 @@ if (!defined('IN_PHPBB')) */ class cron_task_wrapper { + /** + * Wraps a task $task, which must implement cron_task interface. + */ public function __construct($task) { $this->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. * @@ -49,12 +63,28 @@ class cron_task_wrapper return preg_replace('/^cron_task_/', '', $class); } + /** + * 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(); - $url = append_sid($phpbb_root_path . 'cron.' . $phpEx, 'cron_type=' . $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; } diff --git a/phpBB/includes/cron/tasks/core/prune_forum.php b/phpBB/includes/cron/tasks/core/prune_forum.php index 4925447162..f9fea7a5b8 100644 --- a/phpBB/includes/cron/tasks/core/prune_forum.php +++ b/phpBB/includes/cron/tasks/core/prune_forum.php @@ -27,6 +27,8 @@ if (!defined('IN_PHPBB')) */ class cron_task_core_prune_forum extends cron_task_base implements parametrized_cron_task { + private $forum_data; + /** * Constructor. * @@ -46,22 +48,7 @@ class cron_task_core_prune_forum extends cron_task_base implements parametrized_ } else { - $forum_id = request_var('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) - { - // FIXME what to do? - break; - } - - $this->forum_data = $row; + $this->forum_data = null; } } @@ -90,7 +77,7 @@ class cron_task_core_prune_forum extends cron_task_base implements parametrized_ public function is_runnable() { global $config; - return !$config['use_system_cron']; + return !$config['use_system_cron'] && $this->forum_data; } /** @@ -103,10 +90,42 @@ class cron_task_core_prune_forum extends cron_task_base implements parametrized_ } /** - * Returns parameters of this cron task as a query string. + * 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. */ - public function get_url_query_string() + public function get_parameters() { - return 'f=' . $this->forum_data['forum_id']; + return array('f' => $this->forum_data['forum_id']); + } + + /** + * Parses parameters found in $params, which is an array. + * + * $params may contain user input and is not trusted. + * + * $params is expected to have a key f whose value is id of the forum to be pruned. + */ + public function parse_parameters($params) + { + global $db; + + $this->forum_data = null; + if (isset($params['f'])) + { + $forum_id = int($params['f']); + + $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; + } + } } } From b620f299375426e04af6d8f61a849490560da781 Mon Sep 17 00:00:00 2001 From: Oleg Pudeyev Date: Sat, 17 Apr 2010 06:58:00 -0400 Subject: [PATCH 08/67] [feature/system-cron] Wrap cron tasks in cron manager. PHPBB3-9596 --- phpBB/includes/cron/cron_manager.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/phpBB/includes/cron/cron_manager.php b/phpBB/includes/cron/cron_manager.php index 5ee06ac102..a9101ff872 100644 --- a/phpBB/includes/cron/cron_manager.php +++ b/phpBB/includes/cron/cron_manager.php @@ -101,7 +101,8 @@ class cron_manager include_once($phpbb_root_path . "includes/cron/$mod/$filename.$phpEx"); $class = "cron_task_${mod}_${filename}"; $object = new $class; - $this->tasks[] = $object; + $wrapper = new cron_task_wrapper($object); + $this->tasks[] = $wrapper; } } } From 8f567a21a3fe5c4ceeaed8c38c6bcf8251821e3a Mon Sep 17 00:00:00 2001 From: Oleg Pudeyev Date: Sat, 17 Apr 2010 07:05:54 -0400 Subject: [PATCH 09/67] [feature/system-cron] Updated includes after moving files. PHPBB3-9596 --- phpBB/common.php | 2 +- phpBB/cron.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/phpBB/common.php b/phpBB/common.php index 3586031c36..e2300f0bd5 100644 --- a/phpBB/common.php +++ b/phpBB/common.php @@ -242,6 +242,6 @@ foreach ($cache->obtain_hooks() as $hook) if (!$config['use_system_cron']) { - require($phpbb_root_path . 'includes/cron.' . $phpEx); + include($phpbb_root_path . 'includes/cron/cron_manager.' . $phpEx); $cron = new cron(); } diff --git a/phpBB/cron.php b/phpBB/cron.php index 2d9e6afe5d..0621d7651b 100644 --- a/phpBB/cron.php +++ b/phpBB/cron.php @@ -15,7 +15,7 @@ define('IN_CRON', true); $phpbb_root_path = (defined('PHPBB_ROOT_PATH')) ? PHPBB_ROOT_PATH : './'; $phpEx = substr(strrchr(__FILE__, '.'), 1); include($phpbb_root_path . 'common.' . $phpEx); -include($phpbb_root_path . 'includes/cron_lock.' . $phpEx); +include($phpbb_root_path . 'includes/cron/cron_lock.' . $phpEx); // Do not update users last page entry $user->session_begin(false); From e151741fb6aa26a6fa96515cac79a22fe70f44cd Mon Sep 17 00:00:00 2001 From: Oleg Pudeyev Date: Sat, 17 Apr 2010 07:10:53 -0400 Subject: [PATCH 10/67] [feature/system-cron] Added missing implements declaration to cron task base. PHPBB3-9596 --- phpBB/includes/cron/cron_task_base.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/phpBB/includes/cron/cron_task_base.php b/phpBB/includes/cron/cron_task_base.php index f66894e7b2..3561f4e14f 100644 --- a/phpBB/includes/cron/cron_task_base.php +++ b/phpBB/includes/cron/cron_task_base.php @@ -27,7 +27,7 @@ if (!defined('IN_PHPBB')) * * @package phpBB3 */ -abstract class cron_task_base +abstract class cron_task_base implements cron_task { /** * Runs this cron task. From fe72fe98786f50d91ef184aa9c5c9d3511b41167 Mon Sep 17 00:00:00 2001 From: Oleg Pudeyev Date: Sat, 17 Apr 2010 07:13:02 -0400 Subject: [PATCH 11/67] [feature/system-cron] Added includes for class dependencies. PHPBB3-9596 --- phpBB/includes/cron/cron_manager.php | 2 ++ phpBB/includes/cron/cron_task_base.php | 2 ++ phpBB/includes/cron/tasks/core/prune_all_forums.php | 2 ++ phpBB/includes/cron/tasks/core/prune_forum.php | 2 ++ phpBB/includes/cron/tasks/core/queue.php | 2 ++ phpBB/includes/cron/tasks/core/tidy_cache.php | 2 ++ phpBB/includes/cron/tasks/core/tidy_database.php | 2 ++ phpBB/includes/cron/tasks/core/tidy_search.php | 2 ++ phpBB/includes/cron/tasks/core/tidy_sessions.php | 2 ++ phpBB/includes/cron/tasks/core/tidy_warnings.php | 2 ++ 10 files changed, 20 insertions(+) diff --git a/phpBB/includes/cron/cron_manager.php b/phpBB/includes/cron/cron_manager.php index a9101ff872..84b9c9783c 100644 --- a/phpBB/includes/cron/cron_manager.php +++ b/phpBB/includes/cron/cron_manager.php @@ -16,6 +16,8 @@ if (!defined('IN_PHPBB')) exit; } +include_once($phpbb_root_path . 'includes/cron/cron_task_wrapper.' . $phpEx); + /** * Cron manager class. * diff --git a/phpBB/includes/cron/cron_task_base.php b/phpBB/includes/cron/cron_task_base.php index 3561f4e14f..bdd96ec8f5 100644 --- a/phpBB/includes/cron/cron_task_base.php +++ b/phpBB/includes/cron/cron_task_base.php @@ -16,6 +16,8 @@ if (!defined('IN_PHPBB')) exit; } +include_once($phpbb_root_path . 'includes/cron/cron_task.' . $phpEx); + /** * Cron task base class. Provides sensible defaults for cron tasks * and partially implements cron task interface, making writing cron tasks easier. diff --git a/phpBB/includes/cron/tasks/core/prune_all_forums.php b/phpBB/includes/cron/tasks/core/prune_all_forums.php index 13286de2b0..23d8a05d92 100644 --- a/phpBB/includes/cron/tasks/core/prune_all_forums.php +++ b/phpBB/includes/cron/tasks/core/prune_all_forums.php @@ -16,6 +16,8 @@ if (!defined('IN_PHPBB')) exit; } +include_once($phpbb_root_path . 'includes/cron/cron_task_base.' . $phpEx); + /** * Prune all forums cron task. * diff --git a/phpBB/includes/cron/tasks/core/prune_forum.php b/phpBB/includes/cron/tasks/core/prune_forum.php index f9fea7a5b8..93023ce38d 100644 --- a/phpBB/includes/cron/tasks/core/prune_forum.php +++ b/phpBB/includes/cron/tasks/core/prune_forum.php @@ -16,6 +16,8 @@ if (!defined('IN_PHPBB')) exit; } +include_once($phpbb_root_path . 'includes/cron/cron_task_base.' . $phpEx); + /** * Prune one forum cron task. * diff --git a/phpBB/includes/cron/tasks/core/queue.php b/phpBB/includes/cron/tasks/core/queue.php index d7dfeb9319..37a9d1aceb 100644 --- a/phpBB/includes/cron/tasks/core/queue.php +++ b/phpBB/includes/cron/tasks/core/queue.php @@ -16,6 +16,8 @@ if (!defined('IN_PHPBB')) exit; } +include_once($phpbb_root_path . 'includes/cron/cron_task_base.' . $phpEx); + /** * Queue cron task. Sends email and jabber messages queued by other scripts. * diff --git a/phpBB/includes/cron/tasks/core/tidy_cache.php b/phpBB/includes/cron/tasks/core/tidy_cache.php index 69038a8a5a..5be27c3a42 100644 --- a/phpBB/includes/cron/tasks/core/tidy_cache.php +++ b/phpBB/includes/cron/tasks/core/tidy_cache.php @@ -16,6 +16,8 @@ if (!defined('IN_PHPBB')) exit; } +include_once($phpbb_root_path . 'includes/cron/cron_task_base.' . $phpEx); + /** * Tidy cache cron task. * diff --git a/phpBB/includes/cron/tasks/core/tidy_database.php b/phpBB/includes/cron/tasks/core/tidy_database.php index c6c2a60445..715ab2250e 100644 --- a/phpBB/includes/cron/tasks/core/tidy_database.php +++ b/phpBB/includes/cron/tasks/core/tidy_database.php @@ -16,6 +16,8 @@ if (!defined('IN_PHPBB')) exit; } +include_once($phpbb_root_path . 'includes/cron/cron_task_base.' . $phpEx); + /** * Tidy database cron task. * diff --git a/phpBB/includes/cron/tasks/core/tidy_search.php b/phpBB/includes/cron/tasks/core/tidy_search.php index 0f409d114a..42120cdf17 100644 --- a/phpBB/includes/cron/tasks/core/tidy_search.php +++ b/phpBB/includes/cron/tasks/core/tidy_search.php @@ -16,6 +16,8 @@ if (!defined('IN_PHPBB')) exit; } +include_once($phpbb_root_path . 'includes/cron/cron_task_base.' . $phpEx); + /** * Tidy search cron task. * diff --git a/phpBB/includes/cron/tasks/core/tidy_sessions.php b/phpBB/includes/cron/tasks/core/tidy_sessions.php index ea6aa70699..cf02058c62 100644 --- a/phpBB/includes/cron/tasks/core/tidy_sessions.php +++ b/phpBB/includes/cron/tasks/core/tidy_sessions.php @@ -16,6 +16,8 @@ if (!defined('IN_PHPBB')) exit; } +include_once($phpbb_root_path . 'includes/cron/cron_task_base.' . $phpEx); + /** * Tidy sessions cron task. * diff --git a/phpBB/includes/cron/tasks/core/tidy_warnings.php b/phpBB/includes/cron/tasks/core/tidy_warnings.php index c1ab14d788..20a4bcbc60 100644 --- a/phpBB/includes/cron/tasks/core/tidy_warnings.php +++ b/phpBB/includes/cron/tasks/core/tidy_warnings.php @@ -16,6 +16,8 @@ if (!defined('IN_PHPBB')) exit; } +include_once($phpbb_root_path . 'includes/cron/cron_task_base.' . $phpEx); + /** * Tidy warnings cron task. * From ea3b98ab493c12442f7da1b52798294bcb086457 Mon Sep 17 00:00:00 2001 From: Oleg Pudeyev Date: Sun, 18 Apr 2010 13:09:53 -0400 Subject: [PATCH 12/67] [feature/system-cron] Changed include_once to *_exists/include. PHPBB3-9596 --- phpBB/includes/cron/cron_manager.php | 10 ++++++++-- phpBB/includes/cron/cron_task_base.php | 5 ++++- phpBB/includes/cron/cron_task_wrapper.php | 6 ++++++ phpBB/includes/cron/tasks/core/prune_all_forums.php | 5 ++++- phpBB/includes/cron/tasks/core/prune_forum.php | 10 ++++++++-- phpBB/includes/cron/tasks/core/queue.php | 10 ++++++++-- phpBB/includes/cron/tasks/core/tidy_cache.php | 5 ++++- phpBB/includes/cron/tasks/core/tidy_database.php | 10 ++++++++-- phpBB/includes/cron/tasks/core/tidy_search.php | 10 ++++++++-- phpBB/includes/cron/tasks/core/tidy_sessions.php | 5 ++++- phpBB/includes/cron/tasks/core/tidy_warnings.php | 10 ++++++++-- 11 files changed, 70 insertions(+), 16 deletions(-) diff --git a/phpBB/includes/cron/cron_manager.php b/phpBB/includes/cron/cron_manager.php index 84b9c9783c..a99f1369de 100644 --- a/phpBB/includes/cron/cron_manager.php +++ b/phpBB/includes/cron/cron_manager.php @@ -16,7 +16,10 @@ if (!defined('IN_PHPBB')) exit; } -include_once($phpbb_root_path . 'includes/cron/cron_task_wrapper.' . $phpEx); +if (!class_exists('cron_task_wrapper')) +{ + include($phpbb_root_path . 'includes/cron/cron_task_wrapper.' . $phpEx); +} /** * Cron manager class. @@ -100,8 +103,11 @@ class cron_manager list($mod, $filename) = $task_file; if ($this->is_valid_name($mod) && $this->is_valid_name($filename)) { - include_once($phpbb_root_path . "includes/cron/$mod/$filename.$phpEx"); $class = "cron_task_${mod}_${filename}"; + if (!class_exists($class)) + { + include($phpbb_root_path . "includes/cron/$mod/$filename.$phpEx"); + } $object = new $class; $wrapper = new cron_task_wrapper($object); $this->tasks[] = $wrapper; diff --git a/phpBB/includes/cron/cron_task_base.php b/phpBB/includes/cron/cron_task_base.php index bdd96ec8f5..a75e27ac13 100644 --- a/phpBB/includes/cron/cron_task_base.php +++ b/phpBB/includes/cron/cron_task_base.php @@ -16,7 +16,10 @@ if (!defined('IN_PHPBB')) exit; } -include_once($phpbb_root_path . 'includes/cron/cron_task.' . $phpEx); +if (!class_exists('cron_task')) +{ + include_once($phpbb_root_path . 'includes/cron/cron_task.' . $phpEx); +} /** * Cron task base class. Provides sensible defaults for cron tasks diff --git a/phpBB/includes/cron/cron_task_wrapper.php b/phpBB/includes/cron/cron_task_wrapper.php index 0e63000846..00b330464e 100644 --- a/phpBB/includes/cron/cron_task_wrapper.php +++ b/phpBB/includes/cron/cron_task_wrapper.php @@ -16,6 +16,12 @@ if (!defined('IN_PHPBB')) exit; } +// We use parametrized_cron_task in is_parametrized +if (!interface_exists('cron_task')) +{ + include($phpbb_root_path . 'includes/cron/cron_task.' . $phpEx); +} + /** * Cron task wrapper class. * Enhances cron tasks with convenience methods that work identically for all tasks. diff --git a/phpBB/includes/cron/tasks/core/prune_all_forums.php b/phpBB/includes/cron/tasks/core/prune_all_forums.php index 23d8a05d92..ebd09cbade 100644 --- a/phpBB/includes/cron/tasks/core/prune_all_forums.php +++ b/phpBB/includes/cron/tasks/core/prune_all_forums.php @@ -16,7 +16,10 @@ if (!defined('IN_PHPBB')) exit; } -include_once($phpbb_root_path . 'includes/cron/cron_task_base.' . $phpEx); +if (!class_exists('cron_task_base')) +{ + include($phpbb_root_path . 'includes/cron/cron_task_base.' . $phpEx); +} /** * Prune all forums cron task. diff --git a/phpBB/includes/cron/tasks/core/prune_forum.php b/phpBB/includes/cron/tasks/core/prune_forum.php index 93023ce38d..bfc8beb2de 100644 --- a/phpBB/includes/cron/tasks/core/prune_forum.php +++ b/phpBB/includes/cron/tasks/core/prune_forum.php @@ -16,7 +16,10 @@ if (!defined('IN_PHPBB')) exit; } -include_once($phpbb_root_path . 'includes/cron/cron_task_base.' . $phpEx); +if (!class_exists('cron_task_base')) +{ + include($phpbb_root_path . 'includes/cron/cron_task_base.' . $phpEx); +} /** * Prune one forum cron task. @@ -60,7 +63,10 @@ class cron_task_core_prune_forum extends cron_task_base implements parametrized_ public function run() { global $phpbb_root_path, $phpEx; - include_once($phpbb_root_path . 'includes/functions_admin.' . $phpEx); + if (!function_exists('auto_prune')) + { + include($phpbb_root_path . 'includes/functions_admin.' . $phpEx); + } if ($this->forum_data['prune_days']) { diff --git a/phpBB/includes/cron/tasks/core/queue.php b/phpBB/includes/cron/tasks/core/queue.php index 37a9d1aceb..5b6b69c269 100644 --- a/phpBB/includes/cron/tasks/core/queue.php +++ b/phpBB/includes/cron/tasks/core/queue.php @@ -16,7 +16,10 @@ if (!defined('IN_PHPBB')) exit; } -include_once($phpbb_root_path . 'includes/cron/cron_task_base.' . $phpEx); +if (!class_exists('cron_task_base')) +{ + include($phpbb_root_path . 'includes/cron/cron_task_base.' . $phpEx); +} /** * Queue cron task. Sends email and jabber messages queued by other scripts. @@ -31,7 +34,10 @@ class cron_task_core_queue extends cron_task_base public function run() { global $phpbb_root_path, $phpEx; - include_once($phpbb_root_path . 'includes/functions_messenger.' . $phpEx); + if (!class_exists('queue')) + { + include($phpbb_root_path . 'includes/functions_messenger.' . $phpEx); + } $queue = new queue(); $queue->process(); } diff --git a/phpBB/includes/cron/tasks/core/tidy_cache.php b/phpBB/includes/cron/tasks/core/tidy_cache.php index 5be27c3a42..72843ba680 100644 --- a/phpBB/includes/cron/tasks/core/tidy_cache.php +++ b/phpBB/includes/cron/tasks/core/tidy_cache.php @@ -16,7 +16,10 @@ if (!defined('IN_PHPBB')) exit; } -include_once($phpbb_root_path . 'includes/cron/cron_task_base.' . $phpEx); +if (!class_exists('cron_task_base')) +{ + include($phpbb_root_path . 'includes/cron/cron_task_base.' . $phpEx); +} /** * Tidy cache cron task. diff --git a/phpBB/includes/cron/tasks/core/tidy_database.php b/phpBB/includes/cron/tasks/core/tidy_database.php index 715ab2250e..f2de216c3a 100644 --- a/phpBB/includes/cron/tasks/core/tidy_database.php +++ b/phpBB/includes/cron/tasks/core/tidy_database.php @@ -16,7 +16,10 @@ if (!defined('IN_PHPBB')) exit; } -include_once($phpbb_root_path . 'includes/cron/cron_task_base.' . $phpEx); +if (!class_exists('cron_task_base')) +{ + include($phpbb_root_path . 'includes/cron/cron_task_base.' . $phpEx); +} /** * Tidy database cron task. @@ -30,7 +33,10 @@ class cron_task_core_tidy_database extends cron_task_base */ public function run() { - include_once($phpbb_root_path . 'includes/functions_admin.' . $phpEx); + if (!function_exists('tidy_database')) + { + include($phpbb_root_path . 'includes/functions_admin.' . $phpEx); + } tidy_database(); } diff --git a/phpBB/includes/cron/tasks/core/tidy_search.php b/phpBB/includes/cron/tasks/core/tidy_search.php index 42120cdf17..42a46142b1 100644 --- a/phpBB/includes/cron/tasks/core/tidy_search.php +++ b/phpBB/includes/cron/tasks/core/tidy_search.php @@ -16,7 +16,10 @@ if (!defined('IN_PHPBB')) exit; } -include_once($phpbb_root_path . 'includes/cron/cron_task_base.' . $phpEx); +if (!class_exists('cron_task_base')) +{ + include($phpbb_root_path . 'includes/cron/cron_task_base.' . $phpEx); +} /** * Tidy search cron task. @@ -37,7 +40,10 @@ class cron_task_core_tidy_sessions extends cron_task_base // Select the search method $search_type = basename($config['search_type']); - include_once("{$phpbb_root_path}includes/search/$search_type.$phpEx"); + 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; diff --git a/phpBB/includes/cron/tasks/core/tidy_sessions.php b/phpBB/includes/cron/tasks/core/tidy_sessions.php index cf02058c62..ecf0be3978 100644 --- a/phpBB/includes/cron/tasks/core/tidy_sessions.php +++ b/phpBB/includes/cron/tasks/core/tidy_sessions.php @@ -16,7 +16,10 @@ if (!defined('IN_PHPBB')) exit; } -include_once($phpbb_root_path . 'includes/cron/cron_task_base.' . $phpEx); +if (!class_exists('cron_task_base')) +{ + include($phpbb_root_path . 'includes/cron/cron_task_base.' . $phpEx); +} /** * Tidy sessions cron task. diff --git a/phpBB/includes/cron/tasks/core/tidy_warnings.php b/phpBB/includes/cron/tasks/core/tidy_warnings.php index 20a4bcbc60..4d70f3dc3d 100644 --- a/phpBB/includes/cron/tasks/core/tidy_warnings.php +++ b/phpBB/includes/cron/tasks/core/tidy_warnings.php @@ -16,7 +16,10 @@ if (!defined('IN_PHPBB')) exit; } -include_once($phpbb_root_path . 'includes/cron/cron_task_base.' . $phpEx); +if (!class_exists('cron_task_base')) +{ + include($phpbb_root_path . 'includes/cron/cron_task_base.' . $phpEx); +} /** * Tidy warnings cron task. @@ -33,7 +36,10 @@ class cron_task_core_tidy_warnings extends cron_task_base public function run() { global $phpbb_root_path, $phpEx; - include_once($phpbb_root_path . 'includes/functions_admin.' . $phpEx); + if (!function_exists('tidy_warnings')) + { + include($phpbb_root_path . 'includes/functions_admin.' . $phpEx); + } tidy_warnings(); } From 763dc86c166ab850007fea8da4622e4cd1eeafc8 Mon Sep 17 00:00:00 2001 From: Oleg Pudeyev Date: Sun, 18 Apr 2010 13:48:32 -0400 Subject: [PATCH 13/67] [feature/system-cron] Fixes to make cron actually run. PHPBB3-9596 --- phpBB/common.php | 2 +- phpBB/cron.php | 20 ++++++++++++++----- phpBB/includes/cron/cron_manager.php | 13 +++++++----- phpBB/includes/cron/cron_task_base.php | 5 ----- .../cron/tasks/core/tidy_database.php | 1 + .../includes/cron/tasks/core/tidy_search.php | 2 +- 6 files changed, 26 insertions(+), 17 deletions(-) diff --git a/phpBB/common.php b/phpBB/common.php index e2300f0bd5..cbd8399206 100644 --- a/phpBB/common.php +++ b/phpBB/common.php @@ -243,5 +243,5 @@ foreach ($cache->obtain_hooks() as $hook) if (!$config['use_system_cron']) { include($phpbb_root_path . 'includes/cron/cron_manager.' . $phpEx); - $cron = new cron(); + $cron = new cron_manager(); } diff --git a/phpBB/cron.php b/phpBB/cron.php index 0621d7651b..53540b58e2 100644 --- a/phpBB/cron.php +++ b/phpBB/cron.php @@ -48,12 +48,16 @@ function do_cron($run_tasks) garbage_collection(); } +$cron_lock = new cron_lock; if ($cron_lock->lock()) { if ($config['use_system_cron']) { $use_shutdown_function = false; + include($phpbb_root_path . 'includes/cron/cron_manager.' . $phpEx); + $cron = new cron_manager; + $run_tasks = $cron->find_all_ready_tasks(); } else @@ -62,14 +66,20 @@ if ($cron_lock->lock()) $use_shutdown_function = (@function_exists('register_shutdown_function')) ? true : false; output_image(); - + + // If invalid task is specified, empty $run_tasks is passed to do_cron which then does nothing + $run_tasks = array(); $task = $cron->find_task($cron_type); - if ($task) { - if ($task->is_parametrized()) { + if ($task) + { + if ($task->is_parametrized()) + { $task->parse_parameters($_GET); } - if ($task->is_ready()) { - if ($use_shutdown_function && !$task->is_shutdown_function_safe()) { + if ($task->is_ready()) + { + if ($use_shutdown_function && !$task->is_shutdown_function_safe()) + { $use_shutdown_function = false; } $run_tasks = array($task); diff --git a/phpBB/includes/cron/cron_manager.php b/phpBB/includes/cron/cron_manager.php index a99f1369de..5c3bfd01b5 100644 --- a/phpBB/includes/cron/cron_manager.php +++ b/phpBB/includes/cron/cron_manager.php @@ -61,7 +61,7 @@ class cron_manager { // ignore ., .. and dot directories // todo: change is_dir to account for symlinks - if ($entry[0] == '.' || !is_dir($entry)) + if ($entry[0] == '.' || !is_dir("$tasks_root_path/$entry")) { continue; } @@ -76,10 +76,13 @@ class cron_manager { $path = $phpbb_root_path . 'includes/cron/tasks/' . $task_dir; $dir = opendir($path); - while (($entry = readdir($dir)) !== false && substr($entry, -$ext_length) == $ext) + while (($entry = readdir($dir)) !== false) { - $task_file = substr($entry, 0, -$ext_length); - $task_files[] = array($task_dir, $task_file); + if (substr($entry, -$ext_length) == $ext) + { + $task_file = substr($entry, 0, -$ext_length); + $task_files[] = array($task_dir, $task_file); + } } closedir($dir); } @@ -106,7 +109,7 @@ class cron_manager $class = "cron_task_${mod}_${filename}"; if (!class_exists($class)) { - include($phpbb_root_path . "includes/cron/$mod/$filename.$phpEx"); + include($phpbb_root_path . "includes/cron/tasks/$mod/$filename.$phpEx"); } $object = new $class; $wrapper = new cron_task_wrapper($object); diff --git a/phpBB/includes/cron/cron_task_base.php b/phpBB/includes/cron/cron_task_base.php index a75e27ac13..f35ecd4ae0 100644 --- a/phpBB/includes/cron/cron_task_base.php +++ b/phpBB/includes/cron/cron_task_base.php @@ -34,11 +34,6 @@ if (!class_exists('cron_task')) */ abstract class cron_task_base implements cron_task { - /** - * Runs this cron task. - */ - abstract public function run(); - /** * Returns whether this cron task can run, given current board configuration. * diff --git a/phpBB/includes/cron/tasks/core/tidy_database.php b/phpBB/includes/cron/tasks/core/tidy_database.php index f2de216c3a..6724ec092e 100644 --- a/phpBB/includes/cron/tasks/core/tidy_database.php +++ b/phpBB/includes/cron/tasks/core/tidy_database.php @@ -33,6 +33,7 @@ class cron_task_core_tidy_database extends cron_task_base */ public function run() { + global $phpbb_root_path, $phpEx; if (!function_exists('tidy_database')) { include($phpbb_root_path . 'includes/functions_admin.' . $phpEx); diff --git a/phpBB/includes/cron/tasks/core/tidy_search.php b/phpBB/includes/cron/tasks/core/tidy_search.php index 42a46142b1..e821310c9f 100644 --- a/phpBB/includes/cron/tasks/core/tidy_search.php +++ b/phpBB/includes/cron/tasks/core/tidy_search.php @@ -28,7 +28,7 @@ if (!class_exists('cron_task_base')) * * @package phpBB3 */ -class cron_task_core_tidy_sessions extends cron_task_base +class cron_task_core_tidy_search extends cron_task_base { /** * Runs this cron task. From bb7496bac23d5aa70e0f22e1b845b5f971e725b8 Mon Sep 17 00:00:00 2001 From: Oleg Pudeyev Date: Mon, 19 Apr 2010 07:29:26 -0400 Subject: [PATCH 14/67] [feature/system-cron] Changed stray include_once to include. PHPBB3-9596 --- phpBB/includes/cron/cron_task_base.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/phpBB/includes/cron/cron_task_base.php b/phpBB/includes/cron/cron_task_base.php index f35ecd4ae0..2059794cec 100644 --- a/phpBB/includes/cron/cron_task_base.php +++ b/phpBB/includes/cron/cron_task_base.php @@ -18,7 +18,7 @@ if (!defined('IN_PHPBB')) if (!class_exists('cron_task')) { - include_once($phpbb_root_path . 'includes/cron/cron_task.' . $phpEx); + include($phpbb_root_path . 'includes/cron/cron_task.' . $phpEx); } /** From c25736ca256d127dd824aadb0075a3f8acca39c4 Mon Sep 17 00:00:00 2001 From: Oleg Pudeyev Date: Mon, 19 Apr 2010 07:30:50 -0400 Subject: [PATCH 15/67] [feature/system-cron] Changed !! to (bool) as requested. PHPBB3-9596 --- phpBB/includes/cron/tasks/core/prune_all_forums.php | 2 +- phpBB/includes/cron/tasks/core/tidy_warnings.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/phpBB/includes/cron/tasks/core/prune_all_forums.php b/phpBB/includes/cron/tasks/core/prune_all_forums.php index ebd09cbade..6d39fe6a15 100644 --- a/phpBB/includes/cron/tasks/core/prune_all_forums.php +++ b/phpBB/includes/cron/tasks/core/prune_all_forums.php @@ -63,6 +63,6 @@ class cron_task_core_prune_all_forums extends cron_task_base public function is_runnable() { global $config; - return !!$config['use_system_cron']; + return (bool) $config['use_system_cron']; } } diff --git a/phpBB/includes/cron/tasks/core/tidy_warnings.php b/phpBB/includes/cron/tasks/core/tidy_warnings.php index 4d70f3dc3d..6ef6a17fab 100644 --- a/phpBB/includes/cron/tasks/core/tidy_warnings.php +++ b/phpBB/includes/cron/tasks/core/tidy_warnings.php @@ -49,7 +49,7 @@ class cron_task_core_tidy_warnings extends cron_task_base public function is_runnable() { global $config; - return !!$config['warnings_expire_days']; + return (bool) $config['warnings_expire_days']; } /** From ce5204dcfdc4d0f6a688acf6a015a14da4b4383c Mon Sep 17 00:00:00 2001 From: Oleg Pudeyev Date: Mon, 19 Apr 2010 07:38:10 -0400 Subject: [PATCH 16/67] [feature/system-cron] Comment about shutdown function safety in queue cron task. PHPBB3-9596 --- phpBB/includes/cron/tasks/core/queue.php | 1 + 1 file changed, 1 insertion(+) diff --git a/phpBB/includes/cron/tasks/core/queue.php b/phpBB/includes/cron/tasks/core/queue.php index 5b6b69c269..dffaf5aed4 100644 --- a/phpBB/includes/cron/tasks/core/queue.php +++ b/phpBB/includes/cron/tasks/core/queue.php @@ -67,6 +67,7 @@ class cron_task_core_queue extends cron_task_base public function is_shutdown_function_safe() { global $config; + // A user reported using the mail() function while using shutdown does not work. We do not want to risk that. return !$config['smtp_delivery']; } } From f07aeba9c056c7aea72a81d012ba8e793345f130 Mon Sep 17 00:00:00 2001 From: Oleg Pudeyev Date: Sun, 25 Apr 2010 11:07:10 -0400 Subject: [PATCH 17/67] [feature/system-cron] Added acp ui for use_system_cron config setting. PHPBB3-9596 --- phpBB/includes/acp/acp_board.php | 1 + phpBB/language/en/acp/board.php | 2 ++ 2 files changed, 3 insertions(+) diff --git a/phpBB/includes/acp/acp_board.php b/phpBB/includes/acp/acp_board.php index 5dd7673a79..d77fbca7c2 100644 --- a/phpBB/includes/acp/acp_board.php +++ b/phpBB/includes/acp/acp_board.php @@ -351,6 +351,7 @@ class acp_board 'vars' => array( 'legend1' => 'ACP_SERVER_SETTINGS', 'gzip_compress' => array('lang' => 'ENABLE_GZIP', 'validate' => 'bool', 'type' => 'radio:yes_no', 'explain' => true), + 'use_system_cron' => array('lang' => 'USE_SYSTEM_CRON', 'validate' => 'bool', 'type' => 'radio:yes_no', 'explain' => true), 'legend2' => 'PATH_SETTINGS', 'smilies_path' => array('lang' => 'SMILIES_PATH', 'validate' => 'rpath', 'type' => 'text:20:255', 'explain' => true), diff --git a/phpBB/language/en/acp/board.php b/phpBB/language/en/acp/board.php index bdd5f0d2f3..fe023958a9 100644 --- a/phpBB/language/en/acp/board.php +++ b/phpBB/language/en/acp/board.php @@ -433,6 +433,8 @@ $lang = array_merge($lang, array( 'SMILIES_PATH_EXPLAIN' => 'Path under your phpBB root directory, e.g. images/smilies.', 'UPLOAD_ICONS_PATH' => 'Extension group icons storage path', 'UPLOAD_ICONS_PATH_EXPLAIN' => 'Path under your phpBB root directory, e.g. images/upload_icons.', + 'USE_SYSTEM_CRON' => 'Run periodic tasks from system cron', + 'USE_SYSTEM_CRON_EXPLAIN' => 'When off, phpBB will arrange for periodic tasks to be run automatically. When on, phpBB will not schedule any periodic tasks by itself; a system administrator must arrange for cron.php to be invoked by the system cron facility at regular intervals (e.g. every 5 minutes).', )); // Security Settings From 451198b4671b8feb05c644f9172212cdc6b32df5 Mon Sep 17 00:00:00 2001 From: Oleg Pudeyev Date: Sun, 25 Apr 2010 12:51:45 -0400 Subject: [PATCH 18/67] [feature/system-cron] Set use_system_cron config value during upgrade. PHPBB3-9596 --- phpBB/install/database_update.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/phpBB/install/database_update.php b/phpBB/install/database_update.php index 06b37bfcca..c47e9b790a 100644 --- a/phpBB/install/database_update.php +++ b/phpBB/install/database_update.php @@ -1863,6 +1863,10 @@ function change_database_data(&$no_updates, $version) // No changes from 3.0.8-RC1 to 3.0.8 case '3.0.8-RC1': break; + + case '3.0.9-dev': + set_config('use_system_cron', 0); + break; } } From 562bbc885eaa79fb4de05e926d4c657263496c04 Mon Sep 17 00:00:00 2001 From: Oleg Pudeyev Date: Sun, 9 May 2010 13:33:52 -0400 Subject: [PATCH 19/67] [feature/system-cron] Use interface_exists to check for interface existence. PHPBB3-9596 --- phpBB/includes/cron/cron_task_base.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/phpBB/includes/cron/cron_task_base.php b/phpBB/includes/cron/cron_task_base.php index 2059794cec..1edb4aae51 100644 --- a/phpBB/includes/cron/cron_task_base.php +++ b/phpBB/includes/cron/cron_task_base.php @@ -16,7 +16,7 @@ if (!defined('IN_PHPBB')) exit; } -if (!class_exists('cron_task')) +if (!interface_exists('cron_task')) { include($phpbb_root_path . 'includes/cron/cron_task.' . $phpEx); } From d6fcea23a427322e70170f2a90e8efbea52926f6 Mon Sep 17 00:00:00 2001 From: Oleg Pudeyev Date: Sun, 9 May 2010 14:58:26 -0400 Subject: [PATCH 20/67] [feature/system-cron] Note that releasing a released cron lock is harmless. PHPBB3-9596 --- phpBB/includes/cron/cron_lock.php | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/phpBB/includes/cron/cron_lock.php b/phpBB/includes/cron/cron_lock.php index 1046d62da4..1af3724709 100644 --- a/phpBB/includes/cron/cron_lock.php +++ b/phpBB/includes/cron/cron_lock.php @@ -63,6 +63,11 @@ class cron_lock return true; } + /** + * Releases cron lock. + * + * Attempting to release a cron lock that is already released is harmless. + */ function unlock() { global $db; From 5c03040ddd531eabf495b48b626b72447c5d9b75 Mon Sep 17 00:00:00 2001 From: Oleg Pudeyev Date: Sun, 9 May 2010 14:58:45 -0400 Subject: [PATCH 21/67] [feature/system-cron] Try to work around stealth cron locks Try to work around stealth cron locks resulting from fatal errors during cron processing. PHPBB3-9596 --- phpBB/cron.php | 39 +++++++++++++++++++++++++++++---------- 1 file changed, 29 insertions(+), 10 deletions(-) diff --git a/phpBB/cron.php b/phpBB/cron.php index 53540b58e2..92fef57623 100644 --- a/phpBB/cron.php +++ b/phpBB/cron.php @@ -48,25 +48,37 @@ function do_cron($run_tasks) garbage_collection(); } +// Thanks to various fatal errors and lack of try/finally, it is quite easy to leave +// the cron lock locked, especially when working on cron-related code. +// +// Attempt to alleviate the problem by doing setup outside of the lock as much as possible. +// +// If DEBUG_EXTRA is defined and cron lock cannot be obtained, a message will be printed. + +if ($config['use_system_cron']) +{ + $use_shutdown_function = false; + + include($phpbb_root_path . 'includes/cron/cron_manager.' . $phpEx); + $cron = new cron_manager; +} +else +{ + $cron_type = request_var('cron_type', ''); + $use_shutdown_function = (@function_exists('register_shutdown_function')) ? true : false; + + output_image(); +} + $cron_lock = new cron_lock; if ($cron_lock->lock()) { if ($config['use_system_cron']) { - $use_shutdown_function = false; - - include($phpbb_root_path . 'includes/cron/cron_manager.' . $phpEx); - $cron = new cron_manager; - $run_tasks = $cron->find_all_ready_tasks(); } else { - $cron_type = request_var('cron_type', ''); - $use_shutdown_function = (@function_exists('register_shutdown_function')) ? true : false; - - output_image(); - // If invalid task is specified, empty $run_tasks is passed to do_cron which then does nothing $run_tasks = array(); $task = $cron->find_task($cron_type); @@ -95,3 +107,10 @@ if ($cron_lock->lock()) do_cron($run_tasks); } } +else +{ + if (defined('DEBUG_EXTRA')) + { + echo "Could not obtain cron lock."; + } +} From be087826b084815802e45195db4adc72d0b57e17 Mon Sep 17 00:00:00 2001 From: Oleg Pudeyev Date: Sun, 9 May 2010 15:02:39 -0400 Subject: [PATCH 22/67] [feature/system-cron] Added missing include to prune all forums task. PHPBB3-9596 --- phpBB/includes/cron/tasks/core/prune_all_forums.php | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/phpBB/includes/cron/tasks/core/prune_all_forums.php b/phpBB/includes/cron/tasks/core/prune_all_forums.php index 6d39fe6a15..de7ea557b0 100644 --- a/phpBB/includes/cron/tasks/core/prune_all_forums.php +++ b/phpBB/includes/cron/tasks/core/prune_all_forums.php @@ -37,7 +37,13 @@ class cron_task_core_prune_all_forums extends cron_task_base */ public function run() { - global $db; + 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(); From 72699b72fba7b4fb67874353c01c37b7ae4dfea5 Mon Sep 17 00:00:00 2001 From: Oleg Pudeyev Date: Sun, 9 May 2010 16:03:18 -0400 Subject: [PATCH 23/67] [feature/system-cron] Updated prune forum task name in viewforum. PHPBB3-9596 --- phpBB/viewforum.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/phpBB/viewforum.php b/phpBB/viewforum.php index a3fd9fa6fb..c60721e3f1 100644 --- a/phpBB/viewforum.php +++ b/phpBB/viewforum.php @@ -195,7 +195,7 @@ if ($forum_data['forum_topics_per_page']) // Do the forum Prune thang - cron type job ... if (!$config['use_system_cron']) { - $task = $cron->instantiate_task('core_prune_forum', $forum_data); + $task = $cron->instantiate_task('cron_task_core_prune_forum', $forum_data); if ($task && $task->is_ready()) { $url = $task->get_url(); $template->assign_var('RUN_CRON_TASK', 'cron'); From 8cc2819610319a52fd093238377a54084ade0feb Mon Sep 17 00:00:00 2001 From: Oleg Pudeyev Date: Sun, 9 May 2010 16:19:01 -0400 Subject: [PATCH 24/67] [feature/system-cron] Require cron task wrapper constructor to be a cron task. PHPBB3-9596 --- phpBB/includes/cron/cron_task_wrapper.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/phpBB/includes/cron/cron_task_wrapper.php b/phpBB/includes/cron/cron_task_wrapper.php index 00b330464e..c306b56193 100644 --- a/phpBB/includes/cron/cron_task_wrapper.php +++ b/phpBB/includes/cron/cron_task_wrapper.php @@ -33,7 +33,7 @@ class cron_task_wrapper /** * Wraps a task $task, which must implement cron_task interface. */ - public function __construct($task) + public function __construct(cron_task $task) { $this->task = $task; } From 071472a6fdef07c0e1e6d79b419412452c2425a7 Mon Sep 17 00:00:00 2001 From: Oleg Pudeyev Date: Sun, 9 May 2010 16:42:13 -0400 Subject: [PATCH 25/67] [feature/system-cron] Use complete cron task class name as cron task name PHPBB3-9596 --- phpBB/includes/cron/cron_task_wrapper.php | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/phpBB/includes/cron/cron_task_wrapper.php b/phpBB/includes/cron/cron_task_wrapper.php index c306b56193..17f80278a7 100644 --- a/phpBB/includes/cron/cron_task_wrapper.php +++ b/phpBB/includes/cron/cron_task_wrapper.php @@ -61,12 +61,11 @@ class cron_task_wrapper } /** - * Returns the name of wrapped task. + * Returns the name of wrapped task. It is the same as the wrapped class's class name. */ public function get_name() { - $class = get_class($this->task); - return preg_replace('/^cron_task_/', '', $class); + return get_class($this->task); } /** From ccf5902c50ecd8fc5307efbb64a20aa19a2652b4 Mon Sep 17 00:00:00 2001 From: Oleg Pudeyev Date: Sun, 9 May 2010 16:47:52 -0400 Subject: [PATCH 26/67] [feature/system-cron] Fixed instantiate_task to work correctly. PHPBB3-9596 --- phpBB/includes/cron/cron_manager.php | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/phpBB/includes/cron/cron_manager.php b/phpBB/includes/cron/cron_manager.php index 5c3bfd01b5..a97f2bde4e 100644 --- a/phpBB/includes/cron/cron_manager.php +++ b/phpBB/includes/cron/cron_manager.php @@ -168,13 +168,23 @@ class cron_manager 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. + */ function instantiate_task($name, $args) { $task = $this->find_task($name); if ($task) { - $class = get_class($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; } From f345662b214bd1ad18c66fcce057dfb73335657e Mon Sep 17 00:00:00 2001 From: Oleg Pudeyev Date: Sun, 9 May 2010 16:58:53 -0400 Subject: [PATCH 27/67] [feature/system-cron] Use intval() to convert to int. PHPBB3-9596 --- phpBB/includes/cron/tasks/core/prune_forum.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/phpBB/includes/cron/tasks/core/prune_forum.php b/phpBB/includes/cron/tasks/core/prune_forum.php index bfc8beb2de..48c502917e 100644 --- a/phpBB/includes/cron/tasks/core/prune_forum.php +++ b/phpBB/includes/cron/tasks/core/prune_forum.php @@ -121,7 +121,7 @@ class cron_task_core_prune_forum extends cron_task_base implements parametrized_ $this->forum_data = null; if (isset($params['f'])) { - $forum_id = int($params['f']); + $forum_id = intval($params['f']); $sql = 'SELECT forum_id, prune_next, enable_prune, prune_days, prune_viewed, forum_flags, prune_freq FROM ' . FORUMS_TABLE . " From 3e707cf26645f66cdc46a17699e156119d20ef75 Mon Sep 17 00:00:00 2001 From: Oleg Pudeyev Date: Wed, 18 Aug 2010 22:51:49 -0400 Subject: [PATCH 28/67] [feature/system-cron] Replaced intval with (int) cast as requested. PHPBB3-9596 --- phpBB/includes/cron/tasks/core/prune_forum.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/phpBB/includes/cron/tasks/core/prune_forum.php b/phpBB/includes/cron/tasks/core/prune_forum.php index 48c502917e..7728932d7e 100644 --- a/phpBB/includes/cron/tasks/core/prune_forum.php +++ b/phpBB/includes/cron/tasks/core/prune_forum.php @@ -121,7 +121,7 @@ class cron_task_core_prune_forum extends cron_task_base implements parametrized_ $this->forum_data = null; if (isset($params['f'])) { - $forum_id = intval($params['f']); + $forum_id = (int) $params['f']; $sql = 'SELECT forum_id, prune_next, enable_prune, prune_days, prune_viewed, forum_flags, prune_freq FROM ' . FORUMS_TABLE . " From 652372bdb1f32a16e6747ad90965dcd25fec06f9 Mon Sep 17 00:00:00 2001 From: Igor Wiedler Date: Thu, 19 Aug 2010 16:32:21 -0400 Subject: [PATCH 29/67] [feature/system-cron] Add debug information PHPBB3-9596 --- phpBB/cron.php | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/phpBB/cron.php b/phpBB/cron.php index 92fef57623..7c9ab23bdb 100644 --- a/phpBB/cron.php +++ b/phpBB/cron.php @@ -36,10 +36,15 @@ function output_image() function do_cron($run_tasks) { - global $cron_lock; + global $cron_lock, $config; foreach ($run_tasks as $task) { + if (defined('DEBUG_EXTRA') && $config['use_system_cron']) + { + echo "[phpBB cron] Running task '{$task->get_name()}'\n"; + } + $task->run(); } @@ -111,6 +116,6 @@ else { if (defined('DEBUG_EXTRA')) { - echo "Could not obtain cron lock."; + echo "Could not obtain cron lock.\n"; } } From 8c8211d15ae6b3378450637e4a2cfe6732bad110 Mon Sep 17 00:00:00 2001 From: Oleg Pudeyev Date: Sun, 22 Aug 2010 21:49:12 -0400 Subject: [PATCH 30/67] [feature/system-cron] Added missing visibility keywords. PHPBB3-9596 --- phpBB/includes/cron/cron_lock.php | 4 ++-- phpBB/includes/cron/cron_manager.php | 10 +++++----- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/phpBB/includes/cron/cron_lock.php b/phpBB/includes/cron/cron_lock.php index 1af3724709..64b82128ce 100644 --- a/phpBB/includes/cron/cron_lock.php +++ b/phpBB/includes/cron/cron_lock.php @@ -24,7 +24,7 @@ class cron_lock { private $cron_id; - function lock() + public function lock() { global $config, $db; @@ -68,7 +68,7 @@ class cron_lock * * Attempting to release a cron lock that is already released is harmless. */ - function unlock() + public function unlock() { global $db; diff --git a/phpBB/includes/cron/cron_manager.php b/phpBB/includes/cron/cron_manager.php index a97f2bde4e..0c395ce3f0 100644 --- a/phpBB/includes/cron/cron_manager.php +++ b/phpBB/includes/cron/cron_manager.php @@ -123,7 +123,7 @@ class cron_manager * * If several tasks are ready, any one of them could be returned. */ - function find_one_ready_task() + public function find_one_ready_task() { foreach ($this->tasks as $task) { @@ -138,7 +138,7 @@ class cron_manager /** * Finds all tasks that are ready to run. */ - function find_all_ready_tasks() + public function find_all_ready_tasks() { $tasks = array(); foreach ($this->tasks as $task) @@ -156,7 +156,7 @@ class cron_manager * * Web runner uses this method to resolve names to tasks. */ - function find_task($name) + public function find_task($name) { foreach ($this->tasks as $task) { @@ -175,7 +175,7 @@ class cron_manager * $args will be passed to the task class's constructor. * The constructed task is wrapped with cron task wrapper before being returned. */ - function instantiate_task($name, $args) + public function instantiate_task($name, $args) { $task = $this->find_task($name); if ($task) @@ -189,7 +189,7 @@ class cron_manager return $task; } - function generate_generic_task_code($cron_type) + public function generate_generic_task_code($cron_type) { global $phpbb_root_path, $phpEx; return 'cron'; From fb461c57edf764114abe0b3dbae2c8a5cb5df8c5 Mon Sep 17 00:00:00 2001 From: Oleg Pudeyev Date: Sun, 22 Aug 2010 21:49:36 -0400 Subject: [PATCH 31/67] [feature/system-cron] Removed generate_generic_task_code function PHPBB3-9596 --- phpBB/includes/cron/cron_manager.php | 6 ------ 1 file changed, 6 deletions(-) diff --git a/phpBB/includes/cron/cron_manager.php b/phpBB/includes/cron/cron_manager.php index 0c395ce3f0..58333b0b66 100644 --- a/phpBB/includes/cron/cron_manager.php +++ b/phpBB/includes/cron/cron_manager.php @@ -188,10 +188,4 @@ class cron_manager } return $task; } - - public function generate_generic_task_code($cron_type) - { - global $phpbb_root_path, $phpEx; - return 'cron'; - } } From 1c7d29783b9b1a103265cfe62b4a62dbe81fa85a Mon Sep 17 00:00:00 2001 From: Oleg Pudeyev Date: Tue, 7 Sep 2010 10:10:19 -0400 Subject: [PATCH 32/67] [feature/system-cron] Added use_system_cron to schema_data.sql. PHPBB3-9596 --- phpBB/install/schemas/schema_data.sql | 1 + 1 file changed, 1 insertion(+) diff --git a/phpBB/install/schemas/schema_data.sql b/phpBB/install/schemas/schema_data.sql index 355af802ef..32e411123a 100644 --- a/phpBB/install/schemas/schema_data.sql +++ b/phpBB/install/schemas/schema_data.sql @@ -242,6 +242,7 @@ INSERT INTO phpbb_config (config_name, config_value) VALUES ('topics_per_page', INSERT INTO phpbb_config (config_name, config_value) VALUES ('tpl_allow_php', '0'); INSERT INTO phpbb_config (config_name, config_value) VALUES ('upload_icons_path', 'images/upload_icons'); INSERT INTO phpbb_config (config_name, config_value) VALUES ('upload_path', 'files'); +INSERT INTO phpbb_config (config_name, config_value) VALUES ('use_system_cron', '0'); INSERT INTO phpbb_config (config_name, config_value) VALUES ('version', '3.0.9-dev'); INSERT INTO phpbb_config (config_name, config_value) VALUES ('warnings_expire_days', '90'); INSERT INTO phpbb_config (config_name, config_value) VALUES ('warnings_gc', '14400'); From 794d376be4d3587f21bf7149de428a37deaa71c8 Mon Sep 17 00:00:00 2001 From: Igor Wiedler Date: Thu, 28 Oct 2010 21:35:53 +0200 Subject: [PATCH 33/67] [feature/system-cron] rename tasks to task PHPBB3-9596 --- phpBB/includes/cron/{tasks => task}/core/prune_all_forums.php | 0 phpBB/includes/cron/{tasks => task}/core/prune_forum.php | 0 phpBB/includes/cron/{tasks => task}/core/queue.php | 0 phpBB/includes/cron/{tasks => task}/core/tidy_cache.php | 0 phpBB/includes/cron/{tasks => task}/core/tidy_database.php | 0 phpBB/includes/cron/{tasks => task}/core/tidy_search.php | 0 phpBB/includes/cron/{tasks => task}/core/tidy_sessions.php | 0 phpBB/includes/cron/{tasks => task}/core/tidy_warnings.php | 0 8 files changed, 0 insertions(+), 0 deletions(-) rename phpBB/includes/cron/{tasks => task}/core/prune_all_forums.php (100%) rename phpBB/includes/cron/{tasks => task}/core/prune_forum.php (100%) rename phpBB/includes/cron/{tasks => task}/core/queue.php (100%) rename phpBB/includes/cron/{tasks => task}/core/tidy_cache.php (100%) rename phpBB/includes/cron/{tasks => task}/core/tidy_database.php (100%) rename phpBB/includes/cron/{tasks => task}/core/tidy_search.php (100%) rename phpBB/includes/cron/{tasks => task}/core/tidy_sessions.php (100%) rename phpBB/includes/cron/{tasks => task}/core/tidy_warnings.php (100%) diff --git a/phpBB/includes/cron/tasks/core/prune_all_forums.php b/phpBB/includes/cron/task/core/prune_all_forums.php similarity index 100% rename from phpBB/includes/cron/tasks/core/prune_all_forums.php rename to phpBB/includes/cron/task/core/prune_all_forums.php diff --git a/phpBB/includes/cron/tasks/core/prune_forum.php b/phpBB/includes/cron/task/core/prune_forum.php similarity index 100% rename from phpBB/includes/cron/tasks/core/prune_forum.php rename to phpBB/includes/cron/task/core/prune_forum.php diff --git a/phpBB/includes/cron/tasks/core/queue.php b/phpBB/includes/cron/task/core/queue.php similarity index 100% rename from phpBB/includes/cron/tasks/core/queue.php rename to phpBB/includes/cron/task/core/queue.php diff --git a/phpBB/includes/cron/tasks/core/tidy_cache.php b/phpBB/includes/cron/task/core/tidy_cache.php similarity index 100% rename from phpBB/includes/cron/tasks/core/tidy_cache.php rename to phpBB/includes/cron/task/core/tidy_cache.php diff --git a/phpBB/includes/cron/tasks/core/tidy_database.php b/phpBB/includes/cron/task/core/tidy_database.php similarity index 100% rename from phpBB/includes/cron/tasks/core/tidy_database.php rename to phpBB/includes/cron/task/core/tidy_database.php diff --git a/phpBB/includes/cron/tasks/core/tidy_search.php b/phpBB/includes/cron/task/core/tidy_search.php similarity index 100% rename from phpBB/includes/cron/tasks/core/tidy_search.php rename to phpBB/includes/cron/task/core/tidy_search.php diff --git a/phpBB/includes/cron/tasks/core/tidy_sessions.php b/phpBB/includes/cron/task/core/tidy_sessions.php similarity index 100% rename from phpBB/includes/cron/tasks/core/tidy_sessions.php rename to phpBB/includes/cron/task/core/tidy_sessions.php diff --git a/phpBB/includes/cron/tasks/core/tidy_warnings.php b/phpBB/includes/cron/task/core/tidy_warnings.php similarity index 100% rename from phpBB/includes/cron/tasks/core/tidy_warnings.php rename to phpBB/includes/cron/task/core/tidy_warnings.php From 1bfbcb8b7e91f4b36b703eeee7e6eeb99fbca3a0 Mon Sep 17 00:00:00 2001 From: Igor Wiedler Date: Thu, 28 Oct 2010 21:38:50 +0200 Subject: [PATCH 34/67] [feature/system-cron] remove cron_ prefix from files PHPBB3-9596 --- phpBB/includes/cron/{cron_lock.php => lock.php} | 0 phpBB/includes/cron/{cron_manager.php => manager.php} | 0 phpBB/includes/cron/{cron_task.php => task.php} | 0 phpBB/includes/cron/{cron_task_base.php => task_base.php} | 0 phpBB/includes/cron/{cron_task_wrapper.php => task_wrapper.php} | 0 5 files changed, 0 insertions(+), 0 deletions(-) rename phpBB/includes/cron/{cron_lock.php => lock.php} (100%) rename phpBB/includes/cron/{cron_manager.php => manager.php} (100%) rename phpBB/includes/cron/{cron_task.php => task.php} (100%) rename phpBB/includes/cron/{cron_task_base.php => task_base.php} (100%) rename phpBB/includes/cron/{cron_task_wrapper.php => task_wrapper.php} (100%) diff --git a/phpBB/includes/cron/cron_lock.php b/phpBB/includes/cron/lock.php similarity index 100% rename from phpBB/includes/cron/cron_lock.php rename to phpBB/includes/cron/lock.php diff --git a/phpBB/includes/cron/cron_manager.php b/phpBB/includes/cron/manager.php similarity index 100% rename from phpBB/includes/cron/cron_manager.php rename to phpBB/includes/cron/manager.php diff --git a/phpBB/includes/cron/cron_task.php b/phpBB/includes/cron/task.php similarity index 100% rename from phpBB/includes/cron/cron_task.php rename to phpBB/includes/cron/task.php diff --git a/phpBB/includes/cron/cron_task_base.php b/phpBB/includes/cron/task_base.php similarity index 100% rename from phpBB/includes/cron/cron_task_base.php rename to phpBB/includes/cron/task_base.php diff --git a/phpBB/includes/cron/cron_task_wrapper.php b/phpBB/includes/cron/task_wrapper.php similarity index 100% rename from phpBB/includes/cron/cron_task_wrapper.php rename to phpBB/includes/cron/task_wrapper.php From c9bd1b392d098dce1c406f7da7654d5c1b4833a7 Mon Sep 17 00:00:00 2001 From: Igor Wiedler Date: Thu, 28 Oct 2010 21:41:53 +0200 Subject: [PATCH 35/67] [feature/system-cron] remove conditional includes in favour of autoloading PHPBB3-9596 --- phpBB/includes/cron/manager.php | 5 ----- phpBB/includes/cron/task/core/prune_all_forums.php | 5 ----- phpBB/includes/cron/task/core/prune_forum.php | 5 ----- phpBB/includes/cron/task/core/queue.php | 5 ----- phpBB/includes/cron/task/core/tidy_cache.php | 5 ----- phpBB/includes/cron/task/core/tidy_database.php | 5 ----- phpBB/includes/cron/task/core/tidy_search.php | 5 ----- phpBB/includes/cron/task/core/tidy_sessions.php | 5 ----- phpBB/includes/cron/task/core/tidy_warnings.php | 5 ----- phpBB/includes/cron/task_base.php | 5 ----- phpBB/includes/cron/task_wrapper.php | 6 ------ 11 files changed, 56 deletions(-) diff --git a/phpBB/includes/cron/manager.php b/phpBB/includes/cron/manager.php index 58333b0b66..170724dc2e 100644 --- a/phpBB/includes/cron/manager.php +++ b/phpBB/includes/cron/manager.php @@ -16,11 +16,6 @@ if (!defined('IN_PHPBB')) exit; } -if (!class_exists('cron_task_wrapper')) -{ - include($phpbb_root_path . 'includes/cron/cron_task_wrapper.' . $phpEx); -} - /** * Cron manager class. * diff --git a/phpBB/includes/cron/task/core/prune_all_forums.php b/phpBB/includes/cron/task/core/prune_all_forums.php index de7ea557b0..b218aac6c5 100644 --- a/phpBB/includes/cron/task/core/prune_all_forums.php +++ b/phpBB/includes/cron/task/core/prune_all_forums.php @@ -16,11 +16,6 @@ if (!defined('IN_PHPBB')) exit; } -if (!class_exists('cron_task_base')) -{ - include($phpbb_root_path . 'includes/cron/cron_task_base.' . $phpEx); -} - /** * Prune all forums cron task. * diff --git a/phpBB/includes/cron/task/core/prune_forum.php b/phpBB/includes/cron/task/core/prune_forum.php index 7728932d7e..090a90c212 100644 --- a/phpBB/includes/cron/task/core/prune_forum.php +++ b/phpBB/includes/cron/task/core/prune_forum.php @@ -16,11 +16,6 @@ if (!defined('IN_PHPBB')) exit; } -if (!class_exists('cron_task_base')) -{ - include($phpbb_root_path . 'includes/cron/cron_task_base.' . $phpEx); -} - /** * Prune one forum cron task. * diff --git a/phpBB/includes/cron/task/core/queue.php b/phpBB/includes/cron/task/core/queue.php index dffaf5aed4..d32e133973 100644 --- a/phpBB/includes/cron/task/core/queue.php +++ b/phpBB/includes/cron/task/core/queue.php @@ -16,11 +16,6 @@ if (!defined('IN_PHPBB')) exit; } -if (!class_exists('cron_task_base')) -{ - include($phpbb_root_path . 'includes/cron/cron_task_base.' . $phpEx); -} - /** * Queue cron task. Sends email and jabber messages queued by other scripts. * diff --git a/phpBB/includes/cron/task/core/tidy_cache.php b/phpBB/includes/cron/task/core/tidy_cache.php index 72843ba680..69038a8a5a 100644 --- a/phpBB/includes/cron/task/core/tidy_cache.php +++ b/phpBB/includes/cron/task/core/tidy_cache.php @@ -16,11 +16,6 @@ if (!defined('IN_PHPBB')) exit; } -if (!class_exists('cron_task_base')) -{ - include($phpbb_root_path . 'includes/cron/cron_task_base.' . $phpEx); -} - /** * Tidy cache cron task. * diff --git a/phpBB/includes/cron/task/core/tidy_database.php b/phpBB/includes/cron/task/core/tidy_database.php index 6724ec092e..3ae8c42273 100644 --- a/phpBB/includes/cron/task/core/tidy_database.php +++ b/phpBB/includes/cron/task/core/tidy_database.php @@ -16,11 +16,6 @@ if (!defined('IN_PHPBB')) exit; } -if (!class_exists('cron_task_base')) -{ - include($phpbb_root_path . 'includes/cron/cron_task_base.' . $phpEx); -} - /** * Tidy database cron task. * diff --git a/phpBB/includes/cron/task/core/tidy_search.php b/phpBB/includes/cron/task/core/tidy_search.php index e821310c9f..e21d48c13a 100644 --- a/phpBB/includes/cron/task/core/tidy_search.php +++ b/phpBB/includes/cron/task/core/tidy_search.php @@ -16,11 +16,6 @@ if (!defined('IN_PHPBB')) exit; } -if (!class_exists('cron_task_base')) -{ - include($phpbb_root_path . 'includes/cron/cron_task_base.' . $phpEx); -} - /** * Tidy search cron task. * diff --git a/phpBB/includes/cron/task/core/tidy_sessions.php b/phpBB/includes/cron/task/core/tidy_sessions.php index ecf0be3978..ea6aa70699 100644 --- a/phpBB/includes/cron/task/core/tidy_sessions.php +++ b/phpBB/includes/cron/task/core/tidy_sessions.php @@ -16,11 +16,6 @@ if (!defined('IN_PHPBB')) exit; } -if (!class_exists('cron_task_base')) -{ - include($phpbb_root_path . 'includes/cron/cron_task_base.' . $phpEx); -} - /** * Tidy sessions cron task. * diff --git a/phpBB/includes/cron/task/core/tidy_warnings.php b/phpBB/includes/cron/task/core/tidy_warnings.php index 6ef6a17fab..257c116b5e 100644 --- a/phpBB/includes/cron/task/core/tidy_warnings.php +++ b/phpBB/includes/cron/task/core/tidy_warnings.php @@ -16,11 +16,6 @@ if (!defined('IN_PHPBB')) exit; } -if (!class_exists('cron_task_base')) -{ - include($phpbb_root_path . 'includes/cron/cron_task_base.' . $phpEx); -} - /** * Tidy warnings cron task. * diff --git a/phpBB/includes/cron/task_base.php b/phpBB/includes/cron/task_base.php index 1edb4aae51..d867b3abf1 100644 --- a/phpBB/includes/cron/task_base.php +++ b/phpBB/includes/cron/task_base.php @@ -16,11 +16,6 @@ if (!defined('IN_PHPBB')) exit; } -if (!interface_exists('cron_task')) -{ - include($phpbb_root_path . 'includes/cron/cron_task.' . $phpEx); -} - /** * Cron task base class. Provides sensible defaults for cron tasks * and partially implements cron task interface, making writing cron tasks easier. diff --git a/phpBB/includes/cron/task_wrapper.php b/phpBB/includes/cron/task_wrapper.php index 17f80278a7..ecc0d1901d 100644 --- a/phpBB/includes/cron/task_wrapper.php +++ b/phpBB/includes/cron/task_wrapper.php @@ -16,12 +16,6 @@ if (!defined('IN_PHPBB')) exit; } -// We use parametrized_cron_task in is_parametrized -if (!interface_exists('cron_task')) -{ - include($phpbb_root_path . 'includes/cron/cron_task.' . $phpEx); -} - /** * Cron task wrapper class. * Enhances cron tasks with convenience methods that work identically for all tasks. From 5a075c3dca431797fd21420e6bdeb25bbc08bee6 Mon Sep 17 00:00:00 2001 From: Igor Wiedler Date: Thu, 28 Oct 2010 21:48:58 +0200 Subject: [PATCH 36/67] [feature/system-cron] remove more includes, adjust path PHPBB3-9596 --- phpBB/common.php | 1 - phpBB/cron.php | 6 ++---- phpBB/includes/cron/manager.php | 2 +- 3 files changed, 3 insertions(+), 6 deletions(-) diff --git a/phpBB/common.php b/phpBB/common.php index cbd8399206..c4c1d2979a 100644 --- a/phpBB/common.php +++ b/phpBB/common.php @@ -242,6 +242,5 @@ foreach ($cache->obtain_hooks() as $hook) if (!$config['use_system_cron']) { - include($phpbb_root_path . 'includes/cron/cron_manager.' . $phpEx); $cron = new cron_manager(); } diff --git a/phpBB/cron.php b/phpBB/cron.php index 7c9ab23bdb..cad83109dc 100644 --- a/phpBB/cron.php +++ b/phpBB/cron.php @@ -15,7 +15,6 @@ define('IN_CRON', true); $phpbb_root_path = (defined('PHPBB_ROOT_PATH')) ? PHPBB_ROOT_PATH : './'; $phpEx = substr(strrchr(__FILE__, '.'), 1); include($phpbb_root_path . 'common.' . $phpEx); -include($phpbb_root_path . 'includes/cron/cron_lock.' . $phpEx); // Do not update users last page entry $user->session_begin(false); @@ -64,8 +63,7 @@ if ($config['use_system_cron']) { $use_shutdown_function = false; - include($phpbb_root_path . 'includes/cron/cron_manager.' . $phpEx); - $cron = new cron_manager; + $cron = new cron_manager(); } else { @@ -75,7 +73,7 @@ else output_image(); } -$cron_lock = new cron_lock; +$cron_lock = new cron_lock(); if ($cron_lock->lock()) { if ($config['use_system_cron']) diff --git a/phpBB/includes/cron/manager.php b/phpBB/includes/cron/manager.php index 170724dc2e..5eb262c4f2 100644 --- a/phpBB/includes/cron/manager.php +++ b/phpBB/includes/cron/manager.php @@ -49,7 +49,7 @@ class cron_manager { global $phpbb_root_path, $phpEx; - $tasks_root_path = $phpbb_root_path . 'includes/cron/tasks'; + $tasks_root_path = $phpbb_root_path . 'includes/cron/task'; $dir = opendir($tasks_root_path); $task_dirs = array(); while (($entry = readdir($dir)) !== false) From 0aa491ffa20dbd45ed7b1142005041d2ddc975a9 Mon Sep 17 00:00:00 2001 From: Igor Wiedler Date: Thu, 28 Oct 2010 22:15:30 +0200 Subject: [PATCH 37/67] [feature/system-cron] rewrite cron manager to use autoloading PHPBB3-9596 --- phpBB/includes/cron/manager.php | 68 ++++++++++++++------------------- 1 file changed, 28 insertions(+), 40 deletions(-) diff --git a/phpBB/includes/cron/manager.php b/phpBB/includes/cron/manager.php index 5eb262c4f2..899fd5e325 100644 --- a/phpBB/includes/cron/manager.php +++ b/phpBB/includes/cron/manager.php @@ -29,15 +29,15 @@ class cron_manager public function __construct() { - $task_files = $this->find_cron_task_files(); - $this->load_tasks($task_files); + $task_names = $this->find_cron_task_names(); + $this->load_tasks($task_names); } /** * Finds cron task files. * * A cron task file must follow the naming convention: - * includes/cron/tasks/$mod/$name.php. + * includes/cron/task/$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. @@ -45,43 +45,41 @@ class cron_manager * * Todo: consider caching found task file list in global cache. */ - public function find_cron_task_files() + public function find_cron_task_names() { global $phpbb_root_path, $phpEx; - $tasks_root_path = $phpbb_root_path . 'includes/cron/task'; - $dir = opendir($tasks_root_path); - $task_dirs = array(); - while (($entry = readdir($dir)) !== false) + $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) { // ignore ., .. and dot directories // todo: change is_dir to account for symlinks - if ($entry[0] == '.' || !is_dir("$tasks_root_path/$entry")) + if ($mod[0] == '.' || !is_dir($tasks_root_path . $mod)) { 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) + $dh2 = opendir($tasks_root_path . $mod); + while (($file = readdir($dh2)) !== false) { - if (substr($entry, -$ext_length) == $ext) + $task_name = substr($file, 0, -$ext_length); + if (substr($file, -$ext_length) == $ext && $this->is_valid_name($mod) && $this->is_valid_name($task_name)) { - $task_file = substr($entry, 0, -$ext_length); - $task_files[] = array($task_dir, $task_file); + $full_task_name = $mod . '_' . $task_name; + $task_names[] = $full_task_name; } } - closedir($dir); + closedir($dh2); } - return $task_files; + closedir($dh); + + return $task_names; } /** @@ -92,24 +90,14 @@ class cron_manager return preg_match('/^[a-zA-Z][a-zA-Z0-9_]*$/', $name); } - public function load_tasks($task_files) + public function load_tasks($task_names) { - global $phpbb_root_path, $phpEx; - - foreach ($task_files as $task_file) + foreach ($task_names as $task_name) { - 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); + $class = "cron_task_$task_name"; + $task = new $class(); + $wrapper = new cron_task_wrapper($task); $this->tasks[] = $wrapper; - } } } From a9e0f9947d1d71779a6c02dbc4c40f70f6a98723 Mon Sep 17 00:00:00 2001 From: Igor Wiedler Date: Thu, 28 Oct 2010 22:26:49 +0200 Subject: [PATCH 38/67] [feature/system-cron] add phpbb_ prefix to all class names PHPBB3-9596 --- phpBB/common.php | 2 +- phpBB/cron.php | 2 +- phpBB/includes/cron/lock.php | 2 +- phpBB/includes/cron/manager.php | 8 ++++---- phpBB/includes/cron/task.php | 4 ++-- phpBB/includes/cron/task/core/prune_all_forums.php | 2 +- phpBB/includes/cron/task/core/prune_forum.php | 2 +- phpBB/includes/cron/task/core/queue.php | 2 +- phpBB/includes/cron/task/core/tidy_cache.php | 2 +- phpBB/includes/cron/task/core/tidy_database.php | 2 +- phpBB/includes/cron/task/core/tidy_search.php | 2 +- phpBB/includes/cron/task/core/tidy_sessions.php | 2 +- phpBB/includes/cron/task/core/tidy_warnings.php | 2 +- phpBB/includes/cron/task_base.php | 2 +- phpBB/includes/cron/task_wrapper.php | 4 ++-- 15 files changed, 20 insertions(+), 20 deletions(-) diff --git a/phpBB/common.php b/phpBB/common.php index c4c1d2979a..00fc1a5cb7 100644 --- a/phpBB/common.php +++ b/phpBB/common.php @@ -242,5 +242,5 @@ foreach ($cache->obtain_hooks() as $hook) if (!$config['use_system_cron']) { - $cron = new cron_manager(); + $cron = new phpbb_cron_manager(); } diff --git a/phpBB/cron.php b/phpBB/cron.php index cad83109dc..0520fae1b4 100644 --- a/phpBB/cron.php +++ b/phpBB/cron.php @@ -63,7 +63,7 @@ if ($config['use_system_cron']) { $use_shutdown_function = false; - $cron = new cron_manager(); + $cron = new phpbb_cron_manager(); } else { diff --git a/phpBB/includes/cron/lock.php b/phpBB/includes/cron/lock.php index 64b82128ce..1d75079ee4 100644 --- a/phpBB/includes/cron/lock.php +++ b/phpBB/includes/cron/lock.php @@ -20,7 +20,7 @@ if (!defined('IN_PHPBB')) * Cron lock class * @package phpBB3 */ -class cron_lock +class phpbb_cron_lock { private $cron_id; diff --git a/phpBB/includes/cron/manager.php b/phpBB/includes/cron/manager.php index 899fd5e325..73b06afca0 100644 --- a/phpBB/includes/cron/manager.php +++ b/phpBB/includes/cron/manager.php @@ -23,7 +23,7 @@ if (!defined('IN_PHPBB')) * * @package phpBB3 */ -class cron_manager +class phpbb_cron_manager { private $tasks = array(); @@ -94,9 +94,9 @@ class cron_manager { foreach ($task_names as $task_name) { - $class = "cron_task_$task_name"; + $class = "phpbb_cron_task_$task_name"; $task = new $class(); - $wrapper = new cron_task_wrapper($task); + $wrapper = new phpbb_cron_task_wrapper($task); $this->tasks[] = $wrapper; } } @@ -167,7 +167,7 @@ class cron_manager $class = $task->get_name(); $task = new $class($args); // need to wrap the new task too - $task = new cron_task_wrapper($task); + $task = new phpbb_cron_task_wrapper($task); } return $task; } diff --git a/phpBB/includes/cron/task.php b/phpBB/includes/cron/task.php index 46eacff517..e66acd5492 100644 --- a/phpBB/includes/cron/task.php +++ b/phpBB/includes/cron/task.php @@ -20,7 +20,7 @@ if (!defined('IN_PHPBB')) * Cron task interface * @package phpBB3 */ -interface cron_task +interface phpbb_cron_task { /** * Runs this cron task. @@ -58,7 +58,7 @@ interface cron_task * * @package phpBB3 */ -interface parametrized_cron_task extends cron_task +interface phpbb_parametrized_cron_task extends cron_task { /** * Returns parameters of this cron task as an array. diff --git a/phpBB/includes/cron/task/core/prune_all_forums.php b/phpBB/includes/cron/task/core/prune_all_forums.php index b218aac6c5..154409a37c 100644 --- a/phpBB/includes/cron/task/core/prune_all_forums.php +++ b/phpBB/includes/cron/task/core/prune_all_forums.php @@ -25,7 +25,7 @@ if (!defined('IN_PHPBB')) * * @package phpBB3 */ -class cron_task_core_prune_all_forums extends cron_task_base +class phpbb_cron_task_core_prune_all_forums extends phpbb_cron_task_base { /** * Runs this cron task. diff --git a/phpBB/includes/cron/task/core/prune_forum.php b/phpBB/includes/cron/task/core/prune_forum.php index 090a90c212..eb01e535a9 100644 --- a/phpBB/includes/cron/task/core/prune_forum.php +++ b/phpBB/includes/cron/task/core/prune_forum.php @@ -25,7 +25,7 @@ if (!defined('IN_PHPBB')) * * @package phpBB3 */ -class cron_task_core_prune_forum extends cron_task_base implements parametrized_cron_task +class phpbb_cron_task_core_prune_forum extends phpbb_cron_task_base implements phpbb_parametrized_cron_task { private $forum_data; diff --git a/phpBB/includes/cron/task/core/queue.php b/phpBB/includes/cron/task/core/queue.php index d32e133973..271a03937d 100644 --- a/phpBB/includes/cron/task/core/queue.php +++ b/phpBB/includes/cron/task/core/queue.php @@ -21,7 +21,7 @@ if (!defined('IN_PHPBB')) * * @package phpBB3 */ -class cron_task_core_queue extends cron_task_base +class phpbb_cron_task_core_queue extends phpbb_cron_task_base { /** * Runs this cron task. diff --git a/phpBB/includes/cron/task/core/tidy_cache.php b/phpBB/includes/cron/task/core/tidy_cache.php index 69038a8a5a..9656275742 100644 --- a/phpBB/includes/cron/task/core/tidy_cache.php +++ b/phpBB/includes/cron/task/core/tidy_cache.php @@ -21,7 +21,7 @@ if (!defined('IN_PHPBB')) * * @package phpBB3 */ -class cron_task_core_tidy_cache extends cron_task_base +class phpbb_cron_task_core_tidy_cache extends phpbb_cron_task_base { /** * Runs this cron task. diff --git a/phpBB/includes/cron/task/core/tidy_database.php b/phpBB/includes/cron/task/core/tidy_database.php index 3ae8c42273..a2a0b84a68 100644 --- a/phpBB/includes/cron/task/core/tidy_database.php +++ b/phpBB/includes/cron/task/core/tidy_database.php @@ -21,7 +21,7 @@ if (!defined('IN_PHPBB')) * * @package phpBB3 */ -class cron_task_core_tidy_database extends cron_task_base +class phpbb_cron_task_core_tidy_database extends phpbb_cron_task_base { /** * Runs this cron task. diff --git a/phpBB/includes/cron/task/core/tidy_search.php b/phpBB/includes/cron/task/core/tidy_search.php index e21d48c13a..dffc44174c 100644 --- a/phpBB/includes/cron/task/core/tidy_search.php +++ b/phpBB/includes/cron/task/core/tidy_search.php @@ -23,7 +23,7 @@ if (!defined('IN_PHPBB')) * * @package phpBB3 */ -class cron_task_core_tidy_search extends cron_task_base +class phpbb_cron_task_core_tidy_search extends phpbb_cron_task_base { /** * Runs this cron task. diff --git a/phpBB/includes/cron/task/core/tidy_sessions.php b/phpBB/includes/cron/task/core/tidy_sessions.php index ea6aa70699..1d471f9333 100644 --- a/phpBB/includes/cron/task/core/tidy_sessions.php +++ b/phpBB/includes/cron/task/core/tidy_sessions.php @@ -21,7 +21,7 @@ if (!defined('IN_PHPBB')) * * @package phpBB3 */ -class cron_task_core_tidy_sessions extends cron_task_base +class phpbb_cron_task_core_tidy_sessions extends phpbb_cron_task_base { /** * Runs this cron task. diff --git a/phpBB/includes/cron/task/core/tidy_warnings.php b/phpBB/includes/cron/task/core/tidy_warnings.php index 257c116b5e..5120369178 100644 --- a/phpBB/includes/cron/task/core/tidy_warnings.php +++ b/phpBB/includes/cron/task/core/tidy_warnings.php @@ -23,7 +23,7 @@ if (!defined('IN_PHPBB')) * * @package phpBB3 */ -class cron_task_core_tidy_warnings extends cron_task_base +class phpbb_cron_task_core_tidy_warnings extends phpbb_cron_task_base { /** * Runs this cron task. diff --git a/phpBB/includes/cron/task_base.php b/phpBB/includes/cron/task_base.php index d867b3abf1..ba399c18b1 100644 --- a/phpBB/includes/cron/task_base.php +++ b/phpBB/includes/cron/task_base.php @@ -27,7 +27,7 @@ if (!defined('IN_PHPBB')) * * @package phpBB3 */ -abstract class cron_task_base implements cron_task +abstract class phpbb_cron_task_base implements phpbb_cron_task { /** * Returns whether this cron task can run, given current board configuration. diff --git a/phpBB/includes/cron/task_wrapper.php b/phpBB/includes/cron/task_wrapper.php index ecc0d1901d..b3662a4e31 100644 --- a/phpBB/includes/cron/task_wrapper.php +++ b/phpBB/includes/cron/task_wrapper.php @@ -22,7 +22,7 @@ if (!defined('IN_PHPBB')) * * @package phpBB3 */ -class cron_task_wrapper +class phpbb_cron_task_wrapper { /** * Wraps a task $task, which must implement cron_task interface. @@ -40,7 +40,7 @@ class cron_task_wrapper */ public function is_parametrized() { - return $this->task instanceof parametrized_cron_task; + return $this->task instanceof phpbb_parametrized_cron_task; } /** From bd58fa49c000e99d049cbd96306315e1bd35b938 Mon Sep 17 00:00:00 2001 From: Igor Wiedler Date: Thu, 28 Oct 2010 22:30:44 +0200 Subject: [PATCH 39/67] [feature/system-cron] make parameterized interface autoloadable also extract it to a separate file PHPBB3-9596 --- phpBB/includes/cron/task.php | 32 ------------ phpBB/includes/cron/task/core/prune_forum.php | 2 +- phpBB/includes/cron/task_parametrized.php | 49 +++++++++++++++++++ phpBB/includes/cron/task_wrapper.php | 2 +- 4 files changed, 51 insertions(+), 34 deletions(-) create mode 100644 phpBB/includes/cron/task_parametrized.php diff --git a/phpBB/includes/cron/task.php b/phpBB/includes/cron/task.php index e66acd5492..38fb2a6cd1 100644 --- a/phpBB/includes/cron/task.php +++ b/phpBB/includes/cron/task.php @@ -46,35 +46,3 @@ interface phpbb_cron_task */ public function is_shutdown_function_safe(); } - -/** -* 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_parametrized_cron_task extends cron_task -{ - /** - * Returns parameters of this cron task as an array. - * - * The array must map string keys to string values. - */ - public function get_parameters(); - - /** - * Parses parameters found in $params, which is an array. - * - * $params contains user input and must not be trusted. - * In normal operation $params contains the same data that was returned by - * get_parameters method. However, a malicious user can supply arbitrary - * data in $params. - * Cron task must validate all keys and values in $params before using them. - */ - public function parse_parameters($params); -} diff --git a/phpBB/includes/cron/task/core/prune_forum.php b/phpBB/includes/cron/task/core/prune_forum.php index eb01e535a9..5efcde4102 100644 --- a/phpBB/includes/cron/task/core/prune_forum.php +++ b/phpBB/includes/cron/task/core/prune_forum.php @@ -25,7 +25,7 @@ if (!defined('IN_PHPBB')) * * @package phpBB3 */ -class phpbb_cron_task_core_prune_forum extends phpbb_cron_task_base implements phpbb_parametrized_cron_task +class phpbb_cron_task_core_prune_forum extends phpbb_cron_task_base implements phpbb_cron_task_parametrized { private $forum_data; diff --git a/phpBB/includes/cron/task_parametrized.php b/phpBB/includes/cron/task_parametrized.php new file mode 100644 index 0000000000..1906009ebe --- /dev/null +++ b/phpBB/includes/cron/task_parametrized.php @@ -0,0 +1,49 @@ +task instanceof phpbb_parametrized_cron_task; + return $this->task instanceof phpbb_cron_task_parametrized; } /** From 132d2c2bd85bac6ed87ea3c57de27a9675192f29 Mon Sep 17 00:00:00 2001 From: Igor Wiedler Date: Fri, 29 Oct 2010 11:40:18 +0200 Subject: [PATCH 40/67] [feature/system-cron] adjust some last filenames to make autoloading work PHPBB3-9596 --- phpBB/includes/cron/{task_base.php => task/base.php} | 0 .../cron/{task_parametrized.php => task/parametrized.php} | 2 +- phpBB/includes/cron/{ => task}/task.php | 0 phpBB/includes/cron/{task_wrapper.php => task/wrapper.php} | 2 +- 4 files changed, 2 insertions(+), 2 deletions(-) rename phpBB/includes/cron/{task_base.php => task/base.php} (100%) rename phpBB/includes/cron/{task_parametrized.php => task/parametrized.php} (94%) rename phpBB/includes/cron/{ => task}/task.php (100%) rename phpBB/includes/cron/{task_wrapper.php => task/wrapper.php} (97%) diff --git a/phpBB/includes/cron/task_base.php b/phpBB/includes/cron/task/base.php similarity index 100% rename from phpBB/includes/cron/task_base.php rename to phpBB/includes/cron/task/base.php diff --git a/phpBB/includes/cron/task_parametrized.php b/phpBB/includes/cron/task/parametrized.php similarity index 94% rename from phpBB/includes/cron/task_parametrized.php rename to phpBB/includes/cron/task/parametrized.php index 1906009ebe..d505cc3328 100644 --- a/phpBB/includes/cron/task_parametrized.php +++ b/phpBB/includes/cron/task/parametrized.php @@ -27,7 +27,7 @@ if (!defined('IN_PHPBB')) * * @package phpBB3 */ -interface phpbb_cron_task_parametrized extends cron_task +interface phpbb_cron_task_parametrized extends phpbb_cron_task { /** * Returns parameters of this cron task as an array. diff --git a/phpBB/includes/cron/task.php b/phpBB/includes/cron/task/task.php similarity index 100% rename from phpBB/includes/cron/task.php rename to phpBB/includes/cron/task/task.php diff --git a/phpBB/includes/cron/task_wrapper.php b/phpBB/includes/cron/task/wrapper.php similarity index 97% rename from phpBB/includes/cron/task_wrapper.php rename to phpBB/includes/cron/task/wrapper.php index 159382a8d1..e31f467cc8 100644 --- a/phpBB/includes/cron/task_wrapper.php +++ b/phpBB/includes/cron/task/wrapper.php @@ -27,7 +27,7 @@ class phpbb_cron_task_wrapper /** * Wraps a task $task, which must implement cron_task interface. */ - public function __construct(cron_task $task) + public function __construct(phpbb_cron_task $task) { $this->task = $task; } From 4f86b4d205589a11c5c4403c215db522dd8fbac4 Mon Sep 17 00:00:00 2001 From: Igor Wiedler Date: Fri, 29 Oct 2010 11:42:47 +0200 Subject: [PATCH 41/67] [feature/system-cron] add spaces to comply with coding guidelines PHPBB3-9596 --- phpBB/includes/cron/task/core/prune_forum.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/phpBB/includes/cron/task/core/prune_forum.php b/phpBB/includes/cron/task/core/prune_forum.php index 5efcde4102..440dc5e358 100644 --- a/phpBB/includes/cron/task/core/prune_forum.php +++ b/phpBB/includes/cron/task/core/prune_forum.php @@ -39,7 +39,7 @@ class phpbb_cron_task_core_prune_forum extends phpbb_cron_task_base implements p * and a database query will be performed to load the necessary information * about the forum. */ - public function __construct($forum_data=null) + public function __construct($forum_data = null) { global $db; if ($forum_data) From b50e4865675e86ad4f00b5e864b55a327a777e21 Mon Sep 17 00:00:00 2001 From: Igor Wiedler Date: Fri, 29 Oct 2010 11:50:39 +0200 Subject: [PATCH 42/67] [feature/system-cron] adjust some comments to reflect phpbb_ prefix PHPBB3-9596 --- phpBB/includes/cron/manager.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/phpBB/includes/cron/manager.php b/phpBB/includes/cron/manager.php index 73b06afca0..b1eacf70fd 100644 --- a/phpBB/includes/cron/manager.php +++ b/phpBB/includes/cron/manager.php @@ -34,14 +34,14 @@ class phpbb_cron_manager } /** - * Finds cron task files. + * Finds cron task names. * * A cron task file must follow the naming convention: * includes/cron/task/$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}. + * Cron task is expected to be a class named phpbb_cron_task_${mod}_${name}. * * Todo: consider caching found task file list in global cache. */ From 5a95340ee89515aaddd0dd3d3b5cfc2e666d33b2 Mon Sep 17 00:00:00 2001 From: Igor Wiedler Date: Fri, 29 Oct 2010 13:28:16 +0200 Subject: [PATCH 43/67] [feature/system-cron] fix remaining missing phpbb_ prefix in cron.php PHPBB3-9596 --- phpBB/cron.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/phpBB/cron.php b/phpBB/cron.php index 0520fae1b4..2363f5b10b 100644 --- a/phpBB/cron.php +++ b/phpBB/cron.php @@ -73,7 +73,7 @@ else output_image(); } -$cron_lock = new cron_lock(); +$cron_lock = new phpbb_cron_lock(); if ($cron_lock->lock()) { if ($config['use_system_cron']) From e48b850ab5dfed646e91d87f0dd9c560e7fd76cf Mon Sep 17 00:00:00 2001 From: Igor Wiedler Date: Fri, 29 Oct 2010 13:29:00 +0200 Subject: [PATCH 44/67] [feature/system-cron] use phpbb_request instead of plain superglobals PHPBB3-9596 --- phpBB/cron.php | 2 +- phpBB/includes/cron/task/core/prune_forum.php | 13 ++++++------- phpBB/includes/cron/task/parametrized.php | 12 +++++------- 3 files changed, 12 insertions(+), 15 deletions(-) diff --git a/phpBB/cron.php b/phpBB/cron.php index 2363f5b10b..d1b96b12e1 100644 --- a/phpBB/cron.php +++ b/phpBB/cron.php @@ -89,7 +89,7 @@ if ($cron_lock->lock()) { if ($task->is_parametrized()) { - $task->parse_parameters($_GET); + $task->parse_parameters($request); } if ($task->is_ready()) { diff --git a/phpBB/includes/cron/task/core/prune_forum.php b/phpBB/includes/cron/task/core/prune_forum.php index 440dc5e358..b3f63c9f6c 100644 --- a/phpBB/includes/cron/task/core/prune_forum.php +++ b/phpBB/includes/cron/task/core/prune_forum.php @@ -103,20 +103,19 @@ class phpbb_cron_task_core_prune_forum extends phpbb_cron_task_base implements p } /** - * Parses parameters found in $params, which is an array. + * Parses parameters found in $request, which is an instance of + * phpbb_request_interface. * - * $params may contain user input and is not trusted. - * - * $params is expected to have a key f whose value is id of the forum to be pruned. + * It is expected to have a key f whose value is id of the forum to be pruned. */ - public function parse_parameters($params) + public function parse_parameters(phpbb_request_interface $request) { global $db; $this->forum_data = null; - if (isset($params['f'])) + if ($request->is_set('f')) { - $forum_id = (int) $params['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 . " diff --git a/phpBB/includes/cron/task/parametrized.php b/phpBB/includes/cron/task/parametrized.php index d505cc3328..a9481250e1 100644 --- a/phpBB/includes/cron/task/parametrized.php +++ b/phpBB/includes/cron/task/parametrized.php @@ -37,13 +37,11 @@ interface phpbb_cron_task_parametrized extends phpbb_cron_task public function get_parameters(); /** - * Parses parameters found in $params, which is an array. + * Parses parameters found in $request, which is an instance of + * phpbb_request_interface. * - * $params contains user input and must not be trusted. - * In normal operation $params contains the same data that was returned by - * get_parameters method. However, a malicious user can supply arbitrary - * data in $params. - * Cron task must validate all keys and values in $params before using them. + * $request contains user input and must not be trusted. + * Cron task must validate all data before using it. */ - public function parse_parameters($params); + public function parse_parameters(phpbb_request_interface $request); } \ No newline at end of file From 4302dab5fa964039d53d14d4a80e7bcd8f5e2db5 Mon Sep 17 00:00:00 2001 From: Igor Wiedler Date: Fri, 29 Oct 2010 13:36:11 +0200 Subject: [PATCH 45/67] [feature/system-cron] coding guidelines adjustments PHPBB3-9596 --- phpBB/includes/cron/task/core/prune_all_forums.php | 2 +- phpBB/viewforum.php | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/phpBB/includes/cron/task/core/prune_all_forums.php b/phpBB/includes/cron/task/core/prune_all_forums.php index 154409a37c..7159ba6ae3 100644 --- a/phpBB/includes/cron/task/core/prune_all_forums.php +++ b/phpBB/includes/cron/task/core/prune_all_forums.php @@ -33,7 +33,7 @@ class phpbb_cron_task_core_prune_all_forums extends phpbb_cron_task_base public function run() { global $phpbb_root_path, $phpEx, $db; - + if (!function_exists('auto_prune')) { include($phpbb_root_path . 'includes/functions_admin.' . $phpEx); diff --git a/phpBB/viewforum.php b/phpBB/viewforum.php index c60721e3f1..2672703042 100644 --- a/phpBB/viewforum.php +++ b/phpBB/viewforum.php @@ -196,7 +196,8 @@ if ($forum_data['forum_topics_per_page']) if (!$config['use_system_cron']) { $task = $cron->instantiate_task('cron_task_core_prune_forum', $forum_data); - if ($task && $task->is_ready()) { + if ($task && $task->is_ready()) + { $url = $task->get_url(); $template->assign_var('RUN_CRON_TASK', 'cron'); } From afc538366bc9660f9b7035e48034d1b7dccbd978 Mon Sep 17 00:00:00 2001 From: Andreas Fischer Date: Tue, 14 Dec 2010 13:20:54 +0100 Subject: [PATCH 46/67] [feature/system-cron] Remove $Id$ from PHP file header PHPBB3-9596 --- phpBB/includes/cron/lock.php | 1 - phpBB/includes/cron/manager.php | 1 - phpBB/includes/cron/task/base.php | 1 - phpBB/includes/cron/task/core/prune_all_forums.php | 1 - phpBB/includes/cron/task/core/prune_forum.php | 1 - phpBB/includes/cron/task/core/queue.php | 1 - phpBB/includes/cron/task/core/tidy_cache.php | 1 - phpBB/includes/cron/task/core/tidy_database.php | 1 - phpBB/includes/cron/task/core/tidy_search.php | 1 - phpBB/includes/cron/task/core/tidy_sessions.php | 1 - phpBB/includes/cron/task/core/tidy_warnings.php | 1 - phpBB/includes/cron/task/parametrized.php | 1 - phpBB/includes/cron/task/task.php | 1 - phpBB/includes/cron/task/wrapper.php | 1 - 14 files changed, 14 deletions(-) diff --git a/phpBB/includes/cron/lock.php b/phpBB/includes/cron/lock.php index 1d75079ee4..c7b9e3b169 100644 --- a/phpBB/includes/cron/lock.php +++ b/phpBB/includes/cron/lock.php @@ -2,7 +2,6 @@ /** * * @package phpBB3 -* @version $Id$ * @copyright (c) 2010 phpBB Group * @license http://opensource.org/licenses/gpl-license.php GNU Public License * diff --git a/phpBB/includes/cron/manager.php b/phpBB/includes/cron/manager.php index b1eacf70fd..f6f1ab025c 100644 --- a/phpBB/includes/cron/manager.php +++ b/phpBB/includes/cron/manager.php @@ -2,7 +2,6 @@ /** * * @package phpBB3 -* @version $Id$ * @copyright (c) 2010 phpBB Group * @license http://opensource.org/licenses/gpl-license.php GNU Public License * diff --git a/phpBB/includes/cron/task/base.php b/phpBB/includes/cron/task/base.php index ba399c18b1..dff98175fb 100644 --- a/phpBB/includes/cron/task/base.php +++ b/phpBB/includes/cron/task/base.php @@ -2,7 +2,6 @@ /** * * @package phpBB3 -* @version $Id$ * @copyright (c) 2010 phpBB Group * @license http://opensource.org/licenses/gpl-license.php GNU Public License * diff --git a/phpBB/includes/cron/task/core/prune_all_forums.php b/phpBB/includes/cron/task/core/prune_all_forums.php index 7159ba6ae3..44b04611d5 100644 --- a/phpBB/includes/cron/task/core/prune_all_forums.php +++ b/phpBB/includes/cron/task/core/prune_all_forums.php @@ -2,7 +2,6 @@ /** * * @package phpBB3 -* @version $Id$ * @copyright (c) 2010 phpBB Group * @license http://opensource.org/licenses/gpl-license.php GNU Public License * diff --git a/phpBB/includes/cron/task/core/prune_forum.php b/phpBB/includes/cron/task/core/prune_forum.php index b3f63c9f6c..6393281d32 100644 --- a/phpBB/includes/cron/task/core/prune_forum.php +++ b/phpBB/includes/cron/task/core/prune_forum.php @@ -2,7 +2,6 @@ /** * * @package phpBB3 -* @version $Id$ * @copyright (c) 2010 phpBB Group * @license http://opensource.org/licenses/gpl-license.php GNU Public License * diff --git a/phpBB/includes/cron/task/core/queue.php b/phpBB/includes/cron/task/core/queue.php index 271a03937d..852a8d97f4 100644 --- a/phpBB/includes/cron/task/core/queue.php +++ b/phpBB/includes/cron/task/core/queue.php @@ -2,7 +2,6 @@ /** * * @package phpBB3 -* @version $Id$ * @copyright (c) 2010 phpBB Group * @license http://opensource.org/licenses/gpl-license.php GNU Public License * diff --git a/phpBB/includes/cron/task/core/tidy_cache.php b/phpBB/includes/cron/task/core/tidy_cache.php index 9656275742..8a91f68a48 100644 --- a/phpBB/includes/cron/task/core/tidy_cache.php +++ b/phpBB/includes/cron/task/core/tidy_cache.php @@ -2,7 +2,6 @@ /** * * @package phpBB3 -* @version $Id$ * @copyright (c) 2010 phpBB Group * @license http://opensource.org/licenses/gpl-license.php GNU Public License * diff --git a/phpBB/includes/cron/task/core/tidy_database.php b/phpBB/includes/cron/task/core/tidy_database.php index a2a0b84a68..847b882c7d 100644 --- a/phpBB/includes/cron/task/core/tidy_database.php +++ b/phpBB/includes/cron/task/core/tidy_database.php @@ -2,7 +2,6 @@ /** * * @package phpBB3 -* @version $Id$ * @copyright (c) 2010 phpBB Group * @license http://opensource.org/licenses/gpl-license.php GNU Public License * diff --git a/phpBB/includes/cron/task/core/tidy_search.php b/phpBB/includes/cron/task/core/tidy_search.php index dffc44174c..735dc14b68 100644 --- a/phpBB/includes/cron/task/core/tidy_search.php +++ b/phpBB/includes/cron/task/core/tidy_search.php @@ -2,7 +2,6 @@ /** * * @package phpBB3 -* @version $Id$ * @copyright (c) 2010 phpBB Group * @license http://opensource.org/licenses/gpl-license.php GNU Public License * diff --git a/phpBB/includes/cron/task/core/tidy_sessions.php b/phpBB/includes/cron/task/core/tidy_sessions.php index 1d471f9333..dc398fa5e4 100644 --- a/phpBB/includes/cron/task/core/tidy_sessions.php +++ b/phpBB/includes/cron/task/core/tidy_sessions.php @@ -2,7 +2,6 @@ /** * * @package phpBB3 -* @version $Id$ * @copyright (c) 2010 phpBB Group * @license http://opensource.org/licenses/gpl-license.php GNU Public License * diff --git a/phpBB/includes/cron/task/core/tidy_warnings.php b/phpBB/includes/cron/task/core/tidy_warnings.php index 5120369178..c3826dc687 100644 --- a/phpBB/includes/cron/task/core/tidy_warnings.php +++ b/phpBB/includes/cron/task/core/tidy_warnings.php @@ -2,7 +2,6 @@ /** * * @package phpBB3 -* @version $Id$ * @copyright (c) 2010 phpBB Group * @license http://opensource.org/licenses/gpl-license.php GNU Public License * diff --git a/phpBB/includes/cron/task/parametrized.php b/phpBB/includes/cron/task/parametrized.php index a9481250e1..b839904642 100644 --- a/phpBB/includes/cron/task/parametrized.php +++ b/phpBB/includes/cron/task/parametrized.php @@ -2,7 +2,6 @@ /** * * @package phpBB3 -* @version $Id$ * @copyright (c) 2010 phpBB Group * @license http://opensource.org/licenses/gpl-license.php GNU Public License * diff --git a/phpBB/includes/cron/task/task.php b/phpBB/includes/cron/task/task.php index 38fb2a6cd1..050e85c35e 100644 --- a/phpBB/includes/cron/task/task.php +++ b/phpBB/includes/cron/task/task.php @@ -2,7 +2,6 @@ /** * * @package phpBB3 -* @version $Id$ * @copyright (c) 2010 phpBB Group * @license http://opensource.org/licenses/gpl-license.php GNU Public License * diff --git a/phpBB/includes/cron/task/wrapper.php b/phpBB/includes/cron/task/wrapper.php index e31f467cc8..c2e7e42830 100644 --- a/phpBB/includes/cron/task/wrapper.php +++ b/phpBB/includes/cron/task/wrapper.php @@ -2,7 +2,6 @@ /** * * @package phpBB3 -* @version $Id$ * @copyright (c) 2010 phpBB Group * @license http://opensource.org/licenses/gpl-license.php GNU Public License * From 6235957aa13bbb58d59c38d739331e9982c671d6 Mon Sep 17 00:00:00 2001 From: Andreas Fischer Date: Tue, 14 Dec 2010 13:26:42 +0100 Subject: [PATCH 47/67] [feature/system-cron] Fix indention in phpbb_cron_manager::load_tasks() PHPBB3-9596 --- phpBB/includes/cron/manager.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/phpBB/includes/cron/manager.php b/phpBB/includes/cron/manager.php index f6f1ab025c..8c5df8cecc 100644 --- a/phpBB/includes/cron/manager.php +++ b/phpBB/includes/cron/manager.php @@ -93,10 +93,10 @@ class phpbb_cron_manager { foreach ($task_names as $task_name) { - $class = "phpbb_cron_task_$task_name"; - $task = new $class(); - $wrapper = new phpbb_cron_task_wrapper($task); - $this->tasks[] = $wrapper; + $class = "phpbb_cron_task_$task_name"; + $task = new $class(); + $wrapper = new phpbb_cron_task_wrapper($task); + $this->tasks[] = $wrapper; } } From 6fc11184e7d2fd2a46d915738ae7f686c8547433 Mon Sep 17 00:00:00 2001 From: Andreas Fischer Date: Thu, 16 Dec 2010 02:59:38 +0100 Subject: [PATCH 48/67] [feature/system-cron] Adjust SQL query style to follow coding guidelines. PHPBB3-9596 --- phpBB/includes/cron/lock.php | 6 ++++-- phpBB/includes/cron/task/core/prune_all_forums.php | 3 ++- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/phpBB/includes/cron/lock.php b/phpBB/includes/cron/lock.php index c7b9e3b169..b407191922 100644 --- a/phpBB/includes/cron/lock.php +++ b/phpBB/includes/cron/lock.php @@ -50,7 +50,8 @@ class phpbb_cron_lock $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']) . "'"; + 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 @@ -73,7 +74,8 @@ class phpbb_cron_lock $sql = 'UPDATE ' . CONFIG_TABLE . " SET config_value = '0' - WHERE config_name = 'cron_lock' AND config_value = '" . $db->sql_escape($this->cron_id) . "'"; + WHERE config_name = 'cron_lock' + AND config_value = '" . $db->sql_escape($this->cron_id) . "'"; $db->sql_query($sql); } } diff --git a/phpBB/includes/cron/task/core/prune_all_forums.php b/phpBB/includes/cron/task/core/prune_all_forums.php index 44b04611d5..2e6f2444d3 100644 --- a/phpBB/includes/cron/task/core/prune_all_forums.php +++ b/phpBB/includes/cron/task/core/prune_all_forums.php @@ -40,7 +40,8 @@ class phpbb_cron_task_core_prune_all_forums extends phpbb_cron_task_base $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(); + WHERE enable_prune = 1 + AND prune_next < " . time(); $result = $db->sql_query($sql); while ($row = $db->sql_fetchrow($result)) { From 134afe36e2c9734ccc6ed6be5a1896c41dc52227 Mon Sep 17 00:00:00 2001 From: Andreas Fischer Date: Thu, 16 Dec 2010 03:14:41 +0100 Subject: [PATCH 49/67] [feature/system-cron] Add phpDoc documentation for phpbb_cron_lock class. PHPBB3-9596 --- phpBB/includes/cron/lock.php | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/phpBB/includes/cron/lock.php b/phpBB/includes/cron/lock.php index b407191922..b2ba89ad6c 100644 --- a/phpBB/includes/cron/lock.php +++ b/phpBB/includes/cron/lock.php @@ -21,8 +21,20 @@ if (!defined('IN_PHPBB')) */ class phpbb_cron_lock { + /** + * Unique identifier for this lock. + * + * @var string + */ private $cron_id; + /** + * Tries to acquire the cron lock by updating + * the 'cron_lock' configuration variable in the database. + * + * @return bool true if lock was acquired + * false otherwise + */ public function lock() { global $config, $db; @@ -67,6 +79,8 @@ class phpbb_cron_lock * Releases cron lock. * * Attempting to release a cron lock that is already released is harmless. + * + * @return void */ public function unlock() { From f4f8523ca337b790503760e847a197f6343e1dbc Mon Sep 17 00:00:00 2001 From: Andreas Fischer Date: Fri, 17 Dec 2010 01:15:57 +0100 Subject: [PATCH 50/67] [feature/system-cron] Add phpDoc documentation for phpbb_cron_manager class. PHPBB3-9596 --- phpBB/includes/cron/manager.php | 44 ++++++++++++++++++++++++++++----- 1 file changed, 38 insertions(+), 6 deletions(-) diff --git a/phpBB/includes/cron/manager.php b/phpBB/includes/cron/manager.php index 8c5df8cecc..f8d795a426 100644 --- a/phpBB/includes/cron/manager.php +++ b/phpBB/includes/cron/manager.php @@ -24,8 +24,19 @@ if (!defined('IN_PHPBB')) */ class phpbb_cron_manager { + /** + * Set of phpbb_cron_task_wrapper objects. + * Array holding all tasks that have been found. + * + * @var array + */ private $tasks = array(); + /** + * Constructor. Loads all available tasks. + * + * @return void + */ public function __construct() { $task_names = $this->find_cron_task_names(); @@ -43,6 +54,8 @@ class phpbb_cron_manager * Cron task is expected to be a class named phpbb_cron_task_${mod}_${name}. * * Todo: consider caching found task file list in global cache. + * + * @return array Array of strings */ public function find_cron_task_names() { @@ -82,13 +95,26 @@ class phpbb_cron_manager } /** - * Checks whether $name is a valid identifier, and therefore part of valid cron task class name. + * Checks whether $name is a valid identifier, and + * therefore part of valid cron task class name. + * + * @param string $name Name to check + * + * @return int */ public function is_valid_name($name) { return preg_match('/^[a-zA-Z][a-zA-Z0-9_]*$/', $name); } + /** + * Loads tasks given by name, wraps them + * and puts them into $this->tasks. + * + * @param array $task_names Array of strings + * + * @return void + */ public function load_tasks($task_names) { foreach ($task_names as $task_name) @@ -102,8 +128,9 @@ class phpbb_cron_manager /** * Finds a task that is ready to run. - * * If several tasks are ready, any one of them could be returned. + * + * @return phpbb_cron_task_wrapper|null */ public function find_one_ready_task() { @@ -119,6 +146,8 @@ class phpbb_cron_manager /** * Finds all tasks that are ready to run. + * + * @return array Array of phpbb_cron_task_wrapper */ public function find_all_ready_tasks() { @@ -135,8 +164,9 @@ class phpbb_cron_manager /** * Finds a task by name. - * * Web runner uses this method to resolve names to tasks. + * + * @return array|null Array of phpbb_cron_task_wrapper */ public function find_task($name) { @@ -152,10 +182,12 @@ class phpbb_cron_manager /** * 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. + * + * @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, $args) { From 3c0561b68fc3bcff387d46d255ca1accc80df88a Mon Sep 17 00:00:00 2001 From: Andreas Fischer Date: Fri, 17 Dec 2010 01:20:23 +0100 Subject: [PATCH 51/67] [feature/system-cron] Cast result in cron_manager::is_valid_name() to bool. PHPBB3-9596 --- phpBB/includes/cron/manager.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/phpBB/includes/cron/manager.php b/phpBB/includes/cron/manager.php index f8d795a426..1cbf4c1f72 100644 --- a/phpBB/includes/cron/manager.php +++ b/phpBB/includes/cron/manager.php @@ -100,11 +100,11 @@ class phpbb_cron_manager * * @param string $name Name to check * - * @return int + * @return bool */ public function is_valid_name($name) { - return preg_match('/^[a-zA-Z][a-zA-Z0-9_]*$/', $name); + return (bool) preg_match('/^[a-zA-Z][a-zA-Z0-9_]*$/', $name); } /** From 47702b8ca72aeaa2b33dd13f2aa762ae7271c10a Mon Sep 17 00:00:00 2001 From: Andreas Fischer Date: Fri, 17 Dec 2010 01:24:27 +0100 Subject: [PATCH 52/67] [feature/system-cron] Add phpDoc documentation for everything else. PHPBB3-9596 --- phpBB/includes/cron/task/base.php | 6 ++++++ phpBB/includes/cron/task/core/prune_all_forums.php | 4 ++++ phpBB/includes/cron/task/core/prune_forum.php | 13 ++++++++++++- phpBB/includes/cron/task/core/queue.php | 8 ++++++++ phpBB/includes/cron/task/core/tidy_cache.php | 6 ++++++ phpBB/includes/cron/task/core/tidy_database.php | 4 ++++ phpBB/includes/cron/task/core/tidy_search.php | 6 ++++++ phpBB/includes/cron/task/core/tidy_sessions.php | 4 ++++ phpBB/includes/cron/task/core/tidy_warnings.php | 6 ++++++ phpBB/includes/cron/task/parametrized.php | 4 ++++ phpBB/includes/cron/task/task.php | 8 ++++++++ phpBB/includes/cron/task/wrapper.php | 12 ++++++++++++ 12 files changed, 80 insertions(+), 1 deletion(-) diff --git a/phpBB/includes/cron/task/base.php b/phpBB/includes/cron/task/base.php index dff98175fb..a4e89f137f 100644 --- a/phpBB/includes/cron/task/base.php +++ b/phpBB/includes/cron/task/base.php @@ -33,6 +33,8 @@ abstract class phpbb_cron_task_base implements phpbb_cron_task * * For example, a cron task that prunes forums can only run when * forum pruning is enabled. + * + * @return bool */ public function is_runnable() { @@ -42,6 +44,8 @@ abstract class phpbb_cron_task_base implements phpbb_cron_task /** * Returns whether this cron task should run now, because enough time * has passed since it was last run. + * + * @return bool */ public function should_run() { @@ -50,6 +54,8 @@ abstract class phpbb_cron_task_base implements phpbb_cron_task /** * Returns whether this cron task can be run in shutdown function. + * + * @return bool */ public function is_shutdown_function_safe() { diff --git a/phpBB/includes/cron/task/core/prune_all_forums.php b/phpBB/includes/cron/task/core/prune_all_forums.php index 2e6f2444d3..69ae7f63cd 100644 --- a/phpBB/includes/cron/task/core/prune_all_forums.php +++ b/phpBB/includes/cron/task/core/prune_all_forums.php @@ -28,6 +28,8 @@ class phpbb_cron_task_core_prune_all_forums extends phpbb_cron_task_base { /** * Runs this cron task. + * + * @return void */ public function run() { @@ -60,6 +62,8 @@ class phpbb_cron_task_core_prune_all_forums extends phpbb_cron_task_base /** * Returns whether this cron task can run, given current board configuration. + * + * @return bool */ public function is_runnable() { diff --git a/phpBB/includes/cron/task/core/prune_forum.php b/phpBB/includes/cron/task/core/prune_forum.php index 6393281d32..18db44cf2d 100644 --- a/phpBB/includes/cron/task/core/prune_forum.php +++ b/phpBB/includes/cron/task/core/prune_forum.php @@ -37,6 +37,8 @@ class phpbb_cron_task_core_prune_forum extends phpbb_cron_task_base implements p * 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. + * + * @return void */ public function __construct($forum_data = null) { @@ -53,6 +55,8 @@ class phpbb_cron_task_core_prune_forum extends phpbb_cron_task_base implements p /** * Runs this cron task. + * + * @return void */ public function run() { @@ -75,6 +79,8 @@ class phpbb_cron_task_core_prune_forum extends phpbb_cron_task_base implements p /** * Returns whether this cron task can run, given current board configuration. + * + * @return bool */ public function is_runnable() { @@ -85,6 +91,8 @@ class phpbb_cron_task_core_prune_forum extends phpbb_cron_task_base implements p /** * Returns whether this cron task should run now, because enough time * has passed since it was last run. + * + * @return bool */ public function should_run() { @@ -93,8 +101,9 @@ class phpbb_cron_task_core_prune_forum extends phpbb_cron_task_base implements p /** * 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() { @@ -106,6 +115,8 @@ class phpbb_cron_task_core_prune_forum extends phpbb_cron_task_base implements p * phpbb_request_interface. * * It is expected to have a key f whose value is id of the forum to be pruned. + * + * @return void */ public function parse_parameters(phpbb_request_interface $request) { diff --git a/phpBB/includes/cron/task/core/queue.php b/phpBB/includes/cron/task/core/queue.php index 852a8d97f4..ccea4b85bd 100644 --- a/phpBB/includes/cron/task/core/queue.php +++ b/phpBB/includes/cron/task/core/queue.php @@ -24,6 +24,8 @@ class phpbb_cron_task_core_queue extends phpbb_cron_task_base { /** * Runs this cron task. + * + * @return void */ public function run() { @@ -38,6 +40,8 @@ class phpbb_cron_task_core_queue extends phpbb_cron_task_base /** * Returns whether this cron task can run, given current board configuration. + * + * @return bool */ public function is_runnable() { @@ -48,6 +52,8 @@ class phpbb_cron_task_core_queue extends phpbb_cron_task_base /** * Returns whether this cron task should run now, because enough time * has passed since it was last run. + * + * @return bool */ public function should_run() { @@ -57,6 +63,8 @@ class phpbb_cron_task_core_queue extends phpbb_cron_task_base /** * Returns whether this cron task can be run in shutdown function. + * + * @return bool */ public function is_shutdown_function_safe() { diff --git a/phpBB/includes/cron/task/core/tidy_cache.php b/phpBB/includes/cron/task/core/tidy_cache.php index 8a91f68a48..83a60d8760 100644 --- a/phpBB/includes/cron/task/core/tidy_cache.php +++ b/phpBB/includes/cron/task/core/tidy_cache.php @@ -24,6 +24,8 @@ class phpbb_cron_task_core_tidy_cache extends phpbb_cron_task_base { /** * Runs this cron task. + * + * @return void */ public function run() { @@ -33,6 +35,8 @@ class phpbb_cron_task_core_tidy_cache extends phpbb_cron_task_base /** * Returns whether this cron task can run, given current board configuration. + * + * @return bool */ public function is_runnable() { @@ -43,6 +47,8 @@ class phpbb_cron_task_core_tidy_cache extends phpbb_cron_task_base /** * 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/core/tidy_database.php b/phpBB/includes/cron/task/core/tidy_database.php index 847b882c7d..82a0a4583e 100644 --- a/phpBB/includes/cron/task/core/tidy_database.php +++ b/phpBB/includes/cron/task/core/tidy_database.php @@ -24,6 +24,8 @@ class phpbb_cron_task_core_tidy_database extends phpbb_cron_task_base { /** * Runs this cron task. + * + * @return void */ public function run() { @@ -38,6 +40,8 @@ class phpbb_cron_task_core_tidy_database extends phpbb_cron_task_base /** * 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/core/tidy_search.php b/phpBB/includes/cron/task/core/tidy_search.php index 735dc14b68..a781005960 100644 --- a/phpBB/includes/cron/task/core/tidy_search.php +++ b/phpBB/includes/cron/task/core/tidy_search.php @@ -26,6 +26,8 @@ class phpbb_cron_task_core_tidy_search extends phpbb_cron_task_base { /** * Runs this cron task. + * + * @return void */ public function run() { @@ -51,6 +53,8 @@ class phpbb_cron_task_core_tidy_search extends phpbb_cron_task_base /** * Returns whether this cron task can run, given current board configuration. + * + * @return bool */ public function is_runnable() { @@ -65,6 +69,8 @@ class phpbb_cron_task_core_tidy_search extends phpbb_cron_task_base /** * 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/core/tidy_sessions.php b/phpBB/includes/cron/task/core/tidy_sessions.php index dc398fa5e4..5826584691 100644 --- a/phpBB/includes/cron/task/core/tidy_sessions.php +++ b/phpBB/includes/cron/task/core/tidy_sessions.php @@ -24,6 +24,8 @@ class phpbb_cron_task_core_tidy_sessions extends phpbb_cron_task_base { /** * Runs this cron task. + * + * @return void */ public function run() { @@ -34,6 +36,8 @@ class phpbb_cron_task_core_tidy_sessions extends phpbb_cron_task_base /** * 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/core/tidy_warnings.php b/phpBB/includes/cron/task/core/tidy_warnings.php index c3826dc687..3b0cf57f0c 100644 --- a/phpBB/includes/cron/task/core/tidy_warnings.php +++ b/phpBB/includes/cron/task/core/tidy_warnings.php @@ -26,6 +26,8 @@ class phpbb_cron_task_core_tidy_warnings extends phpbb_cron_task_base { /** * Runs this cron task. + * + * @return void */ public function run() { @@ -39,6 +41,8 @@ class phpbb_cron_task_core_tidy_warnings extends phpbb_cron_task_base /** * Returns whether this cron task can run, given current board configuration. + * + * @return bool */ public function is_runnable() { @@ -49,6 +53,8 @@ class phpbb_cron_task_core_tidy_warnings extends phpbb_cron_task_base /** * 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/parametrized.php b/phpBB/includes/cron/task/parametrized.php index b839904642..6f87e1c818 100644 --- a/phpBB/includes/cron/task/parametrized.php +++ b/phpBB/includes/cron/task/parametrized.php @@ -32,6 +32,8 @@ 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(); @@ -41,6 +43,8 @@ interface phpbb_cron_task_parametrized extends phpbb_cron_task * * $request contains user input and must not be trusted. * Cron task must validate all data before using it. + * + * @return void */ public function parse_parameters(phpbb_request_interface $request); } \ No newline at end of file diff --git a/phpBB/includes/cron/task/task.php b/phpBB/includes/cron/task/task.php index 050e85c35e..72fda91dd0 100644 --- a/phpBB/includes/cron/task/task.php +++ b/phpBB/includes/cron/task/task.php @@ -23,6 +23,8 @@ interface phpbb_cron_task { /** * Runs this cron task. + * + * @return void */ public function run(); @@ -31,17 +33,23 @@ interface phpbb_cron_task * * 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(); /** * Returns whether this cron task can be run in shutdown function. + * + * @return bool */ public function is_shutdown_function_safe(); } diff --git a/phpBB/includes/cron/task/wrapper.php b/phpBB/includes/cron/task/wrapper.php index c2e7e42830..23cd4de724 100644 --- a/phpBB/includes/cron/task/wrapper.php +++ b/phpBB/includes/cron/task/wrapper.php @@ -25,6 +25,8 @@ class phpbb_cron_task_wrapper { /** * Wraps a task $task, which must implement cron_task interface. + * + * @return void */ public function __construct(phpbb_cron_task $task) { @@ -36,6 +38,8 @@ class phpbb_cron_task_wrapper * * 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() { @@ -47,6 +51,8 @@ class phpbb_cron_task_wrapper * * 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() { @@ -55,6 +61,8 @@ class phpbb_cron_task_wrapper /** * 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() { @@ -63,6 +71,8 @@ class phpbb_cron_task_wrapper /** * Returns a url through which this task may be invoked via web. + * + * @return string URL */ public function get_url() { @@ -88,6 +98,8 @@ class phpbb_cron_task_wrapper /** * Forwards all other method calls to the wrapped task implementation. + * + * @return mixed */ public function __call($name, $args) { From 53dd847dd582930c84518d77a805468543e32ad0 Mon Sep 17 00:00:00 2001 From: Oleg Pudeyev Date: Sun, 19 Dec 2010 23:56:43 +0100 Subject: [PATCH 53/67] [feature/system-cron] Added @param/@return documentation Also adjusted some function descriptions for greater informativity. PHPBB3-9596 --- phpBB/includes/cron/lock.php | 13 +++++++++++-- phpBB/includes/cron/manager.php | 13 ++++++++++--- phpBB/includes/cron/task/base.php | 11 ++++++++++- phpBB/includes/cron/task/core/prune_all_forums.php | 2 ++ phpBB/includes/cron/task/core/prune_forum.php | 12 +++++++++++- phpBB/includes/cron/task/core/queue.php | 9 +++++++++ phpBB/includes/cron/task/core/tidy_cache.php | 6 ++++++ phpBB/includes/cron/task/core/tidy_database.php | 3 +++ phpBB/includes/cron/task/core/tidy_search.php | 7 +++++++ phpBB/includes/cron/task/core/tidy_sessions.php | 3 +++ phpBB/includes/cron/task/core/tidy_warnings.php | 5 +++++ phpBB/includes/cron/task/parametrized.php | 4 +++- phpBB/includes/cron/task/task.php | 9 +++++++++ phpBB/includes/cron/task/wrapper.php | 12 +++++++++--- 14 files changed, 98 insertions(+), 11 deletions(-) diff --git a/phpBB/includes/cron/lock.php b/phpBB/includes/cron/lock.php index b2ba89ad6c..27b8b425c1 100644 --- a/phpBB/includes/cron/lock.php +++ b/phpBB/includes/cron/lock.php @@ -32,6 +32,11 @@ class phpbb_cron_lock * Tries to acquire the cron lock by updating * the 'cron_lock' configuration variable in the database. * + * As a lock may only be held by one process at a time, lock + * acquisition may fail if another process is holding the lock + * or if another process obtained the lock but never released it. + * Locks are forcibly released after a timeout of 1 hour. + * * @return bool true if lock was acquired * false otherwise */ @@ -76,9 +81,13 @@ class phpbb_cron_lock } /** - * Releases cron lock. + * Releases the cron lock. * - * Attempting to release a cron lock that is already released is harmless. + * The lock must have been previously obtained, that is, lock() call + * was issued and returned true. + * + * Note: Attempting to release a cron lock that is already released, + * that is, calling unlock() multiple times, is harmless. * * @return void */ diff --git a/phpBB/includes/cron/manager.php b/phpBB/includes/cron/manager.php index 1cbf4c1f72..4c7f59092f 100644 --- a/phpBB/includes/cron/manager.php +++ b/phpBB/includes/cron/manager.php @@ -55,7 +55,7 @@ class phpbb_cron_manager * * Todo: consider caching found task file list in global cache. * - * @return array Array of strings + * @return array List of task names */ public function find_cron_task_names() { @@ -128,8 +128,11 @@ class phpbb_cron_manager /** * 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() @@ -147,7 +150,7 @@ class phpbb_cron_manager /** * Finds all tasks that are ready to run. * - * @return array Array of phpbb_cron_task_wrapper + * @return array List of tasks which are ready to run (wrapped in phpbb_cron_task_wrapper). */ public function find_all_ready_tasks() { @@ -164,9 +167,13 @@ class phpbb_cron_manager /** * 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. * - * @return array|null Array of phpbb_cron_task_wrapper + * @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) { diff --git a/phpBB/includes/cron/task/base.php b/phpBB/includes/cron/task/base.php index a4e89f137f..38c0b844d9 100644 --- a/phpBB/includes/cron/task/base.php +++ b/phpBB/includes/cron/task/base.php @@ -55,7 +55,16 @@ abstract class phpbb_cron_task_base implements phpbb_cron_task /** * Returns whether this cron task can be run in shutdown function. * - * @return bool + * By the time shutdown sequence invokes a particular piece of code, + * resources that that code requires may already be released. + * If so, a particular cron task may be marked shutdown function- + * unsafe, and it will be executed in normal program flow. + * + * Generally speaking cron tasks should start off as shutdown function- + * safe, and only be marked shutdown function-unsafe if a problem + * is discovered. + * + * @return bool Whether the cron task is shutdown function-safe. */ public function is_shutdown_function_safe() { diff --git a/phpBB/includes/cron/task/core/prune_all_forums.php b/phpBB/includes/cron/task/core/prune_all_forums.php index 69ae7f63cd..39b5765229 100644 --- a/phpBB/includes/cron/task/core/prune_all_forums.php +++ b/phpBB/includes/cron/task/core/prune_all_forums.php @@ -63,6 +63,8 @@ class phpbb_cron_task_core_prune_all_forums extends phpbb_cron_task_base /** * 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() diff --git a/phpBB/includes/cron/task/core/prune_forum.php b/phpBB/includes/cron/task/core/prune_forum.php index 18db44cf2d..55b1c58cd4 100644 --- a/phpBB/includes/cron/task/core/prune_forum.php +++ b/phpBB/includes/cron/task/core/prune_forum.php @@ -38,7 +38,7 @@ class phpbb_cron_task_core_prune_forum extends phpbb_cron_task_base implements p * and a database query will be performed to load the necessary information * about the forum. * - * @return void + * @param array $forum_data Information about a forum to be pruned. */ public function __construct($forum_data = null) { @@ -80,6 +80,12 @@ class phpbb_cron_task_core_prune_forum extends phpbb_cron_task_base implements p /** * 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() @@ -92,6 +98,8 @@ class phpbb_cron_task_core_prune_forum extends phpbb_cron_task_base implements p * 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() @@ -116,6 +124,8 @@ class phpbb_cron_task_core_prune_forum extends phpbb_cron_task_base implements p * * 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) diff --git a/phpBB/includes/cron/task/core/queue.php b/phpBB/includes/cron/task/core/queue.php index ccea4b85bd..0e9de05984 100644 --- a/phpBB/includes/cron/task/core/queue.php +++ b/phpBB/includes/cron/task/core/queue.php @@ -41,6 +41,8 @@ class phpbb_cron_task_core_queue extends phpbb_cron_task_base /** * 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() @@ -53,6 +55,8 @@ class phpbb_cron_task_core_queue extends phpbb_cron_task_base * 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() @@ -64,6 +68,11 @@ class phpbb_cron_task_core_queue extends phpbb_cron_task_base /** * Returns whether this cron task can be run in shutdown function. * + * A user reported that using the mail() function during shutdown + * function execution does not work. Therefore if email is delivered + * via the mail() function (as opposed to SMTP) queue cron task marks + * itself shutdown function-unsafe. + * * @return bool */ public function is_shutdown_function_safe() diff --git a/phpBB/includes/cron/task/core/tidy_cache.php b/phpBB/includes/cron/task/core/tidy_cache.php index 83a60d8760..793ce746b4 100644 --- a/phpBB/includes/cron/task/core/tidy_cache.php +++ b/phpBB/includes/cron/task/core/tidy_cache.php @@ -36,6 +36,9 @@ class phpbb_cron_task_core_tidy_cache extends phpbb_cron_task_base /** * 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() @@ -48,6 +51,9 @@ class phpbb_cron_task_core_tidy_cache extends phpbb_cron_task_base * 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() diff --git a/phpBB/includes/cron/task/core/tidy_database.php b/phpBB/includes/cron/task/core/tidy_database.php index 82a0a4583e..fb0e81eaba 100644 --- a/phpBB/includes/cron/task/core/tidy_database.php +++ b/phpBB/includes/cron/task/core/tidy_database.php @@ -41,6 +41,9 @@ class phpbb_cron_task_core_tidy_database extends phpbb_cron_task_base * 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() diff --git a/phpBB/includes/cron/task/core/tidy_search.php b/phpBB/includes/cron/task/core/tidy_search.php index a781005960..dcc78abbb8 100644 --- a/phpBB/includes/cron/task/core/tidy_search.php +++ b/phpBB/includes/cron/task/core/tidy_search.php @@ -54,6 +54,10 @@ class phpbb_cron_task_core_tidy_search extends phpbb_cron_task_base /** * 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() @@ -70,6 +74,9 @@ class phpbb_cron_task_core_tidy_search extends phpbb_cron_task_base * 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() diff --git a/phpBB/includes/cron/task/core/tidy_sessions.php b/phpBB/includes/cron/task/core/tidy_sessions.php index 5826584691..81e7e6a147 100644 --- a/phpBB/includes/cron/task/core/tidy_sessions.php +++ b/phpBB/includes/cron/task/core/tidy_sessions.php @@ -37,6 +37,9 @@ class phpbb_cron_task_core_tidy_sessions extends phpbb_cron_task_base * 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() diff --git a/phpBB/includes/cron/task/core/tidy_warnings.php b/phpBB/includes/cron/task/core/tidy_warnings.php index 3b0cf57f0c..e7d4cc9eea 100644 --- a/phpBB/includes/cron/task/core/tidy_warnings.php +++ b/phpBB/includes/cron/task/core/tidy_warnings.php @@ -42,6 +42,8 @@ class phpbb_cron_task_core_tidy_warnings extends phpbb_cron_task_base /** * 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() @@ -54,6 +56,9 @@ class phpbb_cron_task_core_tidy_warnings extends phpbb_cron_task_base * 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() diff --git a/phpBB/includes/cron/task/parametrized.php b/phpBB/includes/cron/task/parametrized.php index 6f87e1c818..c6c45be0c0 100644 --- a/phpBB/includes/cron/task/parametrized.php +++ b/phpBB/includes/cron/task/parametrized.php @@ -44,7 +44,9 @@ interface phpbb_cron_task_parametrized extends phpbb_cron_task * $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); -} \ No newline at end of file +} diff --git a/phpBB/includes/cron/task/task.php b/phpBB/includes/cron/task/task.php index 72fda91dd0..58c4a96f8e 100644 --- a/phpBB/includes/cron/task/task.php +++ b/phpBB/includes/cron/task/task.php @@ -49,6 +49,15 @@ interface phpbb_cron_task /** * Returns whether this cron task can be run in shutdown function. * + * By the time shutdown sequence invokes a particular piece of code, + * resources that that code requires may already be released. + * If so, a particular cron task may be marked shutdown function- + * unsafe, and it will be executed in normal program flow. + * + * Generally speaking cron tasks should start off as shutdown function- + * safe, and only be marked shutdown function-unsafe if a problem + * is discovered. + * * @return bool */ public function is_shutdown_function_safe(); diff --git a/phpBB/includes/cron/task/wrapper.php b/phpBB/includes/cron/task/wrapper.php index 23cd4de724..238d97853c 100644 --- a/phpBB/includes/cron/task/wrapper.php +++ b/phpBB/includes/cron/task/wrapper.php @@ -24,9 +24,11 @@ if (!defined('IN_PHPBB')) class phpbb_cron_task_wrapper { /** + * Constructor. + * * Wraps a task $task, which must implement cron_task interface. * - * @return void + * @param phpbb_cron_task $task The cron task to wrap. */ public function __construct(phpbb_cron_task $task) { @@ -34,7 +36,7 @@ class phpbb_cron_task_wrapper } /** - * Returns whether this task is parametrized. + * Returns whether the wrapped task is parametrised. * * Parametrized tasks accept parameters during initialization and must * normally be scheduled with parameters. @@ -72,7 +74,11 @@ class phpbb_cron_task_wrapper /** * Returns a url through which this task may be invoked via web. * - * @return string URL + * 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() { From 4fbf3fbd40ad7f6137526c4f36e8a932015a0c81 Mon Sep 17 00:00:00 2001 From: Oleg Pudeyev Date: Sun, 19 Dec 2010 22:41:15 -0500 Subject: [PATCH 54/67] [feature/system-cron] Basic tests for cron manager. PHPBB3-9596 --- tests/cron/all_tests.php | 41 ++++++++++++++++++++++++++ tests/cron/manager_test.php | 57 +++++++++++++++++++++++++++++++++++++ 2 files changed, 98 insertions(+) create mode 100644 tests/cron/all_tests.php create mode 100644 tests/cron/manager_test.php diff --git a/tests/cron/all_tests.php b/tests/cron/all_tests.php new file mode 100644 index 0000000000..1fa5af7f5e --- /dev/null +++ b/tests/cron/all_tests.php @@ -0,0 +1,41 @@ +addTestSuite('phpbb_cron_manager_test'); + + return $suite; + } +} + +if (PHPUnit_MAIN_METHOD == 'phpbb_cron_all_tests::main') +{ + phpbb_cron_all_tests::main(); +} + diff --git a/tests/cron/manager_test.php b/tests/cron/manager_test.php new file mode 100644 index 0000000000..4d3fa1d120 --- /dev/null +++ b/tests/cron/manager_test.php @@ -0,0 +1,57 @@ +register(); + +require_once '../phpBB/includes/cron/manager.php'; + +class phpbb_cron_manager_test extends PHPUnit_Framework_TestCase +{ + public function setUp() + { + $this->manager = new phpbb_cron_manager(); + } + + public function test_manager_finds_shipped_tasks() + { + $tasks = $this->manager->find_cron_task_names(); + $this->assertGreaterThan(1, count($tasks)); + } + + public function test_manager_finds_shipped_task_by_name() + { + $task = $this->manager->find_task('phpbb_cron_task_core_queue'); + $this->assertNotNull($task); + } + + public function test_manager_instantiates_task_by_name() + { + $task = $this->manager->instantiate_task('phpbb_cron_task_core_queue', array()); + $this->assertNotNull($task); + } + + public function test_manager_finds_all_ready_tasks() + { + $tasks = $this->manager->find_all_ready_tasks(); + $this->assertGreaterThan(0, count($tasks)); + } + + public function test_manager_finds_one_ready_task() { + $task = $this->manager->find_one_ready_task(); + $this->assertNotNull($task); + } +} From 6e5e4721d83c8bde9780b02bd011bdbf5d188dea Mon Sep 17 00:00:00 2001 From: Nils Adermann Date: Fri, 7 Jan 2011 18:40:32 +0100 Subject: [PATCH 55/67] [feature/system-cron] Move tests to phpunit.xml and always load class loader PHPBB3-9596 --- tests/bootstrap.php | 5 +++++ tests/cron/all_tests.php | 41 ------------------------------------- tests/cron/manager_test.php | 25 +++++++--------------- 3 files changed, 12 insertions(+), 59 deletions(-) delete mode 100644 tests/cron/all_tests.php diff --git a/tests/bootstrap.php b/tests/bootstrap.php index cee60c451b..1c0c0240ab 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -31,6 +31,11 @@ require_once $phpbb_root_path . 'includes/class_loader.' . $phpEx; $class_loader = new phpbb_class_loader($phpbb_root_path, '.php'); $class_loader->register(); +require $phpbb_root_path . 'includes/class_loader.php'; + +$class_loader = new phpbb_class_loader($phpbb_root_path, '.php'); +$class_loader->register(); + require_once 'test_framework/phpbb_test_case_helpers.php'; require_once 'test_framework/phpbb_test_case.php'; require_once 'test_framework/phpbb_database_test_case.php'; diff --git a/tests/cron/all_tests.php b/tests/cron/all_tests.php deleted file mode 100644 index 1fa5af7f5e..0000000000 --- a/tests/cron/all_tests.php +++ /dev/null @@ -1,41 +0,0 @@ -addTestSuite('phpbb_cron_manager_test'); - - return $suite; - } -} - -if (PHPUnit_MAIN_METHOD == 'phpbb_cron_all_tests::main') -{ - phpbb_cron_all_tests::main(); -} - diff --git a/tests/cron/manager_test.php b/tests/cron/manager_test.php index 4d3fa1d120..dcb3c6435a 100644 --- a/tests/cron/manager_test.php +++ b/tests/cron/manager_test.php @@ -7,50 +7,39 @@ * */ -define('IN_PHPBB', true); - -require_once 'test_framework/framework.php'; - -// need the class loader since cron does not include/require cron task files -require($phpbb_root_path . 'includes/class_loader.' . $phpEx); -// do not use cache -$class_loader = new phpbb_class_loader($phpbb_root_path, '.' . $phpEx); -$class_loader->register(); - -require_once '../phpBB/includes/cron/manager.php'; - class phpbb_cron_manager_test extends PHPUnit_Framework_TestCase { public function setUp() { $this->manager = new phpbb_cron_manager(); } - + public function test_manager_finds_shipped_tasks() { $tasks = $this->manager->find_cron_task_names(); $this->assertGreaterThan(1, count($tasks)); } - + public function test_manager_finds_shipped_task_by_name() { $task = $this->manager->find_task('phpbb_cron_task_core_queue'); $this->assertNotNull($task); } - + public function test_manager_instantiates_task_by_name() { $task = $this->manager->instantiate_task('phpbb_cron_task_core_queue', array()); $this->assertNotNull($task); } - + public function test_manager_finds_all_ready_tasks() { $tasks = $this->manager->find_all_ready_tasks(); $this->assertGreaterThan(0, count($tasks)); } - - public function test_manager_finds_one_ready_task() { + + public function test_manager_finds_one_ready_task() + { $task = $this->manager->find_one_ready_task(); $this->assertNotNull($task); } From 3a3a8bb96d0cb7be2529ab095f305fd3b042783c Mon Sep 17 00:00:00 2001 From: Nils Adermann Date: Fri, 7 Jan 2011 20:58:28 +0100 Subject: [PATCH 56/67] [feature/system-cron] Abstract the database locking mechanism out of cron. Added a number of tests for the locking mechanism which can now lock arbitrary config variables. PHPBB3-9596 --- phpBB/cron.php | 10 +-- phpBB/includes/cron/lock.php | 104 ---------------------- phpBB/includes/lock/db.php | 158 +++++++++++++++++++++++++++++++++ tests/lock/db_test.php | 67 ++++++++++++++ tests/lock/fixtures/config.xml | 13 +++ 5 files changed, 243 insertions(+), 109 deletions(-) delete mode 100644 phpBB/includes/cron/lock.php create mode 100644 phpBB/includes/lock/db.php create mode 100644 tests/lock/db_test.php create mode 100644 tests/lock/fixtures/config.xml diff --git a/phpBB/cron.php b/phpBB/cron.php index d1b96b12e1..80e744d358 100644 --- a/phpBB/cron.php +++ b/phpBB/cron.php @@ -33,9 +33,9 @@ function output_image() // flush(); } -function do_cron($run_tasks) +function do_cron($cron_lock, $run_tasks) { - global $cron_lock, $config; + global $config; foreach ($run_tasks as $task) { @@ -73,7 +73,7 @@ else output_image(); } -$cron_lock = new phpbb_cron_lock(); +$cron_lock = new phpbb_lock_db('cron_lock', $config, $db); if ($cron_lock->lock()) { if ($config['use_system_cron']) @@ -103,11 +103,11 @@ if ($cron_lock->lock()) } if ($use_shutdown_function) { - register_shutdown_function('do_cron', $run_tasks); + register_shutdown_function('do_cron', $cron_lock, $run_tasks); } else { - do_cron($run_tasks); + do_cron($cron_lock, $run_tasks); } } else diff --git a/phpBB/includes/cron/lock.php b/phpBB/includes/cron/lock.php deleted file mode 100644 index 27b8b425c1..0000000000 --- a/phpBB/includes/cron/lock.php +++ /dev/null @@ -1,104 +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 the cron lock. - * - * The lock must have been previously obtained, that is, lock() call - * was issued and returned true. - * - * Note: Attempting to release a cron lock that is already released, - * that is, calling unlock() multiple times, is harmless. - * - * @return void - */ - 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/lock/db.php b/phpBB/includes/lock/db.php new file mode 100644 index 0000000000..e9885285d9 --- /dev/null +++ b/phpBB/includes/lock/db.php @@ -0,0 +1,158 @@ +config_name = $config_name; + $this->config = $config; + $this->db = $db; + } + + /** + * Tries to acquire the cron lock by updating + * the 'cron_lock' configuration variable in the database. + * + * As a lock may only be held by one process at a time, lock + * acquisition may fail if another process is holding the lock + * or if another process obtained the lock but never released it. + * Locks are forcibly released after a timeout of 1 hour. + * + * @return bool true if lock was acquired + * false otherwise + */ + public function lock() + { + if ($this->locked) + { + return true; + } + + if (!isset($this->config[$this->config_name])) + { + set_config($this->config_name, '0', true); + global $config; + $this->config = $config; + } + $lock_value = $this->config[$this->config_name]; + + // make sure cron doesn't run multiple times in parallel + if ($lock_value) + { + // if the other process is running more than an hour already we have to assume it + // aborted without cleaning the lock + $time = explode(' ', $lock_value); + $time = $time[0]; + + if ($time + 3600 >= time()) + { + return false; + } + } + + $this->unique_id = time() . ' ' . unique_id(); + + $sql = 'UPDATE ' . CONFIG_TABLE . " + SET config_value = '" . $this->db->sql_escape($this->unique_id) . "' + WHERE config_name = '" . $this->db->sql_escape($this->config_name) . "' + AND config_value = '" . $this->db->sql_escape($lock_value) . "'"; + $this->db->sql_query($sql); + + if ($this->db->sql_affectedrows()) + { + $this->locked = true; + } + else + { + // another cron process altered the table between script start and + // UPDATE query so return false + $this->locked = false; + } + + return $this->locked; + } + + /** + * Releases the cron lock. + * + * The lock must have been previously obtained, that is, lock() call + * was issued and returned true. + * + * Note: Attempting to release a cron lock that is already released, + * that is, calling unlock() multiple times, is harmless. + * + * @return void + */ + public function unlock() + { + if ($this->locked) + { + $sql = 'UPDATE ' . CONFIG_TABLE . " + SET config_value = '0' + WHERE config_name = '" . $this->db->sql_escape($this->config_name) . "' + AND config_value = '" . $this->db->sql_escape($this->unique_id) . "'"; + $this->db->sql_query($sql); + + $this->locked = false; + } + } +} diff --git a/tests/lock/db_test.php b/tests/lock/db_test.php new file mode 100644 index 0000000000..0702a2c21e --- /dev/null +++ b/tests/lock/db_test.php @@ -0,0 +1,67 @@ +createXMLDataSet(dirname(__FILE__).'/fixtures/config.xml'); + } + + public function setUp() + { + global $db, $config; + + $db = $this->db = $this->new_dbal(); + $config = $this->config = array('rand_seed' => '', 'rand_seed_last_update' => '0'); + $this->lock = new phpbb_lock_db('test_lock', $this->config, $this->db); + } + + public function test_new_lock() + { + global $config; + + $this->assertTrue($this->lock->lock()); + $this->assertTrue(isset($config['test_lock']), 'Lock was created'); + + $lock2 = new phpbb_lock_db('test_lock', $config, $this->db); + $this->assertFalse($lock2->lock()); + + $this->lock->unlock(); + $this->assertEquals('0', $config['test_lock'], 'Lock was released'); + } + + public function test_expire_lock() + { + $lock = new phpbb_lock_db('foo_lock', $this->config, $this->db); + $this->assertTrue($lock->lock()); + } + + public function test_double_lock() + { + global $config; + + $this->assertTrue($this->lock->lock()); + $this->assertTrue(isset($config['test_lock']), 'Lock was created'); + + $value = $config['test_lock']; + + $this->assertTrue($this->lock->lock()); + $this->assertEquals($value, $config['test_lock'], 'Second lock was ignored'); + + $this->lock->unlock(); + $this->assertEquals('0', $config['test_lock'], 'Lock was released'); + } +} diff --git a/tests/lock/fixtures/config.xml b/tests/lock/fixtures/config.xml new file mode 100644 index 0000000000..f36c8b929a --- /dev/null +++ b/tests/lock/fixtures/config.xml @@ -0,0 +1,13 @@ + + + + config_name + config_value + is_dynamic + + foo_lock + 1 abcd + 1 + +
+
From 311a7ff75321ae6fa491fc360ebdd52b7692fc96 Mon Sep 17 00:00:00 2001 From: Nils Adermann Date: Wed, 12 Jan 2011 22:05:53 +0100 Subject: [PATCH 57/67] [feature/system-cron] Fix duplicate instantiation of class loader in tests. PHPBB3-9596 --- tests/bootstrap.php | 5 ----- 1 file changed, 5 deletions(-) diff --git a/tests/bootstrap.php b/tests/bootstrap.php index 1c0c0240ab..cee60c451b 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -31,11 +31,6 @@ require_once $phpbb_root_path . 'includes/class_loader.' . $phpEx; $class_loader = new phpbb_class_loader($phpbb_root_path, '.php'); $class_loader->register(); -require $phpbb_root_path . 'includes/class_loader.php'; - -$class_loader = new phpbb_class_loader($phpbb_root_path, '.php'); -$class_loader->register(); - require_once 'test_framework/phpbb_test_case_helpers.php'; require_once 'test_framework/phpbb_test_case.php'; require_once 'test_framework/phpbb_database_test_case.php'; From 165b0ec0b2297ab32ca2c0e4184474720f005e51 Mon Sep 17 00:00:00 2001 From: Nils Adermann Date: Wed, 12 Jan 2011 22:43:09 +0100 Subject: [PATCH 58/67] [feature/system-cron] Make use of the new config class in locks. PHPBB3-9596 --- phpBB/includes/lock/db.php | 44 +++++++++++--------------------------- tests/lock/db_test.php | 44 ++++++++++++++++++++++++++------------ 2 files changed, 42 insertions(+), 46 deletions(-) diff --git a/phpBB/includes/lock/db.php b/phpBB/includes/lock/db.php index e9885285d9..092f4f9789 100644 --- a/phpBB/includes/lock/db.php +++ b/phpBB/includes/lock/db.php @@ -42,7 +42,7 @@ class phpbb_lock_db /** * The phpBB configuration - * @var array + * @var phpbb_config */ private $config; @@ -61,7 +61,7 @@ class phpbb_lock_db * @param array $config The phpBB configuration * @param dbal $db A database connection */ - public function __construct($config_name, $config, dbal $db) + public function __construct($config_name, phpbb_config $config, dbal $db) { $this->config_name = $config_name; $this->config = $config; @@ -69,8 +69,8 @@ class phpbb_lock_db } /** - * Tries to acquire the cron lock by updating - * the 'cron_lock' configuration variable in the database. + * Tries to acquire the lock by updating + * the configuration variable in the database. * * As a lock may only be held by one process at a time, lock * acquisition may fail if another process is holding the lock @@ -89,13 +89,11 @@ class phpbb_lock_db if (!isset($this->config[$this->config_name])) { - set_config($this->config_name, '0', true); - global $config; - $this->config = $config; + $this->config->set($this->config_name, '0', false); } $lock_value = $this->config[$this->config_name]; - // make sure cron doesn't run multiple times in parallel + // make sure lock cannot be acquired by multiple processes if ($lock_value) { // if the other process is running more than an hour already we have to assume it @@ -111,33 +109,20 @@ class phpbb_lock_db $this->unique_id = time() . ' ' . unique_id(); - $sql = 'UPDATE ' . CONFIG_TABLE . " - SET config_value = '" . $this->db->sql_escape($this->unique_id) . "' - WHERE config_name = '" . $this->db->sql_escape($this->config_name) . "' - AND config_value = '" . $this->db->sql_escape($lock_value) . "'"; - $this->db->sql_query($sql); - - if ($this->db->sql_affectedrows()) - { - $this->locked = true; - } - else - { - // another cron process altered the table between script start and - // UPDATE query so return false - $this->locked = false; - } + // try to update the config value, if it was already modified by another + // process we failed to acquire the lock. + $this->locked = $this->config->set_atomic($this->config_name, $lock_value, $this->unique_id, false); return $this->locked; } /** - * Releases the cron lock. + * Releases the lock. * * The lock must have been previously obtained, that is, lock() call * was issued and returned true. * - * Note: Attempting to release a cron lock that is already released, + * Note: Attempting to release a lock that is already released, * that is, calling unlock() multiple times, is harmless. * * @return void @@ -146,12 +131,7 @@ class phpbb_lock_db { if ($this->locked) { - $sql = 'UPDATE ' . CONFIG_TABLE . " - SET config_value = '0' - WHERE config_name = '" . $this->db->sql_escape($this->config_name) . "' - AND config_value = '" . $this->db->sql_escape($this->unique_id) . "'"; - $this->db->sql_query($sql); - + $this->config->set_atomic($this->config_name, $this->unique_id, '0', false); $this->locked = false; } } diff --git a/tests/lock/db_test.php b/tests/lock/db_test.php index 0702a2c21e..f60d7e3ee8 100644 --- a/tests/lock/db_test.php +++ b/tests/lock/db_test.php @@ -25,22 +25,21 @@ class phpbb_lock_db_test extends phpbb_database_test_case global $db, $config; $db = $this->db = $this->new_dbal(); - $config = $this->config = array('rand_seed' => '', 'rand_seed_last_update' => '0'); + $config = $this->config = new phpbb_config(array('rand_seed' => '', 'rand_seed_last_update' => '0')); + set_config(null, null, null, $this->config); $this->lock = new phpbb_lock_db('test_lock', $this->config, $this->db); } public function test_new_lock() { - global $config; - $this->assertTrue($this->lock->lock()); - $this->assertTrue(isset($config['test_lock']), 'Lock was created'); + $this->assertTrue(isset($this->config['test_lock']), 'Lock was created'); - $lock2 = new phpbb_lock_db('test_lock', $config, $this->db); + $lock2 = new phpbb_lock_db('test_lock', $this->config, $this->db); $this->assertFalse($lock2->lock()); $this->lock->unlock(); - $this->assertEquals('0', $config['test_lock'], 'Lock was released'); + $this->assertEquals('0', $this->config['test_lock'], 'Lock was released'); } public function test_expire_lock() @@ -51,17 +50,34 @@ class phpbb_lock_db_test extends phpbb_database_test_case public function test_double_lock() { - global $config; + $this->assertTrue($this->lock->lock()); + $this->assertTrue(isset($this->config['test_lock']), 'Lock was created'); + + $value = $this->config['test_lock']; $this->assertTrue($this->lock->lock()); - $this->assertTrue(isset($config['test_lock']), 'Lock was created'); - - $value = $config['test_lock']; - - $this->assertTrue($this->lock->lock()); - $this->assertEquals($value, $config['test_lock'], 'Second lock was ignored'); + $this->assertEquals($value, $this->config['test_lock'], 'Second lock was ignored'); $this->lock->unlock(); - $this->assertEquals('0', $config['test_lock'], 'Lock was released'); + $this->assertEquals('0', $this->config['test_lock'], 'Lock was released'); + } + + public function test_double_unlock() + { + $this->assertTrue($this->lock->lock()); + $this->assertFalse(empty($this->config['test_lock']), 'First lock is acquired'); + + $this->lock->unlock(); + $this->assertEquals('0', $this->config['test_lock'], 'First lock is released'); + + $lock2 = new phpbb_lock_db('test_lock', $this->config, $this->db); + $this->assertTrue($lock2->lock()); + $this->assertFalse(empty($this->config['test_lock']), 'Second lock is acquired'); + + $this->lock->unlock(); + $this->assertFalse(empty($this->config['test_lock']), 'Double release of first lock is ignored'); + + $lock2->unlock(); + $this->assertEquals('0', $this->config['test_lock'], 'Second lock is released'); } } From 9bc62056b27a54cccc4e626350838113a3ba80db Mon Sep 17 00:00:00 2001 From: Nils Adermann Date: Wed, 12 Jan 2011 23:02:00 +0100 Subject: [PATCH 59/67] [feature/system-cron] Add array type hints if appropriate and remove globals. PHPBB3-9596 --- phpBB/cron.php | 2 +- phpBB/includes/cron/manager.php | 33 ++++++++++++++++++++++----------- 2 files changed, 23 insertions(+), 12 deletions(-) diff --git a/phpBB/cron.php b/phpBB/cron.php index 80e744d358..cdfd8d0fbe 100644 --- a/phpBB/cron.php +++ b/phpBB/cron.php @@ -30,7 +30,7 @@ function output_image() echo base64_decode('R0lGODlhAQABAIAAAP///wAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw=='); // test without flush ;) - // flush(); + flush(); } function do_cron($cron_lock, $run_tasks) diff --git a/phpBB/includes/cron/manager.php b/phpBB/includes/cron/manager.php index 4c7f59092f..d7d161793e 100644 --- a/phpBB/includes/cron/manager.php +++ b/phpBB/includes/cron/manager.php @@ -28,17 +28,30 @@ class phpbb_cron_manager * Set of phpbb_cron_task_wrapper objects. * Array holding all tasks that have been found. * - * @var array + * @var array */ - private $tasks = array(); + protected $tasks = array(); + + /** + * phpBB's root directory. + * @var string + */ + protected $phpbb_root_path; + + /** + * PHP file extension + * @var string + */ + protected $phpEx; /** * Constructor. Loads all available tasks. - * - * @return void */ - public function __construct() + public function __construct($phpbb_root_path, $phpEx) { + $this->phpbb_root_path = $phpbb_root_path; + $this->phpEx = $phpEx; + $task_names = $this->find_cron_task_names(); $this->load_tasks($task_names); } @@ -59,12 +72,10 @@ class phpbb_cron_manager */ public function find_cron_task_names() { - global $phpbb_root_path, $phpEx; - - $tasks_root_path = $phpbb_root_path . 'includes/cron/task/'; + $tasks_root_path = $this->phpbb_root_path . 'includes/cron/task/'; $task_names = array(); - $ext = '.' . $phpEx; + $ext = '.' . $this->phpEx; $ext_length = strlen($ext); $dh = opendir($tasks_root_path); @@ -115,7 +126,7 @@ class phpbb_cron_manager * * @return void */ - public function load_tasks($task_names) + public function load_tasks(array $task_names) { foreach ($task_names as $task_name) { @@ -196,7 +207,7 @@ class phpbb_cron_manager * * @return phpbb_cron_task_wrapper|null */ - public function instantiate_task($name, $args) + public function instantiate_task($name, array $args) { $task = $this->find_task($name); if ($task) From 7a8233020bdeb872dad6057b7799c43c1543aba4 Mon Sep 17 00:00:00 2001 From: Nils Adermann Date: Thu, 13 Jan 2011 00:07:40 +0100 Subject: [PATCH 60/67] [feature/system-cron] Use a RecursiveDirectoryIterator instead of readdir. PHPBB3-9596 --- phpBB/common.php | 2 +- phpBB/cron.php | 2 +- phpBB/includes/cron/manager.php | 31 ++++++++++++------------------- tests/cron/manager_test.php | 2 +- 4 files changed, 15 insertions(+), 22 deletions(-) diff --git a/phpBB/common.php b/phpBB/common.php index 00fc1a5cb7..e099a324bf 100644 --- a/phpBB/common.php +++ b/phpBB/common.php @@ -242,5 +242,5 @@ foreach ($cache->obtain_hooks() as $hook) if (!$config['use_system_cron']) { - $cron = new phpbb_cron_manager(); + $cron = new phpbb_cron_manager($phpbb_root_path, $phpEx); } diff --git a/phpBB/cron.php b/phpBB/cron.php index cdfd8d0fbe..9f13a9f462 100644 --- a/phpBB/cron.php +++ b/phpBB/cron.php @@ -63,7 +63,7 @@ if ($config['use_system_cron']) { $use_shutdown_function = false; - $cron = new phpbb_cron_manager(); + $cron = new phpbb_cron_manager($phpbb_root_path, $phpEx); } else { diff --git a/phpBB/includes/cron/manager.php b/phpBB/includes/cron/manager.php index d7d161793e..6be7d6ec0c 100644 --- a/phpBB/includes/cron/manager.php +++ b/phpBB/includes/cron/manager.php @@ -72,35 +72,28 @@ class phpbb_cron_manager */ public function find_cron_task_names() { - $tasks_root_path = $this->phpbb_root_path . 'includes/cron/task/'; + $task_root_path = $this->phpbb_root_path . 'includes/cron/task/'; $task_names = array(); $ext = '.' . $this->phpEx; $ext_length = strlen($ext); - $dh = opendir($tasks_root_path); - while (($mod = readdir($dh)) !== false) - { - // ignore ., .. and dot directories - // todo: change is_dir to account for symlinks - if ($mod[0] == '.' || !is_dir($tasks_root_path . $mod)) - { - continue; - } + $iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($task_root_path)); - $dh2 = opendir($tasks_root_path . $mod); - while (($file = readdir($dh2)) !== false) + foreach ($iterator as $fileinfo) + { + $file = preg_replace("#^$task_root_path#", '', $fileinfo->getPathname()); + + // skip directories and files direclty in the task root path + if ($fileinfo->isFile() && strpos($file, '/') !== false) { - $task_name = substr($file, 0, -$ext_length); - if (substr($file, -$ext_length) == $ext && $this->is_valid_name($mod) && $this->is_valid_name($task_name)) + $task_name = str_replace('/', '_', substr($file, 0, -$ext_length)); + if (substr($file, -$ext_length) == $ext && $this->is_valid_name($task_name)) { - $full_task_name = $mod . '_' . $task_name; - $task_names[] = $full_task_name; + $task_names[] = $task_name; } } - closedir($dh2); } - closedir($dh); return $task_names; } @@ -115,7 +108,7 @@ class phpbb_cron_manager */ public function is_valid_name($name) { - return (bool) preg_match('/^[a-zA-Z][a-zA-Z0-9_]*$/', $name); + return preg_match('/^[a-zA-Z][a-zA-Z0-9_]*$/', $name); } /** diff --git a/tests/cron/manager_test.php b/tests/cron/manager_test.php index dcb3c6435a..39e052bd57 100644 --- a/tests/cron/manager_test.php +++ b/tests/cron/manager_test.php @@ -11,7 +11,7 @@ class phpbb_cron_manager_test extends PHPUnit_Framework_TestCase { public function setUp() { - $this->manager = new phpbb_cron_manager(); + $this->manager = new phpbb_cron_manager(__DIR__ . '/../../phpBB/', 'php'); } public function test_manager_finds_shipped_tasks() From 09b136272b9ec25824f1c72d0148bdfe43a43603 Mon Sep 17 00:00:00 2001 From: Nils Adermann Date: Thu, 13 Jan 2011 00:51:32 +0100 Subject: [PATCH 61/67] [feature/system-cron] Cache cron's task names. Instead of using a path relative to phpbb_root_path the path to the task directory is directly passed to the cron manager. Dummy tasks are now in the tests directory directly. PHPBB3-9596 --- phpBB/common.php | 2 +- phpBB/cron.php | 2 +- phpBB/includes/cron/manager.php | 45 ++++++++++++++----- tests/cron/manager_test.php | 32 +++++++++---- tests/cron/task/testmod/dummy_task.php | 23 ++++++++++ tests/cron/task/testmod/second_dummy_task.php | 23 ++++++++++ 6 files changed, 106 insertions(+), 21 deletions(-) create mode 100644 tests/cron/task/testmod/dummy_task.php create mode 100644 tests/cron/task/testmod/second_dummy_task.php diff --git a/phpBB/common.php b/phpBB/common.php index e099a324bf..68be033578 100644 --- a/phpBB/common.php +++ b/phpBB/common.php @@ -242,5 +242,5 @@ foreach ($cache->obtain_hooks() as $hook) if (!$config['use_system_cron']) { - $cron = new phpbb_cron_manager($phpbb_root_path, $phpEx); + $cron = new phpbb_cron_manager($phpbb_root_path . 'includes/cron/task', $phpEx, $cache->get_driver()); } diff --git a/phpBB/cron.php b/phpBB/cron.php index 9f13a9f462..fb85e9fe18 100644 --- a/phpBB/cron.php +++ b/phpBB/cron.php @@ -63,7 +63,7 @@ if ($config['use_system_cron']) { $use_shutdown_function = false; - $cron = new phpbb_cron_manager($phpbb_root_path, $phpEx); + $cron = new phpbb_cron_manager($phpbb_root_path . 'includes/cron/task', $phpEx, $cache->get_driver()); } else { diff --git a/phpBB/includes/cron/manager.php b/phpBB/includes/cron/manager.php index 6be7d6ec0c..e6ad974d6d 100644 --- a/phpBB/includes/cron/manager.php +++ b/phpBB/includes/cron/manager.php @@ -33,10 +33,10 @@ class phpbb_cron_manager protected $tasks = array(); /** - * phpBB's root directory. + * Directory containing cron tasks * @var string */ - protected $phpbb_root_path; + protected $task_path; /** * PHP file extension @@ -45,12 +45,23 @@ class phpbb_cron_manager protected $phpEx; /** - * Constructor. Loads all available tasks. + * Cache driver + * @var phpbb_cache_driver_interface */ - public function __construct($phpbb_root_path, $phpEx) + protected $cache; + + /** + * Constructor. Loads all available tasks. + * + * @param string $task_path Directory containing cron tasks + * @param string $phpEx PHP file extension + * @param phpbb_cache_driver_interface $cache Cache for task names (optional) + */ + public function __construct($task_path, $phpEx, phpbb_cache_driver_interface $cache = null) { - $this->phpbb_root_path = $phpbb_root_path; + $this->task_path = $task_path; $this->phpEx = $phpEx; + $this->cache = $cache; $task_names = $this->find_cron_task_names(); $this->load_tasks($task_names); @@ -72,17 +83,25 @@ class phpbb_cron_manager */ public function find_cron_task_names() { - $task_root_path = $this->phpbb_root_path . 'includes/cron/task/'; + if ($this->cache) + { + $task_names = $this->cache->get('_cron_tasks'); + + if ($task_names !== false) + { + return $task_names; + } + } $task_names = array(); $ext = '.' . $this->phpEx; $ext_length = strlen($ext); - $iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($task_root_path)); + $iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($this->task_path)); foreach ($iterator as $fileinfo) { - $file = preg_replace("#^$task_root_path#", '', $fileinfo->getPathname()); + $file = preg_replace('#^' . preg_quote($this->task_path, '#') . '#', '', $fileinfo->getPathname()); // skip directories and files direclty in the task root path if ($fileinfo->isFile() && strpos($file, '/') !== false) @@ -90,11 +109,16 @@ class phpbb_cron_manager $task_name = str_replace('/', '_', substr($file, 0, -$ext_length)); if (substr($file, -$ext_length) == $ext && $this->is_valid_name($task_name)) { - $task_names[] = $task_name; + $task_names[] = 'phpbb_cron_task_' . $task_name; } } } + if ($this->cache) + { + $this->cache->put('_cron_tasks', $task_names, $ttl = 3600); + } + return $task_names; } @@ -123,8 +147,7 @@ class phpbb_cron_manager { foreach ($task_names as $task_name) { - $class = "phpbb_cron_task_$task_name"; - $task = new $class(); + $task = new $task_name(); $wrapper = new phpbb_cron_task_wrapper($task); $this->tasks[] = $wrapper; } diff --git a/tests/cron/manager_test.php b/tests/cron/manager_test.php index 39e052bd57..c282a802b2 100644 --- a/tests/cron/manager_test.php +++ b/tests/cron/manager_test.php @@ -7,40 +7,56 @@ * */ +require_once __DIR__ . '/../mock/cache.php'; +require_once __DIR__ . '/task/testmod/dummy_task.php'; +require_once __DIR__ . '/task/testmod/second_dummy_task.php'; + class phpbb_cron_manager_test extends PHPUnit_Framework_TestCase { public function setUp() { - $this->manager = new phpbb_cron_manager(__DIR__ . '/../../phpBB/', 'php'); + $this->manager = new phpbb_cron_manager(__DIR__ . '/task/', 'php'); + $this->task_name = 'phpbb_cron_task_testmod_dummy_task'; } public function test_manager_finds_shipped_tasks() { $tasks = $this->manager->find_cron_task_names(); - $this->assertGreaterThan(1, count($tasks)); + $this->assertEquals(2, sizeof($tasks)); } public function test_manager_finds_shipped_task_by_name() { - $task = $this->manager->find_task('phpbb_cron_task_core_queue'); - $this->assertNotNull($task); + $task = $this->manager->find_task($this->task_name); + $this->assertInstanceOf('phpbb_cron_task_wrapper', $task); + $this->assertEquals($this->task_name, $task->get_name()); } public function test_manager_instantiates_task_by_name() { - $task = $this->manager->instantiate_task('phpbb_cron_task_core_queue', array()); - $this->assertNotNull($task); + $task = $this->manager->instantiate_task($this->task_name, array()); + $this->assertInstanceOf('phpbb_cron_task_wrapper', $task); + $this->assertEquals($this->task_name, $task->get_name()); } public function test_manager_finds_all_ready_tasks() { $tasks = $this->manager->find_all_ready_tasks(); - $this->assertGreaterThan(0, count($tasks)); + $this->assertEquals(2, sizeof($tasks)); } public function test_manager_finds_one_ready_task() { $task = $this->manager->find_one_ready_task(); - $this->assertNotNull($task); + $this->assertInstanceOf('phpbb_cron_task_wrapper', $task); + } + + public function test_manager_finds_all_ready_tasks_cached() + { + $cache = new phpbb_mock_cache(array('_cron_tasks' => array($this->task_name))); + $manager = new phpbb_cron_manager(__DIR__ . '/../../phpBB/', 'php', $cache); + + $tasks = $manager->find_all_ready_tasks(); + $this->assertEquals(1, sizeof($tasks)); } } diff --git a/tests/cron/task/testmod/dummy_task.php b/tests/cron/task/testmod/dummy_task.php new file mode 100644 index 0000000000..5941157589 --- /dev/null +++ b/tests/cron/task/testmod/dummy_task.php @@ -0,0 +1,23 @@ + Date: Thu, 13 Jan 2011 02:03:16 +0100 Subject: [PATCH 62/67] [feature/system-cron] Rename lock() to acquire and unlock() to release. PHPBB3-9596 --- phpBB/cron.php | 4 ++-- phpBB/includes/lock/db.php | 10 +++++----- tests/lock/db_test.php | 26 +++++++++++++------------- 3 files changed, 20 insertions(+), 20 deletions(-) diff --git a/phpBB/cron.php b/phpBB/cron.php index fb85e9fe18..e179b3e8d6 100644 --- a/phpBB/cron.php +++ b/phpBB/cron.php @@ -48,7 +48,7 @@ function do_cron($cron_lock, $run_tasks) } // Unloading cache and closing db after having done the dirty work. - $cron_lock->unlock(); + $cron_lock->release(); garbage_collection(); } @@ -74,7 +74,7 @@ else } $cron_lock = new phpbb_lock_db('cron_lock', $config, $db); -if ($cron_lock->lock()) +if ($cron_lock->acquire()) { if ($config['use_system_cron']) { diff --git a/phpBB/includes/lock/db.php b/phpBB/includes/lock/db.php index 092f4f9789..f5f1b4c6f9 100644 --- a/phpBB/includes/lock/db.php +++ b/phpBB/includes/lock/db.php @@ -80,11 +80,11 @@ class phpbb_lock_db * @return bool true if lock was acquired * false otherwise */ - public function lock() + public function acquire() { if ($this->locked) { - return true; + return false; } if (!isset($this->config[$this->config_name])) @@ -119,15 +119,15 @@ class phpbb_lock_db /** * Releases the lock. * - * The lock must have been previously obtained, that is, lock() call + * The lock must have been previously obtained, that is, acquire() call * was issued and returned true. * * Note: Attempting to release a lock that is already released, - * that is, calling unlock() multiple times, is harmless. + * that is, calling release() multiple times, is harmless. * * @return void */ - public function unlock() + public function release() { if ($this->locked) { diff --git a/tests/lock/db_test.php b/tests/lock/db_test.php index f60d7e3ee8..3b2e3ea3b2 100644 --- a/tests/lock/db_test.php +++ b/tests/lock/db_test.php @@ -32,52 +32,52 @@ class phpbb_lock_db_test extends phpbb_database_test_case public function test_new_lock() { - $this->assertTrue($this->lock->lock()); + $this->assertTrue($this->lock->acquire()); $this->assertTrue(isset($this->config['test_lock']), 'Lock was created'); $lock2 = new phpbb_lock_db('test_lock', $this->config, $this->db); - $this->assertFalse($lock2->lock()); + $this->assertFalse($lock2->acquire()); - $this->lock->unlock(); + $this->lock->release(); $this->assertEquals('0', $this->config['test_lock'], 'Lock was released'); } public function test_expire_lock() { $lock = new phpbb_lock_db('foo_lock', $this->config, $this->db); - $this->assertTrue($lock->lock()); + $this->assertTrue($lock->acquire()); } public function test_double_lock() { - $this->assertTrue($this->lock->lock()); + $this->assertTrue($this->lock->acquire()); $this->assertTrue(isset($this->config['test_lock']), 'Lock was created'); $value = $this->config['test_lock']; - $this->assertTrue($this->lock->lock()); - $this->assertEquals($value, $this->config['test_lock'], 'Second lock was ignored'); + $this->assertFalse($this->lock->acquire()); + $this->assertEquals($value, $this->config['test_lock'], 'Second lock failed'); - $this->lock->unlock(); + $this->lock->release(); $this->assertEquals('0', $this->config['test_lock'], 'Lock was released'); } public function test_double_unlock() { - $this->assertTrue($this->lock->lock()); + $this->assertTrue($this->lock->acquire()); $this->assertFalse(empty($this->config['test_lock']), 'First lock is acquired'); - $this->lock->unlock(); + $this->lock->release(); $this->assertEquals('0', $this->config['test_lock'], 'First lock is released'); $lock2 = new phpbb_lock_db('test_lock', $this->config, $this->db); - $this->assertTrue($lock2->lock()); + $this->assertTrue($lock2->acquire()); $this->assertFalse(empty($this->config['test_lock']), 'Second lock is acquired'); - $this->lock->unlock(); + $this->lock->release(); $this->assertFalse(empty($this->config['test_lock']), 'Double release of first lock is ignored'); - $lock2->unlock(); + $lock2->release(); $this->assertEquals('0', $this->config['test_lock'], 'Second lock is released'); } } From 7a2c5e618b62d80df05f7b21c77d840b9e764ef6 Mon Sep 17 00:00:00 2001 From: Nils Adermann Date: Thu, 13 Jan 2011 02:17:24 +0100 Subject: [PATCH 63/67] [feature/system-cron] preg_match returns int so cast to bool, fix comment PHPBB3-9596 --- phpBB/includes/cron/manager.php | 2 +- phpBB/includes/lock/db.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/phpBB/includes/cron/manager.php b/phpBB/includes/cron/manager.php index e6ad974d6d..aa09fb41ac 100644 --- a/phpBB/includes/cron/manager.php +++ b/phpBB/includes/cron/manager.php @@ -132,7 +132,7 @@ class phpbb_cron_manager */ public function is_valid_name($name) { - return preg_match('/^[a-zA-Z][a-zA-Z0-9_]*$/', $name); + return (bool) preg_match('/^[a-zA-Z][a-zA-Z0-9_]*$/', $name); } /** diff --git a/phpBB/includes/lock/db.php b/phpBB/includes/lock/db.php index f5f1b4c6f9..20dbb63e0c 100644 --- a/phpBB/includes/lock/db.php +++ b/phpBB/includes/lock/db.php @@ -55,7 +55,7 @@ class phpbb_lock_db /** * Creates a named released instance of the lock. * - * You have to call lock to actually create the lock. + * You have to call acquire() to actually create the lock. * * @param string $config_name A config variable to be used for locking * @param array $config The phpBB configuration From c6a8abb409c01dd0ae85bf71cd41b62f47db9b77 Mon Sep 17 00:00:00 2001 From: Nils Adermann Date: Thu, 13 Jan 2011 03:01:39 +0100 Subject: [PATCH 64/67] [feature/system-cron] Clarify comments about flush() call in cron. PHPBB3-9596 --- phpBB/cron.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/phpBB/cron.php b/phpBB/cron.php index e179b3e8d6..6de493f0bf 100644 --- a/phpBB/cron.php +++ b/phpBB/cron.php @@ -29,7 +29,8 @@ function output_image() echo base64_decode('R0lGODlhAQABAIAAAP///wAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw=='); - // test without flush ;) + // Flush here to prevent browser from showing the page as loading while + // running cron. flush(); } @@ -70,6 +71,7 @@ else $cron_type = request_var('cron_type', ''); $use_shutdown_function = (@function_exists('register_shutdown_function')) ? true : false; + // Comment this line out for debugging so the page does not return an image. output_image(); } From 92c278d39a5db7ea296c86b3a70a28b99b16ec5e Mon Sep 17 00:00:00 2001 From: Nils Adermann Date: Thu, 13 Jan 2011 03:30:38 +0100 Subject: [PATCH 65/67] [feature/system-cron] Remove an unecessary assignment and an unecessary comment PHPBB3-9596 --- phpBB/includes/cron/manager.php | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/phpBB/includes/cron/manager.php b/phpBB/includes/cron/manager.php index aa09fb41ac..d954775956 100644 --- a/phpBB/includes/cron/manager.php +++ b/phpBB/includes/cron/manager.php @@ -77,8 +77,6 @@ class phpbb_cron_manager * $name is the name of the cron task. * Cron task is expected to be a class named phpbb_cron_task_${mod}_${name}. * - * Todo: consider caching found task file list in global cache. - * * @return array List of task names */ public function find_cron_task_names() @@ -116,7 +114,7 @@ class phpbb_cron_manager if ($this->cache) { - $this->cache->put('_cron_tasks', $task_names, $ttl = 3600); + $this->cache->put('_cron_tasks', $task_names, 3600); } return $task_names; From 4e689c522f8cdb60984eb47020bee3c0535210bb Mon Sep 17 00:00:00 2001 From: Oleg Pudeyev Date: Tue, 18 Jan 2011 17:03:45 -0500 Subject: [PATCH 66/67] [feature/system-cron] Added documentation for cron manager constructor. PHPBB3-9596 --- phpBB/includes/cron/manager.php | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/phpBB/includes/cron/manager.php b/phpBB/includes/cron/manager.php index d954775956..21dcb91695 100644 --- a/phpBB/includes/cron/manager.php +++ b/phpBB/includes/cron/manager.php @@ -33,7 +33,9 @@ class phpbb_cron_manager protected $tasks = array(); /** - * Directory containing cron tasks + * Path to the root of directory tree with tasks. + * For bundled phpBB tasks, this is the path to includes/cron/tasks + * under phpBB root. * @var string */ protected $task_path; @@ -53,9 +55,21 @@ class phpbb_cron_manager /** * Constructor. Loads all available tasks. * + * Tasks will be looked up in directory tree rooted at $task_path. + * Task classes will be autoloaded and must be named according to + * autoloading naming conventions. To load cron tasks shipped with + * phpbb, pass $phpbb_root_path . 'includes/cron/task' as $task_path. + * + * If $cache is given, names of found cron tasks will be cached in it + * for one hour. Note that the cron task names are stored without + * namespacing; if two different phbb_cron_manager instances are + * constructed with different $task_path arguments but the same $cache, + * the second instance will use task names found by the first instance. + * * @param string $task_path Directory containing cron tasks * @param string $phpEx PHP file extension * @param phpbb_cache_driver_interface $cache Cache for task names (optional) + * @return void */ public function __construct($task_path, $phpEx, phpbb_cache_driver_interface $cache = null) { From 1fd8d6de7f6bb41505530c83e487a9dc18bd25af Mon Sep 17 00:00:00 2001 From: Oleg Pudeyev Date: Thu, 20 Jan 2011 08:52:09 -0500 Subject: [PATCH 67/67] [feature/system-cron] More tests for cron manager. PHPBB3-9596 --- tests/cron/manager_test.php | 21 +++++++++++++++++++ .../task2/testmod/simple_not_runnable.php | 13 ++++++++++++ tests/cron/task2/testmod/simple_ready.php | 8 +++++++ .../task2/testmod/simple_should_not_run.php | 13 ++++++++++++ 4 files changed, 55 insertions(+) create mode 100644 tests/cron/task2/testmod/simple_not_runnable.php create mode 100644 tests/cron/task2/testmod/simple_ready.php create mode 100644 tests/cron/task2/testmod/simple_should_not_run.php diff --git a/tests/cron/manager_test.php b/tests/cron/manager_test.php index c282a802b2..6288a5c641 100644 --- a/tests/cron/manager_test.php +++ b/tests/cron/manager_test.php @@ -10,6 +10,9 @@ require_once __DIR__ . '/../mock/cache.php'; require_once __DIR__ . '/task/testmod/dummy_task.php'; require_once __DIR__ . '/task/testmod/second_dummy_task.php'; +require_once __DIR__ . '/task2/testmod/simple_ready.php'; +require_once __DIR__ . '/task2/testmod/simple_not_runnable.php'; +require_once __DIR__ . '/task2/testmod/simple_should_not_run.php'; class phpbb_cron_manager_test extends PHPUnit_Framework_TestCase { @@ -59,4 +62,22 @@ class phpbb_cron_manager_test extends PHPUnit_Framework_TestCase $tasks = $manager->find_all_ready_tasks(); $this->assertEquals(1, sizeof($tasks)); } + + public function test_manager_finds_only_ready_tasks() + { + $manager = new phpbb_cron_manager(__DIR__ . '/task2/', 'php'); + $tasks = $manager->find_all_ready_tasks(); + $task_names = $this->tasks_to_names($tasks); + $this->assertEquals(array('phpbb_cron_task_testmod_simple_ready'), $task_names); + } + + private function tasks_to_names($tasks) + { + $names = array(); + foreach ($tasks as $task) + { + $names[] = get_class($task->task); + } + return $names; + } } diff --git a/tests/cron/task2/testmod/simple_not_runnable.php b/tests/cron/task2/testmod/simple_not_runnable.php new file mode 100644 index 0000000000..54869fa1cc --- /dev/null +++ b/tests/cron/task2/testmod/simple_not_runnable.php @@ -0,0 +1,13 @@ +