Exemplo n.º 1
0
 /**
  * This method is called, when the onIsDispatchable event was triggered.
  *
  * The onIsDispatchable event is fired in an early stage of the OntoWiki
  * request lifecycle. Hence it is not decided in that moment, which controller
  * an action will be used.
  *
  * The given Erfurt_Event object has an uri property, which contains the
  * requested URI. The method then checks if a resource identified by that
  * URI exists in the local store. Iff this is the case it sends a redirect
  * to another URL depending on the requested MIME type.
  *
  * $event->uri contains the request URI.
  *
  * @param Erfurt_Event $event The event containing the required parameters.
  *
  * @return boolean false if the request was not handled, i.e. no resource was found.
  */
 public function onIsDispatchable($event)
 {
     $store = null;
     try {
         $store = OntoWiki::getInstance()->erfurt->getStore();
     } catch (Exception $e) {
         // if we can't get the store, we do nothing
         return;
     }
     $request = Zend_Controller_Front::getInstance()->getRequest();
     $response = Zend_Controller_Front::getInstance()->getResponse();
     $aliascheck = new Erfurt_Event('onResolveDomainAliasInput');
     $aliascheck->uri = $event->uri;
     $aliascheck->trigger();
     $uri = $aliascheck->uri;
     try {
         // Check for a supported type by investigating the suffix of the URI or by
         // checking the Accept header (content negotiation). The $matchingSuffixFlag
         // parameter contains true if the suffix was used instead of the Accept header.
         $matchingSuffixFlag = false;
         $type = $this->_getTypeForRequest($request, $uri, $matchingSuffixFlag);
         // We need a readable graph to query. We use the first graph that was found.
         // If no readable graph is available for the current user, we cancel here.
         list($graph, $matchedUri) = $this->_matchGraphAndUri($uri);
         if (!$graph || !$matchedUri) {
             // URI not found
             return false;
         }
         if ($uri !== $matchedUri) {
             // Re-append faux file extension
             if ($matchingSuffixFlag) {
                 $matchedUri .= '.' . $type;
             }
             // Redirect to new (correct URI)
             $response->setRedirect((string) $matchedUri, 301);
             return;
         }
         // Prepare for redirect according to the given type.
         $url = null;
         // This will contain the URL to redirect to.
         switch ($type) {
             case 'rdf':
             case 'n3':
             case 'ttl':
                 // Check the config, whether provenance information should be included.
                 $prov = false;
                 if (isset($this->_privateConfig->provenance) && isset($this->_privateConfig->provenance->enabled)) {
                     $prov = (bool) $this->_privateConfig->provenance->enabled;
                 }
                 // Special case: If the graph URI is identical to the requested URI, we export
                 // the whole graph instead of only data regarding the resource.
                 if ($graph === $uri) {
                     $controllerName = 'model';
                     $actionName = 'export';
                 } else {
                     $controllerName = 'resource';
                     $actionName = 'export';
                 }
                 // Create a URL with the export action on the resource or model controller.
                 // Set the required parameters for this action.
                 $url = new OntoWiki_Url(array('controller' => $controllerName, 'action' => $actionName), array());
                 $url->setParam('r', $uri, true)->setParam('f', $type)->setParam('m', $graph)->setParam('provenance', $prov);
                 break;
             case 'html':
             default:
                 // Defaults to the standard property view.
                 // Set the required parameters for this action.
                 $url = new OntoWiki_Url(array('route' => 'properties'), array());
                 $url->setParam('r', $uri, true)->setParam('m', $graph);
                 break;
         }
         // Make $graph the active graph (session required) and make the resource
         // in $uri the active resource.
         $activeModel = $store->getModel($graph);
         OntoWiki::getInstance()->selectedModel = $activeModel;
         OntoWiki::getInstance()->selectedResource = new OntoWiki_Resource($uri, $activeModel);
         // Mark the request as dispatched, since we have all required information now.
         $request->setDispatched(true);
         // Give plugins a chance to do something before redirecting.
         $event = new Erfurt_Event('onBeforeLinkedDataRedirect');
         $event->response = $response;
         $event->trigger();
         // Give plugins a chance to handle the redirection instead of doing it here.
         $event = new Erfurt_Event('onShouldLinkedDataRedirect');
         $event->request = $request;
         $event->response = $response;
         $event->type = $type;
         $event->uri = $uri;
         $event->flag = $matchingSuffixFlag;
         $event->setDefault(true);
         $shouldRedirect = $event->trigger();
         if ($shouldRedirect) {
             // set redirect and send immediately
             $response->setRedirect((string) $url, 303);
             return;
         }
         return !$shouldRedirect;
         // will default to false
     } catch (Erfurt_Exception $e) {
         // don't handle errors since other plug-ins
         // could chain this event
         return false;
     }
 }
