예제 #1
0
 public function testSetWithMethodsAsk()
 {
     $queryString = '
         ASK
         WHERE {
             ?comment <http://rdfs.org/sioc/ns#about> ?resource.
             ?comment a <http://rdfs.org/sioc/types#Comment>.
             ?comment <http://rdfs.org/sioc/ns#has_creator> ?author.
             ?comment <http://rdfs.org/sioc/ns#content> ?content.
             ?comment <http://purl.org/dc/terms/created> ?date.
         }
         ORDER BY DESC(?date)
         LIMIT 6';
     $queryObject = new Erfurt_Sparql_SimpleQuery();
     $queryObject->setAsk();
     $queryObject->setWherePart('WHERE {
             ?comment <http://rdfs.org/sioc/ns#about> ?resource.
             ?comment a <http://rdfs.org/sioc/types#Comment>.
             ?comment <http://rdfs.org/sioc/ns#has_creator> ?author.
             ?comment <http://rdfs.org/sioc/ns#content> ?content.
             ?comment <http://purl.org/dc/terms/created> ?date.
         }');
     $queryObject->setOrderClause('DESC(?date)');
     $queryObject->setLimit(6);
     $this->assertQueryEquals($queryString, (string) $queryObject);
     $this->assertEquals(true, $queryObject->isAsk());
     $this->assertEquals(null, $queryObject->getSelectClause());
 }
