forked from caresetjournal/AHRQ_search_clone
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgo_wayback.function.php
More file actions
159 lines (108 loc) · 3.88 KB
/
Copy pathgo_wayback.function.php
File metadata and controls
159 lines (108 loc) · 3.88 KB
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
<?php
/*
This is a generic function to download data from the wayback machine...
First you start with some urls that hopefully might have been backed up..
Then we will check to see if there are actually saved copies in
wayback machine and then try and download the latest copy..
*/
if(get_included_files()[0] == __FILE__){
$test_urls = [
'https://www.guideline.gov/summaries/summary/51178/cataracts-in-adults-management',
'https://www.guideline.gov/summaries/summary/51178/cataracts-in-adults-management-this-one-is-missing',
];
foreach($test_urls as $this_url){
$results = go_wayback($this_url, './tmp');
$saved_to_file = $results['saved_to_file'];
$when = $results['timestamp'];
echo "$this_url mirrored $when saved to $file_save_as\n";
}
}
function go_wayback($file_url = null,$save_to_dir = null){
$fail_results = [
'saved_to_file' => false,
'timestamp' => false,
'is_status_good' => false,
];
if(is_null($file_url) || is_null($save_to_dir)){
echo "Error: I really need a list of urls and a place where to save the archives on the local disk";
exit();
}
$file_links = [$file_url];
$arrayOfLines = [];
foreach($file_links as $this_file_link){
$this_file_link_encoded = urlencode($this_file_link);
$archive_list = "http://web.archive.org/cdx/search/cdx?url=$this_file_link_encoded";
$str = file_get_contents($archive_list);
//from: http://stackoverflow.com/a/28725803/144364
$this_arrayOfLines = explode("\n",
str_replace(array("\r\n","\n\r","\r"),"\n",$str)
);
$arrayOfLines = array_merge($arrayOfLines, $this_arrayOfLines);
}
if(count($arrayOfLines) == 0){
//this means that wayback machine has no record of this url...
$fail_results['error_message'] = "Wayback machine has nothing";
return($fail_results);
}
$last_timestamp = 0;
$total_to_do = count($arrayOfLines);
$i = 0;
foreach($arrayOfLines as $file_string){
if(strlen($file_string) == 0){
continue;
}
$i++;
$string_array = explode(' ',$file_string);
//get the data points we need from the string array...
$timestamp = $string_array[1];
if($timestamp > $last_timestamp){
//we only care if this is an html file...
if($string_array[3] == 'text/html'){
//then our assumption is wrong, this is a zip file
$lastest_line = $file_string;
$last_timestamp = $timestamp;
}
}
}
if(!isset($lastest_line)){
$fail_results['error_message'] = "Could not find an latest_line probably returned a blank";
return($fail_results);
}
$string_array = explode(' ',$lastest_line);
$file_url = $string_array[2];
//and finally use the timestamp and the file_url to calculate the download url for the file on archive.org
$download_url = "https://web.archive.org/web/$last_timestamp/$file_url";
$get_file = true;
//use native php functions to calculate the file name from the file_url
//this lets us future proof...
$path = parse_url($file_url, PHP_URL_PATH);
$path_parts = pathinfo($path);
$file_name = $path_parts['basename'];
$file_name = substr($file_name,0,254); //file names can be too long..
$save_to_file = $save_to_dir . '/' . $file_name;
if(file_exists($save_to_file)){
//why would we download it again?
echo "We already downloaded $save_to_file\n";
$get_file = false;
if(filesize($save_to_file) == 0){
//then the download failed last time..
$get_file = true;
echo "But it is zero length... downloading again\n";
}
}
if($get_file){
$wget_command = "wget -O $save_to_file $download_url";
echo "getting\n $download_url\n to\n $save_to_file\n";
exec($wget_command);
$left = $total_to_do - $i;
echo "done. $left left..\n";
//be respectful to the api...
sleep(1);
}
$results = [
'saved_to_file' => $save_to_file,
'timestamp' => $last_timestamp,
'is_status_good' => true,
];
return($results);
}//end function