This repository has been archived by the owner on Jun 11, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 51
/
owadmin.php
218 lines (189 loc) · 5.72 KB
/
owadmin.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
<?php
define('SCRIPT_DIR', dirname(__FILE__) . DIRECTORY_SEPARATOR);
if (!defined('APPLICATION_PATH')) {
define('APPLICATION_PATH', realpath(dirname(__FILE__) . '/../application'));
}
set_include_path(implode(PATH_SEPARATOR, array(
APPLICATION_PATH . '/../libraries',
get_include_path(),
)));
require_once 'Zend/Loader/Autoloader.php';
Zend_Loader_Autoloader::getInstance();
// Define some CLI options
$getopt = new Zend_Console_Getopt(array(
'virtload' => 'Import files in data directory into Virtuoso',
'virtclear' => 'Clear a specified graph',
'help|h' => 'Help -- usage message',
));
try {
$getopt->parse();
} catch (Zend_Console_Getopt_Exception $e) {
// Bad options passed: report usage
echo $e->getUsageMessage();
return false;
}
// If help requested, report usage message
if ($getopt->getOption('h')) {
echo $getopt->getUsageMessage();
return true;
} else if ($getopt->getOption('virtload')) {
return _importData();
} else if ($getopt->getOption('virtclear')) {
return _clearGraph();
} else {
echo $getopt->getUsageMessage();
return true;
}
return true;
/* Functions */
function _clearGraph()
{
$graph = _chooseGraph();
if (!$graph) {
return false;
}
$command = "isql VERBOSE=OFF \"EXEC=sparql DELETE FROM <$graph> {?s ?p ?o} WHERE {?s ?p ?o}\" 2>&1 1> /dev/null";
$output = shell_exec($command);
if (null !== $output) {
return false;
}
echo "DONE!" . PHP_EOL;
return true;
}
function _importData()
{
// Check for files
$importFiles = array();
$files = scandir(SCRIPT_DIR . 'data');
foreach ($files as $file) {
if ($file[0] === '.') {
continue;
}
$importFiles[$file] = SCRIPT_DIR . 'data' . DIRECTORY_SEPARATOR . $file;
}
$failed = false;
$failReason = '';
foreach ($importFiles as $file=>$fullPath) {
$ending = substr($file, -4);
switch ($ending) {
case '.ttl':
break;
case '.owl':
case '.rdf':
$newPath = SCRIPT_DIR . 'tmp' . DIRECTORY_SEPARATOR . 'sourceNtriples.ttl';
$cmd = "rapper -gqo ntriples '$fullPath' 2>&1 1> /dev/null > '$newPath'";
$output = shell_exec($cmd);
if (null !== $output) {
echo 'Error while converting source file to TTL.';
exit;
}
$fullPath = $newPath;
break;
default:
continue;
}
echo "Preparing import of file $file now." . PHP_EOL;
$graph = _chooseGraph();
if (!$graph) {
$failed = true;
$failReason = 'Invalid Graph!';
break;
}
echo "Will import data into graph <$graph>." . PHP_EOL;
$splitFiles = _splitFiles($fullPath);
$count = count($splitFiles);
foreach ($splitFiles as $i=>$splitFile) {
$progress = round(($i/$count)*100.0);
echo "\rImporting $file now: $progress%";
if (!$failed) {
$result = _importFile($splitFile, $graph);
}
unlink($splitFile);
if (!$result) {
$failed = true;
$failReason = 'Import failed';
}
}
// Move file to done dir!
if (!$failed) {
copy($fullPath, SCRIPT_DIR.'done'.DIRECTORY_SEPARATOR.$file);
unlink($fullPath);
}
echo PHP_EOL;
}
if ($failed) {
echo $failReason . PHP_EOL;
} else {
echo 'DONE!' . PHP_EOL;
}
return true;
}
function _splitFiles($fullPath)
{
$splitFiles = array();
$fHandle = fopen($fullPath, 'r');
$fileCount = 0;
$i = 0;
$currentContent = '';
if ($fHandle) {
while ($line = fgets($fHandle)) {
$currentContent .= $line;
$i++;
if ($i === 10000) {
$tmp = SCRIPT_DIR . 'tmp' . DIRECTORY_SEPARATOR . $fileCount++;
file_put_contents($tmp, $currentContent);
$splitFiles[] = $tmp;
$i = 0;
$currentContent = '';
}
}
if ($currentContent !== '') {
$tmp = SCRIPT_DIR . 'tmp' . DIRECTORY_SEPARATOR . $fileCount++;
file_put_contents($tmp, $currentContent);
$splitFiles[] = $tmp;
}
}
fclose($fHandle);
return $splitFiles;
}
function _chooseGraph()
{
$command = 'isql VERBOSE=OFF "EXEC=SELECT ID_TO_IRI(REC_GRAPH_IID) AS GRAPH FROM DB.DBA.RDF_EXPLICITLY_CREATED_GRAPH"';
$output = shell_exec($command);
$outputLines = explode("\n", $output);
$startReached = false;
$graphs = array();
foreach ($outputLines as $line) {
$trimmedLine = trim($line);
if ($trimmedLine === '') {
continue;
}
if (strpos($trimmedLine, '______________________________') !== false) {
$startReached = true;
continue;
}
if ($startReached) {
$graphs[] = $line;
}
}
echo "Choose a graph:\n";
foreach ($graphs as $i=>$g) {
echo " ($i) $g" . PHP_EOL;
}
echo "Just type in the number: ";
$input = intval(trim(fgets(STDIN)));
if (isset($graphs[$input])) {
return $graphs[$input];
}
return false;
}
function _importFile($file, $graph)
{
$command = "isql 1111 dba dba \"EXEC=TTLP(file_to_string_output('$file'), '', '$graph', 255)\" 2>&1 1> /dev/null";
$output = shell_exec($command);
if (null !== $output) {
var_dump($output);exit;
return false;
}
return true;
}