-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.php
140 lines (113 loc) · 4.05 KB
/
index.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
<?php
require_once __DIR__ . '/lib/web.php/web.php';
require_once __DIR__ . '/lib/LibRDF/LibRDF/LibRDF.php';
define('REPO', __DIR__ . '/resource/');
$routes = array(
'/resource/:id' => 'ResourceController',
);
class ResourceController extends WebController {
public function PUT($id) {
$ntriples = file_get_contents("php://input");
$ntriples_parser = new LibRDF_Parser('ntriples');
$rdf_model = new LibRDF_Model(new LibRDF_Storage());
$rdf_serializer = new LibRDF_Serializer('ntriples');
try {
$rdf_model->loadStatementsFromString($ntriples_parser, $ntriples);
$this->_response->writeHead(200, array());
} catch (LibRDF_Error $e) {
$this->_response->writeHead(400, array());
}
$path_info = pathinfo(REPO.$id);
if (!array_key_exists('extension', $path_info)) {
$path_info['extension'] = 'nt';
}
$path = "{$path_info['dirname']}/{$path_info['filename']}.{$path_info['extension']}";
$rdf_model->serializeStatementsToFile($rdf_serializer, $path);
$this->__commit(
$path_info['dirname'],
"{$path_info['filename']}.{$path_info['extension']}",
"Add {$path_info['filename']}.{$path_info['extension']}"
);
$this->_response->terminate();
}
public function PATCH($id) {
$path_info = pathinfo(REPO.$id);
if (!array_key_exists('extension', $path_info)) {
$path_info['extension'] = 'nt';
}
$path = "{$path_info['dirname']}/{$path_info['filename']}.{$path_info['extension']}";
if (file_exists($path)) {
$diff_data = fopen("php://input", 'r');
try {
$this->__apply($diff_data, $path);
$this->_response->writeHead(204, array(
"Content-Location" => "{$path_info['filename']}.{$path_info['extension']}"
));
} catch (LibRDF_Error $e) {
// Conflicting state
$this->_response->writeHead(409, array());
$this->_response->write($e->getMessage());
}
$this->__commit(
$path_info['dirname'],
"{$path_info['filename']}.{$path_info['extension']}",
"Update {$path_info['filename']}.{$path_info['extension']}"
);
} else {
// Resource not found
$this->_response->writeHead(404, array());
}
$this->_response->terminate();
}
public function DELETE($id) {
$path_info = pathinfo(REPO.$id);
if (!array_key_exists('extension', $path_info)) {
$path_info['extension'] = 'nt';
}
$path = "{$path_info['dirname']}/{$path_info['filename']}.{$path_info['extension']}";
if (file_exists($path)) {
unlink($path);
} else {
// Resource not found
$this->_response->writeHead(404, array());
}
}
private function __apply($diff, $path) {
$ntriples_parser = new LibRDF_Parser('ntriples');
$rdf_model = new LibRDF_Model(new LibRDF_Storage());
$ntriples = file_get_contents($path);
$rdf_model->loadStatementsFromString($ntriples_parser, $ntriples);
$removed_ntriples = new LibRDF_Model(new LibRDF_Storage());
$added_ntriples = new LibRDF_Model(new LibRDF_Storage());
while($diff_line = fgets($diff)) {
$op = substr($diff_line, 0, 1);
$ntriple = substr($diff_line, 1);
switch($op) {
case '+':
$added_ntriples->loadStatementsFromString($ntriples_parser, $ntriple);
break;
case '-':
$removed_ntriples->loadStatementsFromString($ntriples_parser, $ntriple);
break;
}
}
foreach ($added_ntriples as $added_triple) {
$rdf_model->addStatement($added_triple);
}
foreach ($removed_ntriples as $removed_triple) {
$rdf_model->removeStatement($removed_triple);
}
$rdf_serializer = new LibRDF_Serializer('ntriples');
$rdf_model->serializeStatementsToFile($rdf_serializer, $path);
}
private function __commit($repo_dir, $file_name, $commit_message) {
$command = "cd $repo_dir"
. " && git add $file_name"
. " && git commit "
. " --author='Author Name <[email protected]>'"
. " -m '$commit_message'";
exec($command);
}
}
$app = new WebApp();
$app->dispatch($routes);