PageRenderTime 41ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/docroot/transform/app/arc/ARC2.php

https://github.com/mterenzio/FollowThis
PHP | 450 lines | 349 code | 64 blank | 37 comment | 64 complexity | 8b1b9028e9c0b9d2f2a14cf0aedcbe13 MD5 | raw file
  1. <?php
  2. /**
  3. * ARC2 core class (static, not instantiated)
  4. *
  5. * @author Benjamin Nowack
  6. * @license <http://arc.semsol.org/license>
  7. * @homepage <http://arc.semsol.org/>
  8. * @package ARC2
  9. * @version 2010-04-26
  10. */
  11. class ARC2 {
  12. function getVersion() {
  13. return '2010-04-26';
  14. }
  15. /* */
  16. function setStatic($val) {
  17. static $arc_static = '';
  18. if ($val) $arc_static = $val; /* set */
  19. if (!$val) return $arc_static; /* get */
  20. }
  21. function getStatic() {
  22. return ARC2::setStatic('');
  23. }
  24. /* */
  25. function getIncPath($f = '') {
  26. $r = realpath(dirname(__FILE__)) . '/';
  27. $dirs = array(
  28. 'plugin' => 'plugins',
  29. 'trigger' => 'triggers',
  30. 'store' => 'store',
  31. 'serializer' => 'serializers',
  32. 'extractor' => 'extractors',
  33. 'sparqlscript' => 'sparqlscript',
  34. 'parser' => 'parsers',
  35. );
  36. foreach ($dirs as $k => $dir) {
  37. if (preg_match('/' . $k . '/i', $f)) {
  38. return $r . $dir . '/';
  39. }
  40. }
  41. return $r;
  42. }
  43. function getScriptURI() {
  44. if (isset($_SERVER) && isset($_SERVER['SERVER_NAME'])) {
  45. return preg_replace('/^([a-z]+)\/.*$/', '\\1', strtolower($_SERVER['SERVER_PROTOCOL'])) .
  46. '://' . $_SERVER['SERVER_NAME'] .
  47. ($_SERVER['SERVER_PORT'] != 80 ? ':' . $_SERVER['SERVER_PORT'] : '') .
  48. $_SERVER['SCRIPT_NAME'];
  49. }
  50. elseif (isset($_SERVER['SCRIPT_FILENAME'])) {
  51. return 'file://' . realpath($_SERVER['SCRIPT_FILENAME']);
  52. }
  53. return 'http://localhost/unknown_path';
  54. }
  55. function getRequestURI() {
  56. if (isset($_SERVER) && isset($_SERVER['REQUEST_URI'])) {
  57. return preg_replace('/^([a-z]+)\/.*$/', '\\1', strtolower($_SERVER['SERVER_PROTOCOL'])) .
  58. '://' . $_SERVER['SERVER_NAME'] .
  59. ($_SERVER['SERVER_PORT'] != 80 ? ':' . $_SERVER['SERVER_PORT'] : '') .
  60. $_SERVER['REQUEST_URI'];
  61. }
  62. return ARC2::getScriptURI();
  63. }
  64. function inc($f, $path = '') {
  65. $prefix = 'ARC2';
  66. if (preg_match('/^([^\_]+)\_(.*)$/', $f, $m)) {
  67. $prefix = $m[1];
  68. $f = $m[2];
  69. }
  70. $inc_path = $path ? $path : ARC2::getIncPath($f);
  71. $path = $inc_path . $prefix . '_' . urlencode($f) . '.php';
  72. if (file_exists($path)) {
  73. include_once($path);
  74. return 1;
  75. }
  76. if ($prefix != 'ARC2') {
  77. $path = $inc_path . strtolower($prefix) . '/' . $prefix . '_' . urlencode($f) . '.php';
  78. if (file_exists($path)) {
  79. include_once($path);
  80. return 1;
  81. }
  82. }
  83. return 0;
  84. }
  85. /* */
  86. function mtime(){
  87. list($msec, $sec) = explode(" ", microtime());
  88. return ((float)$msec + (float)$sec);
  89. }
  90. function x($re, $v, $options = 'si') {
  91. return preg_match("/^\s*" . $re . "(.*)$/" . $options, $v, $m) ? $m : false;
  92. }
  93. /* */
  94. function getFormat($val, $mtype = '', $ext = '') {
  95. ARC2::inc('getFormat');
  96. return ARC2_getFormat($val, $mtype, $ext);
  97. }
  98. function getPreferredFormat($default = 'plain') {
  99. ARC2::inc('getPreferredFormat');
  100. return ARC2_getPreferredFormat($default);
  101. }
  102. /* */
  103. function toUTF8($v) {
  104. if (urlencode($v) === $v) return $v;
  105. //if (utf8_decode($v) == $v) return $v;
  106. $v = (strpos(utf8_decode(str_replace('?', '', $v)), '?') === false) ? utf8_decode($v) : $v;
  107. 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);
  108. }
  109. function getUTF8Char($v) {
  110. $val = $v[1];
  111. if (strlen(trim($val)) === 1) return utf8_encode($val);
  112. if (preg_match('/^([\x00-\x7f])(.+)/', $val, $m)) return $m[1] . ARC2::toUTF8($m[2]);
  113. return $val;
  114. }
  115. /* */
  116. function splitURI($v) {
  117. /* the following namespaces may lead to conflated URIs,
  118. * we have to set the split position manually
  119. */
  120. if (strpos($v, 'www.w3.org')) {
  121. $specials = array(
  122. 'http://www.w3.org/XML/1998/namespace',
  123. 'http://www.w3.org/2005/Atom',
  124. 'http://www.w3.org/1999/xhtml',
  125. );
  126. foreach ($specials as $ns) {
  127. if (strpos($v, $ns) === 0) {
  128. $local_part = substr($v, strlen($ns));
  129. if (!preg_match('/^[\/\#]/', $local_part)) {
  130. return array($ns, $local_part);
  131. }
  132. }
  133. }
  134. }
  135. /* auto-splitting on / or # */
  136. //$re = '^(.*?)([A-Z_a-z][-A-Z_a-z0-9.]*)$';
  137. if (preg_match('/^(.*[\/\#])([^\/\#]+)$/', $v, $m)) return array($m[1], $m[2]);
  138. /* auto-splitting on last special char, e.g. urn:foo:bar */
  139. if (preg_match('/^(.*[\:\/])([^\:\/]+)$/', $v, $m)) return array($m[1], $m[2]);
  140. return array($v, '');
  141. }
  142. /* */
  143. function getSimpleIndex($triples, $flatten_objects = 1, $vals = '') {
  144. $r = array();
  145. foreach ($triples as $t) {
  146. $skip_t = 0;
  147. foreach (array('s', 'p', 'o') as $term) {
  148. $$term = $t[$term];
  149. /* template var */
  150. if (isset($t[$term . '_type']) && ($t[$term . '_type'] == 'var')) {
  151. $val = isset($vals[$$term]) ? $vals[$$term] : '';
  152. $skip_t = isset($vals[$$term]) ? $skip_t : 1;
  153. $type = '';
  154. $type = !$type && isset($vals[$$term . ' type']) ? $vals[$$term . ' type'] : $type;
  155. $type = !$type && preg_match('/^\_\:/', $val) ? 'bnode' : $type;
  156. if ($term == 'o') {
  157. $type = !$type && (preg_match('/\s/s', $val) || !preg_match('/\:/', $val)) ? 'literal' : $type;
  158. $type = !$type && !preg_match('/[\/]/', $val) ? 'literal' : $type;
  159. }
  160. $type = !$type ? 'uri' : $type;
  161. $t[$term . '_type'] = $type;
  162. $$term = $val;
  163. }
  164. }
  165. if ($skip_t) {
  166. continue;
  167. }
  168. if (!isset($r[$s])) $r[$s] = array();
  169. if (!isset($r[$s][$p])) $r[$s][$p] = array();
  170. if ($flatten_objects) {
  171. if (!in_array($o, $r[$s][$p])) $r[$s][$p][] = $o;
  172. }
  173. else {
  174. $o = array('value' => $o);
  175. foreach (array('lang', 'type', 'datatype') as $suffix) {
  176. if (isset($t['o_' . $suffix]) && $t['o_' . $suffix]) {
  177. $o[$suffix] = $t['o_' . $suffix];
  178. }
  179. elseif (isset($t['o ' . $suffix]) && $t['o ' . $suffix]) {
  180. $o[$suffix] = $t['o ' . $suffix];
  181. }
  182. }
  183. if (!in_array($o, $r[$s][$p])) {
  184. $r[$s][$p][] = $o;
  185. }
  186. }
  187. }
  188. return $r;
  189. }
  190. function getTriplesFromIndex($index) {
  191. $r = array();
  192. foreach ($index as $s => $ps) {
  193. foreach ($ps as $p => $os) {
  194. foreach ($os as $o) {
  195. $r[] = array(
  196. 's' => $s,
  197. 'p' => $p,
  198. 'o' => $o['value'],
  199. 's_type' => preg_match('/^\_\:/', $s) ? 'bnode' : 'uri',
  200. 'o_type' => $o['type'],
  201. 'o_datatype' => isset($o['datatype']) ? $o['datatype'] : '',
  202. 'o_lang' => isset($o['lang']) ? $o['lang'] : '',
  203. );
  204. }
  205. }
  206. }
  207. return $r;
  208. }
  209. function getMergedIndex() {
  210. $r = array();
  211. foreach (func_get_args() as $index) {
  212. foreach ($index as $s => $ps) {
  213. if (!isset($r[$s])) $r[$s] = array();
  214. foreach ($ps as $p => $os) {
  215. if (!isset($r[$s][$p])) $r[$s][$p] = array();
  216. foreach ($os as $o) {
  217. if (!in_array($o, $r[$s][$p])) {
  218. $r[$s][$p][] = $o;
  219. }
  220. }
  221. }
  222. }
  223. }
  224. return $r;
  225. }
  226. function getCleanedIndex() {/* removes triples from a given index */
  227. $indexes = func_get_args();
  228. $r = $indexes[0];
  229. for ($i = 1, $i_max = count($indexes); $i < $i_max; $i++) {
  230. $index = $indexes[$i];
  231. foreach ($index as $s => $ps) {
  232. if (!isset($r[$s])) continue;
  233. foreach ($ps as $p => $os) {
  234. if (!isset($r[$s][$p])) continue;
  235. $r_os = $r[$s][$p];
  236. $new_os = array();
  237. foreach ($r_os as $r_o) {
  238. $r_o_val = is_array($r_o) ? $r_o['value'] : $r_o;
  239. $keep = 1;
  240. foreach ($os as $o) {
  241. $del_o_val = is_array($o) ? $o['value'] : $o;
  242. if ($del_o_val == $r_o_val) {
  243. $keep = 0;
  244. break;
  245. }
  246. }
  247. if ($keep) {
  248. $new_os[] = $r_o;
  249. }
  250. }
  251. if ($new_os) {
  252. $r[$s][$p] = $new_os;
  253. }
  254. else {
  255. unset($r[$s][$p]);
  256. }
  257. }
  258. }
  259. }
  260. /* check r */
  261. $has_data = 0;
  262. foreach ($r as $s => $ps) {
  263. if ($ps) {
  264. $has_data = 1;
  265. break;
  266. }
  267. }
  268. return $has_data ? $r : array();
  269. }
  270. /* */
  271. function getStructType($v) {
  272. /* string */
  273. if (is_string($v)) return 'string';
  274. /* triples */
  275. if (isset($v[0]) && isset($v[0]['s']) && isset($v[0]['p'])) return 'triples';
  276. /* index */
  277. foreach ($v as $s => $ps) {
  278. if (is_array($ps)) {
  279. foreach ($ps as $p => $os) {
  280. if (is_array($os) && isset($os[0]) && isset($os[0]['value'])) return 'index';
  281. }
  282. }
  283. }
  284. /* array */
  285. return 'array';
  286. }
  287. /* */
  288. function getComponent($name, $a = '', $caller = '') {
  289. ARC2::inc($name);
  290. $prefix = 'ARC2';
  291. if (preg_match('/^([^\_]+)\_(.+)$/', $name, $m)) {
  292. $prefix = $m[1];
  293. $name = $m[2];
  294. }
  295. $cls = $prefix . '_' . $name;
  296. if (!$caller) $caller = new stdClass();
  297. return new $cls($a, $caller);
  298. }
  299. /* resource */
  300. function getResource($a = '') {
  301. return ARC2::getComponent('Resource', $a);
  302. }
  303. /* parsers */
  304. function getParser($prefix, $a = '') {
  305. return ARC2::getComponent($prefix . 'Parser', $a);
  306. }
  307. function getRDFParser($a = '') {
  308. return ARC2::getParser('RDF', $a);
  309. }
  310. function getRDFXMLParser($a = '') {
  311. return ARC2::getParser('RDFXML', $a);
  312. }
  313. function getTurtleParser($a = '') {
  314. return ARC2::getParser('Turtle', $a);
  315. }
  316. function getRSSParser($a = '') {
  317. return ARC2::getParser('RSS', $a);
  318. }
  319. function getSemHTMLParser($a = '') {
  320. return ARC2::getParser('SemHTML', $a);
  321. }
  322. function getSPARQLParser($a = '') {
  323. return ARC2::getComponent('SPARQLParser', $a);
  324. }
  325. function getSPARQLPlusParser($a = '') {
  326. return ARC2::getParser('SPARQLPlus', $a);
  327. }
  328. function getSPARQLXMLResultParser($a = '') {
  329. return ARC2::getParser('SPARQLXMLResult', $a);
  330. }
  331. function getJSONParser($a = '') {
  332. return ARC2::getParser('JSON', $a);
  333. }
  334. function getSGAJSONParser($a = '') {
  335. return ARC2::getParser('SGAJSON', $a);
  336. }
  337. function getCBJSONParser($a = '') {
  338. return ARC2::getParser('CBJSON', $a);
  339. }
  340. function getSPARQLScriptParser($a = '') {
  341. return ARC2::getParser('SPARQLScript', $a);
  342. }
  343. /* store */
  344. function getStore($a = '', $caller = '') {
  345. return ARC2::getComponent('Store', $a, $caller);
  346. }
  347. function getStoreEndpoint($a = '', $caller = '') {
  348. return ARC2::getComponent('StoreEndpoint', $a, $caller);
  349. }
  350. function getRemoteStore($a = '', $caller = '') {
  351. return ARC2::getComponent('RemoteStore', $a, $caller);
  352. }
  353. function getMemStore($a = '') {
  354. return ARC2::getComponent('MemStore', $a);
  355. }
  356. /* serializers */
  357. function getSer($prefix, $a = '') {
  358. return ARC2::getComponent($prefix . 'Serializer', $a);
  359. }
  360. function getTurtleSerializer($a = '') {
  361. return ARC2::getSer('Turtle', $a);
  362. }
  363. function getRDFXMLSerializer($a = '') {
  364. return ARC2::getSer('RDFXML', $a);
  365. }
  366. function getNTriplesSerializer($a = '') {
  367. return ARC2::getSer('NTriples', $a);
  368. }
  369. function getRDFJSONSerializer($a = '') {
  370. return ARC2::getSer('RDFJSON', $a);
  371. }
  372. function getPOSHRDFSerializer($a = '') {/* deprecated */
  373. return ARC2::getSer('POSHRDF', $a);
  374. }
  375. function getMicroRDFSerializer($a = '') {
  376. return ARC2::getSer('MicroRDF', $a);
  377. }
  378. function getRSS10Serializer($a = '') {
  379. return ARC2::getSer('RSS10', $a);
  380. }
  381. /* sparqlscript */
  382. function getSPARQLScriptProcessor($a = '') {
  383. return ARC2::getComponent('SPARQLScriptProcessor', $a);
  384. }
  385. /* */
  386. }