1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* vipnytt/RobotsTxtParser |
4
|
|
|
* |
5
|
|
|
* @link https://github.com/VIPnytt/RobotsTxtParser |
6
|
|
|
* @license https://github.com/VIPnytt/RobotsTxtParser/blob/master/LICENSE The MIT License (MIT) |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace vipnytt\RobotsTxtParser\Client\Delay\MySQL; |
10
|
|
|
|
11
|
|
|
use vipnytt\RobotsTxtParser\Client\Delay\BaseCore; |
12
|
|
|
use vipnytt\RobotsTxtParser\Exceptions; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Class Base |
16
|
|
|
* |
17
|
|
|
* @see https://vipnytt.github.io/RobotsTxtParser/methods/DelayClient.html for documentation |
18
|
|
|
* @package vipnytt\RobotsTxtParser\Client\Delay\MySQL |
19
|
|
|
*/ |
20
|
|
|
class Base extends BaseCore |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* Queue |
24
|
|
|
* |
25
|
|
|
* @return float|int |
26
|
|
|
*/ |
27
|
|
|
public function checkQueue() |
28
|
|
|
{ |
29
|
|
|
if ($this->delay == 0) { |
30
|
|
|
return 0; |
31
|
|
|
} |
32
|
|
|
$query = $this->pdo->prepare(<<<SQL |
33
|
|
|
SELECT GREATEST(0, (delayUntil / 1000000) - UNIX_TIMESTAMP(CURTIME(6))) |
34
|
|
|
FROM robotstxt__delay0 |
35
|
|
|
WHERE base = :base AND userAgent = :userAgent; |
36
|
|
|
SQL |
37
|
|
|
); |
38
|
|
|
$query->bindValue('base', $this->base, \PDO::PARAM_STR); |
39
|
|
|
$query->bindValue('userAgent', $this->userAgent, \PDO::PARAM_STR); |
40
|
|
|
$query->execute(); |
41
|
|
|
return $query->rowCount() > 0 ? floatval($query->fetch(\PDO::FETCH_COLUMN)) : 0; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Reset queue |
46
|
|
|
* |
47
|
|
|
* @param float|int $newDelay |
48
|
|
|
* @return bool |
49
|
|
|
*/ |
50
|
|
|
public function reset($newDelay = self::RESET_NEW_DELAY) |
51
|
|
|
{ |
52
|
|
|
if (empty($newDelay)) { |
53
|
|
|
$query = $this->pdo->prepare(<<<SQL |
54
|
|
|
DELETE FROM robotstxt__delay0 |
55
|
|
|
WHERE base = :base AND userAgent = :userAgent; |
56
|
|
|
SQL |
57
|
|
|
); |
58
|
|
|
$query->bindValue('base', $this->base, \PDO::PARAM_STR); |
59
|
|
|
$query->bindValue('userAgent', $this->userAgent, \PDO::PARAM_STR); |
60
|
|
|
return $query->execute(); |
61
|
|
|
} |
62
|
|
|
$query = $this->pdo->prepare(<<<SQL |
63
|
|
|
INSERT INTO robotstxt__delay0 (base, userAgent, delayUntil, lastDelay) |
64
|
|
|
VALUES (:base, :userAgent, (UNIX_TIMESTAMP(CURTIME(6)) + :delay) * 1000000, :delay * 1000000) |
65
|
|
|
ON DUPLICATE KEY UPDATE |
66
|
|
|
delayUntil = (UNIX_TIMESTAMP(CURTIME(6)) + :delay) * 1000000, |
67
|
|
|
lastDelay = :delay * 1000000; |
68
|
|
|
SQL |
69
|
|
|
); |
70
|
|
|
$query->bindValue('base', $this->base, \PDO::PARAM_STR); |
71
|
|
|
$query->bindValue('userAgent', $this->userAgent, \PDO::PARAM_STR); |
72
|
|
|
$query->bindValue('delay', $newDelay, \PDO::PARAM_INT); |
73
|
|
|
return $query->execute(); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Timestamp with milliseconds |
78
|
|
|
* |
79
|
|
|
* @return float|int |
80
|
|
|
* @throws Exceptions\OutOfSyncException |
81
|
|
|
*/ |
82
|
|
|
public function getTimeSleepUntil() |
83
|
|
|
{ |
84
|
|
|
if ($this->delay == 0) { |
85
|
|
|
return 0; |
86
|
|
|
} |
87
|
|
|
$query = $this->pdo->prepare(<<<SQL |
88
|
|
|
SELECT |
89
|
|
|
delayUntil, |
90
|
|
|
UNIX_TIMESTAMP() |
91
|
|
|
FROM robotstxt__delay0 |
92
|
|
|
WHERE base = :base AND userAgent = :userAgent; |
93
|
|
|
SQL |
94
|
|
|
); |
95
|
|
|
$query->bindValue('base', $this->base, \PDO::PARAM_STR); |
96
|
|
|
$query->bindValue('userAgent', $this->userAgent, \PDO::PARAM_STR); |
97
|
|
|
$query->execute(); |
98
|
|
|
$this->increment(); |
99
|
|
|
if ($query->rowCount() > 0) { |
100
|
|
|
$row = $query->fetch(\PDO::FETCH_ASSOC); |
101
|
|
|
$this->clockSyncCheck($row['UNIX_TIMESTAMP()'], self::OUT_OF_SYNC_TIME_LIMIT); |
102
|
|
|
return $row['delayUntil'] / 1000000; |
103
|
|
|
} |
104
|
|
|
return 0; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Set new delayUntil timestamp |
109
|
|
|
* |
110
|
|
|
* @return bool |
111
|
|
|
*/ |
112
|
|
View Code Duplication |
private function increment() |
|
|
|
|
113
|
|
|
{ |
114
|
|
|
$query = $this->pdo->prepare(<<<SQL |
115
|
|
|
INSERT INTO robotstxt__delay0 (base, userAgent, delayUntil, lastDelay) |
116
|
|
|
VALUES (:base, :userAgent, (UNIX_TIMESTAMP(CURTIME(6)) + :delay) * 1000000, :delay * 1000000) |
117
|
|
|
ON DUPLICATE KEY UPDATE |
118
|
|
|
delayUntil = GREATEST((UNIX_TIMESTAMP(CURTIME(6)) + :delay) * 1000000, delayUntil + (:delay * 1000000)), |
119
|
|
|
lastDelay = :delay * 1000000; |
120
|
|
|
SQL |
121
|
|
|
); |
122
|
|
|
$query->bindValue('base', $this->base, \PDO::PARAM_STR); |
123
|
|
|
$query->bindValue('userAgent', $this->userAgent, \PDO::PARAM_STR); |
124
|
|
|
$query->bindValue('delay', $this->delay, \PDO::PARAM_INT); |
125
|
|
|
return $query->execute(); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Debug - Get raw data |
130
|
|
|
* |
131
|
|
|
* @return array |
132
|
|
|
*/ |
133
|
|
View Code Duplication |
public function debug() |
|
|
|
|
134
|
|
|
{ |
135
|
|
|
$query = $this->pdo->prepare(<<<SQL |
136
|
|
|
SELECT * |
137
|
|
|
FROM robotstxt__delay0 |
138
|
|
|
WHERE base = :base AND userAgent = :userAgent; |
139
|
|
|
SQL |
140
|
|
|
); |
141
|
|
|
$query->bindValue('base', $this->base, \PDO::PARAM_STR); |
142
|
|
|
$query->bindValue('userAgent', $this->userAgent, \PDO::PARAM_STR); |
143
|
|
|
$query->execute(); |
144
|
|
|
return $query->rowCount() > 0 ? $query->fetch(\PDO::FETCH_ASSOC) : []; |
145
|
|
|
} |
146
|
|
|
} |
147
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.