-
Notifications
You must be signed in to change notification settings - Fork 1
/
updater.php
113 lines (107 loc) · 3.1 KB
/
updater.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
<?php
//if (php_sapi_name() !== 'cli') exit(1);
define('DATAREG_HOME', 'http://www.mksmart.org/datareg/');
define('ENDPOINT', 'http://localhost:4444/catalogue/data');
// define('PROXY','wwwcache.open.ac.uk:80');
include 'lib/translation.inc';
global $argv;
function arg($index){
global $argv;
if(array_key_exists($index, $argv)){
$arg = $argv[$index];
}else{
throw new Exception("Missing argument: " . $index);
}
return $arg;
}
function downloadJson($p){
$ch = curl_init();
$url = DATAREG_HOME . '?p=' . $p . '&json=1';
curl_setopt($ch, CURLOPT_URL, $url );
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt ($ch, CURLOPT_TIMEOUT, 60);
$json = curl_exec ($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close ($ch);
if(strpos("$httpCode", '2') !== 0){
throw new Exception("Cannot download json. HTTP " . $httpCode . ' '. strpos($httpCode, '2') . ' '. $json);
}
$o = json_decode($json);
if(!isset($o->post)){
throw new Exception("Bad Json response: \nURL: $url \nResponse: " . $json ."\n");
}
return $o;
}
function getGraphName($type, $p){
if($type == 'mksdc-datasets'){
return dataset($p);
}else if($type == 'mksdc-policies'){
return policy($p);
}
}
function put_ntriples($ntriples, $graphname) {
/** use a max of 256KB of RAM before going to disk */
$fp = fopen('php://temp/maxmemory:256000', 'w');
if (!$fp) {
throw new Exception('Could not open temp memory data! PUT failed.');
}
fwrite($fp, $ntriples);
fseek($fp, 0);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, ENDPOINT . '?graph=' . urlencode($graphname) );
curl_setopt($ch, CURLOPT_PUT, 1);
// curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: text/plain; charset=utf-8'));
curl_setopt($ch, CURLOPT_INFILE, $fp); // file pointer
curl_setopt($ch, CURLOPT_INFILESIZE, strlen($ntriples));
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$out = curl_exec ($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close ($ch);
if(strpos($httpCode, '2') !== 0){
throw new Exception("Cannot PUT dataset. HTTP " . $httpCode . ' ' . $out . "\n" . "=====\n$body\n=====");
}
}
function put_json($json, $type, $id){
$rdf = translate($json->post);
put_ntriples($rdf, $type, $id);
}
function put($type, $p){
$json = downloadJson($p);
$rdf = translate($json->post); // from lib/translation.inc
// PUT
$body = $rdf;
put_raw($body, $type, $p);
}
function delete($type, $p){
// Not implemente yet
throw new Exception ("Not implemented yet :(");
}
function updater($action, $type, $p){
if($action == 'put'){
put($type, $p);
}else if ($action == 'delete'){
delete($type, $p);
}else{
throw new Exception("Unsupported action: " . $action);
}
}
function main(){
try {
echo 'Disabled';
//echo updater(arg(1), arg(2), arg(3));
exit(0);
} catch (Exception $e) {
echo $e->getMessage();
echo "\n";
echo $e->getTraceAsString();
echo "\n";
exit(1);
}
}
if (php_sapi_name() === 'cli'){
main();
}