function runQuery($q, $qt = '', $infos = '')
 {
     /* ep */
     $ep = $this->v('remote_store_endpoint', 0, $this->a);
     if (!$ep) {
         return false;
     }
     /* prefixes */
     $q = $this->completeQuery($q);
     /* custom handling */
     $mthd = 'run' . $this->camelCase($qt) . 'Query';
     if (method_exists($this, $mthd)) {
         return $this->{$mthd}($q, $infos);
     }
     /* http verb */
     $mthd = in_array($qt, array('load', 'insert', 'delete')) ? 'POST' : 'GET';
     /* reader */
     ARC2::inc('Reader');
     $reader = new ARC2_Reader($this->a, $this);
     $reader->setAcceptHeader('Accept: application/sparql-results+xml; q=0.9, application/rdf+xml; q=0.9, */*; q=0.1');
     if ($mthd == 'GET') {
         $url = $ep;
         $url .= strpos($ep, '?') ? '&' : '?';
         $url .= 'query=' . urlencode($q);
         if ($k = $this->v('store_read_key', '', $this->a)) {
             $url .= '&key=' . urlencode($k);
         }
     } else {
         $url = $ep;
         $reader->setHTTPMethod($mthd);
         $reader->setCustomHeaders("Content-Type: application/x-www-form-urlencoded");
         $suffix = ($k = $this->v('store_write_key', '', $this->a)) ? '&key=' . rawurlencode($k) : '';
         $reader->setMessageBody('query=' . rawurlencode($q) . $suffix);
     }
     $to = $this->v('remote_store_timeout', 0, $this->a);
     $reader->activate($url, '', 0, $to);
     $format = $reader->getFormat();
     $resp = '';
     while ($d = $reader->readStream()) {
         $resp .= $this->toUTF8($d);
     }
     $reader->closeStream();
     $ers = $reader->getErrors();
     $this->a['reader_auth_infos'] = $reader->getAuthInfos();
     unset($this->reader);
     if ($ers) {
         return array('errors' => $ers);
     }
     $mappings = array('rdfxml' => 'RDFXML', 'sparqlxml' => 'SPARQLXMLResult', 'turtle' => 'Turtle');
     if (!$format || !isset($mappings[$format])) {
         return $resp;
         //return $this->addError('No parser available for "' . $format . '" SPARQL result');
     }
     /* format parser */
     $suffix = $mappings[$format] . 'Parser';
     ARC2::inc($suffix);
     $cls = 'ARC2_' . $suffix;
     $parser = new $cls($this->a, $this);
     $parser->parse($ep, $resp);
     /* ask|load|insert|delete */
     if (in_array($qt, array('ask', 'load', 'insert', 'delete'))) {
         $bid = $parser->getBooleanInsertedDeleted();
         if ($qt == 'ask') {
             $r = $bid['boolean'];
         } else {
             $r = $bid;
         }
     } elseif ($qt == 'select' && !method_exists($parser, 'getRows')) {
         $r = $resp;
     } elseif ($qt == 'select') {
         $r = array('rows' => $parser->getRows(), 'variables' => $parser->getVariables());
     } else {
         $r = $parser->getSimpleIndex(0);
     }
     unset($parser);
     return $r;
 }
 function processHTTPCall($block, $mthd = 'GET')
 {
     ARC2::inc('Reader');
     $reader = new ARC2_Reader($this->a, $this);
     $url = $this->replacePlaceholders($block['args'][0]['value'], 'function_call');
     if ($mthd != 'GET') {
         $reader->setHTTPMethod($mthd);
         $reader->setCustomHeaders("Content-Type: application/x-www-form-urlencoded");
     }
     $to = $this->v('remote_call_timeout', 0, $this->a);
     $reader->activate($url, '', 0, $to);
     $format = $reader->getFormat();
     $resp = '';
     while ($d = $reader->readStream()) {
         $resp .= $d;
     }
     $reader->closeStream();
     unset($this->reader);
     return array('value_type' => 'http_response', 'value' => $resp);
 }