Exemplo n.º 2
0
 /**
  * Returns the property values for all resources at once.
  * This method generate one SPARQL query to receive all values .
  * Original Name was getValues()
  * @return array
  */
 public function getValuesBySingleQuery()
 {
     if ($this->_valuesUptodate) {
         return $this->_values;
     } else {
         $this->updateValueQuery();
     }
     if (empty($this->_resources)) {
         return array();
     }
     $this->getResults();
     $result = array();
     if (isset($this->_results['results']['bindings'])) {
         $result = $this->_results['results']['bindings'];
     }
     //fill titlehelper
     foreach ($result as $row) {
         foreach ($this->_shownProperties as $property) {
             if (isset($row[$property['varName']]) && $row[$property['varName']]['type'] == 'uri' && substr($row[$property['varName']]['value'], 0, 2) != '_:') {
                 $this->_titleHelper->addResource($row[$property['varName']]['value']);
             }
         }
         if (isset($row['__TYPE']) && $row['__TYPE']['type'] == 'uri') {
             $this->_titleHelper->addResource($row['__TYPE']['value']);
         }
         $this->_titleHelper->addResource($row[$this->_resourceVar->getName()]['value']);
     }
     $valueResults = array();
     foreach ($result as $row) {
         $resourceUri = $row[$this->_resourceVar->getName()]['value'];
         if (!array_key_exists($resourceUri, $valueResults)) {
             $valueResults[$resourceUri] = array();
         }
         $url = new OntoWiki_Url(array('route' => 'properties'), array('r'));
         $value = null;
         $link = null;
         foreach ($row as $varName => $data) {
             if (!isset($valueResults[$resourceUri][$varName])) {
                 $valueResults[$resourceUri][$varName] = array();
             }
             if ($data['type'] == 'uri') {
                 if (substr($data['value'], 0, 2) == '_:') {
                     continue;
                     // skip blanknode values here due to backend problems with filters
                 }
                 // object type is uri --> handle object property
                 $objectUri = $data['value'];
                 $url->setParam('r', $objectUri, true);
                 $link = (string) $url;
                 // set up event
                 $event = new Erfurt_Event('onDisplayObjectPropertyValue');
                 //find uri
                 foreach ($this->_shownProperties as $property) {
                     if ($varName == $property['varName']) {
                         $event->property = $property['uri'];
                     }
                 }
                 $event->value = $objectUri;
                 // trigger
                 $value = $event->trigger();
                 // set default if event has not been handled
                 if (!$event->handled()) {
                     $value = $this->_titleHelper->getTitle($objectUri);
                 }
             } else {
                 // object is a literal
                 $object = $data['value'];
                 $propertyUri = null;
                 foreach ($this->_shownProperties as $property) {
                     if ($varName == $property['varName']) {
                         $propertyUri = $property['uri'];
                     }
                 }
                 if ($object !== null) {
                     // set up event
                     $event = new Erfurt_Event('onDisplayLiteralPropertyValue');
                     $event->property = $propertyUri;
                     $event->value = $object;
                     $event->setDefault($object);
                     // trigger
                     $value = $event->trigger();
                 }
             }
             //check for dulplicate values
             if (isset($valueResults[$resourceUri][$varName])) {
                 foreach ($valueResults[$resourceUri][$varName] as $old) {
                     if ($old['origvalue'] == $data['value'] && $old['type'] == $data['type']) {
                         $link = null;
                         continue 2;
                         // dont add this value
                     }
                 }
             }
             //add value
             $valueResults[$resourceUri][$varName][] = array('value' => $value, 'origvalue' => $data['value'], 'type' => $data['type'], 'url' => $link, 'uri' => $data['value']);
             $value = null;
             $link = null;
         }
     }
     foreach ($this->getShownResources() as $resource) {
         if (!isset($valueResults[$resource['value']])) {
             //there are no statements about this resource
             $valueResults[$resource['value']] = array();
         }
     }
     $this->_values = $valueResults;
     $this->_valuesUptodate = true;
     return $valueResults;
 }