Exemplo n.º 1
0
 public function serializeResourceToString($resource, $graphUri, $pretty = false, $useAc = true, array $additional = array())
 {
     require_once 'Erfurt/Sparql/SimpleQuery.php';
     $query = new Erfurt_Sparql_SimpleQuery();
     $query->setProloguePart('SELECT ?resourceUri ?p ?o');
     $query->addFrom($graphUri);
     /*
      * Why not as subject?
      * Because serializeQueryResultToString() expects ?resourceUri to be bound.
      */
     $query->setWherePart('WHERE { ?resourceUri ?p ?o . FILTER (sameTerm(?resourceUri, <' . $resource . '>))}');
     $query->setOrderClause('?resourceUri ?p ?o');
     return $this->serializeQueryResultToString($query, $graphUri, $pretty, $useAc);
 }
Exemplo n.º 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;
 }
 /**
  * Deletes one or more resources denoted by param 'r'
  * TODO: This should be done by a evolution pattern in the future
  */
 public function deleteAction()
 {
     $this->view->clearModuleCache();
     $this->_helper->viewRenderer->setNoRender();
     $this->_helper->layout->disableLayout();
     $store = $this->_erfurt->getStore();
     $model = $this->_owApp->selectedModel;
     $modelIri = (string) $model;
     $redirect = $this->_request->getParam('redirect', $this->_config->urlBase);
     if (isset($this->_request->r)) {
         $resources = $this->_request->getParam('r', array());
     } else {
         throw new OntoWiki_Exception('Missing parameter r!');
     }
     if (!is_array($resources)) {
         $resources = array($resources);
     }
     // get versioning
     $versioning = $this->_erfurt->getVersioning();
     $count = 0;
     if ($this->_erfurt->getAc()->isModelAllowed('edit', $modelIri)) {
         foreach ($resources as $resource) {
             // if we have only a nice uri, fill to full uri
             if (Zend_Uri::check($resource) == false) {
                 // check for namespace
                 if (strstr($resource, ':')) {
                     $resource = OntoWiki_Utils::expandNamespace($resource);
                 } else {
                     $resource = $model->getBaseIri() . $resource;
                 }
             }
             // action spec for versioning
             $actionSpec = array();
             $actionSpec['type'] = 130;
             $actionSpec['modeluri'] = $modelIri;
             $actionSpec['resourceuri'] = $resource;
             // starting action
             $versioning->startAction($actionSpec);
             $stmtArray = array();
             // query for all triples to delete them
             $sparqlQuery = new Erfurt_Sparql_SimpleQuery();
             $sparqlQuery->setProloguePart('SELECT ?p, ?o');
             $sparqlQuery->addFrom($modelIri);
             $sparqlQuery->setWherePart('{ <' . $resource . '> ?p ?o . }');
             $result = $store->sparqlQuery($sparqlQuery, array('result_format' => 'extended'));
             // transform them to statement array to be compatible with store methods
             foreach ($result['results']['bindings'] as $stmt) {
                 $stmtArray[$resource][$stmt['p']['value']][] = $stmt['o'];
             }
             $store->deleteMultipleStatements($modelIri, $stmtArray);
             // stopping action
             $versioning->endAction();
             $count++;
         }
         $message = $count . ' resource' . ($count != 1 ? 's' : '') . ($count ? ' successfully' : '') . ' deleted.';
         $this->_owApp->appendMessage(new OntoWiki_Message($message, OntoWiki_Message::SUCCESS));
     } else {
         $message = 'not allowed.';
         $this->_owApp->appendMessage(new OntoWiki_Message($message, OntoWiki_Message::WARNING));
     }
     $event = new Erfurt_Event('onDeleteResources');
     $event->resourceArray = $resources;
     $event->modelUri = $modelIri;
     $event->trigger();
     $this->_redirect($redirect, array('code' => 302));
 }
Exemplo n.º 5
0
 /**
  * Checks the local database, whether user exists
  */
 protected function _checkWebId($webId)
 {
     $retVal = array('userUri' => false, 'denyLogin' => false);
     // Query the store.
     require_once 'Erfurt/Sparql/SimpleQuery.php';
     $query = new Erfurt_Sparql_SimpleQuery();
     $query->setProloguePart('SELECT ?s ?p ?o');
     $query->addFrom($this->_acModelUri);
     $where = 'WHERE { 
                         ?s ?p ?o . 
                         ?s <' . EF_RDF_TYPE . '> <' . $this->_uris['user_class'] . "> .\n                            FILTER (sameTerm(?s, <{$webId}>))\n                        }";
     $query->setWherePart($where);
     $result = $this->_store->sparqlQuery($query, array('use_ac' => false));
     foreach ((array) $result as $row) {
         // Set user URI
         if ($retVal['userUri'] === false) {
             $retVal['userUri'] = $row['s'];
         }
         // Check predicates, whether needed.
         switch ($row['p']) {
             case $this->_uris['action_deny']:
                 // if login is disallowed
                 if ($row['o'] === $this->_uris['action_login']) {
                     $retVal['denyLogin'] = true;
                 }
                 break;
             case EF_RDFS_LABEL:
                 $retVal['userLabel'] = $row['o'];
                 break;
             case $this->_uris['user_username']:
                 $retVal['username'] = $row['o'];
                 break;
             case $this->_uris['user_mail']:
                 $retVal['email'] = $row['o'];
             default:
                 // Ignore all other statements.
         }
     }
     return $retVal;
 }
