Esempio n. 1
0
 public static function queryFile($modelFile, $e, &$rPointer, &$fPointer)
 {
     global $conf;
     global $lodspk;
     global $results;
     global $firstResults;
     global $uri;
     $data = array();
     $strippedModelFile = str_replace('endpoint.', '', str_replace('.query', '', $modelFile));
     if (!is_dir($modelFile)) {
         require_once $conf['home'] . 'lib/Haanga/lib/Haanga.php';
         Haanga::configure(array('cache_dir' => $conf['home'] . 'cache/', 'autoescape' => FALSE));
         //Haanga supports the dot (.) convention only for objects
         if (is_array($lodspk)) {
             $lodspkObj = Convert::array_to_object($lodspk);
             $lodspk = $lodspkObj;
         }
         $r2 = Convert::array_copy($results);
         $models = Convert::array_to_object($r2);
         $f2 = Convert::array_copy($firstResults);
         $first = Convert::array_to_object($f2);
         $vars = compact('uri', 'lodspk', 'conf', 'models', 'first');
         $q = file_get_contents($modelFile);
         if ($q == false) {
             HTTPStatus::send500("<br/>I can't load " . $modelFile . " in " . getcwd());
         }
         $fnc = Haanga::compile($q);
         $query = $fnc($vars, TRUE);
         if (is_object($lodspk)) {
             $lodspkObj = Convert::object_to_array($lodspk);
             $lodspk = $lodspkObj;
         }
         $query = Utils::addPrefixes($query);
         if ($lodspk['transform_select_query'] == true) {
             include_once $conf['home'] . 'lib/arc2/ARC2.php';
             $parser = ARC2::getSPARQLParser();
             $parser->parse($query);
             $sparqlConstruct = array();
             if (!$parser->getErrors()) {
                 $resultVars = array();
                 $q_infos = $parser->getQueryInfos();
                 foreach ($q_infos['query']['result_vars'] as $v) {
                     if ($v['type'] == 'var') {
                         $resultVars[$v['value']] = 1;
                     }
                 }
                 $x = Utils::extractObj($q_infos['query']['pattern']);
                 foreach ($x as $v) {
                     if ($resultVars[$v['s']] && $v['s_type'] == 'var' || $resultVars[$v['p']] && $v['p_type'] == 'var' || $resultVars[$v['o']] && $v['o_type'] == 'var') {
                         array_push($sparqlConstruct, $v);
                     }
                 }
                 $construct = "";
                 foreach ($sparqlConstruct as $v) {
                     if ($v['s_type'] == 'uri') {
                         $construct .= "<" . $v['s'] . "> ";
                     } elseif ($v['s_type'] == 'var') {
                         $construct .= '?' . $v['s'] . ' ';
                     } else {
                         $construct .= $v['s'] . " ";
                     }
                     if ($v['p_type'] == 'uri') {
                         $construct .= "<" . $v['p'] . "> ";
                     } elseif ($v['p_type'] == 'var') {
                         $construct .= '?' . $v['p'] . ' ';
                     } else {
                         $construct .= $v['p'] . " ";
                     }
                     if ($v['o_type'] == 'uri') {
                         $construct .= "<" . $v['o'] . "> ";
                     } elseif ($v['o_type'] == 'literal') {
                         $construct .= '"' . $v['o'] . '" ';
                     } elseif ($v['o_type'] == 'var') {
                         $construct .= '?' . $v['o'] . ' ';
                     } else {
                         $construct .= $v['o'] . " ";
                     }
                     $construct .= ".\n";
                 }
                 if ($construct == "") {
                     if (sizeof($q_infos['query']['result_vars']) > 0) {
                         //For now, assuming variables are in the GRAPH ?g
                         $query = "CONSTRUCT {?g ?x ?y} WHERE{GRAPH ?g{?g ?x ?y}}";
                     } else {
                         if (!preg_match('/construct/i', $query)) {
                             HTTPStatus::send500();
                         }
                     }
                 } else {
                     $query = preg_replace('/select\\s*[^{]*\\s*(where)?\\s*{/i', 'CONSTRUCT {' . $construct . '} WHERE{', $query);
                 }
             } else {
                 return;
                 //HTTPStatus::send500("invalid query: " . var_export($parser->getErrors(), true)."\n\nQUERY:\n".$query);
             }
         }
         if ($conf['debug']) {
             Logging::log($modelFile . " against " . $e->getSparqlUrl());
             Logging::log($query);
             Logging::log("Running query from " . $modelFile . " on endpoint " . $e->getSparqlURL(), E_USER_NOTICE);
         }
         $lodspk['queryText'][$strippedModelFile] = $query;
         $initTime = microtime(true);
         $aux = $e->query($query, Utils::getResultsType($query));
         $endTime = microtime(true);
         if ($conf['debug']) {
             Logging::log("Execution time: " . ($endTime - $initTime) . " seconds");
         }
         $timeObj = new stdClass();
         $timeObj->query = new stdClass();
         $timeObj->query->value = $strippedModelFile;
         $timeObj->time = new stdClass();
         $timeObj->time->value = $endTime - $initTime;
         $lodspk['queryTimes'][$strippedModelFile] = $timeObj;
         if ($modelFile != $lodspk['type']) {
             if (!isset($rPointer[$strippedModelFile])) {
                 $rPointer[$strippedModelFile] = array();
                 $firstResults[$strippedModelFile] = array();
             }
             if (Utils::getResultsType($query) == $conf['output']['select']) {
                 $rPointer[$strippedModelFile] = Utils::sparqlResult2Obj($aux);
                 $fPointer[$strippedModelFile] = $rPointer[$strippedModelFile][0];
             } else {
                 $lodspk['resultRdf'] = true;
                 $rPointer[$strippedModelFile] = $aux;
             }
         } else {
             if (Utils::getResultsType($query) == $conf['output']['select']) {
                 $rPointer = Utils::sparqlResult2Obj($aux);
                 $fPointer[$strippedModelFile] = $rPointer[0];
             } else {
                 $lodspk['resultRdf'] = true;
                 $rPointer = $aux;
             }
         }
     } else {
         if (strpos('endpoint.', $modelFile) == 0) {
             if ($conf['debug']) {
                 Logging::log("{$modelFile} is a directory, will process it later", E_USER_NOTICE);
             }
             if ($modelFile != $lodspk['type']) {
                 if (!isset($rPointer[$strippedModelFile])) {
                     $rPointer[$strippedModelFile] = array();
                 }
                 Utils::queryDir($modelFile, $rPointer[$strippedModelFile], $fPointer[$strippedModelFile]);
             } else {
                 Utils::queryDir($modelFile, $rPointer, $fPointer);
             }
         }
     }
 }