|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace PiedWeb\SeoPocketCrawler; |
|
4
|
|
|
|
|
5
|
|
|
use PiedWeb\UrlHarvester\Harvest; |
|
6
|
|
|
|
|
7
|
|
|
class Recorder |
|
8
|
|
|
{ |
|
9
|
|
|
const LINKS_DIR = '/links'; |
|
10
|
|
|
const CACHE_DIR = '/cache'; |
|
11
|
|
|
|
|
12
|
|
|
const CACHE_NONE = 0; |
|
13
|
|
|
const CACHE_ID = 2; |
|
14
|
|
|
const CACHE_URI = 1; |
|
15
|
|
|
|
|
16
|
|
|
protected $folder; |
|
17
|
|
|
protected $cacheMethod; |
|
18
|
|
|
|
|
19
|
6 |
|
public function __construct($folder, $cacheMethod = Record::CACHE_ID) |
|
20
|
|
|
{ |
|
21
|
6 |
|
$this->folder = $folder; |
|
22
|
6 |
|
$this->cacheMethod = $cacheMethod; |
|
23
|
|
|
|
|
24
|
|
|
//exec('rm -rf '.$folder); |
|
25
|
6 |
|
if (!file_exists($folder)) { |
|
26
|
3 |
|
mkdir($folder); |
|
27
|
3 |
|
mkdir($folder.Recorder::LINKS_DIR); |
|
28
|
3 |
|
mkdir($folder.Recorder::CACHE_DIR); |
|
29
|
|
|
} |
|
30
|
6 |
|
} |
|
31
|
|
|
|
|
32
|
4 |
|
public function cache(Harvest $harvest, Url $url) |
|
33
|
|
|
{ |
|
34
|
4 |
|
if (Recorder::CACHE_NONE === $this->cacheMethod || !$this->mustWeCache($harvest)) { |
|
35
|
|
|
return; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
4 |
|
$filePath = $this->getCacheFilePath($url); |
|
39
|
4 |
|
if (!file_exists($filePath)) { |
|
40
|
4 |
|
file_put_contents( |
|
41
|
4 |
|
$filePath, |
|
42
|
4 |
|
$harvest->getResponse()->getHeaders(false).PHP_EOL.PHP_EOL.$harvest->getResponse()->getContent() |
|
43
|
|
|
); |
|
44
|
|
|
|
|
45
|
4 |
|
return file_put_contents($filePath.'---info', json_encode($harvest->getResponse()->getInfo())); |
|
46
|
|
|
} |
|
47
|
2 |
|
} |
|
48
|
|
|
|
|
49
|
5 |
|
public function getCacheFilePath(Url $url) |
|
50
|
|
|
{ |
|
51
|
5 |
|
if (Recorder::CACHE_URI === $this->cacheMethod) { |
|
52
|
3 |
|
return $this->getCacheFilePathWithUrlAsFilename($url); |
|
53
|
|
|
} else { |
|
54
|
2 |
|
return $this->getCacheFilePathWithIdAsFilename($url); |
|
55
|
|
|
} |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
3 |
|
protected function getCacheFilePathWithUrlAsFilename(Url $url) |
|
59
|
|
|
{ |
|
60
|
3 |
|
$url = trim($url->uri, '/').'/'; |
|
61
|
3 |
|
$urlPart = explode('/', $url); |
|
62
|
3 |
|
$folder = $this->folder.Recorder::CACHE_DIR; |
|
63
|
|
|
|
|
64
|
3 |
|
$urlPartLenght = count($urlPart); |
|
65
|
3 |
|
for ($i = 0; $i < $urlPartLenght; ++$i) { |
|
66
|
3 |
|
if ($i == $urlPartLenght - 1) { |
|
67
|
3 |
|
return $folder.'/'.(empty($urlPart[$i]) ? 'index.html' : $urlPart[$i]); |
|
68
|
|
|
} else { |
|
69
|
3 |
|
$folder .= '/'.$urlPart[$i]; |
|
70
|
3 |
|
if (!file_exists($folder) || !is_dir($folder)) { |
|
71
|
|
|
mkdir($folder); |
|
72
|
|
|
} |
|
73
|
|
|
} |
|
74
|
|
|
} |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
2 |
|
protected function getCacheFilePathWithIdAsFilename(Url $url) |
|
78
|
|
|
{ |
|
79
|
2 |
|
return $this->folder.Recorder::CACHE_DIR.'/'.(string) $url->id; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
4 |
|
protected function mustWeCache(Harvest $harvest) |
|
83
|
|
|
{ |
|
84
|
4 |
|
return false !== strpos($harvest->getResponse()->getContentType(), 'text/html'); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
6 |
|
public function record(array $urls) |
|
88
|
|
|
{ |
|
89
|
6 |
|
$fp = fopen($this->folder.'/index.csv', 'w'); |
|
90
|
|
|
|
|
91
|
6 |
|
if (false !== $fp) { |
|
92
|
6 |
|
$header = array_keys(get_object_vars(array_values($urls)[0])); |
|
93
|
6 |
|
fputcsv($fp, $header); |
|
94
|
|
|
|
|
95
|
6 |
|
foreach ($urls as $url) { |
|
96
|
6 |
|
fputcsv($fp, get_object_vars($url)); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
6 |
|
fclose($fp); |
|
100
|
|
|
|
|
101
|
6 |
|
return true; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
return false; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
4 |
|
public function recordInboundLink(Url $from, Url $to) |
|
108
|
|
|
{ |
|
109
|
4 |
|
file_put_contents($this->folder.Recorder::LINKS_DIR.'/To_'.(string) $to->id, $from->uri.PHP_EOL, FILE_APPEND); |
|
110
|
4 |
|
} |
|
111
|
|
|
|
|
112
|
4 |
|
public function recordOutboundLink(Url $from, array $links) |
|
113
|
|
|
{ |
|
114
|
|
|
$links = array_map(function ($link) { |
|
115
|
4 |
|
return $link->getUrl(); |
|
116
|
4 |
|
}, $links); |
|
117
|
4 |
|
file_put_contents($this->folder.Recorder::LINKS_DIR.'/From_'.(string) $from->id, implode(PHP_EOL, $links)); |
|
118
|
4 |
|
} |
|
119
|
|
|
} |
|
120
|
|
|
|