crawler.cls.php
2 months ago
database.cls.php
2 months ago
debug.cls.php
2 months ago
image.cls.php
2 months ago
online.cls.php
2 months ago
option.cls.php
2 months ago
presets.cls.php
2 months ago
purge.cls.php
2 months ago
crawler.cls.php
261 lines
| 1 | <?php |
| 2 | /** |
| 3 | * LiteSpeed Cache CLI Crawler Commands |
| 4 | * |
| 5 | * Provides WP-CLI commands for managing LiteSpeed Cache crawlers. |
| 6 | * |
| 7 | * @package LiteSpeed |
| 8 | * @since 1.1.0 |
| 9 | */ |
| 10 | |
| 11 | namespace LiteSpeed\CLI; |
| 12 | |
| 13 | defined('WPINC') || exit(); |
| 14 | |
| 15 | use LiteSpeed\Debug2; |
| 16 | use LiteSpeed\Base; |
| 17 | use LiteSpeed\Task; |
| 18 | use LiteSpeed\Crawler as Crawler2; |
| 19 | use WP_CLI; |
| 20 | |
| 21 | /** |
| 22 | * Crawler |
| 23 | */ |
| 24 | class Crawler extends Base { |
| 25 | /** |
| 26 | * Crawler instance |
| 27 | * |
| 28 | * @var Crawler2 $crawler |
| 29 | */ |
| 30 | private $crawler; |
| 31 | |
| 32 | /** |
| 33 | * Constructor for Crawler CLI commands |
| 34 | * |
| 35 | * @since 1.1.0 |
| 36 | */ |
| 37 | public function __construct() { |
| 38 | Debug2::debug('CLI_Crawler init'); |
| 39 | |
| 40 | $this->crawler = Crawler2::cls(); |
| 41 | } |
| 42 | |
| 43 | /** |
| 44 | * List all crawlers |
| 45 | * |
| 46 | * Displays a table of all crawlers with their details. |
| 47 | * |
| 48 | * ## OPTIONS |
| 49 | * |
| 50 | * ## EXAMPLES |
| 51 | * |
| 52 | * # List all crawlers |
| 53 | * $ wp litespeed-crawler l |
| 54 | * |
| 55 | * @since 1.1.0 |
| 56 | */ |
| 57 | public function l() { |
| 58 | $this->list(); |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * List all crawlers |
| 63 | * |
| 64 | * Displays a table of all crawlers with their details. |
| 65 | * |
| 66 | * ## OPTIONS |
| 67 | * |
| 68 | * ## EXAMPLES |
| 69 | * |
| 70 | * # List all crawlers |
| 71 | * $ wp litespeed-crawler list |
| 72 | * |
| 73 | * @since 1.1.0 |
| 74 | */ |
| 75 | public function list() { |
| 76 | $crawler_list = $this->crawler->list_crawlers(); |
| 77 | $summary = Crawler2::get_summary(); |
| 78 | if ($summary['curr_crawler'] >= count($crawler_list)) { |
| 79 | $summary['curr_crawler'] = 0; |
| 80 | } |
| 81 | $is_running = time() - $summary['is_running'] <= 900; |
| 82 | |
| 83 | $crawler_run_interval = defined('LITESPEED_CRAWLER_RUN_INTERVAL') ? LITESPEED_CRAWLER_RUN_INTERVAL : 600; // Specify time in seconds for the time between each run interval |
| 84 | if ($crawler_run_interval > 0) { |
| 85 | $recurrence = ''; |
| 86 | $hours = (int) floor($crawler_run_interval / 3600); |
| 87 | if ($hours) { |
| 88 | if ($hours > 1) { |
| 89 | $recurrence .= sprintf(__('%d hours', 'litespeed-cache'), $hours); |
| 90 | } else { |
| 91 | $recurrence .= sprintf(__('%d hour', 'litespeed-cache'), $hours); |
| 92 | } |
| 93 | } |
| 94 | $minutes = (int) floor(($crawler_run_interval % 3600) / 60); |
| 95 | if ($minutes) { |
| 96 | $recurrence .= ' '; |
| 97 | if ($minutes > 1) { |
| 98 | $recurrence .= sprintf(__('%d minutes', 'litespeed-cache'), $minutes); |
| 99 | } else { |
| 100 | $recurrence .= sprintf(__('%d minute', 'litespeed-cache'), $minutes); |
| 101 | } |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | $list = array(); |
| 106 | foreach ($crawler_list as $i => $v) { |
| 107 | $hit = !empty($summary['crawler_stats'][$i][Crawler2::STATUS_HIT]) ? $summary['crawler_stats'][$i][Crawler2::STATUS_HIT] : 0; |
| 108 | $miss = !empty($summary['crawler_stats'][$i][Crawler2::STATUS_MISS]) ? $summary['crawler_stats'][$i][Crawler2::STATUS_MISS] : 0; |
| 109 | |
| 110 | $blacklisted = !empty($summary['crawler_stats'][$i][Crawler2::STATUS_BLACKLIST]) ? $summary['crawler_stats'][$i][Crawler2::STATUS_BLACKLIST] : 0; |
| 111 | $blacklisted += !empty($summary['crawler_stats'][$i][Crawler2::STATUS_NOCACHE]) ? $summary['crawler_stats'][$i][Crawler2::STATUS_NOCACHE] : 0; |
| 112 | |
| 113 | if (isset($summary['crawler_stats'][$i][Crawler2::STATUS_WAIT])) { |
| 114 | $waiting = $summary['crawler_stats'][$i][Crawler2::STATUS_WAIT] ?? 0; |
| 115 | } else { |
| 116 | $waiting = $summary['list_size'] - $hit - $miss - $blacklisted; |
| 117 | } |
| 118 | |
| 119 | $analytics = 'Waiting: ' . $waiting; |
| 120 | $analytics .= ' Hit: ' . $hit; |
| 121 | $analytics .= ' Miss: ' . $miss; |
| 122 | $analytics .= ' Blocked: ' . $blacklisted; |
| 123 | |
| 124 | $running = ''; |
| 125 | if ($i === $summary['curr_crawler']) { |
| 126 | $running = 'Pos: ' . ($summary['last_pos'] + 1); |
| 127 | if ($is_running) { |
| 128 | $running .= '(Running)'; |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | $status = $this->crawler->is_active($i) ? '� |
| 133 | ' : '❌'; |
| 134 | |
| 135 | $list[] = array( |
| 136 | 'ID' => $i + 1, |
| 137 | 'Name' => wp_strip_all_tags($v['title']), |
| 138 | 'Frequency' => $recurrence, |
| 139 | 'Status' => $status, |
| 140 | 'Analytics' => $analytics, |
| 141 | 'Running' => $running, |
| 142 | ); |
| 143 | } |
| 144 | |
| 145 | WP_CLI\Utils\format_items('table', $list, array( 'ID', 'Name', 'Frequency', 'Status', 'Analytics', 'Running' )); |
| 146 | } |
| 147 | |
| 148 | /** |
| 149 | * Enable one crawler |
| 150 | * |
| 151 | * ## OPTIONS |
| 152 | * |
| 153 | * <id> |
| 154 | * : The ID of the crawler to enable. |
| 155 | * |
| 156 | * ## EXAMPLES |
| 157 | * |
| 158 | * # Turn on 2nd crawler |
| 159 | * $ wp litespeed-crawler enable 2 |
| 160 | * |
| 161 | * @since 1.1.0 |
| 162 | * @param array $args Command arguments. |
| 163 | */ |
| 164 | public function enable( $args ) { |
| 165 | $id = $args[0] - 1; |
| 166 | if ($this->crawler->is_active($id)) { |
| 167 | WP_CLI::error('ID #' . $id . ' had been enabled'); |
| 168 | return; |
| 169 | } |
| 170 | |
| 171 | $this->crawler->toggle_activeness($id); |
| 172 | WP_CLI::success('Enabled crawler #' . $id); |
| 173 | } |
| 174 | |
| 175 | /** |
| 176 | * Disable one crawler |
| 177 | * |
| 178 | * ## OPTIONS |
| 179 | * |
| 180 | * <id> |
| 181 | * : The ID of the crawler to disable. |
| 182 | * |
| 183 | * ## EXAMPLES |
| 184 | * |
| 185 | * # Turn off 1st crawler |
| 186 | * $ wp litespeed-crawler disable 1 |
| 187 | * |
| 188 | * @since 1.1.0 |
| 189 | * @param array $args Command arguments. |
| 190 | */ |
| 191 | public function disable( $args ) { |
| 192 | $id = $args[0] - 1; |
| 193 | if (!$this->crawler->is_active($id)) { |
| 194 | WP_CLI::error('ID #' . $id . ' has been disabled'); |
| 195 | return; |
| 196 | } |
| 197 | |
| 198 | $this->crawler->toggle_activeness($id); |
| 199 | WP_CLI::success('Disabled crawler #' . $id); |
| 200 | } |
| 201 | |
| 202 | /** |
| 203 | * Run crawling |
| 204 | * |
| 205 | * ## OPTIONS |
| 206 | * |
| 207 | * ## EXAMPLES |
| 208 | * |
| 209 | * # Start crawling |
| 210 | * $ wp litespeed-crawler r |
| 211 | * |
| 212 | * @since 1.1.0 |
| 213 | */ |
| 214 | public function r() { |
| 215 | $this->run(); |
| 216 | } |
| 217 | |
| 218 | /** |
| 219 | * Run crawling |
| 220 | * |
| 221 | * ## OPTIONS |
| 222 | * |
| 223 | * ## EXAMPLES |
| 224 | * |
| 225 | * # Start crawling |
| 226 | * $ wp litespeed-crawler run |
| 227 | * |
| 228 | * @since 1.1.0 |
| 229 | */ |
| 230 | public function run() { |
| 231 | self::debug('⚠️⚠️⚠️ Forced take over lane (CLI)'); |
| 232 | $this->crawler->Release_lane(); |
| 233 | |
| 234 | Task::async_call('crawler'); |
| 235 | |
| 236 | $summary = Crawler2::get_summary(); |
| 237 | |
| 238 | WP_CLI::success('Start crawling. Current crawler #' . ($summary['curr_crawler'] + 1) . ' [position] ' . $summary['last_pos'] . ' [total] ' . $summary['list_size']); |
| 239 | } |
| 240 | |
| 241 | /** |
| 242 | * Reset crawler position |
| 243 | * |
| 244 | * ## OPTIONS |
| 245 | * |
| 246 | * ## EXAMPLES |
| 247 | * |
| 248 | * # Reset crawler position |
| 249 | * $ wp litespeed-crawler reset |
| 250 | * |
| 251 | * @since 1.1.0 |
| 252 | */ |
| 253 | public function reset() { |
| 254 | $this->crawler->reset_pos(); |
| 255 | |
| 256 | $summary = Crawler2::get_summary(); |
| 257 | |
| 258 | WP_CLI::success('Reset position. Current crawler #' . ($summary['curr_crawler'] + 1) . ' [position] ' . $summary['last_pos'] . ' [total] ' . $summary['list_size']); |
| 259 | } |
| 260 | } |
| 261 |