|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace PiedWeb\UrlHarvester; |
|
4
|
|
|
|
|
5
|
|
|
trait HarvestLinksTrait |
|
6
|
|
|
{ |
|
7
|
|
|
/** |
|
8
|
|
|
* @var array |
|
9
|
|
|
*/ |
|
10
|
|
|
protected $links; |
|
11
|
|
|
protected $selfs = []; |
|
12
|
|
|
protected $internals = []; |
|
13
|
|
|
protected $subs = []; |
|
14
|
|
|
protected $externals = []; |
|
15
|
|
|
|
|
16
|
|
|
private $domainWithScheme; |
|
17
|
|
|
|
|
18
|
|
|
abstract public function getDom(); |
|
19
|
|
|
|
|
20
|
|
|
abstract public function getDomain(); |
|
21
|
|
|
|
|
22
|
|
|
public function getLinkedRessources() |
|
23
|
|
|
{ |
|
24
|
|
|
return ExtractLinks::get($this->getDom(), $this->response->getEffectiveUrl(), ExtractLinks::SELECT_ALL); |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
3 |
|
public function getLinks($type = null) |
|
28
|
|
|
{ |
|
29
|
3 |
|
if (null === $this->links) { |
|
30
|
3 |
|
$this->links = ExtractLinks::get($this->getDom(), $this->response->getEffectiveUrl()); |
|
31
|
3 |
|
$this->classifyLinks(); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
3 |
|
switch ($type) { |
|
35
|
3 |
|
case 'self': |
|
36
|
3 |
|
return $this->selfs; |
|
37
|
3 |
|
case 'internal': |
|
38
|
3 |
|
return $this->internals; |
|
39
|
3 |
|
case 'sub': |
|
40
|
3 |
|
return $this->subs; |
|
41
|
3 |
|
case 'external': |
|
42
|
3 |
|
return $this->externals; |
|
43
|
|
|
default: |
|
44
|
3 |
|
return $this->links; |
|
45
|
|
|
} |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
3 |
|
public function getDomainAndScheme() |
|
49
|
|
|
{ |
|
50
|
3 |
|
if (null === $this->domainWithScheme) { |
|
51
|
3 |
|
$url = parse_url($this->response->getEffectiveUrl()); |
|
52
|
3 |
|
$this->domainWithScheme = $url['scheme'].'://'.$url['host']; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
3 |
|
return $this->domainWithScheme; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
3 |
|
public function classifyLinks() |
|
59
|
|
|
{ |
|
60
|
3 |
|
$links = $this->getLinks(); |
|
61
|
|
|
|
|
62
|
3 |
|
foreach ($links as $link) { |
|
63
|
3 |
|
$urlParsed = parse_url($link->getUrl()); |
|
64
|
|
|
|
|
65
|
3 |
|
if (preg_match('/^'.preg_quote($this->getDomainAndScheme().'/', '/').'/si', $link->getUrl().'/')) { |
|
66
|
3 |
|
if (preg_replace('/(\#.*)/si', '', $link->getUrl()) == $this->response->getEffectiveUrl()) { |
|
67
|
3 |
|
$this->selfs[] = $link; |
|
68
|
|
|
} else { |
|
69
|
3 |
|
$this->internals[] = $link; |
|
70
|
|
|
} |
|
71
|
3 |
|
} elseif (preg_match('/'.preg_quote($this->getDomain(), '/').'$/si', $urlParsed['host'])) { |
|
72
|
3 |
|
$this->subs[] = $link; |
|
73
|
|
|
} else { |
|
74
|
3 |
|
$this->externals[] = $link; |
|
75
|
|
|
} |
|
76
|
|
|
} |
|
77
|
3 |
|
} |
|
78
|
|
|
} |
|
79
|
|
|
|