|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace PiedWeb\UrlHarvester; |
|
4
|
|
|
|
|
5
|
|
|
trait HarvestLinksTrait |
|
6
|
|
|
{ |
|
7
|
|
|
/** |
|
8
|
|
|
* @var array |
|
9
|
|
|
*/ |
|
10
|
|
|
protected $links; |
|
11
|
|
|
protected $linksPerType; |
|
12
|
|
|
|
|
13
|
|
|
abstract public function getDom(); |
|
14
|
|
|
|
|
15
|
|
|
abstract public function getDomain(); |
|
16
|
|
|
|
|
17
|
3 |
|
public function getLinkedRessources() |
|
18
|
|
|
{ |
|
19
|
3 |
|
return ExtractLinks::get($this->getDom(), $this->response->getEffectiveUrl(), ExtractLinks::SELECT_ALL); |
|
20
|
|
|
} |
|
21
|
|
|
|
|
22
|
3 |
|
public function getLinks($type = null) |
|
23
|
|
|
{ |
|
24
|
3 |
|
if (null === $this->links) { |
|
25
|
3 |
|
$this->links = ExtractLinks::get($this->getDom(), $this->response->getEffectiveUrl()); |
|
26
|
3 |
|
$this->classifyLinks(); |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
3 |
|
switch ($type) { |
|
30
|
|
|
case Harvest::LINK_SELF: |
|
31
|
3 |
|
return $this->linksPerType[Harvest::LINK_SELF] ?? []; |
|
32
|
|
|
case Harvest::LINK_INTERNAL: |
|
33
|
3 |
|
return $this->linksPerType[Harvest::LINK_INTERNAL] ?? []; |
|
34
|
|
|
case Harvest::LINK_SUB: |
|
35
|
3 |
|
return $this->linksPerType[Harvest::LINK_SUB] ?? []; |
|
36
|
|
|
case Harvest::LINK_EXTERNAL: |
|
37
|
3 |
|
return $this->linksPerType[Harvest::LINK_EXTERNAL] ?? []; |
|
38
|
|
|
default: |
|
39
|
3 |
|
return $this->links; |
|
40
|
|
|
} |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
3 |
|
public function getNbrDuplicateLinks() |
|
44
|
|
|
{ |
|
45
|
3 |
|
$links = $this->getLinks(); |
|
46
|
3 |
|
$u = []; |
|
47
|
3 |
|
foreach ($links as $link) { |
|
48
|
3 |
|
$u[$link->getUrl()] = 1; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
3 |
|
return count($links) - count($u); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
abstract public function getDomainAndScheme(); |
|
55
|
|
|
|
|
56
|
3 |
|
public function classifyLinks() |
|
57
|
|
|
{ |
|
58
|
3 |
|
$links = $this->getLinks(); |
|
59
|
|
|
|
|
60
|
3 |
|
foreach ($links as $link) { |
|
61
|
3 |
|
$type = $this->getType($link->getPageUrl()); |
|
62
|
3 |
|
$this->linksPerType[$type][] = $link; |
|
63
|
|
|
} |
|
64
|
3 |
|
} |
|
65
|
|
|
|
|
66
|
3 |
|
public function isInternalType(string $url) |
|
67
|
|
|
{ |
|
68
|
3 |
|
return 0 === strpos($url, $this->getDomainAndScheme()); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
public function isSubType(string $host) |
|
72
|
|
|
{ |
|
73
|
|
|
return strtolower(substr($host, -strlen($this->getDomain()))) === $this->getDomain(); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
3 |
|
public function isSelfType(string $url) |
|
77
|
|
|
{ |
|
78
|
3 |
|
if (0 !== strpos($url, '#')) { |
|
79
|
3 |
|
$url = substr($url, 0, -(strlen(parse_url($url, PHP_URL_FRAGMENT)) + 1)); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
3 |
|
return $this->isInternalType($url) && $url == $this->response->getEffectiveUrl(); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
3 |
|
public function getType(string $url): string |
|
86
|
|
|
{ |
|
87
|
3 |
|
if ($this->isSelfType($url)) { |
|
88
|
|
|
return Harvest::LINK_SELF; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
3 |
|
if ($this->isInternalType($url)) { |
|
92
|
3 |
|
return Harvest::LINK_INTERNAL; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
$host = parse_url($url, PHP_URL_HOST); |
|
96
|
|
|
if ($host && $this->isSubType($host)) { |
|
97
|
|
|
return Harvest::LINK_SUB; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
return Harvest::LINK_EXTERNAL; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
3 |
|
public function getAbsoluteInternalLink(string $url) |
|
104
|
|
|
{ |
|
105
|
3 |
|
return substr($url, strlen($this->getDomainAndScheme())); |
|
106
|
|
|
} |
|
107
|
|
|
} |
|
108
|
|
|
|