Example #3
0
 function runQuery($q, $qt = '')
 {
     /* ep */
     $ep = $this->v('remote_store_endpoint', 0, $this->a);
     if (!$ep) {
         return false;
     }
     /* prefixes */
     $ns = isset($this->a['ns']) ? $this->a['ns'] : array();
     $added_prefixes = array();
     $prologue = '';
     foreach ($ns as $k => $v) {
         $k = rtrim($k, ':');
         if (in_array($k, $added_prefixes)) {
             continue;
         }
         if (preg_match('/(^|\\s)' . $k . ':/s', $q) && !preg_match('/PREFIX\\s+' . $k . '\\:/is', $q)) {
             $prologue .= "\n" . 'PREFIX ' . $k . ': <' . $v . '>';
         }
         $added_prefixes[] = $k;
     }
     $q = $prologue . "\n" . $q;
     /* http verb */
     $mthd = in_array($qt, array('load', 'insert', 'delete')) ? 'POST' : 'GET';
     /* reader */
     ARC2::inc('Reader');
     $reader = new ARC2_Reader($this->a, $this);
     $reader->setAcceptHeader('Accept: application/sparql-results+xml; q=0.9, application/rdf+xml; q=0.9, */*; q=0.1');
     if ($mthd == 'GET') {
         $url = $ep;
         $url .= strpos($ep, '?') ? '&' : '?';
         $url .= 'query=' . urlencode($q);
         if ($k = $this->v('store_read_key', '', $this->a)) {
             $url .= '&key=' . urlencode($k);
         }
     } else {
         $url = $ep;
         $reader->setHTTPMethod($mthd);
         $reader->setCustomHeaders("Content-Type: application/x-www-form-urlencoded");
         $suffix = ($k = $this->v('store_write_key', '', $this->a)) ? '&key=' . rawurlencode($k) : '';
         $reader->setMessageBody('query=' . rawurlencode($q) . $suffix);
     }
     $to = $this->v('remote_store_timeout', 0, $this->a);
     $reader->activate($url, '', 0, $to);
     $format = $reader->getFormat();
     $resp = '';
     while ($d = $reader->readStream()) {
         $resp .= $d;
     }
     $reader->closeStream();
     $ers = $reader->getErrors();
     unset($this->reader);
     if ($ers) {
         return array('errors' => $ers);
     }
     $mappings = array('rdfxml' => 'RDFXML', 'sparqlxml' => 'SPARQLXMLResult', 'turtle' => 'Turtle');
     if (!$format || !isset($mappings[$format])) {
         return $resp;
         //return $this->addError('No parser available for "' . $format . '" SPARQL result');
     }
     /* format parser */
     $suffix = $mappings[$format] . 'Parser';
     ARC2::inc($suffix);
     $cls = 'ARC2_' . $suffix;
     $parser = new $cls($this->a, $this);
     $parser->parse($ep, $resp);
     /* ask|load|insert|delete */
     if (in_array($qt, array('ask', 'load', 'insert', 'delete'))) {
         $bid = $parser->getBooleanInsertedDeleted();
         switch ($qt) {
             case 'ask':
                 return $bid['boolean'];
             default:
                 return $bid;
         }
     }
     /* select */
     if ($qt == 'select' && !method_exists($parser, 'getRows')) {
         return $resp;
     }
     if ($qt == 'select') {
         return array('rows' => $parser->getRows(), 'variables' => $parser->getVariables());
     }
     /* any other */
     return $parser->getSimpleIndex(0);
 }
 function runQuery($infos, $data = '', $keep_bnode_ids = 0)
 {
     $url = $infos['query']['url'];
     $graph = $infos['query']['target_graph'];
     $this->target_graph = $graph ? $this->calcURI($graph) : $this->calcURI($url);
     $this->fixed_target_graph = $graph ? $this->target_graph : '';
     $this->keep_bnode_ids = $keep_bnode_ids;
     /* reader */
     ARC2::inc('Reader');
     $reader = new ARC2_Reader($this->a, $this);
     $reader->activate($url, $data);
     /* format detection */
     $mappings = array('rdfxml' => 'RDFXML', 'sparqlxml' => 'SPOG', 'turtle' => 'Turtle', 'ntriples' => 'Turtle', 'rss' => 'RSS', 'atom' => 'Atom', 'n3' => 'Turtle', 'html' => 'SemHTML', 'sgajson' => 'SGAJSON', 'cbjson' => 'CBJSON');
     $format = $reader->getFormat();
     if (!$format || !isset($mappings[$format])) {
         return $this->addError('No loader available for "' . $url . '": ' . $format);
     }
     /* format loader */
     $suffix = 'Store' . $mappings[$format] . 'Loader';
     ARC2::inc($suffix);
     $cls = 'ARC2_' . $suffix;
     $loader = new $cls($this->a, $this);
     $loader->setReader($reader);
     /* lock */
     if (!$this->store->getLock()) {
         $l_name = $this->a['db_name'] . '.' . $this->store->getTablePrefix() . '.write_lock';
         return $this->addError('Could not get lock in "runQuery" (' . $l_name . ')');
     }
     $this->has_lock = 1;
     /* logging */
     $this->t_count = 0;
     $this->t_start = ARC2::mtime();
     $this->log_inserts = $this->v('store_log_inserts', 0, $this->a);
     if ($this->log_inserts) {
         @unlink("arc_insert_log.txt");
         $this->inserts = array();
         $this->insert_times = array();
         $this->t_prev = $this->t_start;
         $this->t_count_prev = 0;
     }
     /* load and parse */
     $this->max_term_id = $this->getMaxTermID();
     $this->max_triple_id = $this->getMaxTripleID();
     $this->column_type = $this->store->getColumnType();
     //$this->createMergeTable();
     $this->term_ids = array();
     $this->triple_ids = array();
     $this->sql_buffers = array();
     $r = $loader->parse($url, $data);
     /* done */
     $this->checkSQLBuffers(1);
     if ($this->log_inserts) {
         $this->logInserts();
     }
     $this->store->releaseLock();
     //$this->dropMergeTable();
     if (rand(1, 100) == 1) {
         $this->store->optimizeTables();
     }
     $t2 = ARC2::mtime();
     $dur = round($t2 - $this->t_start, 4);
     $r = array('t_count' => $this->t_count, 'load_time' => $dur);
     if ($this->log_inserts) {
         $r['inserts'] = $this->inserts;
         $r['insert_times'] = $this->insert_times;
     }
     return $r;
 }