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