This repository has been archived by the owner on Nov 21, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathparseLogMap.php
129 lines (107 loc) · 4.2 KB
/
parseLogMap.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
<?php
if (php_sapi_name() == 'cli') {
echo "Script cannot run in CLI\n";
exit;
}
// A proxy for the EMBL-EBI Live Data Map
// fetch KML and return as simple CSV
// the available sources
$dataSources = array(
"ebi" => "http://ves-ebi-0f.ebi.ac.uk/ebiwebtrafficmap/fcgi-bin/ebi.fcgi",
"portals" => "http://ves-ebi-0f.ebi.ac.uk/ebiwebtrafficmap/fcgi-bin/portals.fcgi",
"uniprot" => "http://ves-ebi-0f.ebi.ac.uk/ebiwebtrafficmap/fcgi-bin/uniprot.fcgi",
);
// No automatic caching ...
header("Cache-Control: no-store, no-cache, must-revalidate, max-age=0");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");
$isMonitor = strpos($_SERVER['HTTP_USER_AGENT'], 'StatusCake') !== FALSE;
$isEbi = strpos(parse_url($_SERVER['HTTP_REFERER'], PHP_URL_HOST), 'ebi.ac.uk') !== FALSE;
// Don't allow direct use of the file except StatusCake, and don't allow non-EBI hosted use of file
if (!$isMonitor && !$isEbi) {
http_response_code(403);
exit;
}
// Which file(s) is the requester after?
$desiredFile = isset($_GET['file']) ? htmlspecialchars($_GET["file"]) : '';
// what's been requested
switch ($desiredFile) {
case 'all':
default:
$desiredFile = 'all'; // cleanup wild url requests
$desiredFiles = array($dataSources["ebi"],$dataSources["portals"],$dataSources["uniprot"]);
$pointType = array('markerClustersEBI','markerClustersPortals','markerClustersUniprot');
break;
case 'ebi':
$desiredFiles = array($dataSources[$desiredFile]);
$pointType = array('markerClustersEBI');
break;
case 'portals':
$desiredFiles = array($dataSources[$desiredFile]);
$pointType = array('markerClustersPortals');
break;
case 'uniprot':
$desiredFiles = array($dataSources[$desiredFile]);
$pointType = array('markerClustersUniprot');
break;
}
// set up a local file based cache
$cacheFileName = 'cache/' . $desiredFile . '.csv';
// is the data already cahced?
if (file_exists($cacheFileName)) {
// use cache if less than XXX seconds old
if (time() - filemtime($cacheFileName) < 6) {
header("Map-cache-creation: ". urlencode(filemtime($cacheFileName)));
header("Map-cache-server-time: ". urlencode(time()));
require $cacheFileName;
exit; // dump the cahced contents and go home
}
}
// OK then, we're getting new data...
// Function to fetch the file
// We use curl as we can't use nicer functions on our infrastructure
function curlFileAsXml($url) {
$curl_handle=curl_init();
curl_setopt($curl_handle,CURLOPT_URL,$url);
curl_setopt($curl_handle,CURLOPT_CONNECTTIMEOUT,2);
curl_setopt($curl_handle,CURLOPT_RETURNTRANSFER,1);
$buffer = curl_exec($curl_handle);
$return_code = curl_getinfo($curl_handle, CURLINFO_HTTP_CODE);
curl_close($curl_handle);
if (empty($buffer) || $return_code !== 200) {
echo "Data source returning error code: $return_code\n";
http_response_code(204);
exit;
} else {
return simplexml_load_string($buffer);
}
}
// Reusable function to fuzzy-ify the lat/long string
// in: 0.7000,51.5333,0
// out: 0.70,51.53,0
function lowerPrecision($incoming) {
$processed = explode(",", $incoming);
$processed[0] = round($processed[0], 2);
$processed[1] = round($processed[1], 2);
$output = $processed[0] . "," . $processed[1] . "," . $processed[2];
return $output;
}
// Write the data CSV
// -----------
// CSV header
$output = "target-layer,longitude,latitude,point-type \r\n";
// to do: in the future, we could add a column to indicate the number of times a lat/lon combination appears so we can add N number of dots instead of one at a time. Would offer over the wire savings, and perhaps speed up the JS
foreach ($desiredFiles as $key => $desiredFile) {
// Get the data
$fetchedData = curlFileAsXml($desiredFile);
// Results
$rowCounter = 0;
foreach($fetchedData as $item) {
$rowCounter++;
$output .= $pointType[$key] . "," . lowerPrecision($item->Point->coordinates) . "\r\n";
}
}
// write the new contents
echo $output;
// save to the cache
file_put_contents($cacheFileName, $output);