PageRenderTime 75ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/DemiBlog/lib/arc/store/ARC2_RemoteStore.php

https://bitbucket.org/tobyink/php-demiblog3
PHP | 195 lines | 146 code | 24 blank | 25 comment | 35 complexity | 617192b57b3492e0aa866dc17a76d77b MD5 | raw file
Possible License(s): GPL-2.0
  1. <?php
  2. /**
  3. * ARC2 Remote RDF Store
  4. *
  5. * @author Benjamin Nowack <bnowack@semsol.com>
  6. * @license http://arc.semsol.org/license
  7. * @package ARC2
  8. * @version 2009-10-16
  9. */
  10. ARC2::inc('Class');
  11. class ARC2_RemoteStore extends ARC2_Class {
  12. function __construct($a = '', &$caller) {
  13. parent::__construct($a, $caller);
  14. }
  15. function ARC2_RemoteStore($a = '', &$caller) {
  16. $this->__construct($a, $caller);
  17. $this->is_remote = 1;
  18. }
  19. function __init() {
  20. parent::__init();
  21. }
  22. /* */
  23. function isSetUp() {
  24. return 1;
  25. }
  26. function setUp() {}
  27. /* */
  28. function reset() {}
  29. function drop() {}
  30. function insert($doc, $g, $keep_bnode_ids = 0) {
  31. return $this->query('INSERT INTO <' . $g . '> { ' . $this->toNTriples($doc, '', 1) . ' }');
  32. }
  33. function delete($doc, $g) {
  34. if (!$doc) {
  35. return $this->query('DELETE FROM <' . $g . '>');
  36. }
  37. else {
  38. return $this->query('DELETE FROM <' . $g . '> { ' . $this->toNTriples($doc, '', 1) . ' }');
  39. }
  40. }
  41. function replace($doc, $g, $doc_2) {
  42. return array($this->delete($doc, $g), $this->insert($doc_2, $g));
  43. }
  44. /* */
  45. function query($q, $result_format = '', $src = '', $keep_bnode_ids = 0, $log_query = 0, $queryparam='query') {
  46. if ($log_query) $this->logQuery($q);
  47. ARC2::inc('SPARQLPlusParser');
  48. $p = & new ARC2_SPARQLPlusParser($this->a, $this);
  49. $p->parse($q, $src);
  50. $infos = $p->getQueryInfos();
  51. $t1 = ARC2::mtime();
  52. if (!$errs = $p->getErrors()) {
  53. $qt = $infos['query']['type'];
  54. $r = array('query_type' => $qt, 'result' => $this->runQuery($q, $qt, $infos, $queryparam));
  55. }
  56. else {
  57. $r = array('result' => '');
  58. }
  59. $t2 = ARC2::mtime();
  60. $r['query_time'] = $t2 - $t1;
  61. /* query result */
  62. if ($result_format == 'raw') {
  63. return $r['result'];
  64. }
  65. if ($result_format == 'rows') {
  66. return $this->v('rows', array(), $r['result']);
  67. }
  68. if ($result_format == 'row') {
  69. if (!isset($r['result']['rows'])) return array();
  70. return $r['result']['rows'] ? $r['result']['rows'][0] : array();
  71. }
  72. return $r;
  73. }
  74. function runQuery($q, $qt = '', $infos = '', $queryparam='query') {
  75. /* ep */
  76. $ep = $this->v('remote_store_endpoint', 0, $this->a);
  77. if (!$ep) return false;
  78. /* prefixes */
  79. $q = $this->completeQuery($q);
  80. /* custom handling */
  81. $mthd = 'run' . $this->camelCase($qt) . 'Query';
  82. if (method_exists($this, $mthd)) {
  83. return $this->$mthd($q, $infos);
  84. }
  85. /* http verb */
  86. $mthd = in_array($qt, array('load', 'insert', 'delete', 'create', 'drop', 'clear')) ? 'POST' : 'GET';
  87. /* reader */
  88. ARC2::inc('Reader');
  89. $reader =& new ARC2_Reader($this->a, $this);
  90. $reader->setAcceptHeader('Accept: application/sparql-results+xml; q=0.9, application/rdf+xml; q=0.9, */*; q=0.1');
  91. if ($mthd == 'GET') {
  92. $url = $ep;
  93. $url .= strpos($ep, '?') ? '&' : '?';
  94. $url .= $queryparam . '=' . urlencode($q);
  95. if ($k = $this->v('store_read_key', '', $this->a)) $url .= '&key=' . urlencode($k);
  96. }
  97. else {
  98. $url = $ep;
  99. $reader->setHTTPMethod($mthd);
  100. $reader->setCustomHeaders("Content-Type: application/x-www-form-urlencoded");
  101. $suffix = ($k = $this->v('store_write_key', '', $this->a)) ? '&key=' . rawurlencode($k) : '';
  102. $reader->setMessageBody($queryparam . '=' . rawurlencode($q) . $suffix);
  103. }
  104. $to = $this->v('remote_store_timeout', 0, $this->a);
  105. $reader->activate($url, '', 0, $to);
  106. $format = $reader->getFormat();
  107. $resp = '';
  108. while ($d = $reader->readStream()) {
  109. $resp .= $d;
  110. }
  111. $reader->closeStream();
  112. $ers = $reader->getErrors();
  113. unset($this->reader);
  114. if ($ers) return array('errors' => $ers);
  115. $mappings = array('rdfxml' => 'RDFXML', 'sparqlxml' => 'SPARQLXMLResult', 'turtle' => 'Turtle');
  116. if (!$format || !isset($mappings[$format])) {
  117. return $resp;
  118. //return $this->addError('No parser available for "' . $format . '" SPARQL result');
  119. }
  120. /* format parser */
  121. $suffix = $mappings[$format] . 'Parser';
  122. ARC2::inc($suffix);
  123. $cls = 'ARC2_' . $suffix;
  124. $parser =& new $cls($this->a, $this);
  125. $parser->parse($ep, $resp);
  126. /* ask|load|insert|delete */
  127. if (in_array($qt, array('ask', 'load', 'insert', 'delete'))) {
  128. $bid = $parser->getBooleanInsertedDeleted();
  129. switch ($qt) {
  130. case 'ask': return $bid['boolean'];
  131. default: return $bid;
  132. }
  133. }
  134. /* select */
  135. if (($qt == 'select') && !method_exists($parser, 'getRows')) return $resp;
  136. if ($qt == 'select') return array('rows' => $parser->getRows(), 'variables' => $parser->getVariables());
  137. /* any other */
  138. return $parser->getSimpleIndex(0);
  139. }
  140. /* */
  141. function optimizeTables() {}
  142. /* */
  143. function getResourceLabel($res, $unnamed_label = 'An unnamed resource') {
  144. if (!isset($this->resource_labels)) $this->resource_labels = array();
  145. if (isset($this->resource_labels[$res])) return $this->resource_labels[$res];
  146. if (!preg_match('/^[a-z0-9\_]+\:[^\s]+$/si', $res)) return $res;/* literal */
  147. $r = '';
  148. if (preg_match('/^\_\:/', $res)) {
  149. return $unnamed_label;
  150. }
  151. $row = $this->query('SELECT ?o WHERE { <' . $res . '> ?p ?o . FILTER(REGEX(str(?p), "(label|name)$", "i"))}', 'row');
  152. if ($row) {
  153. $r = $row['o'];
  154. }
  155. else {
  156. $r = preg_replace("/^(.*[\/\#])([^\/\#]+)$/", '\\2', str_replace('#self', '', $res));
  157. $r = str_replace('_', ' ', $r);
  158. $r = preg_replace('/([a-z])([A-Z])/e', '"\\1 " . strtolower("\\2")', $r);
  159. }
  160. $this->resource_labels[$res] = $r;
  161. return $r;
  162. }
  163. function getDomains($p) {
  164. $r = array();
  165. foreach($this->query('SELECT DISTINCT ?type WHERE {?s <' . $p . '> ?o ; a ?type . }', 'rows') as $row) {
  166. $r[] = $row['type'];
  167. }
  168. return $r;
  169. }
  170. /* */
  171. }