|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Spatie\Crawler; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Support\Collection; |
|
6
|
|
|
|
|
7
|
|
|
class CrawlQueue |
|
8
|
|
|
{ |
|
9
|
|
|
/** @var \Illuminate\Support\Collection */ |
|
10
|
|
|
public $pending; |
|
11
|
|
|
|
|
12
|
|
|
/** @var \Illuminate\Support\Collection */ |
|
13
|
|
|
public $processed; |
|
14
|
|
|
|
|
15
|
|
|
public function __construct() |
|
16
|
|
|
{ |
|
17
|
|
|
$this->pending = collect(); |
|
18
|
|
|
|
|
19
|
|
|
$this->processed = collect(); |
|
20
|
|
|
} |
|
21
|
|
|
|
|
22
|
|
|
public function add(CrawlUrl $url) |
|
23
|
|
|
{ |
|
24
|
|
|
if ($this->has($url)) { |
|
25
|
|
|
return; |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
$this->pending->push($url); |
|
29
|
|
|
|
|
30
|
|
|
return $this; |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
public function hasPendingUrls(): bool |
|
34
|
|
|
{ |
|
35
|
|
|
return count($this->pending); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
public function getPendingUrls(): Collection |
|
39
|
|
|
{ |
|
40
|
|
|
return $this->pending->values(); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @param int $index |
|
45
|
|
|
* |
|
46
|
|
|
* @return \Spatie\Crawler\CrawlUrl|null |
|
47
|
|
|
*/ |
|
48
|
|
|
public function getPendingUrlAtIndex(int $index) |
|
49
|
|
|
{ |
|
50
|
|
|
if (!isset($this->getPendingUrls()[$index])) { |
|
51
|
|
|
return; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
return $this->getPendingUrls()[$index]; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
public function hasAlreadyBeenProcessed(CrawlUrl $url) |
|
58
|
|
|
{ |
|
59
|
|
|
return $this->contains($this->processed, $url); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
public function markAsProcessed(CrawlUrl $crawlUrl) |
|
63
|
|
|
{ |
|
64
|
|
|
$this->processed->push($crawlUrl); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* @param CrawlUrl|Url $crawlUrl |
|
69
|
|
|
* |
|
70
|
|
|
* @return bool |
|
71
|
|
|
*/ |
|
72
|
|
|
public function has($crawlUrl): bool |
|
73
|
|
|
{ |
|
74
|
|
|
if ($crawlUrl instanceof Url) { |
|
75
|
|
|
$crawlUrl = CrawlUrl::create($crawlUrl); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
if ($this->contains($this->pending, $crawlUrl)) { |
|
79
|
|
|
return true; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
if ($this->contains($this->processed, $crawlUrl)) { |
|
83
|
|
|
return true; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
return false; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
public function removeProcessedUrlsFromPending() |
|
90
|
|
|
{ |
|
91
|
|
|
$this->pending = $this->pending |
|
92
|
|
|
->reject(function (CrawlUrl $crawlUrl) { |
|
93
|
|
|
return $this->contains($this->processed, $crawlUrl); |
|
94
|
|
|
}) |
|
95
|
|
|
->values(); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
protected function contains(Collection $collection, CrawlUrl $searchCrawlUrl): bool |
|
99
|
|
|
{ |
|
100
|
|
|
foreach ($collection as $crawlUrl) { |
|
101
|
|
|
if ($crawlUrl->url->isEqual($searchCrawlUrl->url)) { |
|
102
|
|
|
return true; |
|
103
|
|
|
} |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
return false; |
|
107
|
|
|
} |
|
108
|
|
|
} |