|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Spatie\Sitemap; |
|
4
|
|
|
|
|
5
|
|
|
use GuzzleHttp\Psr7\Uri; |
|
6
|
|
|
use Illuminate\Support\Collection; |
|
7
|
|
|
use Spatie\Crawler\Crawler; |
|
8
|
|
|
use Spatie\Sitemap\Tags\Url; |
|
9
|
|
|
use Spatie\Crawler\CrawlProfile; |
|
10
|
|
|
use Psr\Http\Message\UriInterface; |
|
11
|
|
|
use Spatie\Sitemap\Crawler\Profile; |
|
12
|
|
|
use Spatie\Sitemap\Crawler\Observer; |
|
13
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
14
|
|
|
|
|
15
|
|
|
class SitemapGenerator |
|
16
|
|
|
{ |
|
17
|
|
|
/** @var \Illuminate\Support\Collection */ |
|
18
|
|
|
protected $sitemaps; |
|
19
|
|
|
|
|
20
|
|
|
/** @var \GuzzleHttp\Psr7\Uri */ |
|
21
|
|
|
protected $urlToBeCrawled = ''; |
|
22
|
|
|
|
|
23
|
|
|
/** @var \Spatie\Crawler\Crawler */ |
|
24
|
|
|
protected $crawler; |
|
25
|
|
|
|
|
26
|
|
|
/** @var callable */ |
|
27
|
|
|
protected $shouldCrawl; |
|
28
|
|
|
|
|
29
|
|
|
/** @var callable */ |
|
30
|
|
|
protected $hasCrawled; |
|
31
|
|
|
|
|
32
|
|
|
/** @var int */ |
|
33
|
|
|
protected $concurrency = 10; |
|
34
|
|
|
|
|
35
|
|
|
/** @var bool $chunk */ |
|
36
|
|
|
protected $chunk = false; |
|
37
|
|
|
|
|
38
|
|
|
/** @var int|null */ |
|
39
|
|
|
protected $maximumCrawlCount = null; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @param string $urlToBeCrawled |
|
43
|
|
|
* |
|
44
|
|
|
* @return static |
|
45
|
|
|
*/ |
|
46
|
|
|
public static function create(string $urlToBeCrawled) |
|
47
|
|
|
{ |
|
48
|
|
|
return app(static::class)->setUrl($urlToBeCrawled); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
public function __construct(Crawler $crawler) |
|
52
|
|
|
{ |
|
53
|
|
|
$this->crawler = $crawler; |
|
54
|
|
|
|
|
55
|
|
|
$this->sitemaps = new Collection([new Sitemap]); |
|
56
|
|
|
|
|
57
|
|
|
$this->hasCrawled = function (Url $url, ResponseInterface $response = null) { |
|
|
|
|
|
|
58
|
|
|
return $url; |
|
59
|
|
|
}; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
public function setConcurrency(int $concurrency) |
|
63
|
|
|
{ |
|
64
|
|
|
$this->concurrency = $concurrency; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
public function setMaximumCrawlCount(int $maximumCrawlCount) |
|
68
|
|
|
{ |
|
69
|
|
|
$this->maximumCrawlCount = $maximumCrawlCount; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* Enable chunk |
|
74
|
|
|
* |
|
75
|
|
|
* @param int $chunk |
|
76
|
|
|
* @return self |
|
77
|
|
|
*/ |
|
78
|
|
|
public function setChunck(int $chunk = 50000) |
|
79
|
|
|
{ |
|
80
|
|
|
$this->chunk = $chunk; |
|
|
|
|
|
|
81
|
|
|
|
|
82
|
|
|
return $this; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
public function setUrl(string $urlToBeCrawled) |
|
86
|
|
|
{ |
|
87
|
|
|
$this->urlToBeCrawled = new Uri($urlToBeCrawled); |
|
88
|
|
|
|
|
89
|
|
|
if ($this->urlToBeCrawled->getPath() === '') { |
|
90
|
|
|
$this->urlToBeCrawled = $this->urlToBeCrawled->withPath('/'); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
return $this; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
public function shouldCrawl(callable $shouldCrawl) |
|
97
|
|
|
{ |
|
98
|
|
|
$this->shouldCrawl = $shouldCrawl; |
|
99
|
|
|
|
|
100
|
|
|
return $this; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
public function hasCrawled(callable $hasCrawled) |
|
104
|
|
|
{ |
|
105
|
|
|
$this->hasCrawled = $hasCrawled; |
|
106
|
|
|
|
|
107
|
|
|
return $this; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
public function getSitemap(): Sitemap |
|
111
|
|
|
{ |
|
112
|
|
|
if (config('sitemap.execute_javascript')) { |
|
113
|
|
|
$this->crawler->executeJavaScript(config('sitemap.chrome_binary_path')); |
|
|
|
|
|
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
if (! is_null($this->maximumCrawlCount)) { |
|
117
|
|
|
$this->crawler->setMaximumCrawlCount($this->maximumCrawlCount); |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
$this->crawler |
|
121
|
|
|
->setCrawlProfile($this->getCrawlProfile()) |
|
122
|
|
|
->setCrawlObserver($this->getCrawlObserver()) |
|
123
|
|
|
->setConcurrency($this->concurrency) |
|
124
|
|
|
->startCrawling($this->urlToBeCrawled); |
|
125
|
|
|
|
|
126
|
|
|
return $this->sitemaps->first(); |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* @param string $path |
|
131
|
|
|
* |
|
132
|
|
|
* @return $this |
|
133
|
|
|
*/ |
|
134
|
|
|
public function writeToFile(string $path) |
|
135
|
|
|
{ |
|
136
|
|
|
$sitemap = $this->getSitemap(); |
|
137
|
|
|
|
|
138
|
|
|
if ($this->chunk) { |
|
139
|
|
|
// Call the sitemap generation and process each created sitemap |
|
140
|
|
|
$index = SitemapIndex::create(); |
|
141
|
|
|
$format = preg_replace('/\.xml/', '_%d.xml', $path); |
|
142
|
|
|
$this->sitemaps->each(function (Sitemap $sitemap, int $key) use ($index, $format) { |
|
143
|
|
|
$path = sprintf($format, $key); |
|
144
|
|
|
|
|
145
|
|
|
$sitemap->writeToFile(sprintf($format, $key)); |
|
146
|
|
|
$index->add(last(explode('public', $path))); |
|
147
|
|
|
}); |
|
148
|
|
|
|
|
149
|
|
|
$index->writeToFile($path); |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
else { |
|
153
|
|
|
$sitemap->writeToFile($path); |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
return $this; |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
protected function getCrawlProfile(): CrawlProfile |
|
160
|
|
|
{ |
|
161
|
|
|
$shouldCrawl = function (UriInterface $url) { |
|
162
|
|
|
if ($url->getHost() !== $this->urlToBeCrawled->getHost()) { |
|
163
|
|
|
return false; |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
if (! is_callable($this->shouldCrawl)) { |
|
167
|
|
|
return true; |
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
|
|
return ($this->shouldCrawl)($url); |
|
171
|
|
|
}; |
|
172
|
|
|
|
|
173
|
|
|
$profileClass = config('sitemap.crawl_profile', Profile::class); |
|
174
|
|
|
$profile = new $profileClass($this->urlToBeCrawled); |
|
175
|
|
|
|
|
176
|
|
|
if (method_exists($profile, 'shouldCrawlCallback')) { |
|
177
|
|
|
$profile->shouldCrawlCallback($shouldCrawl); |
|
178
|
|
|
} |
|
179
|
|
|
|
|
180
|
|
|
return $profile; |
|
181
|
|
|
} |
|
182
|
|
|
|
|
183
|
|
|
protected function getCrawlObserver(): Observer |
|
184
|
|
|
{ |
|
185
|
|
|
$performAfterUrlHasBeenCrawled = function (UriInterface $crawlerUrl, ResponseInterface $response = null) { |
|
186
|
|
|
$sitemapUrl = ($this->hasCrawled)(Url::create((string) $crawlerUrl), $response); |
|
187
|
|
|
|
|
188
|
|
|
if ($this->chunk and count($this->sitemaps->first()->getTags()) >= $this->chunk) { |
|
|
|
|
|
|
189
|
|
|
$this->sitemaps->prepend(new Sitemap); |
|
190
|
|
|
} |
|
191
|
|
|
|
|
192
|
|
|
if ($sitemapUrl) { |
|
193
|
|
|
$this->sitemaps->first()->add($sitemapUrl); |
|
194
|
|
|
} |
|
195
|
|
|
}; |
|
196
|
|
|
|
|
197
|
|
|
return new Observer($performAfterUrlHasBeenCrawled); |
|
198
|
|
|
} |
|
199
|
|
|
} |
|
200
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.