3 * ARC2 core class (static, not instantiated)
5 * @author Benjamin Nowack
6 * @license <http://arc.semsol.org/license>
7 * @homepage <http://arc.semsol.org/>
14 function getVersion() {
20 function setStatic($val) {
21 static $arc_static = '';
22 if ($val) $arc_static = $val; /* set */
23 if (!$val) return $arc_static; /* get */
26 function getStatic() {
27 return ARC2::setStatic('');
32 function getIncPath($f = '') {
33 $r = realpath(dirname(__FILE__)) . '/';
35 'plugin' => 'plugins',
36 'trigger' => 'triggers',
38 'serializer' => 'serializers',
39 'extractor' => 'extractors',
40 'sparqlscript' => 'sparqlscript',
41 'parser' => 'parsers',
43 foreach ($dirs as $k => $dir) {
44 if (preg_match('/' . $k . '/i', $f)) {
45 return $r . $dir . '/';
51 function getScriptURI() {
52 if (isset($_SERVER) && isset($_SERVER['SERVER_NAME'])) {
53 $proto = preg_replace('/^([a-z]+)\/.*$/', '\\1', strtolower($_SERVER['SERVER_PROTOCOL']));
54 $port = $_SERVER['SERVER_PORT'];
55 $server = $_SERVER['SERVER_NAME'];
56 $script = $_SERVER['SCRIPT_NAME'];
58 if (($proto == 'http') && $port == 443) {
62 return $proto . '://' . $server . ($port != 80 ? ':' . $port : '') . $script;
64 return preg_replace('/^([a-z]+)\/.*$/', '\\1', strtolower($_SERVER['SERVER_PROTOCOL'])) .
65 '://' . $_SERVER['SERVER_NAME'] .
66 ($_SERVER['SERVER_PORT'] != 80 ? ':' . $_SERVER['SERVER_PORT'] : '') .
67 $_SERVER['SCRIPT_NAME'];
70 elseif (isset($_SERVER['SCRIPT_FILENAME'])) {
71 return 'file://' . realpath($_SERVER['SCRIPT_FILENAME']);
73 return 'http://localhost/unknown_path';
76 function getRequestURI() {
77 if (isset($_SERVER) && isset($_SERVER['REQUEST_URI'])) {
78 return preg_replace('/^([a-z]+)\/.*$/', '\\1', strtolower($_SERVER['SERVER_PROTOCOL'])) .
79 '://' . $_SERVER['SERVER_NAME'] .
80 ($_SERVER['SERVER_PORT'] != 80 ? ':' . $_SERVER['SERVER_PORT'] : '') .
81 $_SERVER['REQUEST_URI'];
83 return ARC2::getScriptURI();
86 function inc($f, $path = '') {
88 if (preg_match('/^([^\_]+)\_(.*)$/', $f, $m)) {
92 $inc_path = $path ? $path : ARC2::getIncPath($f);
93 $path = $inc_path . $prefix . '_' . urlencode($f) . '.php';
94 if (file_exists($path)) return include_once($path);
96 if (@include_once($path)) return 1;
98 if ($prefix != 'ARC2') {
99 $path = $inc_path . strtolower($prefix) . '/' . $prefix . '_' . urlencode($f) . '.php';
100 if (file_exists($path)) return include_once($path);
102 if (@include_once($path)) return 1;
110 list($msec, $sec) = explode(" ", microtime());
111 return ((float)$msec + (float)$sec);
114 function x($re, $v, $options = 'si') {
115 return preg_match("/^\s*" . $re . "(.*)$/" . $options, $v, $m) ? $m : false;
120 function getFormat($val, $mtype = '', $ext = '') {
121 ARC2::inc('getFormat');
122 return ARC2_getFormat($val, $mtype, $ext);
125 function getPreferredFormat($default = 'plain') {
126 ARC2::inc('getPreferredFormat');
127 return ARC2_getPreferredFormat($default);
132 function toUTF8($v) {
133 if (urlencode($v) === $v) return $v;
134 //if (utf8_decode($v) == $v) return $v;
135 $v = (strpos(utf8_decode(str_replace('?', '', $v)), '?') === false) ? utf8_decode($v) : $v;
136 /* custom hacks, mainly caused by bugs in PHP's json_decode */
149 $froms = array_keys($mappings);
150 $tos = array_values($mappings);
151 foreach ($froms as $i => $from) $froms[$i] = urldecode($from);
152 $v = str_replace($froms, $tos, $v);
154 return preg_replace_callback('/([\x00-\xdf][\x80-\xbf]|[\xe0-\xef][\x80-\xbf]{2}|[\xf0-\xf7][\x80-\xbf]{3}|[\xf8-\xfb][\x80-\xbf]{4}|[\xfc-\xfd][\x80-\xbf]{5}|[^\x00-\x7f])/', array('ARC2', 'getUTF8Char'), $v);
157 function getUTF8Char($v) {
159 if (strlen(trim($val)) === 1) return utf8_encode($val);
160 if (preg_match('/^([\x00-\x7f])(.+)/', $val, $m)) return $m[1] . ARC2::toUTF8($m[2]);
166 function splitURI($v) {
167 /* the following namespaces may lead to conflated URIs,
168 * we have to set the split position manually
170 if (strpos($v, 'www.w3.org')) {
172 'http://www.w3.org/XML/1998/namespace',
173 'http://www.w3.org/2005/Atom',
174 'http://www.w3.org/1999/xhtml',
176 foreach ($specials as $ns) {
177 if (strpos($v, $ns) === 0) {
178 $local_part = substr($v, strlen($ns));
179 if (!preg_match('/^[\/\#]/', $local_part)) {
180 return array($ns, $local_part);
185 /* auto-splitting on / or # */
186 //$re = '^(.*?)([A-Z_a-z][-A-Z_a-z0-9.]*)$';
187 if (preg_match('/^(.*[\/\#])([^\/\#]+)$/', $v, $m)) return array($m[1], $m[2]);
188 /* auto-splitting on last special char, e.g. urn:foo:bar */
189 if (preg_match('/^(.*[\:\/])([^\:\/]+)$/', $v, $m)) return array($m[1], $m[2]);
190 return array($v, '');
195 function getSimpleIndex($triples, $flatten_objects = 1, $vals = '') {
197 foreach ($triples as $t) {
199 foreach (array('s', 'p', 'o') as $term) {
202 if (isset($t[$term . '_type']) && ($t[$term . '_type'] == 'var')) {
203 $val = isset($vals[$$term]) ? $vals[$$term] : '';
204 $skip_t = isset($vals[$$term]) ? $skip_t : 1;
206 $type = !$type && isset($vals[$$term . ' type']) ? $vals[$$term . ' type'] : $type;
207 $type = !$type && preg_match('/^\_\:/', $val) ? 'bnode' : $type;
209 $type = !$type && (preg_match('/\s/s', $val) || !preg_match('/\:/', $val)) ? 'literal' : $type;
210 $type = !$type && !preg_match('/[\/]/', $val) ? 'literal' : $type;
212 $type = !$type ? 'uri' : $type;
213 $t[$term . '_type'] = $type;
220 if (!isset($r[$s])) $r[$s] = array();
221 if (!isset($r[$s][$p])) $r[$s][$p] = array();
222 if ($flatten_objects) {
223 if (!in_array($o, $r[$s][$p])) $r[$s][$p][] = $o;
226 $o = array('value' => $o);
227 foreach (array('lang', 'type', 'datatype') as $suffix) {
228 if (isset($t['o_' . $suffix]) && $t['o_' . $suffix]) {
229 $o[$suffix] = $t['o_' . $suffix];
231 elseif (isset($t['o ' . $suffix]) && $t['o ' . $suffix]) {
232 $o[$suffix] = $t['o ' . $suffix];
235 if (!in_array($o, $r[$s][$p])) {
243 function getTriplesFromIndex($index) {
245 foreach ($index as $s => $ps) {
246 foreach ($ps as $p => $os) {
247 foreach ($os as $o) {
252 's_type' => preg_match('/^\_\:/', $s) ? 'bnode' : 'uri',
253 'o_type' => $o['type'],
254 'o_datatype' => isset($o['datatype']) ? $o['datatype'] : '',
255 'o_lang' => isset($o['lang']) ? $o['lang'] : '',
263 function getMergedIndex() {
265 foreach (func_get_args() as $index) {
266 foreach ($index as $s => $ps) {
267 if (!isset($r[$s])) $r[$s] = array();
268 foreach ($ps as $p => $os) {
269 if (!isset($r[$s][$p])) $r[$s][$p] = array();
270 foreach ($os as $o) {
271 if (!in_array($o, $r[$s][$p])) {
281 function getCleanedIndex() {/* removes triples from a given index */
282 $indexes = func_get_args();
284 for ($i = 1, $i_max = count($indexes); $i < $i_max; $i++) {
285 $index = $indexes[$i];
286 foreach ($index as $s => $ps) {
287 if (!isset($r[$s])) continue;
288 foreach ($ps as $p => $os) {
289 if (!isset($r[$s][$p])) continue;
292 foreach ($r_os as $r_o) {
293 $r_o_val = is_array($r_o) ? $r_o['value'] : $r_o;
295 foreach ($os as $o) {
296 $del_o_val = is_array($o) ? $o['value'] : $o;
297 if ($del_o_val == $r_o_val) {
307 $r[$s][$p] = $new_os;
317 foreach ($r as $s => $ps) {
323 return $has_data ? $r : array();
328 function getStructType($v) {
330 if (is_string($v)) return 'string';
331 /* flat array, numeric keys */
332 if (in_array(0, array_keys($v))) {/* numeric keys */
334 if (!is_array($v[0])) return 'array';
336 //if (isset($v[0]) && isset($v[0]['s']) && isset($v[0]['p'])) return 'triples';
337 if (in_array('p', array_keys($v[0]))) return 'triples';
339 /* associative array */
342 foreach ($v as $s => $ps) {
343 if (!is_array($ps)) break;
344 foreach ($ps as $p => $os) {
345 if (!is_array($os) || !is_array($os[0])) break;
346 if (in_array('value', array_keys($os[0]))) return 'index';
356 function getComponent($name, $a = '', $caller = '') {
359 if (preg_match('/^([^\_]+)\_(.+)$/', $name, $m)) {
363 $cls = $prefix . '_' . $name;
364 if (!$caller) $caller = new stdClass();
365 return new $cls($a, $caller);
370 function getResource($a = '') {
371 return ARC2::getComponent('Resource', $a);
376 function getReader($a = '') {
377 return ARC2::getComponent('Reader', $a);
382 function getParser($prefix, $a = '') {
383 return ARC2::getComponent($prefix . 'Parser', $a);
386 function getRDFParser($a = '') {
387 return ARC2::getParser('RDF', $a);
390 function getRDFXMLParser($a = '') {
391 return ARC2::getParser('RDFXML', $a);
394 function getTurtleParser($a = '') {
395 return ARC2::getParser('Turtle', $a);
398 function getRSSParser($a = '') {
399 return ARC2::getParser('RSS', $a);
402 function getSemHTMLParser($a = '') {
403 return ARC2::getParser('SemHTML', $a);
406 function getSPARQLParser($a = '') {
407 return ARC2::getComponent('SPARQLParser', $a);
410 function getSPARQLPlusParser($a = '') {
411 return ARC2::getParser('SPARQLPlus', $a);
414 function getSPARQLXMLResultParser($a = '') {
415 return ARC2::getParser('SPARQLXMLResult', $a);
418 function getJSONParser($a = '') {
419 return ARC2::getParser('JSON', $a);
422 function getSGAJSONParser($a = '') {
423 return ARC2::getParser('SGAJSON', $a);
426 function getCBJSONParser($a = '') {
427 return ARC2::getParser('CBJSON', $a);
430 function getSPARQLScriptParser($a = '') {
431 return ARC2::getParser('SPARQLScript', $a);
436 function getStore($a = '', $caller = '') {
437 return ARC2::getComponent('Store', $a, $caller);
440 function getStoreEndpoint($a = '', $caller = '') {
441 return ARC2::getComponent('StoreEndpoint', $a, $caller);
444 function getRemoteStore($a = '', $caller = '') {
445 return ARC2::getComponent('RemoteStore', $a, $caller);
448 function getMemStore($a = '') {
449 return ARC2::getComponent('MemStore', $a);
454 function getSer($prefix, $a = '') {
455 return ARC2::getComponent($prefix . 'Serializer', $a);
458 function getTurtleSerializer($a = '') {
459 return ARC2::getSer('Turtle', $a);
462 function getRDFXMLSerializer($a = '') {
463 return ARC2::getSer('RDFXML', $a);
466 function getNTriplesSerializer($a = '') {
467 return ARC2::getSer('NTriples', $a);
470 function getRDFJSONSerializer($a = '') {
471 return ARC2::getSer('RDFJSON', $a);
474 function getPOSHRDFSerializer($a = '') {/* deprecated */
475 return ARC2::getSer('POSHRDF', $a);
478 function getMicroRDFSerializer($a = '') {
479 return ARC2::getSer('MicroRDF', $a);
482 function getRSS10Serializer($a = '') {
483 return ARC2::getSer('RSS10', $a);
488 function getSPARQLScriptProcessor($a = '') {
489 return ARC2::getComponent('SPARQLScriptProcessor', $a);