-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrdfx.import.inc
65 lines (59 loc) · 1.72 KB
/
rdfx.import.inc
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
<?php
// $Id$
/**
* @file
* Functions for importing and parsing RDF data.
*/
/**
* Loads an RDF file from an HTTP URI or local file, parses it, and builds an
* RDF schema representation (associative array) from it.
*
* @param $uri
* The namespace URI for this graph.
* @param $prefix
* The prefix for this graph.
* @param $filename (optional)
* A local RDF file to parse.
* @return array
* @throws Exception On network or parse error
*/
function rdfx_fetch_rdf($uri, $prefix, $filename = NULL) {
if ($filename != NULL) {
if (!is_file($filename)) {
throw new Exception("File not found: '$filename'");
return;
}
$content = file_get_contents($filename);
return _rdfx_parse_rdf($uri, $content);
}
$schema_url = $uri;
$i = strpos($schema_url, '#');
if ($i !== false) {
$schema_url = substr($schema_url, 0, $i);
}
return _rdfx_parse_rdf($uri);
}
function _rdfx_parse_rdf($base_uri) {
$namespaces = array();
include_once(drupal_get_path('module', 'rdfx') . '/vendor/arc/ARC2.php');
$parser = ARC2::getRDFParser();
$parser->parse($base_uri);
// If this is an N3 file, the namespaces array isn't populated. Iterate
// through the attached parser object's prefixes and remove the colon from
// the end of the prefix.
// @todo File an issue with ARC2.
switch ($parser->format) {
case 'turtle':
foreach ($parser->parser->prefixes as $prefix => $uri) {
$formatted_prefix = str_replace(':', '', $prefix);
$namespaces[$formatted_prefix] = $uri;
}
break;
default:
foreach ($parser->parser->nsp as $uri => $prefix) {
$namespaces[$prefix] = $uri;
}
break;
}
return array($parser->getTriples(), $namespaces);
}