/**
  * 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;
 }
Beispiel #2
0
 /**
  * Returns an array of predicate values for the current resource.
  * The array is indexed with the predicate's URIs.
  *
  * @return array
  */
 public function getValues()
 {
     if (null === $this->_valueResults) {
         $this->_valueResults = array();
         // get results
         $results = $this->getQueryResults();
         // load predicates first
         $this->getPredicates();
         // URL object to build URLs
         $url = new OntoWiki_Url(array('route' => 'properties'), array('r'));
         // keep track of URI objects already used
         $objects = array();
         foreach ($results as $graph => $resultsForGraph) {
             $this->_valueResults[$graph] = array();
             foreach ($resultsForGraph as $row) {
                 $predicateUri = $row['predicate']['value'];
                 if (!array_key_exists($predicateUri, $objects)) {
                     $objects[$predicateUri] = array();
                 }
                 // create space for value information if not exists
                 if (!array_key_exists($predicateUri, $this->_valueResults[$graph])) {
                     $this->_valueResults[$graph][$predicateUri] = array();
                 }
                 // default values
                 $value = array('content' => null, 'object' => null, 'object_hash' => null, 'datatype' => null, 'lang' => null, 'url' => null, 'uri' => null, 'curi' => null);
                 switch ($row['object']['type']) {
                     case 'uri':
                         // every URI objects is only used once for each statement
                         if (in_array($row['object']['value'], $objects[$predicateUri])) {
                             continue;
                         }
                         // URL
                         $url->setParam('r', $row['object']['value'], true);
                         $value['url'] = (string) $url;
                         // URI
                         $value['uri'] = $row['object']['value'];
                         // title
                         $title = $this->_titleHelper->getTitle($row['object']['value']);
                         /**
                          * @trigger onDisplayObjectPropertyValue Triggered if an object value of some
                          * property is returned. Plugins can attach to this trigger in order to modify
                          * the value that gets displayed.
                          * Event payload: value, property, title and link
                          */
                         // set up event
                         $event = new Erfurt_Event('onDisplayObjectPropertyValue');
                         $event->value = $row['object']['value'];
                         $event->property = $predicateUri;
                         $event->title = $title;
                         $event->link = (string) $url;
                         // trigger
                         $value['object'] = $event->trigger();
                         if (!$event->handled()) {
                             // object (modified by plug-ins)
                             $value['object'] = $title;
                         }
                         array_push($objects[$predicateUri], $row['object']['value']);
                         break;
                     case 'typed-literal':
                         $event = new Erfurt_Event('onDisplayLiteralPropertyValue');
                         $value['datatype'] = OntoWiki_Utils::compactUri($row['object']['datatype']);
                         $literalString = Erfurt_Utils::buildLiteralString($row['object']['value'], $row['object']['datatype']);
                         $value['object_hash'] = md5($literalString);
                         $event->value = $row['object']['value'];
                         $event->datatype = $row['object']['datatype'];
                         $event->property = $predicateUri;
                         $value['object'] = $event->trigger();
                         // keep unmodified value in content
                         $value['content'] = $row['object']['value'];
                         if (!$event->handled()) {
                             // object (modified by plug-ins)
                             $value['object'] = $row['object']['value'];
                         }
                         break;
                     case 'literal':
                         // original (unmodified) for RDFa
                         $value['content'] = $row['object']['value'];
                         $literalString = Erfurt_Utils::buildLiteralString($row['object']['value'], null, isset($row['object']['xml:lang']) ? $row['object']['xml:lang'] : null);
                         $value['object_hash'] = md5($literalString);
                         /**
                          * @trigger onDisplayLiteralPropertyValue Triggered if a literal value of some
                          * property is returned. Plugins can attach to this trigger in order to modify
                          * the value that gets displayed.
                          */
                         $event = new Erfurt_Event('onDisplayLiteralPropertyValue');
                         $event->value = $row['object']['value'];
                         $event->property = $predicateUri;
                         // set literal language
                         if (isset($row['object']['xml:lang'])) {
                             $value['lang'] = $row['object']['xml:lang'];
                             $event->language = $row['object']['xml:lang'];
                         }
                         // trigger
                         $value['object'] = $event->trigger();
                         // keep unmodified value in content
                         $value['content'] = $row['object']['value'];
                         // set default if event has not been handled
                         if (!$event->handled()) {
                             $value['object'] = $row['object']['value'];
                         }
                         break;
                 }
                 // push it only if it doesn't exceed number of items to display
                 if (count($this->_valueResults[$graph][$predicateUri]) < $this->_limit) {
                     array_push($this->_valueResults[$graph][$predicateUri], $value);
                 } else {
                     $this->_predicateResults[$graph][$predicateUri]['has_more'] = true;
                 }
                 if (count($this->_valueResults[$graph][$predicateUri]) > 1) {
                     // create the "has more link" (used for area context menu as well)
                     // do it only once per predicate
                     if (!isset($this->_predicateResults[$graph][$predicateUri]['has_more_link'])) {
                         //when all values are literal, we dont use a link to the list,but to the query editor
                         $allValuesAreLiterals = true;
                         foreach ($this->_valueResults[$graph][$predicateUri] as $value) {
                             if (isset($value['uri'])) {
                                 $allValuesAreLiterals = false;
                             }
                         }
                         if (!$allValuesAreLiterals) {
                             $hasMoreUrl = new OntoWiki_Url(array('route' => 'instances', 'action' => 'list'), array());
                             $filterExp = json_encode(array('filter' => array(array('action' => 'add', 'mode' => 'box', 'id' => 'allvalues', 'property' => $predicateUri, 'isInverse' => true, 'propertyLabel' => "value", 'filter' => 'equals', 'value1' => $this->_uri, 'value2' => null, 'valuetype' => 'uri', 'literaltype' => null, 'hidden' => false))));
                             $hasMoreUrl->setParam('instancesconfig', $filterExp)->setParam('init', true);
                         } else {
                             $hasMoreUrl = new OntoWiki_Url(array('controller' => 'queries', 'action' => 'editor'), array());
                             $hasMoreUrl->setParam('query', 'SELECT ?value WHERE {<' . $this->_uri . '> <' . $predicateUri . '> ?value}')->setParam('immediate', true);
                         }
                         $this->_predicateResults[$graph][$predicateUri]['has_more_link'] = (string) $hasMoreUrl;
                     }
                 }
             }
         }
         return $this->_valueResults;
     }
 }
Beispiel #3
0
 public function curie($uri)
 {
     $curi = OntoWiki_Utils::compactUri($uri);
     return $curi;
 }