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