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
|
|
|
public function getFolder() |
33
|
|
|
{ |
34
|
|
|
return $this->folder; |
35
|
|
|
} |
36
|
|
|
|
37
|
4 |
|
public function cache(Harvest $harvest, Url $url) |
38
|
|
|
{ |
39
|
4 |
|
if (Recorder::CACHE_NONE === $this->cacheMethod || !$this->mustWeCache($harvest)) { |
40
|
|
|
return; |
41
|
4 |
|
} elseif (Recorder::CACHE_URI === $this->cacheMethod) { |
42
|
2 |
|
return $this->cacheWithUrlAsFilename($harvest, $url); |
43
|
|
|
} else { |
44
|
2 |
|
return $this->cacheWithIdAsFilename($harvest, $url); |
45
|
|
|
} |
46
|
|
|
} |
47
|
|
|
|
48
|
4 |
|
protected function mustWeCache(Harvest $harvest) |
49
|
|
|
{ |
50
|
4 |
|
return false !== strpos($harvest->getResponse()->getContentType(), 'text/html'); |
51
|
|
|
} |
52
|
|
|
|
53
|
2 |
|
protected function cacheWithIdAsFilename(Harvest $harvest, Url $url) |
54
|
|
|
{ |
55
|
2 |
|
return file_put_contents( |
56
|
2 |
|
$this->folder.Recorder::CACHE_DIR.'/'.$url->id, |
|
|
|
|
57
|
2 |
|
$harvest->getResponse()->getHeaders(false).PHP_EOL.PHP_EOL.$harvest->getResponse()->getContent() |
58
|
|
|
); |
59
|
|
|
} |
60
|
|
|
|
61
|
2 |
|
protected function cacheWithUrlAsFilename(Harvest $harvest, Url $url) |
|
|
|
|
62
|
|
|
{ |
63
|
2 |
|
$url = trim($harvest->getAbsoluteInternalLink($harvest->getResponse()->getEffectiveUrl()), '/').'/'; |
64
|
2 |
|
$urlPart = explode('/', $url); |
65
|
2 |
|
$folder = $this->folder.Recorder::CACHE_DIR; |
66
|
|
|
|
67
|
2 |
|
$urlPartLenght = count($urlPart); |
68
|
2 |
|
for ($i = 0; $i < $urlPartLenght; ++$i) { |
69
|
2 |
|
if ($i == $urlPartLenght - 1) { |
70
|
2 |
|
$filename = empty($urlPart[$i]) ? 'index.html' : $urlPart[$i]; |
71
|
|
|
|
72
|
2 |
|
return file_put_contents( |
73
|
2 |
|
$folder.'/'.$filename, |
74
|
2 |
|
$harvest->getResponse()->getHeaders(false).PHP_EOL.PHP_EOL.$harvest->getResponse()->getContent() |
75
|
|
|
); |
76
|
|
|
} else { |
77
|
2 |
|
$folder .= '/'.$urlPart[$i]; |
78
|
2 |
|
if (!file_exists($folder) || !is_dir($folder)) { |
79
|
|
|
mkdir($folder); |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|
85
|
6 |
|
public function record(array $urls) |
86
|
|
|
{ |
87
|
6 |
|
$fp = fopen($this->folder.'/index.csv', 'w'); |
88
|
|
|
|
89
|
6 |
|
if (false !== $fp) { |
90
|
6 |
|
$header = array_keys(get_object_vars(array_values($urls)[0])); |
91
|
6 |
|
fputcsv($fp, $header); |
92
|
|
|
|
93
|
6 |
|
foreach ($urls as $url) { |
94
|
6 |
|
fputcsv($fp, get_object_vars($url)); |
95
|
|
|
} |
96
|
|
|
|
97
|
6 |
|
fclose($fp); |
98
|
|
|
|
99
|
6 |
|
return true; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
return false; |
103
|
|
|
} |
104
|
|
|
|
105
|
4 |
|
public function recordInboundLink(Url $from, Url $to) |
106
|
|
|
{ |
107
|
4 |
|
file_put_contents($this->folder.Recorder::LINKS_DIR.'/To_'.$to->id.'.txt', $from->uri, FILE_APPEND); |
|
|
|
|
108
|
4 |
|
} |
109
|
|
|
|
110
|
4 |
|
public function recordOutboundLink(Url $from, array $links) |
111
|
|
|
{ |
112
|
|
|
$links = array_map(function ($link) { |
113
|
4 |
|
return $link->getUrl(); |
114
|
4 |
|
}, $links); |
115
|
4 |
|
file_put_contents($this->folder.Recorder::LINKS_DIR.'/From_'.$from->id.'.txt', implode(PHP_EOL, $links)); |
|
|
|
|
116
|
4 |
|
} |
117
|
|
|
} |
118
|
|
|
|