PageRenderTime 55ms CodeModel.GetById 29ms RepoModel.GetById 0ms app.codeStats 0ms

/DemiBlog/lib/arc/store/ARC2_MemStore.php

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