-
Notifications
You must be signed in to change notification settings - Fork 0
/
rdfhash
executable file
·66 lines (55 loc) · 1.48 KB
/
rdfhash
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
#!/usr/bin/env php -q
<?php
namespace RdfHash;
use RdfHash\RdfHashing;
use EasyRdf\Graph;
require("./vendor/autoload.php");
function arguments($argv) {
$_ARG = array();
foreach ($argv as $arg) {
if (preg_match('/--([^=]+)=(.*)/',$arg,$reg)) {
$_ARG[$reg[1]] = $reg[2];
} elseif(preg_match('/--?([a-zA-Z0-9]+)/',$arg,$reg)) {
$_ARG[$reg[1]] = 'true';
}
}
return $_ARG;
}
function printHelpAndExit($message = null) {
if (!is_null($message)) {
print($message);
}
print("\nUsage: rdfhash --source\n");
print(" --source : file or url of rdf source\n");
exit;
}
function getFile($filename) {
$filename = realpath($filename);
if (file_exists($filename) && is_readable($filename)) {
$graph = new \EasyRdf\Graph();
$graph->parseFile($filename);
return $graph;
}
throw new \Exception("File not found or un-readable");
}
function getUrl($source) {
$graph = new \EasyRdf\Graph();
$graph->load($source);
return $graph;
}
$arguments = arguments($argv);
if (!isset($arguments['source'])) {
printHelpAndExit("You must provide a source path or url\n");
}
if (strpos($arguments['source'], 'http') === 0) {
$graph = getUrl($arguments['source']);
} else {
$graph = getFile($arguments['source']);
}
if (isset($arguments['debug'])) {
$string = RdfHashing::getGraphString($graph);
print($string . "\n");
}
$hash = RdfHashing::calculate($graph);
print($hash);
exit;