/** * @desc get a resource as RDF/JSON * * @param string modelIri * @param string resourceIri * @param string format * * @return string */ public function get($modelIri, $resourceIri, $format = 'rdfjson') { if (!$this->_store->isModelAvailable($modelIri)) { return 'Error: Model "' . $modelIri . '" is not available.'; } $editable = $this->_store->getModel($modelIri)->isEditable(); $supportedFormats = Erfurt_Syntax_RdfSerializer::getSupportedFormats(); if (!isset($supportedFormats[$format])) { return 'Error: Format "' . $format . '" is not supported by serializer.'; } $serializer = Erfurt_Syntax_RdfSerializer::rdfSerializerWithFormat($format); // create hash for current status of resource $currentDataHash = $this->_getCurrentResourceHash($modelIri, $resourceIri); $return = new stdClass(); $return->dataHash = $currentDataHash; $return->editable = $editable; $return->data = $serializer->serializeResourceToString($resourceIri, $modelIri); return $return; }
/** * Create the (context) menu for resource and fill it with its default entries */ private function _getResourceMenu($resource = null) { $owApp = OntoWiki::getInstance(); if ($resource === null) { $resource = $owApp->selectedResource; } $config = $owApp->config; $resourceMenu = new OntoWiki_Menu(); // Add the class Menu if the current resource is a class $classMenu = $this->_getClassMenu($resource)->toArray(); foreach ($classMenu as $key => $value) { $resourceMenu->appendEntry($key, $value); } if (count($classMenu) > 0) { $resourceMenu->appendEntry(OntoWiki_Menu::SEPARATOR); } // View resource $url = new OntoWiki_Url(array('action' => 'view'), array()); $url->setParam('r', $resource, true); $resourceMenu->appendEntry('View Resource', (string) $url); // Edit entries if ($owApp->erfurt->getAc()->isModelAllowed('edit', $owApp->selectedModel)) { // edit resource option $resourceMenu->appendEntry('Edit Resource', 'javascript:editResourceFromURI(\'' . (string) $resource . '\')'); // Delete resource option $url = new OntoWiki_Url(array('controller' => 'resource', 'action' => 'delete'), array()); $url->setParam('r', $resource, true); $resourceMenu->appendEntry('Delete Resource', (string) $url); } $resourceMenu->appendEntry('Go to Resource (external)', (string) $resource); $resourceMenu->appendEntry(OntoWiki_Menu::SEPARATOR); foreach (Erfurt_Syntax_RdfSerializer::getSupportedFormats() as $key => $format) { $resourceMenu->appendEntry('Export Resource as ' . $format, $config->urlBase . 'resource/export/f/' . $key . '?r=' . urlencode($resource)); } return $resourceMenu; }
public function exportAction() { $this->_addLastModifiedHeader(); $this->_helper->viewRenderer->setNoRender(); $this->_helper->layout->disableLayout(); if (isset($this->_request->m)) { $modelUri = $this->_request->m; } else { if (isset($this->_owApp->selectedModel)) { $modelUri = $this->_owApp->selectedModel->getModelUri(); } else { $response = $this->getResponse(); $response->setRawHeader('HTTP/1.0 400 Bad Request'); throw new OntoWiki_Controller_Exception("No model given."); } } $resource = $this->getParam('r', true); // Check whether the f parameter is given. If not: default to rdf/xml if (!isset($this->_request->f)) { $format = 'rdfxml'; } else { $format = $this->_request->f; } $format = Erfurt_Syntax_RdfSerializer::normalizeFormat($format); $store = $this->_erfurt->getStore(); // Check whether given format is supported. If not: 400 Bad Request. if (!in_array($format, array_keys(Erfurt_Syntax_RdfSerializer::getSupportedFormats()))) { $response = $this->getResponse(); $response->setRawHeader('HTTP/1.0 400 Bad Request'); throw new OntoWiki_Controller_Exception("Format '{$format}' not supported."); } // Check whether model exists. If not: 404 Not Found. if (!$store->isModelAvailable($modelUri, false)) { $response = $this->getResponse(); $response->setRawHeader('HTTP/1.0 404 Not Found'); throw new OntoWiki_Controller_Exception("Model '{$modelUri}' not found."); } // Check whether model is available (with acl). If not: 403 Forbidden. if (!$store->isModelAvailable($modelUri)) { $response = $this->getResponse(); $response->setRawHeader('HTTP/1.0 403 Forbidden'); throw new OntoWiki_Controller_Exception("Model '{$modelUri}' not available."); } $filename = 'export' . date('Y-m-d_Hi'); switch ($format) { case 'rdfxml': $contentType = 'application/rdf+xml'; $filename .= '.rdf'; break; case 'rdfn3': $contentType = 'text/rdf+n3'; $filename .= '.n3'; break; case 'rdfjson': $contentType = 'application/json'; $filename .= '.json'; break; case 'turtle': $contentType = 'application/x-turtle'; $filename .= '.ttl'; break; } /* * Event: allow for adding / deleting statements to the export * event uses a memory model and gets an empty memory model as * default value, all plugins should add statements to the existing * value and should not create a new model as return value */ $event = new Erfurt_Event('beforeExportResource'); $event->resource = $resource; $event->modelUri = $modelUri; $event->setDefault = new Erfurt_Rdf_MemoryModel(); $addedModel = $event->trigger(); if (is_object($addedModel) && get_class($addedModel) == 'Erfurt_Rdf_MemoryModel') { $addedStatements = $addedModel->getStatements(); } else { $addedStatements = array(); } $response = $this->getResponse(); $response->setHeader('Content-Type', $contentType, true); $response->setHeader('Content-Disposition', 'filename="' . $filename . '"'); $serializer = Erfurt_Syntax_RdfSerializer::rdfSerializerWithFormat($format); echo $serializer->serializeResourceToString($resource, $modelUri, false, true, $addedStatements); }
/** * Serializes a given model or (if supported) all models into a given format. */ public function exportAction() { if (!$this->_owApp->erfurt->getAc()->isActionAllowed(Erfurt_Ac_Default::ACTION_MODEL_EXPORT)) { $this->_owApp->appendMessage(new OntoWiki_Message('Model export not allowed.', OntoWiki_Message::ERROR)); $this->_redirect($_SERVER['HTTP_REFERER'], array('code' => 302)); return; } // Check whether the f parameter is given. If not: default to rdf/xml if (!isset($this->_request->f)) { $format = 'rdfxml'; } else { $format = $this->_request->f; } $format = Erfurt_Syntax_RdfSerializer::normalizeFormat($format); $store = $this->_erfurt->getStore(); // Check whether given format is supported. If not: 400 Bad Request. if (!in_array($format, array_keys(Erfurt_Syntax_RdfSerializer::getSupportedFormats()))) { $response = $this->getResponse(); $response->setRawHeader('HTTP/1.0 400 Bad Request'); throw new OntoWiki_Controller_Exception("Format '{$format}' not supported."); } // Check whether a model uri is given if (isset($this->_request->m)) { $modelUri = $this->_request->m; // Check whether model exists. If not: 404 Not Found. if (!$store->isModelAvailable($modelUri, false)) { $response = $this->getResponse(); $response->setRawHeader('HTTP/1.0 404 Not Found'); throw new OntoWiki_Controller_Exception("Model '{$modelUri}' not found."); } // Check whether model is available (with acl). If not: 403 Forbidden. if (!$store->isModelAvailable($modelUri)) { $response = $this->getResponse(); $response->setRawHeader('HTTP/1.0 403 Forbidden'); throw new OntoWiki_Controller_Exception("Model '{$modelUri}' not available."); } $filename = 'export' . date('Y-m-d_Hi'); switch ($format) { case 'rdfxml': $contentType = 'application/rdf+xml'; $filename .= '.rdf'; break; case 'rdfn3': $contentType = 'text/rdf+n3'; $filename .= '.n3'; break; case 'rdfjson': $contentType = 'application/json'; $filename .= '.json'; break; case 'turtle': $contentType = 'application/x-turtle'; $filename .= '.ttl'; break; } $this->_helper->viewRenderer->setNoRender(); $this->_helper->layout->disableLayout(); $response = $this->getResponse(); $response->setHeader('Content-Type', $contentType, true); $response->setHeader('Content-Disposition', 'filename="' . $filename . '"'); $serializer = Erfurt_Syntax_RdfSerializer::rdfSerializerWithFormat($format); echo $serializer->serializeGraphToString($modelUri); return; } else { // Else use all available models. // TODO Exporters need to support this feature... $response = $this->getResponse(); $response->setRawHeader('HTTP/1.0 400 Bad Request'); throw new OntoWiki_Controller_Exception("No Graph URI given."); } }