function query($q, $result_format = '', $src = '', $keep_bnode_ids = 0, $log_query = 0) { if ($log_query) { $this->logQuery($q); } ARC2::inc('SPARQLPlusParser'); $p = new ARC2_SPARQLPlusParser($this->a, $this); $p->parse($q, $src); $infos = $p->getQueryInfos(); $t1 = ARC2::mtime(); if (!($errs = $p->getErrors())) { $qt = $infos['query']['type']; $r = array('query_type' => $qt, 'result' => $this->runQuery($q, $qt, $infos)); } else { $r = array('result' => ''); } $t2 = ARC2::mtime(); $r['query_time'] = $t2 - $t1; /* query result */ if ($result_format == 'raw') { return $r['result']; } if ($result_format == 'rows') { return $this->v('rows', array(), $r['result']); } if ($result_format == 'row') { if (!isset($r['result']['rows'])) { return array(); } return $r['result']['rows'] ? $r['result']['rows'][0] : array(); } return $r; }
function handleQueryRequest($q) { if (preg_match('/^dump/i', $q)) { $infos = array('query' => array('type' => 'dump')); $this->is_dump = 1; } else { ARC2::inc('SPARQLPlusParser'); $p = new ARC2_SPARQLPlusParser($this->a, $this); $p->parse($q); $infos = $p->getQueryInfos(); } /* errors? */ if ($errors = $this->getErrors()) { $this->setHeader('http', 'HTTP/1.1 400 Bad Request'); $this->setHeader('content-type', 'Content-type: text/plain; charset=utf-8'); $this->result = htmlspecialchars(join("\n", $errors)); return true; } $qt = $infos['query']['type']; /* wrong read key? */ if ($this->read_key && $this->p('key') != $this->read_key && preg_match('/^(select|ask|construct|describe|dump)$/', $qt)) { $this->setHeader('http', 'HTTP/1.1 401 Access denied'); $this->setHeader('content-type', 'Content-type: text/plain; charset=utf-8'); $this->result = 'Access denied. Missing or wrong "key" parameter.'; return true; } /* wrong write key? */ if ($this->write_key && $this->p('key') != $this->write_key && preg_match('/^(load|insert|delete|update)$/', $qt)) { $this->setHeader('http', 'HTTP/1.1 401 Access denied'); $this->setHeader('content-type', 'Content-type: text/plain; charset=utf-8'); $this->result = 'Access denied. Missing or wrong "key" parameter.'; return true; } /* non-allowed query type? */ if (!in_array($qt, $this->getFeatures())) { $this->setHeader('http', 'HTTP/1.1 401 Access denied'); $this->setHeader('content-type', 'Content-type: text/plain; charset=utf-8'); $this->result = 'Access denied for "' . $qt . '" query'; return true; } /* load/insert/delete via GET */ if (in_array($qt, array('load', 'insert', 'delete')) && isset($_GET['query'])) { $this->setHeader('http', 'HTTP/1.1 501 Not Implemented'); $this->setHeader('content-type', 'Content-type: text/plain; charset=utf-8'); $this->result = 'Query type "' . $qt . '" not supported via GET'; return true; } /* unsupported query type */ if (!in_array($qt, array('select', 'ask', 'describe', 'construct', 'load', 'insert', 'delete', 'dump'))) { $this->setHeader('http', 'HTTP/1.1 501 Not Implemented'); $this->setHeader('content-type', 'Content-type: text/plain; charset=utf-8'); $this->result = 'Unsupported query type "' . $qt . '"'; return true; } /* adjust infos */ $infos = $this->adjustQueryInfos($infos); $t1 = ARC2::mtime(); $r = array('result' => $this->runQuery($infos, $qt)); $t2 = ARC2::mtime(); $r['query_time'] = $t2 - $t1; /* query errors? */ if ($errors = $this->getErrors()) { $this->setHeader('http', 'HTTP/1.1 400 Bad Request'); $this->setHeader('content-type', 'Content-type: text/plain; charset=utf-8'); $this->result = 'Error: ' . join("\n", $errors); return true; } /* result */ $m = 'get' . ucfirst($qt) . 'ResultDoc'; if (method_exists($this, $m)) { $this->result = $this->{$m}($r); } else { $this->setHeader('content-type', 'Content-type: text/plain; charset=utf-8'); $this->result = 'Result serializer not available, dumping raw data:' . "\n" . print_r($r, 1); } }
function query($q, $result_format = '', $src = '', $keep_bnode_ids = 0, $log_query = 0) { if ($log_query) { $this->logQuery($q); } $con = $this->getDBCon(); if (preg_match('/^dump/i', $q)) { $infos = array('query' => array('type' => 'dump')); } else { ARC2::inc('SPARQLPlusParser'); $p = new ARC2_SPARQLPlusParser($this->a, $this); $p->parse($q, $src); $infos = $p->getQueryInfos(); } if ($result_format == 'infos') { return $infos; } $infos['result_format'] = $result_format; if (!isset($p) || !$p->getErrors()) { $qt = $infos['query']['type']; if (!in_array($qt, array('select', 'ask', 'describe', 'construct', 'load', 'insert', 'delete', 'dump'))) { return $this->addError('Unsupported query type "' . $qt . '"'); } $t1 = ARC2::mtime(); $r = array('query_type' => $qt, 'result' => $this->runQuery($infos, $qt, $keep_bnode_ids, $q)); $t2 = ARC2::mtime(); $r['query_time'] = $t2 - $t1; /* query result */ if ($result_format == 'raw') { return $r['result']; } if ($result_format == 'rows') { return $r['result']['rows'] ? $r['result']['rows'] : array(); } if ($result_format == 'row') { return $r['result']['rows'] ? $r['result']['rows'][0] : array(); } return $r; } return 0; }
function runQueryEvaluationTest($id) { $nl = "\n"; $r = ''; /* get action */ $q = ' PREFIX mf: <http://www.w3.org/2001/sw/DataAccess/tests/test-manifest#> . PREFIX qt: <http://www.w3.org/2001/sw/DataAccess/tests/test-query#> . SELECT DISTINCT ?query ?data ?graph_data ?result WHERE { <' . $id . '> mf:action ?action ; mf:result ?result . ?action qt:query ?query . OPTIONAL { ?action qt:data ?data . } OPTIONAL { ?action qt:graphData ?graph_data . } } '; $qr = $this->store->query($q); $rows = $qr['result']['rows']; $infos = array(); foreach (array('query', 'data', 'result', 'graph_data') as $var) { $infos[$var] = array(); $infos[$var . '_value'] = array(); foreach ($rows as $row) { if (isset($row[$var])) { if (!in_array($row[$var], $infos[$var])) { $infos[$var][] = $row[$var]; $infos[$var . '_value'][] = $this->getFile($row[$var]); } } } ${$var} = $infos[$var]; ${$var . '_value'} = $infos[$var . '_value']; if (count($infos[$var]) == 1) { ${$var} = $infos[$var][0]; ${$var . '_value'} = $infos[$var . '_value'][0]; } if (${$var} && $var != '-result') { //echo '<pre>' . $$var . $nl . $nl . htmlspecialchars(${$var . '_value'}) . '</pre><hr />'; } } /* query infos */ ARC2::inc('SPARQLPlusParser'); $parser = new ARC2_SPARQLPlusParser($this->a, $this); $parser->parse($query_value, $query); $infos = $parser->getQueryInfos(); $rest = $parser->getUnparsedCode(); $errors = $parser->getErrors(); $q_type = !$errors ? $infos['query']['type'] : ''; /* add data */ $dsets = array(); $gdsets = array(); if ($data) { $dsets = is_array($data) ? array_merge($dsets, $data) : array_merge($dsets, array($data)); } if ($graph_data) { $gdsets = is_array($graph_data) ? array_merge($gdsets, $graph_data) : array_merge($gdsets, array($graph_data)); } if (!$dsets && !$gdsets) { foreach ($infos['query']['dataset'] as $set) { if ($set['named']) { $gdsets[] = $set['graph']; } else { $dsets[] = $set['graph']; } } } $store = $this->data_store; $store->reset(); foreach ($dsets as $graph) { $qr = $store->query('LOAD <' . $graph . '>'); } foreach ($gdsets as $graph) { $qr = $store->query('LOAD <' . $graph . '> INTO <' . $graph . '>'); } /* run query */ if ($query) { $sql = $store->query($query_value, 'sql', $query); $qr = $store->query($query_value, '', $query); $qr_result = $qr['result']; if ($q_type == 'select') { $qr_result = $this->adjustBnodes($qr['result'], $id); } elseif ($q_type == 'construct') { $ser = ARC2::getTurtleSerializer($this->a); $qr_result = $ser->getSerializedIndex($qr_result); } } //echo '<pre>query result: ' . $nl . htmlspecialchars(print_r($qr_result, 1)) . '</pre>'; if (!$query || $errors || $rest) { return array('pass' => 0, 'info' => 'query could not be parsed' . htmlspecialchars($query_value)); } $m = 'isSame' . $q_type . 'Result'; $sub_r = $this->{$m}($qr_result, $result_value, $result, $id); $pass = $sub_r['pass']; if (in_array($id, array('http://www.w3.org/2001/sw/DataAccess/tests/data-r2/sort/manifest#dawg-sort-6', 'http://www.w3.org/2001/sw/DataAccess/tests/data-r2/sort/manifest#dawg-sort-8', 'http://www.w3.org/2001/sw/DataAccess/tests/data-r2/sort/manifest#dawg-sort-builtin'))) { $pass = 0; /* manually checked 2007-09-18 */ } if (in_array($id, array('http://www.w3.org/2001/sw/DataAccess/tests/data-r2/sort/manifest#dawg-sort-function', 'http://www.w3.org/2001/sw/DataAccess/tests/data-r2/reduced/manifest#reduced-1', 'http://www.w3.org/2001/sw/DataAccess/tests/data-r2/reduced/manifest#reduced-2'))) { $pass = 1; /* manually checked 2007-11-28 */ } $pass_info = $sub_r['info']; $info = print_r($pass_info, 1) . $nl; $info .= '<hr />sql: ' . $nl . htmlspecialchars($sql['result']) . '<hr />'; $info .= $pass ? '' : print_r($graph_data, 1) . $nl . htmlspecialchars(print_r($graph_data_value, 1)) . '<hr />'; $info .= $pass ? '' : print_r($data, 1) . $nl . htmlspecialchars(print_r($data_value, 1)) . '<hr />'; $info .= $pass ? '' : $query . $nl . htmlspecialchars($query_value) . '<hr />'; $info .= $pass ? '' : '<pre>query result: ' . $nl . htmlspecialchars(print_r($qr_result, 1)) . '</pre>' . '<hr />'; $info .= $pass ? '' : print_r($infos, 1); return array('pass' => $pass, 'info' => $info); }