Beispiel #1
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($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);
 }