Exemplo n.º 6
0
 /**
  * Returns all existing sync configurations.
  *
  * @return array|bool
  */
 private function _listSyncConfigs()
 {
     if (null === $this->_syncConfigListCache) {
         $store = Erfurt_App::getInstance()->getStore();
         require_once 'Erfurt/Sparql/SimpleQuery.php';
         $query = new Erfurt_Sparql_SimpleQuery();
         $query->setProloguePart('SELECT ?s ?p ?o');
         $query->addFrom($this->_syncModelUri);
         $where = 'WHERE {
             ?s ?p ?o .
             ?s <' . EF_RDF_TYPE . '> <' . $this->_properties['syncConfigClass'] . '> . }';
         $query->setWherePart($where);
         $result = $store->sparqlQuery($query, array('use_ac' => false));
         if (count($result) === 0) {
             return false;
         }
         $retVal = array();
         foreach ($result as $row) {
             if (!isset($retVal[$row['s']])) {
                 $retVal[$row['s']] = array('uri' => $row['s']);
             }
             switch ($row['p']) {
                 case $this->_properties['targetModel']:
                     $retVal[$row['s']]['targetModel'] = $row['o'];
                     break;
                 case $this->_properties['syncResource']:
                     $retVal[$row['s']]['syncResource'] = $row['o'];
                     break;
                 case $this->_properties['wrapperName']:
                     $retVal[$row['s']]['wrapperName'] = $row['o'];
                     break;
                 case $this->_properties['lastSyncPayload']:
                     $retVal[$row['s']]['lastSyncPayload'] = unserialize($row['o']);
                     break;
                 case $this->_properties['lastSyncDateTime']:
                     $retVal[$row['s']]['lastSyncDateTime'] = $row['o'];
                     break;
                 case $this->_properties['syncQuery']:
                     $retVal[$row['s']]['syncQuery'] = $row['o'];
                     break;
                 case $this->_properties['checkHasChanged']:
                     $retVal[$row['s']]['checkHasChanged'] = (bool) $row['o'];
                     break;
             }
         }
         $cacheVal = array();
         foreach ($retVal as $s => $valueArray) {
             $hash = $this->_getHash($valueArray['syncResource'], $valueArray['wrapperName'], $valueArray['targetModel']);
             $cacheVal[$hash] = $valueArray;
         }
         $this->_syncConfigListCache = $cacheVal;
     }
     return $this->_syncConfigListCache;
 }
 /**
  * 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;
 }
Exemplo n.º 8
0
 /**
  *
  */
 public function validateHash($hash)
 {
     $config = Erfurt_App::getInstance()->getConfig();
     $store = Erfurt_App::getInstance()->getStore();
     $query = new Erfurt_Sparql_SimpleQuery();
     $query->addFrom($config->ac->modelUri);
     $query->setProloguePart('SELECT ?user');
     $query->setWherePart('{ ?user <' . $config->ac->user->recoveryHash . '> "' . $hash . '" . }');
     $resultUser = $store->sparqlQuery($query, array('use_ac' => false));
     if (!empty($resultUser)) {
         return $resultUser[0]['user'];
     } else {
         require_once 'Erfurt/Auth/Identity/Exception.php';
         throw new Erfurt_Auth_Identity_Exception('Invalid recovery session identifier.');
     }
 }
Exemplo n.º 9
0
 protected function _sparqlForListResources()
 {
     require_once 'Erfurt/Sparql/SimpleQuery.php';
     $query = new Erfurt_Sparql_SimpleQuery();
     $query->setProloguePart('SELECT ?s ?first ?rest');
     $query->addFrom($this->_graphUri);
     $query->setWherePart('WHERE { ?s <' . EF_RDF_FIRST . '> ?first . ?s <' . EF_RDF_REST . '> ?rest }');
     $result = $this->_store->sparqlQuery($query, array('result_format' => 'extended', 'use_owl_imports' => false, 'use_additional_imports' => false));
     $listArray = array();
     if ($result) {
         foreach ($result['results']['bindings'] as $row) {
             $listArray[$row['s']['value']] = array('first' => $row['first']['value'], 'rest' => $row['rest']['value']);
         }
     }
     $this->_listArray = $listArray;
 }
Exemplo n.º 10
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);
         }
     }
 }