예제 #2
0
 public function serializeGraphToString($graphUri, $pretty = false, $useAc = true)
 {
     $triples = array();
     $store = Erfurt_App::getInstance()->getStore();
     require_once 'Erfurt/Sparql/SimpleQuery.php';
     $sparql = new Erfurt_Sparql_SimpleQuery();
     $sparql->setProloguePart('SELECT ?s ?p ?o');
     $sparql->addFrom($graphUri);
     $sparql->setWherePart('WHERE { ?s ?p ?o }');
     $sparql->setOrderClause('?s ?p ?o');
     $sparql->setLimit(1000);
     $offset = 0;
     while (true) {
         $sparql->setOffset($offset);
         $result = $store->sparqlQuery($sparql, array('result_format' => 'extended', 'use_owl_imports' => false, 'use_additional_imports' => false, 'use_ac' => $useAc));
         $counter = 0;
         foreach ($result['results']['bindings'] as $stm) {
             $s = $stm['s']['value'];
             $p = $stm['p']['value'];
             $o = $stm['o'];
             if (!isset($triples["{$s}"])) {
                 $triples["{$s}"] = array();
             }
             if (!isset($triples["{$s}"]["{$p}"])) {
                 $triples["{$s}"]["{$p}"] = array();
             }
             if ($o['type'] === 'typed-literal') {
                 $triples["{$s}"]["{$p}"][] = array('type' => 'literal', 'value' => $o['value'], 'datatype' => $o['datatype']);
             } else {
                 if ($o['type'] === 'typed-literal') {
                     $oArray = array('type' => 'literal', 'value' => $o['value']);
                     if (isset($o['xml:lang'])) {
                         $oArray['lang'] = $o['xml:lang'];
                     }
                     $triples["{$s}"]["{$p}"][] = $oArray;
                 } else {
                     $triples["{$s}"]["{$p}"][] = array('type' => $o['type'], 'value' => $o['value']);
                 }
             }
             $counter++;
         }
         if ($counter < 1000) {
             break;
         }
         $offset += 1000;
     }
     return json_encode($triples);
 }
 public function onPropertiesActionTemplate($event)
 {
     $store = Erfurt_App::getInstance()->getStore();
     $config = Erfurt_App::getInstance()->getConfig();
     $model = $event->model;
     $graph = $event->graph;
     $resource = $event->resource;
     $predicates = $model->getPredicates();
     $description = $resource->getDescription();
     if ($this->_privateConfig->template->restrictive) {
         foreach ($description as $resource) {
             if (isset($resource[EF_RDF_TYPE])) {
                 $type = $resource[EF_RDF_TYPE][0]['value'];
             } else {
                 return false;
             }
         }
         $query = new Erfurt_Sparql_SimpleQuery();
         $query->setProloguePart('PREFIX erm: <http://vocab.ub.uni-leipzig.de/bibrm/> SELECT DISTINCT ?uri');
         $query->addFrom((string) $event->graph);
         $query->setWherePart('{?template a <' . $this->_template . '> .
                                 ?template erm:providesProperty ?uri .
                                 ?template erm:bindsClass <' . $type . '> .
                             } ');
         $query->setLimit('20');
         $result = $store->sparqlQuery($query);
     }
     if (!empty($result)) {
         // flatten Array and flip keys with values to use array_intersect_key
         $result = array_map(function ($x) {
             return array_flip($x);
         }, $result);
         // FIXME Find a method to add standard properties which will be
         // displayed by default
         $result[] = array(EF_RDF_TYPE => "bla");
         $result[] = array(EF_RDFS_LABEL => "bla");
         $newResult = array();
         foreach ($result as $newKey => $newValue) {
             $newResult = array_merge($newResult, $newValue);
         }
         $matched = array_intersect_key($predicates[(string) $graph], $newResult);
         $matched = array((string) $graph => $matched);
         $event->predicates = $matched;
     } else {
         return false;
     }
     return true;
 }
 /**
  * Searches for properties in the local database.
  *
  * @param array  $termsArray
  * @param string $modelUri
  * @param int    $limit
  *
  * @return array
  */
 private function _searchLocalPropertiesOnly(array $termsArray, $modelUri, $limit)
 {
     require_once 'Erfurt/Sparql/SimpleQuery.php';
     $query = new Erfurt_Sparql_SimpleQuery();
     $query->setProloguePart('SELECT DISTINCT ?uri ?o');
     if (null !== $modelUri) {
         $query->addFrom($modelUri);
     }
     $where = '{ { ?uri ?p ?o . ?uri <' . EF_RDF_TYPE . '> ?o2 .
         FILTER (
             sameTerm(?o2, <http://www.w3.org/1999/02/22-rdf-syntax-ns#Property>) ||
             sameTerm(?o2, <http://www.w3.org/2002/07/owl#DatatypeProperty>) ||
             sameTerm(?o2, <http://www.w3.org/2002/07/owl#ObjectProperty>)
         )
         FILTER ((';
     $uriRegexFilter = array();
     foreach ($termsArray as $t) {
         $uriRegexFilter[] = 'regex(str(?uri), "' . $t . '", "i")';
     }
     $where .= implode(' && ', $uriRegexFilter) . ') || (isLiteral(?o) && ';
     $oRegexFilter = array();
     foreach ($termsArray as $t) {
         $oRegexFilter[] = 'regex(?o, "' . $t . '", "i")';
     }
     $where .= implode(' && ', $oRegexFilter) . ')) } UNION {';
     $where .= '?s ?uri ?o .
               FILTER (';
     $where .= implode(' && ', $uriRegexFilter) . ') } }';
     $query->setWherePart($where);
     $query->setOrderClause('?uri');
     $query->setLimit($limit);
     $store = Erfurt_App::getInstance()->getStore();
     $queryResult = $store->sparqlQuery($query, array('result_format' => 'extended'));
     $tempResult = array();
     foreach ($queryResult['results']['bindings'] as $row) {
         if ($row['o']['type'] === 'literal') {
             $weight = $this->_getWeight($termsArray, $row['uri']['value'], $row['o']['value']);
         } else {
             $weight = $this->_getWeight($termsArray, $row['uri']['value']);
         }
         if (isset($tempResult[$row['uri']['value']])) {
             if ($weight > $tempResult[$row['uri']['value']]) {
                 $tempResult[$row['uri']['value']] = $weight;
             }
         } else {
             $tempResult[$row['uri']['value']] = $weight;
         }
     }
     arsort($tempResult);
     require_once 'OntoWiki/Model/TitleHelper.php';
     require_once 'OntoWiki/Utils.php';
     if (null !== $modelUri) {
         $model = $store->getModel($modelUri, false);
         $titleHelper = new OntoWiki_Model_TitleHelper($model);
     } else {
         $titleHelper = new OntoWiki_Model_TitleHelper();
     }
     $titleHelper->addResources(array_keys($tempResult));
     $translate = $this->_owApp->translate;
     $result = array();
     foreach ($tempResult as $uri => $w) {
         $title = $titleHelper->getTitle($uri);
         if (null !== $title) {
             $result[$uri] = str_replace('|', '&Iota;', $title) . '|' . $uri . '|' . $translate->_('Local Search');
         } else {
             $result[$uri] = OntoWiki_Utils::compactUri($uri) . $uri . '|' . $translate->_('Local Search');
         }
     }
     return $result;
 }
예제 #5
0
 protected function _serializeResource($resource, $useAc = true, $level = 0)
 {
     require_once 'Erfurt/Sparql/SimpleQuery.php';
     $query = new Erfurt_Sparql_SimpleQuery();
     $query->setProloguePart('SELECT ?s ?p ?o');
     $query->addFrom($this->_graphUri);
     $query->setWherePart('WHERE { ?s ?p ?o . FILTER (sameTerm(?s, <' . $resource . '>))}');
     $query->setOrderClause('?s ?p ?o');
     $query->setLimit(1000);
     $offset = 0;
     $bnObjects = array();
     while (true) {
         $query->setOffset($offset);
         $result = $this->_store->sparqlQuery($query, array('result_format' => 'extended', 'use_owl_imports' => false, 'use_additional_imports' => false, 'use_ac' => $useAc));
         foreach ($result['results']['bindings'] as $row) {
             $s = $row['s']['value'];
             $p = $row['p']['value'];
             $o = $row['o']['value'];
             $sType = $row['s']['type'];
             $oType = $row['o']['type'];
             $lang = isset($row['o']['xml:lang']) ? $row['o']['xml:lang'] : null;
             $dType = isset($row['o']['datatype']) ? $row['o']['datatype'] : null;
             if ($oType === 'bnode') {
                 $bnObjects[] = substr($o, 2);
             }
             $this->_handleStatement($s, $p, $o, $sType, $oType, $lang, $dType);
         }
         if (count($result['results']['bindings']) < 1000) {
             break;
         }
         $offset += 1000;
     }
     $this->_forceWrite();
     // SCBD -> Write Bnodes, too
     if ($level <= 10) {
         foreach ($bnObjects as $bn) {
             $this->_serializeResource($bn, $useAc, $level + 1);
         }
     }
     // We only return SCBD of the TOP resource...
     if ($level > 0) {
         return;
     }
     // SCBD: Do the same for all Resources, that have the resource as object
     $query = new Erfurt_Sparql_SimpleQuery();
     $query->setProloguePart('SELECT ?s ?p ?o');
     $query->addFrom($this->_graphUri);
     $query->setWherePart('WHERE { ?s ?p ?o . ?s ?p2 ?o2 . FILTER (sameTerm(?o2, <' . $resource . '>)) }');
     $query->setOrderClause('?s ?p ?o');
     $query->setLimit(1000);
     $offset = 0;
     $bnObjects = array();
     while (true) {
         $query->setOffset($offset);
         $result = $this->_store->sparqlQuery($query, array('result_format' => 'extended', 'use_owl_imports' => false, 'use_additional_imports' => false, 'use_ac' => $useAc));
         foreach ($result['results']['bindings'] as $row) {
             $s = $row['s']['value'];
             $p = $row['p']['value'];
             $o = $row['o']['value'];
             $sType = $row['s']['type'];
             $oType = $row['o']['type'];
             $lang = isset($row['o']['xml:lang']) ? $row['o']['xml:lang'] : null;
             $dType = isset($row['o']['datatype']) ? $row['o']['datatype'] : null;
             if ($oType === 'bnode') {
                 $bnObjects[] = substr($o, 2);
             }
             $this->_handleStatement($s, $p, $o, $sType, $oType, $lang, $dType);
         }
         if (count($result['results']['bindings']) < 1000) {
             break;
         }
         $offset += 1000;
     }
     $this->_forceWrite();
     // SCBD -> Write Bnodes, too
     if ($level <= 10) {
         foreach ($bnObjects as $bn) {
             $this->_serializeResource($bn, $useAc, $level + 1);
         }
     }
 }