1 | <?php
|
---|
2 |
|
---|
3 | /* RDFInt.php - RDF Interfaces for PHP
|
---|
4 | * Copyright 2011 netlabs.org
|
---|
5 | * Author: Christian Langanke, Adrian Gschwend
|
---|
6 | *
|
---|
7 | * Licensed under the Apache License, Version 2.0 (the "License");
|
---|
8 | * you may not use this file except in compliance with the License.
|
---|
9 | * You may obtain a copy of the License at
|
---|
10 | *
|
---|
11 | * http://www.apache.org/licenses/LICENSE-2.0
|
---|
12 | *
|
---|
13 | * Unless required by applicable law or agreed to in writing, software
|
---|
14 | * distributed under the License is distributed on an "AS IS" BASIS,
|
---|
15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
---|
16 | * See the License for the specific language governing permissions and
|
---|
17 | * limitations under the License.
|
---|
18 | */
|
---|
19 |
|
---|
20 | namespace rdfint_core;
|
---|
21 |
|
---|
22 |
|
---|
23 | class Core {
|
---|
24 |
|
---|
25 | private $usedLibrary = NULL;
|
---|
26 | private $aNamespace;
|
---|
27 |
|
---|
28 | // ---------------------------------------
|
---|
29 |
|
---|
30 | public function __construct() {
|
---|
31 |
|
---|
32 | // determ ine library to use
|
---|
33 | $this->usedLibrary = 'arc';
|
---|
34 |
|
---|
35 | $this->aNamespace = array();
|
---|
36 |
|
---|
37 | } // public function __construct
|
---|
38 |
|
---|
39 | // ---------------------------------------
|
---|
40 |
|
---|
41 | public function parse( $uri) {
|
---|
42 |
|
---|
43 | $newTriples = array();
|
---|
44 |
|
---|
45 | switch ($this->usedLibrary) {
|
---|
46 |
|
---|
47 | case 'arc':
|
---|
48 | $arcParser = \ARC2::getRDFParser();
|
---|
49 | $arcParser->parse( $uri);
|
---|
50 | $newTriples = $arcParser->getTriples();
|
---|
51 | break;
|
---|
52 |
|
---|
53 | }
|
---|
54 | return $newTriples;
|
---|
55 | }
|
---|
56 |
|
---|
57 | // ---------------------------------------
|
---|
58 |
|
---|
59 | public function serialize( $aTriples, $type) {
|
---|
60 |
|
---|
61 | $data = Null;
|
---|
62 |
|
---|
63 | switch ($this->usedLibrary) {
|
---|
64 |
|
---|
65 | case 'arc':
|
---|
66 | // set the supported types and shortcut them to the ones we use in there
|
---|
67 | $validTypes = array( 'application/rdf+xml' => 'rdfxml' ,
|
---|
68 | 'application/rdfxml' => 'rdfxml',
|
---|
69 | 'text/turtle' => 'turtle',
|
---|
70 | 'application/x-turtle' => 'turtle',
|
---|
71 | 'text/n3' => 'n3',
|
---|
72 | 'application/json' => 'json');
|
---|
73 |
|
---|
74 | // check if explicit mime type is specified
|
---|
75 | if (isset( $validTypes[ $type])) {
|
---|
76 | $type = $validTypes[ $type];
|
---|
77 | } else {
|
---|
78 | // check if internal type is given
|
---|
79 | $internalTypes = array_unique( array_flip( $validTypes));
|
---|
80 | if (!isset( $internalTypes[ $type])) {
|
---|
81 | $data = false;
|
---|
82 | break;
|
---|
83 | }
|
---|
84 | }
|
---|
85 |
|
---|
86 | switch ($type) {
|
---|
87 |
|
---|
88 | case 'rdfxml': $ser = \ARC2::getRDFXMLSerializer(); break;
|
---|
89 | case 'turtle': $ser = \ARC2::getTurtleSerializer(); break;
|
---|
90 | case 'n3': $ser = \ARC2::getNTriplesSerializer(); break;
|
---|
91 | case 'json': $ser = \ARC2::getRDFJSONSerializer(); break;
|
---|
92 |
|
---|
93 | } // switch ($type)
|
---|
94 |
|
---|
95 | $data = $ser->getSerializedTriples( $aTriples);
|
---|
96 |
|
---|
97 | break;
|
---|
98 | }
|
---|
99 | return $data;
|
---|
100 | }
|
---|
101 |
|
---|
102 | // ---------------------------------------
|
---|
103 |
|
---|
104 | public function runQuery( $config, $statement) {
|
---|
105 |
|
---|
106 | $result = Null;
|
---|
107 |
|
---|
108 | switch ($this->usedLibrary) {
|
---|
109 |
|
---|
110 | case 'arc':
|
---|
111 |
|
---|
112 | $store = \ARC2::getRemoteStore( $config);
|
---|
113 | $index = $store->query( $statement, 'raw');
|
---|
114 |
|
---|
115 | if ((is_array( $index) && (is_array( $index[ "errors"]))))
|
---|
116 | $result = false;
|
---|
117 | else
|
---|
118 | $result = \ARC2::getTriplesFromIndex( $index);
|
---|
119 | break;
|
---|
120 |
|
---|
121 | }
|
---|
122 | return $result;
|
---|
123 |
|
---|
124 | }
|
---|
125 |
|
---|
126 | // ---------------------------------------
|
---|
127 |
|
---|
128 | public function filterTriples( $aTriples,
|
---|
129 | $subject = NULL,
|
---|
130 | $predicate = NULL,
|
---|
131 | $object = NULL) {
|
---|
132 |
|
---|
133 | $searchTriple = array ( 's' => $subject, 'p' => $predicate, 'o' => $object);
|
---|
134 |
|
---|
135 | // resolve namespace prefixes
|
---|
136 | $uriSubject = $this->resolve( $subject);
|
---|
137 | $uriPredicate = $this->resolve( $predicate);
|
---|
138 | $uriObject = $this->resolve( $object);
|
---|
139 |
|
---|
140 | // resolve method may return NULL, then use original value
|
---|
141 | $uriSubject = ($uriSubject === NULL) ? $subject : $uriSubject;
|
---|
142 | $uriPredicate = ($uriPredicate === NULL) ? $predicate : $uriPredicate;
|
---|
143 | $uriObject = ($uriObject === NULL) ? $object : $uriObject;
|
---|
144 |
|
---|
145 | // filter all available triples
|
---|
146 | $aresult = array();
|
---|
147 | foreach ($aTriples as $aTriple) {
|
---|
148 |
|
---|
149 | // must be triple
|
---|
150 | if ((isset( $aTriple[ 'type'])) &&
|
---|
151 | ($aTriple[ 'type'] != 'triple'))
|
---|
152 | continue;
|
---|
153 |
|
---|
154 | // check subject and predicate match if given
|
---|
155 | if (($uriSubject != NULL) && (strcmp( $uriSubject, $aTriple[ 's'])))
|
---|
156 | continue;
|
---|
157 |
|
---|
158 | if (($uriPredicate != NULL) && (strcmp( $uriPredicate, $aTriple[ 'p'])))
|
---|
159 | continue;
|
---|
160 |
|
---|
161 | // check object if given
|
---|
162 | if ($uriObject != NULL) {
|
---|
163 |
|
---|
164 | // check object match
|
---|
165 | if (strcmp( $uriObject, $aTriple[ 'o']))
|
---|
166 | continue;
|
---|
167 |
|
---|
168 | } // if ($uriObject != NULL) {
|
---|
169 |
|
---|
170 | // store in result array
|
---|
171 | $aresult[] = $aTriple;
|
---|
172 |
|
---|
173 | } // foreach ($aTriples as $aTriple)
|
---|
174 |
|
---|
175 | return $aresult;
|
---|
176 |
|
---|
177 | } // static function filterTriples
|
---|
178 |
|
---|
179 | // ---------------------------------------
|
---|
180 |
|
---|
181 | public function setMapping( $prefix, $uriNamespace) {
|
---|
182 |
|
---|
183 | if ($prefix == '')
|
---|
184 | return false;
|
---|
185 |
|
---|
186 | // add colon to prefix as internally req.
|
---|
187 | if (substr( $prefix, -1) != ':')
|
---|
188 | $prefix = "$prefix:";
|
---|
189 |
|
---|
190 | if (isset( $this->aNamespace[ $prefix])) {
|
---|
191 | $oldUriNamespace = $this->aNamespace[ $prefix];
|
---|
192 | if ($oldUriNamespace == $uriNamespace)
|
---|
193 | $debugmessage = "Namespace mapping exists: @prefix $prefix <$uriNamespace> .";
|
---|
194 | else
|
---|
195 | $debugmessage = "Namespace mapping overwritten: @prefix $prefix <$uriNamespace> .\n" .
|
---|
196 | "Old mapping was: @prefix $prefix <$oldUriNamespace>";
|
---|
197 | } else {
|
---|
198 | $debugmessage = "Namespace mapping added: @prefix $prefix <$uriNamespace> .";
|
---|
199 | }
|
---|
200 |
|
---|
201 | // set URI
|
---|
202 | $this->aNamespace[ $prefix] = $uriNamespace;
|
---|
203 | return $debugmessage;
|
---|
204 |
|
---|
205 | } // public function setMapping
|
---|
206 |
|
---|
207 | // ---------------------------------------
|
---|
208 |
|
---|
209 | public function resolve( $curie) {
|
---|
210 |
|
---|
211 | $replacecount = 1;
|
---|
212 | $uri = NULL;
|
---|
213 | if ($curie != NULL) {
|
---|
214 | if ($this->aNamespace != NULL) {
|
---|
215 | if ((strpos( $uri, ":/") !== false) ||
|
---|
216 | (strpos( $uri, "_:") === 0)) {
|
---|
217 | $uri = $curi;
|
---|
218 | } else {
|
---|
219 | // check for namespaces
|
---|
220 | foreach ($this->aNamespace as $prefix => $uriNamespace) {
|
---|
221 | // check for prefix match
|
---|
222 | $posPrefix = strpos( $curie, $prefix);
|
---|
223 | if ($posPrefix === 0) {
|
---|
224 | // replace prefix and bail out
|
---|
225 | $uri = str_replace( $prefix, $uriNamespace, $curie, $replacecount);
|
---|
226 | break;
|
---|
227 | }
|
---|
228 | } // foreach ($this->aNamespace
|
---|
229 | } // if ((strpos ...
|
---|
230 | } // if ($aNamespace != NULL) {
|
---|
231 | } // if ($uri != NULL) {
|
---|
232 |
|
---|
233 | return $uri;
|
---|
234 |
|
---|
235 | } // public function resolve
|
---|
236 |
|
---|
237 | // ---------------------------------------
|
---|
238 |
|
---|
239 | public function shrink( $uri) {
|
---|
240 |
|
---|
241 | $replacecount = 1;
|
---|
242 | if ($uri != NULL) {
|
---|
243 | if ($this->aNamespace != NULL) {
|
---|
244 | if (strpos( $uri, ":/") !== false) {
|
---|
245 | foreach ($this->aNamespace as $prefix => $uriNamespace) {
|
---|
246 | // search namespace URI
|
---|
247 | $posNamespace = strpos( $uri, $uriNamespace);
|
---|
248 | if ($posNamespace === false)
|
---|
249 | continue;
|
---|
250 | // replace namespace URI and bail out
|
---|
251 | $uri = str_replace( $uriNamespace, $prefix, $uri, &$replacecount);
|
---|
252 | break;
|
---|
253 | } // foreach ($aNamespace
|
---|
254 | } // if (strpos( $uri, ":/") !== false)
|
---|
255 | } // if ($aNamespace != NULL) {
|
---|
256 | } // if ($uri != NULL) {
|
---|
257 |
|
---|
258 | return $uri;
|
---|
259 |
|
---|
260 | } // public function shrink
|
---|
261 |
|
---|
262 |
|
---|
263 | } // class Core
|
---|
264 |
|
---|
265 | ?>
|
---|