-
Notifications
You must be signed in to change notification settings - Fork 0
/
ResyncResourcelist.php
260 lines (217 loc) · 8.93 KB
/
ResyncResourcelist.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
<?php
include_once('util/http.php');
include_once('util/file.php');
include_once('ResyncURL.php');
class ResyncResourcelist {
// The URL of the resource list
public $url;
// The urlset raw XML
private $xml;
// The sitemapindex raw XML
private $sitemapxml;
// Is the top level file a sitemapindex or urlset
private $sitemap = false;
// URLs of files processed
private $urls;
// How many files downloaded
private $downloadcount = 0;
// How many files skipped
private $skipcount = 0;
// How long the downloads took (in seconds)
private $downloadtimer = 0;
// How large the downloads were (in kilobytes)
private $downloadsize = 0;
// The callback function to run after each download
private $callback;
// Whether or not to display debug information
private $debug = false;
// Whether the debugging messages are for display in HTML or not
private $htmldebug = false;
// Create the new resourcelist
function __construct($url) {
$this->url = $url;
$xmllist = http_get($this->url);
$xml = simplexml_load_string($xmllist);
// Is this a sitemapindex or a urlset?
if ($xml->getName() == 'sitemapindex') {
$this->sitemap = true;
$this->sitemapxml = $xml;
} else {
$this->sitemap = false;
$this->xml = $xml;
}
}
// Register a callback function for each URL downloaded
public function registerCallback($callback) {
$this->callback = $callback;
}
// Baseline sync (download everything)
// $directory = Where to store the files
// $from = Date of last run
// $clear = Whether to remove all files before running (normally false)
// $checksum = Whether to check file checksums (normally true)
// $force = Whether to force the download of files, even if they haven't changed (normally false)
// $pretend = Pretend to download files - useful for testing large syncs (normally false)
function baseline($directory, $from = '', $clear = false, $checksum = true, $force = false, $pretend = false) {
// Start the timer
$starttime = microtime(true);
// First clear the directory?
if ($clear) {
rmdir_recursive($directory, false);
}
$this->urls = array();
// Was a date given?
if ($from == '') {
$from = new DateTime("0000-01-01T01:00:00Z", new DateTimeZone("UTC"));
}
// Do we need to first process a sitemapindex?
if ($this->sitemap) {
$this->processSitemapindex($this->sitemapxml, $directory, $from, $checksum, $force, $pretend);
} else {
$this->processUrlset($directory, $from, $checksum, $force, $pretend);
}
// End the timer
$endtime = microtime(true);
$this->downloadtimer = $endtime - $starttime;
}
private function processSitemapindex($sitemapxml, $directory, $from, $checksum = true, $force = false, $pretend = false) {
// Sitemap counter
$total = count($sitemapxml->sitemap);
$count = 1;
// Iterate through each sitemap->loc
foreach($sitemapxml->sitemap as $url) {
$this->debug('Processing sitemap (' . $count++ . ' of ' . $total . '): ' . $url->loc);
$xmllist = http_get($url->loc);
$xml = simplexml_load_string($xmllist);
// Is this a sitemapindex or a urlset?
if ($xml->getName() == 'sitemapindex') {
$this->processSitemapindex($xml, $directory, $from, $checksum, $force, $pretend);
} else {
$this->xml = $xml;
$this->processUrlset($directory, $from, $checksum, $force, $pretend);
}
}
}
private function processUrlset($directory, $from, $checksum = true, $force = false, $pretend = false) {
// Namespace handling
$namespaces = $this->xml->getNameSpaces(true);
if (!isset($namespaces['sm'])) $sac_ns['sm'] = 'http://www.sitemaps.org/schemas/sitemap/0.9';
$urls = $this->xml->children($namespaces['sm'])->url;
// File counter
$total = count($urls);
$count = 1;
// Iterate through each url
foreach($urls as $url) {
$this->debug('Processing file (' . $count++ . ' of ' . $total . '): ' . $url->loc);
// Create the directory if required
$parts = explode('/', substr($url->loc, 7));
$build = $directory;
for ($p = 0; $p < count($parts) - 1; $p++) {
$build = $build . '/' . $parts[$p];
if (!file_exists($build)) {
mkdir($build);
$this->debug(' - Adding directory: ' . $build);
}
}
$build .= '/' . $parts[count($parts) - 1];
// Unencode the checksum
$rs = $url->children($namespaces['rs']);
$hash = (string)$rs->md[0]->attributes()->hash;
$hashexists = true;
if (!empty($hash)) {
$algorithm = substr($hash, 0, strpos($hash, ':'));
$cksum = substr($hash, strpos($hash, ':') + 1, (strlen($hash) - strlen($algorithm) - 1));
} else {
$this->debug(' - Missing file hash!');
$hashexists = false;
}
// Does the file exist already?
$exists = false;
if (file_exists($build) && (! $force) && (! $pretend) && ($hashexists)) {
// Does the checksum match? If so, we can skip the download later
$md5 = base64_encode(md5_file($build, true));
$this->debug(' - File already exists, check the checksum:');
if ($md5 == $cksum) {
$exists = true;
$this->debug(' - Checksum matches, skipping...');
$this->skipcount++;
} else {
$this->debug(' - Checksum does not match, not skipping');
}
}
// Check if file has been updated since last run?
$lastmod = new DateTime($url->lastmod, new DateTimeZone("UTC"));
if (($lastmod > $from) && (! $exists)) {
$this->debug(' - Downloading file: ' . $build);
if (! $pretend) {
$start = microtime(true);
http_get_save($url->loc, $build);
$filesize = filesize($build);
$end = microtime(true);
$timer = $end - $start;
$speed = $filesize / $timer;
$this->debug(' - Filesize: ' . $filesize . ' Time: ' . $timer . ' Speed: ' . $speed);
$this->downloadsize += $filesize;
} else {
$this->debug(' - PRETEND mode: not really downloading file');
}
$this->downloadcount++;
$this->urls[(string)$url->loc] = "created";
} else if ($exists) {
} else {
$this->debug(' - Skipping! lastmod: ' . $lastmod->format('Y-m-d H:i:s') .
' is before last run: ' . $from->format('Y-m-d H:i:s'));
$this->skipcount++;
}
// Checksum the file?
if (($checksum) && (!$exists) && (! $pretend) && $hashexists) {
$md5 = base64_encode(md5_file($build, true));
$this->debug(' - Checking checksum: ' . $algorithm . ' with value of ' . $cksum);
if ($md5 == $cksum) {
$this->debug(' - MD5 matched: Expected:' . $cksum . ' Actual:' . $md5);
} else {
echo ' - ERROR MD5 mismatch: Expected:' . $cksum . ' Actual:' . $md5 . "\n";
}
}
// Run the callback method
if (!empty($this->callback)) {
call_user_func($this->callback, $build, new ResyncURL($url->loc, $url, $build));
}
}
}
// Return the number of downloaded files
function getDownloadedFileCount() {
return $this->downloadcount;
}
// Return the number of skipped files
function getSkippedFileCount() {
return $this->skipcount;
}
// Return the total download size
function getDownloadSize() {
return $this->downloadsize;
}
// Return the total download duration
function getDownloadDuration() {
return $this->downloadtimer;
}
// Return the URLs processed
function getURLs() {
return $this->urls;
}
// Whether to display debug messages or not
function enableDebug($debug = true, $html = false) {
$this->debug = $debug;
$this->htmldebug = $html;
}
// Display a debug message
private function debug($message) {
if ($this->debug) echo $message . "\n";
if ($this->htmldebug) {
echo "<br />\n";
flush();
ob_flush();
}
}
}
?>