defined('WPINC') || exit();
use LiteSpeed\Crawler as Crawler2;
class Crawler extends Base
public function __construct()
Debug2::debug('CLI_Crawler init');
$this->__crawler = Crawler2::cls();
* $ wp litespeed-crawler l
* $ wp litespeed-crawler list
$crawler_list = $this->__crawler->list_crawlers();
$summary = Crawler2::get_summary();
if ($summary['curr_crawler'] >= count($crawler_list)) {
$summary['curr_crawler'] = 0;
$is_running = time() - $summary['is_running'] <= $this->conf(Base::O_CRAWLER_RUN_DURATION);
$seconds = $this->conf(Base::O_CRAWLER_RUN_INTERVAL);
$hours = (int) floor($seconds / 3600);
$recurrence .= sprintf(__('%d hours', 'litespeed-cache'), $hours);
$recurrence .= sprintf(__('%d hour', 'litespeed-cache'), $hours);
$minutes = (int) floor(($seconds % 3600) / 60);
$recurrence .= sprintf(__('%d minutes', 'litespeed-cache'), $minutes);
$recurrence .= sprintf(__('%d minute', 'litespeed-cache'), $minutes);
foreach ($crawler_list as $i => $v) {
$hit = !empty($summary['crawler_stats'][$i]['H']) ? $summary['crawler_stats'][$i]['H'] : 0;
$miss = !empty($summary['crawler_stats'][$i]['M']) ? $summary['crawler_stats'][$i]['M'] : 0;
$blacklisted = !empty($summary['crawler_stats'][$i]['B']) ? $summary['crawler_stats'][$i]['B'] : 0;
$blacklisted += !empty($summary['crawler_stats'][$i]['N']) ? $summary['crawler_stats'][$i]['N'] : 0;
if (isset($summary['crawler_stats'][$i]['W'])) {
$waiting = $summary['crawler_stats'][$i]['W'] ?: 0;
$waiting = $summary['list_size'] - $hit - $miss - $blacklisted;
$analytics = 'Waiting: ' . $waiting;
$analytics .= ' Hit: ' . $hit;
$analytics .= ' Miss: ' . $miss;
$analytics .= ' Blocked: ' . $blacklisted;
if ($i == $summary['curr_crawler']) {
$running = 'Pos: ' . ($summary['last_pos'] + 1);
$status = $this->__crawler->is_active($i) ? '✅' : '❌';
'Name' => wp_strip_all_tags($v['title']),
'Frequency' => $recurrence,
'Analytics' => $analytics,
WP_CLI\Utils\format_items('table', $list, array('ID', 'Name', 'Frequency', 'Status', 'Analytics', 'Running'));
* $ wp litespeed-crawler enable 2
public function enable($args)
if ($this->__crawler->is_active($id)) {
WP_CLI::error('ID #' . $id . ' had been enabled');
$this->__crawler->toggle_activeness($id);
WP_CLI::success('Enabled crawler #' . $id);
* $ wp litespeed-crawler disable 1
public function disable($args)
if (!$this->__crawler->is_active($id)) {
WP_CLI::error('ID #' . $id . ' has been disabled');
$this->__crawler->toggle_activeness($id);
WP_CLI::success('Disabled crawler #' . $id);
* $ wp litespeed-crawler r
* $ wp litespeed-crawler run
self::debug('⚠️⚠️⚠️ Forced take over lane (CLI)');
$this->__crawler->Release_lane();
Task::async_call('crawler');
$summary = Crawler2::get_summary();
WP_CLI::success('Start crawling. Current crawler #' . ($summary['curr_crawler'] + 1) . ' [position] ' . $summary['last_pos'] . ' [total] ' . $summary['list_size']);
* # Reset crawler position
* $ wp litespeed-crawler reset
$this->__crawler->reset_pos();
$summary = Crawler2::get_summary();
WP_CLI::success('Reset position. Current crawler #' . ($summary['curr_crawler'] + 1) . ' [position] ' . $summary['last_pos'] . ' [total] ' . $summary['list_size']);