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