public function testInitializeWithInvalidFormats() { $negativeFormats = array('noVALIDformat', '123456789', 'rdf-xml', 'n 3', 'andsoon'); $failCount = 0; foreach ($negativeFormats as $format) { try { $object = new Erfurt_Syntax_RdfSerializer(); $object->initializeWithFormat($format); // We should not reach this point. $this->fail('Initialization with format should fail.'); } catch (Exception $e) { $failCount++; } } $this->assertEquals(count($negativeFormats), $failCount); }
/** * @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; }
public function usersAction() { // TODO Make sure that no sensilbe information (pw) is exported... $this->_helper->viewRenderer->setNoRender(); $this->_helper->layout->disableLayout(); if (null === $this->_request->id) { echo '"id" parameter is missing.'; return; } $id = $this->_config->urlBase . 'auth/users/id/' . $this->_request->id; $modelUri = 'http://localhost/OntoWiki/Config/'; $store = $this->_erfurt->getStore(); require_once 'Erfurt/Syntax/RdfSerializer.php'; $serializer = Erfurt_Syntax_RdfSerializer::rdfSerializerWithFormat('rdfxml'); echo $serializer->serializeResourceToString($id, $modelUri, false, false); $response = $this->getResponse(); $response->setHeader('Content-Type', 'application/rdf+xml', true); }
/** * * @param string $modelIri * @param string $serializationType One of: * - 'xml' * - 'n3' or 'nt' * @param mixed $filename Either a string containing a absolute filename or null. In case null is given, * this method returns a string containing the serialization. * * @return string/null */ public function exportRdf($modelIri, $serializationType = 'xml', $filename = null) { $serializationType = strtolower($serializationType); // check whether model is available if (!$this->isModelAvailable($modelIri)) { throw new Erfurt_Store_Exception('Model "' . $modelIri . '" cannot be exported. Model is not available.'); } if (in_array($serializationType, $this->_backendAdapter->getSupportedExportFormats())) { return $this->_backendAdapter->exportRdf($modelIri, $serializationType, $filename); } else { $serializer = Erfurt_Syntax_RdfSerializer::rdfSerializerWithFormat($serializationType); return $serializer->serializeGraphToString($modelIri); } }
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); }
/** * 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 static function rdfSerializerWithFormat($format) { $serializer = new Erfurt_Syntax_RdfSerializer(); $serializer->initializeWithFormat($format); return $serializer; }
public function editAction() { $store = $this->_owApp->erfurt->getStore(); $resource = $this->_owApp->selectedResource; $translate = $this->_owApp->translate; $allowSaving = false; $showList = false; // window title if (!$resource) { $this->_owApp->appendMessage(new OntoWiki_Message("No resource selected", OntoWiki_Message::WARNING)); $title = 'RDF Source'; } else { $title = $resource->getTitle() ? $resource->getTitle() : OntoWiki_Utils::contractNamespace($resource->getIri()); } $windowTitle = sprintf($translate->_('Source of Statements about %1$s') . ' (' . $translate->_('without imported statements') . ')', $title); $this->view->placeholder('main.window.title')->set($windowTitle); // check for N3 capability if (array_key_exists('ttl', $store->getSupportedImportFormats())) { $allowSaving = true; } else { $this->_owApp->appendMessage(new OntoWiki_Message("Store adapter cannot handle TTL.", OntoWiki_Message::WARNING)); } if (!$this->_owApp->selectedModel || !$this->_owApp->selectedModel->isEditable()) { $allowSaving = false; $this->_owApp->appendMessage(new OntoWiki_Message('No model selected or no permissions to edit this model.', OntoWiki_Message::WARNING)); } if ($this->_owApp->lastRoute === 'instances') { $allowSaving = false; $this->_owApp->appendMessage(new OntoWiki_Message("Modifications of a list currently not supported.", OntoWiki_Message::WARNING)); $showList = true; } // do not show edit stuff if model is not writeable if ($this->_owApp->erfurt->getAc()->isModelAllowed('edit', $this->_owApp->selectedModel)) { $allowSaving = true; } else { $allowSaving = false; } if ($allowSaving) { // toolbar $toolbar = $this->_owApp->toolbar; $toolbar->appendButton(OntoWiki_Toolbar::SUBMIT, array('name' => 'Save Source', 'id' => 'savesource')); $this->view->placeholder('main.window.toolbar')->set($toolbar); } else { $this->_owApp->appendMessage(new OntoWiki_Message("Saving has been disabled.", OntoWiki_Message::WARNING)); } // form $this->view->formActionUrl = $this->_config->urlBase . 'model/update'; $this->view->formEncoding = 'multipart/form-data'; $this->view->formClass = 'simple-input input-justify-left'; $this->view->formMethod = 'post'; $this->view->formName = 'savesource'; $this->view->readonly = $allowSaving ? '' : 'readonly="readonly"'; $this->view->graphUri = (string) $this->_owApp->selectedModel; // construct N3 $exporter = Erfurt_Syntax_RdfSerializer::rdfSerializerWithFormat('ttl'); if (!$showList) { $source = $exporter->serializeResourceToString((string) $this->_owApp->selectedResource, (string) $this->_owApp->selectedModel); } else { $listHelper = Zend_Controller_Action_HelperBroker::getStaticHelper('List'); $listName = "instances"; if ($listHelper->listExists($listName)) { $list = $listHelper->getList($listName); } else { $this->_owApp->appendMessage(new OntoWiki_Message('something went wrong with the list of instances you want to rdf-view', OntoWiki_Message::ERROR)); } $source = $exporter->serializeQueryResultToString(clone $list->getResourceQuery(), (string) $this->_owApp->selectedModel); } $this->view->source = $source; $url = new OntoWiki_Url(array('route' => 'properties'), array()); $url->setParam('r', (string) $resource, true); $this->view->redirectUri = urlencode((string) $url); }
/** * 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."); } }
/** * returns the serialized representation (string) of this resource according * to the notation parameter. It always uses the pretty format option * and the access control. * * @param string $notation the specified notation (see Erfurt_Syntax_RdfSerializer * for possible arguments) * * @return string the representation of this resource in a specified notation */ public function serialize($notation = 'xml') { $modelIri = $this->_model ? $this->_model->getModelIri() : ""; require_once 'Erfurt/Syntax/RdfSerializer.php'; $serializer = Erfurt_Syntax_RdfSerializer::rdfSerializerWithFormat($notation); return $serializer->serializeResourceToString($this->getIri(), $this->_model->getModelIri(), true); }