|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Spatie\HttpStatusCheck; |
|
4
|
|
|
|
|
5
|
|
|
use GuzzleHttp\Exception\RequestException; |
|
6
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
7
|
|
|
use Psr\Http\Message\UriInterface; |
|
8
|
|
|
use Spatie\Crawler\CrawlObserver; |
|
9
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
10
|
|
|
|
|
11
|
|
|
class CrawlLogger extends CrawlObserver |
|
12
|
|
|
{ |
|
13
|
|
|
const UNRESPONSIVE_HOST = 'Host did not respond'; |
|
14
|
|
|
const REDIRECT = 'Redirect'; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* @var \Symfony\Component\Console\Output\OutputInterface |
|
18
|
|
|
*/ |
|
19
|
|
|
protected $consoleOutput; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @var array |
|
23
|
|
|
*/ |
|
24
|
|
|
protected $crawledUrls = []; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @var string|null |
|
28
|
|
|
*/ |
|
29
|
|
|
protected $outputFile = null; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @param \Symfony\Component\Console\Output\OutputInterface $consoleOutput |
|
33
|
|
|
*/ |
|
34
|
|
|
public function __construct(OutputInterface $consoleOutput) |
|
35
|
|
|
{ |
|
36
|
|
|
$this->consoleOutput = $consoleOutput; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Called when the crawl will crawl the url. |
|
41
|
|
|
* |
|
42
|
|
|
* @param \Psr\Http\Message\UriInterface $url |
|
43
|
|
|
*/ |
|
44
|
|
|
public function willCrawl(UriInterface $url) |
|
45
|
|
|
{ |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* Called when the crawl has ended. |
|
50
|
|
|
*/ |
|
51
|
|
|
public function finishedCrawling() |
|
52
|
|
|
{ |
|
53
|
|
|
$this->consoleOutput->writeln(''); |
|
54
|
|
|
$this->consoleOutput->writeln('Crawling summary'); |
|
55
|
|
|
$this->consoleOutput->writeln('----------------'); |
|
56
|
|
|
|
|
57
|
|
|
ksort($this->crawledUrls); |
|
58
|
|
|
|
|
59
|
|
|
foreach ($this->crawledUrls as $statusCode => $urls) { |
|
60
|
|
|
$colorTag = $this->getColorTagForStatusCode($statusCode); |
|
61
|
|
|
|
|
62
|
|
|
$count = count($urls); |
|
63
|
|
|
|
|
64
|
|
|
if (is_numeric($statusCode)) { |
|
65
|
|
|
$this->consoleOutput->writeln("<{$colorTag}>Crawled {$count} url(s) with statuscode {$statusCode}</{$colorTag}>"); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
if ($statusCode == static::UNRESPONSIVE_HOST) { |
|
69
|
|
|
$this->consoleOutput->writeln("<{$colorTag}>{$count} url(s) did have unresponsive host(s)</{$colorTag}>"); |
|
70
|
|
|
} |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
$this->consoleOutput->writeln(''); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
protected function getColorTagForStatusCode(string $code): string |
|
77
|
|
|
{ |
|
78
|
|
|
if ($this->startsWith($code, '2')) { |
|
79
|
|
|
return 'info'; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
if ($this->startsWith($code, '3')) { |
|
83
|
|
|
return 'comment'; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
return 'error'; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* @param string|null $haystack |
|
91
|
|
|
* @param string|array $needles |
|
92
|
|
|
* |
|
93
|
|
|
* @return bool |
|
94
|
|
|
*/ |
|
95
|
|
|
public function startsWith($haystack, $needles): bool |
|
96
|
|
|
{ |
|
97
|
|
|
foreach ((array) $needles as $needle) { |
|
98
|
|
|
if ($needle != '' && substr($haystack, 0, strlen($needle)) === (string) $needle) { |
|
99
|
|
|
return true; |
|
100
|
|
|
} |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
return false; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* Set the filename to write the output log. |
|
108
|
|
|
* |
|
109
|
|
|
* @param string $filename |
|
110
|
|
|
*/ |
|
111
|
|
|
public function setOutputFile($filename) |
|
112
|
|
|
{ |
|
113
|
|
|
$this->outputFile = $filename; |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
public function crawled( |
|
117
|
|
|
UriInterface $url, |
|
118
|
|
|
ResponseInterface $response, |
|
119
|
|
|
?UriInterface $foundOnUrl = null |
|
120
|
|
|
) { |
|
121
|
|
|
// https://github.com/guzzle/guzzle/blob/master/docs/faq.rst#how-can-i-track-redirected-requests |
|
122
|
|
|
if ($response->getHeader('X-Guzzle-Redirect-History')) { |
|
123
|
|
|
// Retrieve both Redirect History headers |
|
124
|
|
|
$fullRedirectReport = []; |
|
|
|
|
|
|
125
|
|
|
// Retrieve both Redirect History headers |
|
126
|
|
|
$redirectUriHistory = $response->getHeader('X-Guzzle-Redirect-History'); // retrieve Redirect URI history |
|
127
|
|
|
$redirectCodeHistory = $response->getHeader('X-Guzzle-Redirect-Status-History'); // retrieve Redirect HTTP Status history |
|
128
|
|
|
// Add the initial URI requested to the (beginning of) URI history |
|
129
|
|
|
array_unshift($redirectUriHistory, (string) $url); |
|
130
|
|
|
// Add the final HTTP status code to the end of HTTP response history |
|
131
|
|
|
array_push($redirectCodeHistory, $response->getStatusCode()); |
|
132
|
|
|
$fullRedirectReport = []; |
|
133
|
|
|
foreach ($redirectUriHistory as $key => $value) { |
|
134
|
|
|
$fullRedirectReport[$key] = ['location' => $value, 'code' => $redirectCodeHistory[$key]]; |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
foreach ($fullRedirectReport as $k=>$redirect) { |
|
138
|
|
|
$this->addResult( |
|
139
|
|
|
(string) $redirect['location'], |
|
140
|
|
|
(string) $foundOnUrl, |
|
141
|
|
|
$redirect['code'], |
|
142
|
|
|
$k+1==count($fullRedirectReport) ? $response->getReasonPhrase() : self::REDIRECT |
|
143
|
|
|
); |
|
144
|
|
|
} |
|
145
|
|
|
} else { |
|
146
|
|
|
$this->addResult( |
|
147
|
|
|
(string) $url, |
|
148
|
|
|
(string) $foundOnUrl, |
|
149
|
|
|
$response->getStatusCode(), |
|
150
|
|
|
$response->getReasonPhrase() |
|
151
|
|
|
); |
|
152
|
|
|
} |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
public function crawlFailed( |
|
156
|
|
|
UriInterface $url, |
|
157
|
|
|
RequestException $requestException, |
|
158
|
|
|
?UriInterface $foundOnUrl = null |
|
159
|
|
|
) { |
|
160
|
|
|
if ($response = $requestException->getResponse()) { |
|
161
|
|
|
$this->crawled($url, $response, $foundOnUrl); |
|
162
|
|
|
} else { |
|
163
|
|
|
$this->addResult((string) $url, (string) $foundOnUrl, '---', self::UNRESPONSIVE_HOST); |
|
164
|
|
|
} |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
|
|
public function addResult($url, $foundOnUrl, $statusCode, $reason) |
|
168
|
|
|
{ |
|
169
|
|
|
// done display duplicate results |
|
170
|
|
|
// this happens if a redirect if a redirect is followed to a page |
|
171
|
|
|
if( isset($this->crawledUrls[$statusCode]) && in_array($url, $this->crawledUrls[$statusCode]) ){ |
|
172
|
|
|
return; |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
|
|
$colorTag = $this->getColorTagForStatusCode($statusCode); |
|
176
|
|
|
|
|
177
|
|
|
$timestamp = date('Y-m-d H:i:s'); |
|
178
|
|
|
|
|
179
|
|
|
$message = "{$statusCode} {$reason} - ".(string) $url; |
|
180
|
|
|
|
|
181
|
|
|
if ($foundOnUrl && $colorTag === 'error') { |
|
182
|
|
|
$message .= " (found on {$foundOnUrl})"; |
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
|
|
if ($this->outputFile && $colorTag === 'error') { |
|
|
|
|
|
|
186
|
|
|
file_put_contents($this->outputFile, $message.PHP_EOL, FILE_APPEND); |
|
187
|
|
|
} |
|
188
|
|
|
|
|
189
|
|
|
$this->consoleOutput->writeln("<{$colorTag}>[{$timestamp}] {$message}</{$colorTag}>"); |
|
190
|
|
|
|
|
191
|
|
|
$this->crawledUrls[$statusCode][] = $url; |
|
192
|
|
|
} |
|
193
|
|
|
} |
|
194
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.