diff --git a/src/base.cls.php b/src/base.cls.php index 870f9f960..9ecd576b6 100644 --- a/src/base.cls.php +++ b/src/base.cls.php @@ -232,6 +232,7 @@ class Base extends Root ## -------------- Crawler ----------------- ## ## -------------------------------------------------- ## const O_CRAWLER = 'crawler'; + const O_CRAWLER_SCHEDULE_TIME = 'crawler-schedule_time'; const O_CRAWLER_USLEEP = 'crawler-usleep'; const O_CRAWLER_RUN_DURATION = 'crawler-run_duration'; const O_CRAWLER_RUN_INTERVAL = 'crawler-run_interval'; @@ -514,6 +515,7 @@ class Base extends Root // Crawler self::O_CRAWLER => false, + self::O_CRAWLER_SCHEDULE_TIME => '', self::O_CRAWLER_USLEEP => 0, self::O_CRAWLER_RUN_DURATION => 0, self::O_CRAWLER_RUN_INTERVAL => 0, diff --git a/src/crawler.cls.php b/src/crawler.cls.php index 7791d76d9..9a91dbddc 100644 --- a/src/crawler.cls.php +++ b/src/crawler.cls.php @@ -8,6 +8,8 @@ namespace LiteSpeed; +use \DateTime; + defined('WPINC') || exit(); class Crawler extends Root @@ -247,6 +249,72 @@ public static function async_handler($manually_run = false) self::start($manually_run); } + /** + * Check if crawler can run in the choosen time period + * + * @since 6.1 + */ + public function _crawler_in_schedule_time() + { + $class_settings = self::cls(); + $schedule_times = $class_settings->conf(Base::O_CRAWLER_SCHEDULE_TIME, ''); + + if ($schedule_times !== '') { + $schedule_times = explode(',', $schedule_times); + + if (count($schedule_times) > 0) { + $now = time(); + + // A single time: e.g. 1, 01, 1:1, 1:01, 1:01:1, or 1:01:01, etc. + $time_re = '(\d{1,2}(?::\d{1,2}){0,2}(?i:[AP]M)?)'; + $re = '/^' . $time_re . '[-]' . $time_re . '$/'; + + // Allow parsing times like 1-3, 1AM-3PM, 1aM-3Pm + $with_minutes = function ($time) { + $has_meridian = stripos($time, 'm'); + if (preg_match('/:\d/', $time)) { + return $time; + } else { + if ($has_meridian !== false) { + $meridian = strtoupper(substr($time, -2)); + $time_only = substr($time, 0, -2); + + return $time_only . ':00' . $meridian; + } else { + return $time . ':00'; + } + } + }; + + foreach ($schedule_times as $time) { + $time = trim($time); + preg_match($re, $time, $matches); + + if (!$matches) { + continue; + } + + $start = strtotime($with_minutes($matches[1])); + $end = strtotime($with_minutes($matches[2])); + + if (false === $start || false === $end || $start > $end) { + continue; + } + + // Test start <= now <= end + if ($now <= $end && $now >= $start) { + return true; + } + } + } + + self::debug('------------crawler schedule time-------------no time period found'); + return false; + } else { + return true; + } + } + /** * Proceed crawling * @@ -260,6 +328,12 @@ public static function start($manually_run = false) return false; } + $crawler_is_in_time = self::cls()->_crawler_in_schedule_time(); + if (!$manually_run && !$crawler_is_in_time) { + self::debug('......crawler is NOT allowed at this time......'); + return false; + } + if ($manually_run) { self::debug('......crawler manually ran......'); } diff --git a/src/lang.cls.php b/src/lang.cls.php index b2d2dce46..f201456b7 100644 --- a/src/lang.cls.php +++ b/src/lang.cls.php @@ -251,6 +251,7 @@ public static function title($id) self::O_CDN_CLOUDFLARE => __('Cloudflare API', 'litespeed-cache'), self::O_CRAWLER => __('Crawler', 'litespeed-cache'), + self::O_CRAWLER_SCHEDULE_TIME => __('Running time', 'litespeed-cache'), self::O_CRAWLER_USLEEP => __('Delay', 'litespeed-cache'), self::O_CRAWLER_RUN_DURATION => __('Run Duration', 'litespeed-cache'), self::O_CRAWLER_RUN_INTERVAL => __('Interval Between Runs', 'litespeed-cache'), diff --git a/tpl/crawler/settings-general.tpl.php b/tpl/crawler/settings-general.tpl.php index 49d2ece76..a06eebf7a 100644 --- a/tpl/crawler/settings-general.tpl.php +++ b/tpl/crawler/settings-general.tpl.php @@ -28,6 +28,25 @@ + + + + title($id); ?> + + + build_input($id); ?> +
+ +
+ HH:mm-HH:mm', ','); ?> +
+ '. date('H:m') . ''; ?> +
+ 00:00-06:00 , 20:00-23:59, 01:00-05:00 +
+ + +