public function headScriptExhibit($propertyInfo = null) { if ($propertyInfo == null) { // set default info $propertyInfo = $this->_defaultPropertyInfo; } else { if (is_string($propertyInfo)) { // transform string to array $propertyInfo = array(array('uri' => $propertyInfo)); } else { if (is_array($propertyInfo) && isset($propertyInfo['uri'])) { // transform single level array to multi-hierarchy form $propertyInfo = array($propertyInfo); } } } // if we have something completly different as option if (!is_array($propertyInfo)) { return; } $description = new Erfurt_Rdf_MemoryModel($this->view->description); $resourceUri = $this->view->resourceUri; $propertyUri = null; $literalMod = null; // the main loop to search for a valid property value // first hit is taken and every other value is not used foreach ($propertyInfo as $info) { if (isset($info['uri'])) { $propertyUri = $info['uri']; $literalMod = isset($info['sprintf']) ? $info['sprintf'] : null; // check for exhibit data URI and integrate this as well as exhibit if ($description->hasSP($resourceUri, $propertyUri)) { // we've found something, so we can add the exhibit script echo ' <script src="' . $this->_exhibitScript . '" type="text/javascript"></script>' . PHP_EOL; $value = $description->getValue($resourceUri, $propertyUri); if ($literalMod != null) { $value = sprintf($literalMod, $value); } // output the data script and return $type = "application/jsonp"; $rel = "exhibit/data"; echo ' <link href="' . $value . '" type="' . $type . '" rel="' . $rel . '" ex:jsonp-callback="cb" />'; echo PHP_EOL; return; } } } }
/** * fetches all titles according the given array if Uris * * @param array resourceUris */ private function _fetchTitlesFromResourcePool($resourceUris) { $resourcePool = $this->_erfurtApp->getResourcePool(); $resources = array(); if (!empty($this->_model)) { $modelUri = $this->_model->getModelIri(); $resources = $resourcePool->getResources($resourceUris, $modelUri); } else { $resources = $resourcePool->getResources($resourceUris); } $memoryModel = new Erfurt_Rdf_MemoryModel(); foreach ($resources as $resourceUri => $resource) { $resourceDescription = $resource->getDescription(); $memoryModel->addStatements($resourceDescription); $found = false; foreach ($this->_titleProperties as $titleProperty) { $values = $memoryModel->getValues($resourceUri, $titleProperty); foreach ($values as $value) { if (!empty($value['lang'])) { $language = $value['lang']; } else { $language = ''; } $this->_resources[$resourceUri][$titleProperty][$language] = $value['value']; } } } }
/** * read a private config part from a doap Erfurt_Rdf_MemoryModel (recursive) * * @static * * @param $memModel * @param $bnUri * @param $privateNS * @param $mapping * * @return array */ private static function getSubConfig(Erfurt_Rdf_MemoryModel $memModel, $bnUri, $privateNS, $mapping) { $kv = array(); $name = $memModel->getValue($bnUri, self::$_owconfigNS . 'id'); if ($name == null) { return array(); } foreach ($memModel->getPO($bnUri) as $key => $values) { if ($key == EF_RDF_TYPE || $key == self::$_owconfigNS . 'id') { continue; } if ($key == self::$_owconfigNS . 'config') { foreach ($values as $value) { $kv = array_merge($kv, self::getSubConfig($memModel, $value['value'], $privateNS, $mapping)); } } else { $mappedKey = self::getPrivateKey($key, $privateNS, $mapping); foreach ($values as $value) { $value = self::getValue($value, $memModel); self::addValue($mappedKey, $value, $kv); } } } $r = array($name => $kv); return $r; }
/** * fetches the PHP/RDF statments array description array * * @param string $resourceIri The Iri, which identifies the resource. * @param string|false $modelIri The Iri, which identifies the model or * false for store wide descriptions * @param array $options Array of different options: * Erfurt_Store::USE_AC = true|false - use access control * maxDepth = int - how much blank node level * fetchInverse - also fetch incoming properties * * @return PHP/RDF statements array of the resource */ private function _fetchResourceDescription($resourceIri, $modelIri, $options = array()) { // overwrite result format $options[Erfurt_Store::RESULTFORMAT] = Erfurt_Store::RESULTFORMAT_EXTENDED; $memoryModel = new Erfurt_Rdf_MemoryModel(); $query = new Erfurt_Sparql_SimpleQuery(); $query->setProloguePart('SELECT ?p ?o')->setWherePart("{<{$resourceIri}> ?p ?o . }"); // prepare an additional query for inverse properties if (isset($options['fetchInverse']) && $options['fetchInverse'] === true) { $inverseQuery = new Erfurt_Sparql_SimpleQuery(); $inverseQuery->setProloguePart('SELECT ?s ?p')->setWherePart("{?s ?p <{$resourceIri}> . }"); } else { $inverseQuery = false; } if ($modelIri === false) { // use complete store if modelIri not given $result = $this->sparqlQuery($query, $options); if ($inverseQuery) { $inverseResult = $this->sparqlQuery($inverseQuery, $options); } } else { // if model is given, try to get it $ac = Erfurt_App::getInstance()->getAc(); if ($ac->isModelAllowed('view', $modelIri)) { $model = $this->getModel($modelIri, $options[Erfurt_Store::USE_AC]); } else { $model = false; } if (!$model) { // return an empty description if model not available or allowed $result = false; } else { // use model query method if model valid and readable $result = $model->sparqlQuery($query, $options); if ($inverseQuery) { $inverseResult = $model->sparqlQuery($inverseQuery, $options); } } } if ($result) { foreach ($result['results']['bindings'] as $row) { // fake the subject array $s = array('type' => 'uri', 'value' => $resourceIri); $memoryModel->addStatementFromExtendedFormatArray($s, $row['p'], $row['o']); // todo: implement blank node fetching here //if ($row['o']['type'] === 'bnode') { //$nodeId = $row['o']['value']; //$bNode = self::initWithBlankNode($nodeId, $this->_model); //$nodeKey = sprintf('_:%s', $nodeId); //$description[$nodeKey] = $bNode->getDescription($maxDepth-1); //} } } if (isset($inverseResult) && $inverseResult !== false) { foreach ($inverseResult['results']['bindings'] as $row) { // fake the object array $o = array('type' => 'uri', 'value' => $resourceIri); $memoryModel->addStatementFromExtendedFormatArray($row['s'], $row['p'], $o); } } return $memoryModel->getStatements(); }
/** * Returns a list of properties used by the current list of resources. * Original Name was getValues() * @return array */ public function getAllProperties($inverse = false) { if (empty($this->_resources) && $this->_resourcesUptodate) { return array(); } $pool = Erfurt_App::getInstance()->getResourcePool(); $propertyUris = array(); $memoryModel = new Erfurt_Rdf_MemoryModel(); foreach ($this->_resources as $item) { $resourceUri = $item['value']; $resource = $pool->getResource($resourceUri, (string) $this->_model); if ($inverse == false) { $resourceDescription = $resource->getDescription(); $memoryModel->addStatements($resourceDescription); } else { $resourceDescription = $resource->getDescription(array('fetchInverse' => true)); $memoryModel->addStatements($resourceDescription); //remove s from memory model to extract only inverse relations $memoryModel->removeS($resourceUri); } } $predicates = $memoryModel->getPredicates(); foreach ($predicates as $uri) { $propertyUris[] = array('uri' => $uri); } return $this->convertProperties($propertyUris); }
public static function import($graphUri, $uri, $locator, $all = true, $presets = array(), $exceptedProperties = array(), $wrapperName = 'linkeddata', $fetchMode = 'none', $action = 'add', $versioning = true, $filterCallback = null) { // Check whether user is allowed to write the model. $erfurt = Erfurt_App::getInstance(); $store = $erfurt->getStore(); $storeGraph = $store->getModel($graphUri); if (!$storeGraph || !$storeGraph->isEditable()) { return self::IMPORT_NOT_EDITABLE; } $r = new Erfurt_Rdf_Resource($uri); $r->setLocator($locator); // Try to instanciate the requested wrapper $wrapper = null; try { $wrapper = Erfurt_Wrapper_Registry::getInstance()->getWrapperInstance($wrapperName); } catch (Erfurt_Wrapper_Exception $e) { return self::IMPORT_WRAPPER_INSTANCIATION_ERR; } if (null == $wrapper) { return self::IMPORT_WRAPPER_NOT_AVAILABLE; } $wrapperResult = null; try { $wrapperResult = $wrapper->run($r, $graphUri, $all); } catch (Erfurt_Wrapper_Exception $e) { return self::IMPORT_WRAPPER_ERR; } if ($wrapperResult === false) { return self::IMPORT_WRAPPER_RESULT_NOT_AVAILABLE; } else { if (is_array($wrapperResult)) { if (isset($wrapperResult['status_codes'])) { if (in_array(Erfurt_Wrapper::RESULT_HAS_ADD, $wrapperResult['status_codes'])) { $newStatements = $wrapperResult['add']; //default filter $newStatements = self::filterStatements($newStatements, $uri, $all, $presets, $exceptedProperties, $fetchMode); //custom filter if ($filterCallback != null && is_array($filterCallback)) { try { $newStatements = call_user_func($filterCallback, $newStatements); } catch (Exception $e) { return self::IMPORT_CUSTOMFILTER_EXCEPTION; } } $stmtBeforeCount = $store->countWhereMatches($graphUri, '{ ?s ?p ?o }', '*'); if ($versioning) { // Prepare versioning... $versioning = $erfurt->getVersioning(); $actionSpec = array('type' => self::VERSIONING_IMPORT_ACTION_TYPE, 'modeluri' => $graphUri, 'resourceuri' => $uri); // Start action $versioning->startAction($actionSpec); } if ($action == 'add') { try { $store->addMultipleStatements($graphUri, $newStatements); } catch (Exception $e) { $versioning->abortAction(); if (defined('_EFDEBUG')) { throw $e; } return self::IMPORT_GENERAL_EXCEPTION; } } else { if ($action == 'update') { $queryoptions = array('use_ac' => false, 'result_format' => Erfurt_Store::RESULTFORMAT_EXTENDED, 'use_additional_imports' => false); $oldStatements = $store->sparqlQuery('SELECT * FROM <' . $graphUri . '> WHERE { ?s ?p ?o }', $queryoptions); //transform resultset to rdf/php statements $modelOld = new Erfurt_Rdf_MemoryModel(); $modelOld->addStatementsFromSPOQuery($oldStatements); $modelNew = new Erfurt_Rdf_MemoryModel($newStatements); $storeGraph->updateWithMutualDifference($modelOld->getStatements(), $modelNew->getStatements()); } } if ($versioning) { $versioning->endAction(); } $stmtAfterCount = $store->countWhereMatches($graphUri, '{ ?s ?p ?o }', '*'); $stmtAddCount = $stmtAfterCount - $stmtBeforeCount; if ($stmtAddCount > 0) { // TODO test ns // If we added some statements, we check for additional namespaces and add them. if (in_array(Erfurt_Wrapper::RESULT_HAS_NS, $wrapperResult['status_codes'])) { $namespaces = $wrapperResult['ns']; $erfurtNamespaces = $erfurt->getNamespaces(); foreach ($namespaces as $ns => $prefix) { try { $erfurtNamespaces->addNamespacePrefix($graphUri, $prefix, $ns, false); } catch (Exception $e) { // Ignore... } } } return self::IMPORT_OK; } else { if (count($newStatements) > 0) { return self::IMPORT_NO_NEW_DATA; } else { return self::IMPORT_NO_DATA; } } } else { return self::IMPORT_NO_DATA; } } else { return self::IMPORT_WRAPPER_ERR; } } else { return self::IMPORT_WRAPPER_ERR; } } }
private function _getResourceFromWrapper($sourceUri, $targetUri, $wrapperName = 'Linkeddata') { $r = new Erfurt_Rdf_Resource($sourceUri); // Try to instanciate the requested wrapper $wrapper = $this->_getWrapper($wrapperName); $wrapperResult = null; $wrapperResult = $wrapper->run($r, null, true); $newStatements = null; if ($wrapperResult === false) { // IMPORT_WRAPPER_NOT_AVAILABLE; } else { if (is_array($wrapperResult)) { $newStatements = $wrapperResult['add']; // TODO make sure to only import the specified resource $newModel = new Erfurt_Rdf_MemoryModel($newStatements); $newStatements = array(); $object = array('type' => 'uri', 'value' => $targetUri); $newStatements = $newModel->getP($sourceUri, $object); } else { // IMPORT_WRAPPER_ERR; } } return $newStatements; }
public function beforeExportResource($event) { $owApp = OntoWiki::getInstance(); // this is the event value if there are other plugins before $prevModel = $event->getValue(); // throw away non memory model values OR use the given one if valid if (is_object($prevModel) && get_class($prevModel) == 'Erfurt_Rdf_MemoryModel') { $newModel = $prevModel; } else { $newModel = new Erfurt_Rdf_MemoryModel(); } // prepare the statement URIs $subjectUri = (string) $event->resource; $propertyUri = 'http://purl.org/net/pingback/to'; $objectUri = $owApp->config->urlBase . 'pingback/ping/'; $newModel->addRelation($subjectUri, $propertyUri, $objectUri); return $newModel; }
/** * Creates a new named graph */ public function createAction() { $this->addModuleContext('main.window.modelcreate'); $store = $this->_erfurt->getStore(); $this->view->clearModuleCache('modellist'); OntoWiki::getInstance()->getNavigation()->disableNavigation(); $this->view->placeholder('main.window.title')->set('Create New Knowledge Base'); $this->view->formActionUrl = $this->_config->urlBase . 'model/create'; $this->view->formEncoding = 'multipart/form-data'; $this->view->formClass = 'simple-input input-justify-left'; $this->view->formMethod = 'post'; $this->view->formName = 'createmodel'; // this is the default action $defaultActions = array('empty' => array('parameter' => 'checked', 'controller' => 'model', 'action' => 'info', 'label' => 'Create a (nearly) empty knowledge base', 'description' => 'Just add the label and type to the new model.')); $importActions = array_merge($defaultActions, $this->_getImportActions()); $this->view->importActions = $importActions; if (!$this->_erfurt->isActionAllowed('ModelManagement')) { $this->_owApp->appendMessage(new OntoWiki_Message('Model management is not allowed.', OntoWiki_Message::ERROR)); $this->view->errorFlag = true; return; } // TODO: add this to the template in order to allow users to tune it $this->view->modelUri = $this->_config->urlBase . 'NEWMODEL/'; $toolbar = $this->_owApp->toolbar; $toolbar->appendButton(OntoWiki_Toolbar::SUBMIT, array('name' => 'Create Knowledge Base', 'id' => 'createmodel'))->appendButton(OntoWiki_Toolbar::RESET, array('name' => 'Cancel', 'id' => 'createmodel')); $this->view->placeholder('main.window.toolbar')->set($toolbar); $this->view->title = $this->view->_('Create Knowledge Base'); if (!$this->_request->isPost()) { // FIX: http://www.webmasterworld.com/macintosh_webmaster/3300569.htm // disable connection keep-alive $response = $this->getResponse(); $response->setHeader('Connection', 'close', true); $response->sendHeaders(); return; } else { // process the user input $post = $this->_request->getPost(); // determine or create the model URI $newModelUri = ''; if (trim($post['modeluri']) != '') { // URI given via form input $newModelUri = trim($post['modeluri']); } else { if (trim($post['title']) != '') { // create a nice URI from the title (poor mans way) $urlBase = $this->_config->urlBase; $title = trim($post['title']); $title = str_replace(' ', '', $title); $title = urlencode($title); $newModelUri = $urlBase . $title . '/'; } else { // create a default model with counter $urlBase = $this->_config->urlBase . 'kb'; $counter = 0; do { $newModelUri = $urlBase . $counter++ . '/'; } while ($store->isModelAvailable($newModelUri, false)); } } if ($newModelUri == '') { $this->_owApp->appendMessage(new OntoWiki_Message('Please provide at least a valid URI for the new Knowledge Base.', OntoWiki_Message::ERROR)); $this->view->errorFlag = true; return; } // create model if ($store->isModelAvailable($newModelUri, false)) { // model exists $this->_owApp->appendMessage(new OntoWiki_Message('Given Knowledge Base already exists.', OntoWiki_Message::ERROR)); $this->view->errorFlag = true; return; } else { // model does not exist, will be created $model = $store->getNewModel($newModelUri, $newModelUri, Erfurt_Store::MODEL_TYPE_OWL); $this->_owApp->appendMessage(new OntoWiki_Message('Knowledge Base successfully created.', OntoWiki_Message::SUCCESS)); // add label $additions = new Erfurt_Rdf_MemoryModel(); if (isset($post['title']) && trim($post['title']) != '') { $additions->addAttribute($newModelUri, EF_RDFS_LABEL, $post['title']); } $model->addMultipleStatements($additions->getStatements()); // TODO: add ACL infos based on the post data $this->_doImportActionRedirect($newModelUri); } } }