-
Notifications
You must be signed in to change notification settings - Fork 6
/
Manager.php
347 lines (331 loc) · 9.53 KB
/
Manager.php
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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
<?php
/**
* vipnytt/RobotsTxtParser
*
* @link https://github.com/VIPnytt/RobotsTxtParser
* @license https://github.com/VIPnytt/RobotsTxtParser/blob/master/LICENSE The MIT License (MIT)
*/
namespace vipnytt\RobotsTxtParser\Client\Cache\MySQL;
use PDO;
use vipnytt\RobotsTxtParser\Client\Cache\ManagerInterface;
use vipnytt\RobotsTxtParser\Exceptions\ClientException;
use vipnytt\RobotsTxtParser\Exceptions\DatabaseException;
use vipnytt\RobotsTxtParser\RobotsTxtInterface;
use vipnytt\RobotsTxtParser\TxtClient;
use vipnytt\RobotsTxtParser\UriClient;
/**
* Class Manager
*
* @see https://github.com/VIPnytt/RobotsTxtParser/blob/master/docs/methods/Cache.md for documentation
* @package vipnytt\RobotsTxtParser\Handler\Cache\MySQL
*/
class Manager implements ManagerInterface, RobotsTxtInterface
{
/**
* Database handler
* @var PDO
*/
private $pdo;
/**
* cURL options
* @var array
*/
private $curlOptions = [];
/**
* Byte limit
* @var int|null
*/
private $byteLimit = self::BYTE_LIMIT;
/**
* Manager constructor.
*
* @param PDO $pdo
* @param array $curlOptions
* @param int|null $byteLimit
*/
public function __construct(PDO $pdo, array $curlOptions, $byteLimit)
{
$this->pdo = $pdo;
$this->curlOptions = $curlOptions;
$this->byteLimit = $byteLimit;
}
/**
* Debug - Get raw data
*
* @param string $base
* @return array
*/
public function debug($base)
{
$query = $this->pdo->prepare(<<<SQL
SELECT *
FROM robotstxt__cache1
WHERE base = :base;
SQL
);
$query->bindParam(':base', $base, PDO::PARAM_STR);
$query->execute();
return $query->rowCount() > 0 ? $query->fetch(PDO::FETCH_ASSOC) : [];
}
/**
* Parser client
*
* @param string $base
* @return TxtClient
*/
public function client($base)
{
$query = $this->pdo->prepare(<<<SQL
SELECT
content,
statusCode,
nextUpdate,
effective,
worker,
UNIX_TIMESTAMP()
FROM robotstxt__cache1
WHERE base = :base;
SQL
);
$query->bindParam(':base', $base, PDO::PARAM_STR);
$query->execute();
if ($query->rowCount() > 0) {
$row = $query->fetch(PDO::FETCH_ASSOC);
$this->clockSyncCheck($row['UNIX_TIMESTAMP()']);
if ($row['nextUpdate'] >= $row['UNIX_TIMESTAMP()']) {
$this->markAsActive($base, $row['worker']);
return new TxtClient($base, $row['statusCode'], $row['content'], self::ENCODING, $row['effective'], $this->byteLimit);
}
}
$query = $this->pdo->prepare(<<<SQL
UPDATE robotstxt__cache1
SET worker = 0
WHERE base = :base;
SQL
);
$query->bindParam(':base', $base, PDO::PARAM_STR);
$query->execute();
$request = new UriClient($base, $this->curlOptions, $this->byteLimit);
$this->push($request, null);
return $request;
}
/**
* Clock sync check
*
* @param int $time
* @throws DatabaseException
*/
private function clockSyncCheck($time)
{
if (abs(time() - $time) >= 10) {
throw new DatabaseException('`PHP server` and `SQL server` timestamps are out of sync. Please fix!');
}
}
/**
* Mark robots.txt as active
*
* @param string $base
* @param int|null $workerID
* @return bool
*/
private function markAsActive($base, $workerID = 0)
{
if ($workerID == 0) {
$query = $this->pdo->prepare(<<<SQL
UPDATE robotstxt__cache1
SET worker = NULL
WHERE base = :base AND worker = 0;
SQL
);
$query->bindParam(':base', $base, PDO::PARAM_STR);
return $query->execute();
}
return true;
}
/**
* Update an robots.txt in the database
*
* @param UriClient $client
* @param int|null $worker
* @return bool
*/
private function push(UriClient $client, $worker = 0)
{
$base = $client->getBaseUri();
$statusCode = $client->getStatusCode();
$nextUpdate = $client->nextUpdate();
$effective = ($effective = $client->getEffectiveUri()) === $base ? null : $effective;
if (strpos($base, 'http') === 0 &&
(
$statusCode === null ||
(
$statusCode >= 500 &&
$statusCode < 600
)
) &&
$this->displacePush($base, $nextUpdate, $worker)
) {
return true;
}
$validUntil = $client->validUntil();
$content = $client->render()->compressed(PHP_EOL);
$query = $this->pdo->prepare(<<<SQL
INSERT INTO robotstxt__cache1 (base, content, statusCode, validUntil, nextUpdate, effective)
VALUES (:base, :content, :statusCode, :validUntil, :nextUpdate, :effective)
ON DUPLICATE KEY UPDATE content = :content, statusCode = :statusCode, validUntil = :validUntil,
nextUpdate = :nextUpdate, effective = :effective, worker = :worker;
SQL
);
$query->bindParam(':base', $base, PDO::PARAM_STR);
$query->bindParam(':content', $content, PDO::PARAM_STR);
$query->bindParam(':statusCode', $statusCode, PDO::PARAM_INT | PDO::PARAM_NULL);
$query->bindParam(':validUntil', $validUntil, PDO::PARAM_INT);
$query->bindParam(':nextUpdate', $nextUpdate, PDO::PARAM_INT);
$query->bindParam(':effective', $effective, PDO::PARAM_STR | PDO::PARAM_NULL);
$query->bindParam(':worker', $worker, PDO::PARAM_INT | PDO::PARAM_NULL);
return $query->execute();
}
/**
* Displace push timestamp
*
* @param string $base
* @param int $nextUpdate
* @param int|null $worker
* @return bool
*/
private function displacePush($base, $nextUpdate, $worker = null)
{
$query = $this->pdo->prepare(<<<SQL
SELECT
validUntil,
UNIX_TIMESTAMP()
FROM robotstxt__cache1
WHERE base = :base;
SQL
);
$query->bindParam(':base', $base, PDO::PARAM_STR);
$query->execute();
if ($query->rowCount() > 0) {
$row = $query->fetch(PDO::FETCH_ASSOC);
$this->clockSyncCheck($row['UNIX_TIMESTAMP()']);
if ($row['validUntil'] > $row['UNIX_TIMESTAMP()']) {
$nextUpdate = min($row['validUntil'], $nextUpdate);
$query = $this->pdo->prepare(<<<SQL
UPDATE robotstxt__cache1
SET nextUpdate = :nextUpdate, worker = :worker
WHERE base = :base;
SQL
);
$query->bindParam(':base', $base, PDO::PARAM_STR);
$query->bindParam(':nextUpdate', $nextUpdate, PDO::PARAM_INT);
$query->bindParam(':worker', $worker, PDO::PARAM_INT | PDO::PARAM_NULL);
return $query->execute();
}
$this->invalidate($base);
}
return false;
}
/**
* Invalidate cache
*
* @param $base
* @return bool
*/
public function invalidate($base)
{
$query = $this->pdo->prepare(<<<SQL
DELETE FROM robotstxt__cache1
WHERE base = :base;
SQL
);
$query->bindParam(':base', $base, PDO::PARAM_STR);
return $query->execute();
}
/**
* Process the update queue
*
* @param float|int|null $timeLimit
* @param int|null $workerID
* @return string[]
* @throws ClientException
*/
public function cron($timeLimit, $workerID)
{
$start = microtime(true);
$worker = $this->setWorkerID($workerID);
$log = [];
$lastCount = -1;
while (count($log) > $lastCount &&
(
empty($timeLimit) ||
$timeLimit > (microtime(true) - $start)
)
) {
$lastCount = count($log);
$query = $this->pdo->prepare(<<<SQL
UPDATE robotstxt__cache1
SET worker = :workerID
WHERE worker IS NULL AND nextUpdate <= UNIX_TIMESTAMP()
ORDER BY nextUpdate ASC
LIMIT 1;
SQL
);
$query->bindParam(':workerID', $worker, PDO::PARAM_INT);
$query->execute();
$query = $this->pdo->prepare(<<<SQL
SELECT base
FROM robotstxt__cache1
WHERE worker = :workerID
ORDER BY nextUpdate DESC
LIMIT 10;
SQL
);
$query->bindParam(':workerID', $worker, PDO::PARAM_INT);
$query->execute();
if ($query->rowCount() > 0) {
while ($row = $query->fetch(PDO::FETCH_ASSOC)) {
if ($this->push(new UriClient($row['base'], $this->curlOptions, $this->byteLimit))) {
$log[(string)microtime(true)] = $row['base'];
}
}
}
}
return $log;
}
/**
* Set WorkerID
*
* @param int|null $workerID
* @return int
* @throws DatabaseException
*/
private function setWorkerID($workerID = null)
{
if (is_int($workerID) &&
$workerID <= 255 &&
$workerID >= 1
) {
return $workerID;
} elseif ($workerID !== null) {
throw new DatabaseException('WorkerID out of range (1-255)');
}
return rand(1, 255);
}
/**
* Clean the cache table
*
* @param int $delay - in seconds
* @return bool
*/
public function clean($delay)
{
$delay = self::CACHE_TIME + $delay;
$query = $this->pdo->prepare(<<<SQL
DELETE FROM robotstxt__cache1
WHERE nextUpdate < (UNIX_TIMESTAMP() - :delay);
SQL
);
$query->bindParam(':delay', $delay, PDO::PARAM_INT);
return $query->execute();
}
}