/** * if the selected resources changed (due to filters or limit or offset) * we have to change the value query as well (because the resources are mentioned as subjects) * * @return OntoWiki_Model_Instances $this */ protected function updateValueQuery() { if ($this->_valueQueryUptodate) { return $this; } $resources = $this->getShownResources(); foreach ($resources as $key => $resource) { $resources[$key] = new Erfurt_Sparql_Query2_SameTerm($this->_resourceVar, new Erfurt_Sparql_Query2_IriRef($resource['value'])); } if ($this->_valueQueryResourceFilter == null) { $this->_valueQueryResourceFilter = new Erfurt_Sparql_Query2_Filter(new Erfurt_Sparql_Query2_BooleanLiteral(false)); $this->_valueQuery->addElement($this->_valueQueryResourceFilter); } $this->_valueQueryResourceFilter->setConstraint(empty($resources) ? new Erfurt_Sparql_Query2_BooleanLiteral(false) : new Erfurt_Sparql_Query2_ConditionalOrExpression($resources)); //fix for a strange issue where a query with only optionals fails //(but there is a magic/unkown condition, that makes it work for some queries!?) //if($this->_store->getBackendName() == 'ZendDb'){ $hasTriple = false; foreach ($this->_valueQuery->getWhere()->getElements() as $element) { if ($element instanceof Erfurt_Sparql_Query2_IF_TriplesSameSubject) { $hasTriple = true; break; } } if (!$hasTriple) { $this->_valueQuery->getWhere()->addElement(new Erfurt_Sparql_Query2_Triple($this->_resourceVar, new Erfurt_Sparql_Query2_Var('p'), new Erfurt_Sparql_Query2_Var('o'))); } //} //remove duplicate triples... $this->_valueQuery->optimize(); $this->_valueQueryUptodate = true; return $this; }
protected function _buildStringSearchQuery($setup) { // define vars $searchVar = new Erfurt_Sparql_Query2_Var('resourceUri'); $subVar = new Erfurt_Sparql_Query2_Var('sub'); // define query $query = new Erfurt_Sparql_Query2(); $query->addProjectionVar($searchVar); $query->setDistinct(); // init union var $union = new Erfurt_Sparql_Query2_GroupOrUnionGraphPattern(); // parse config if (isset($setup->config->hierarchyRelations->out)) { foreach ($setup->config->hierarchyRelations->out as $rel) { // create new graph pattern $groupPattern = new Erfurt_Sparql_Query2_GroupGraphPattern(); // add triplen $groupPattern->addTriple($searchVar, new Erfurt_Sparql_Query2_IriRef($rel), $subVar); // add triplet to union var $union->addElement($groupPattern); } } // parse config if (isset($setup->config->hierarchyRelations->in)) { foreach ($setup->config->hierarchyRelations->in as $rel) { // create new graph pattern $groupPattern = new Erfurt_Sparql_Query2_GroupGraphPattern(); // add triplen $groupPattern->addTriple($subVar, new Erfurt_Sparql_Query2_IriRef($rel), $searchVar); // add triplet to union var $union->addElement($groupPattern); } } // parse config if (isset($setup->config->instanceRelation->out)) { foreach ($setup->config->instanceRelation->out as $rel) { // create new graph pattern $groupPattern = new Erfurt_Sparql_Query2_GroupGraphPattern(); // add triplen $groupPattern->addTriple($subVar, new Erfurt_Sparql_Query2_IriRef($rel), $searchVar); // add triplet to union var $union->addElement($groupPattern); } } // parse config if (isset($setup->config->instanceRelation->in)) { foreach ($setup->config->instanceRelation->in as $rel) { // create new graph pattern $groupPattern = new Erfurt_Sparql_Query2_GroupGraphPattern(); // add triplen $groupPattern->addTriple($searchVar, new Erfurt_Sparql_Query2_IriRef($rel), $subVar); // add triplet to union var $union->addElement($groupPattern); } } $query->addElement($union); // add regex filter for search string $query->addFilter(new Erfurt_Sparql_Query2_Regex(new Erfurt_Sparql_Query2_Str($searchVar), new Erfurt_Sparql_Query2_RDFLiteral($setup->state->searchString))); return $query; }
private function checkConfig($config) { $resVar = new Erfurt_Sparql_Query2_Var('resourceUri'); $typeVar = new Erfurt_Sparql_Query2_IriRef(EF_RDF_TYPE); $query = new Erfurt_Sparql_Query2(); $query->addProjectionVar($resVar)->setDistinct(true); $union = new Erfurt_Sparql_Query2_GroupOrUnionGraphPattern(); if (is_string($config->hierarchyTypes)) { $config->hierarchyTypes = array($config->hierarchyTypes); } foreach ($config->hierarchyTypes->toArray() as $type) { $ggp = new Erfurt_Sparql_Query2_GroupGraphPattern(); $ggp->addTriple($resVar, $typeVar, new Erfurt_Sparql_Query2_IriRef($type)); $union->addElement($ggp); } $query->addElement($union); $query->setLimit(1); $allResults = $this->_owApp->selectedModel->sparqlQuery($query); /*$this->_owApp->logger->info( 'Navigation Query: ' .PHP_EOL . $query->__toString() ); $this->_owApp->logger->info( 'Navigation Query Results: ' .PHP_EOL . print_r($all_results) );*/ return count($allResults); }
/** * Searches the local database for URIs. If mode is set to properties only, * only defined properties and URIs that are used at least once as a * property are returned. * * the classes array is used for class restrictions * * @param array $termsArray * @param string $modelUri * @param int $mode * @param int $limit * @param array $classes * * @return array */ private function _searchLocal(array $termsArray, $modelUri, $mode, $limit, $classes = array()) { if ($mode === self::SEARCH_MODE_PROPERTIES) { return $this->_searchLocalPropertiesOnly($termsArray, $modelUri, $limit); } $store = Erfurt_App::getInstance()->getStore(); // get a store specific text-search query2 object $searchPattern = $store->getSearchPattern(implode(" ", $termsArray), $modelUri); $query = new Erfurt_Sparql_Query2(); $query->addElements($searchPattern); $projVar = new Erfurt_Sparql_Query2_Var('resourceUri'); $query->addProjectionVar($projVar); // add class restriction patterns for each class foreach ($classes as $class) { $classPattern = new Erfurt_Sparql_Query2_GroupGraphPattern(); $classPattern->addTriple($projVar, new Erfurt_Sparql_Query2_IriRef(EF_RDF_TYPE), new Erfurt_Sparql_Query2_IriRef($class)); $query->addElement($classPattern); } $query->setLimit(20); $query->setDistinct(true); $queryResult = $store->sparqlQuery($query, array('result_format' => 'extended')); $tempResult = array(); foreach ($queryResult['results']['bindings'] as $row) { $object = isset($row['o']) ? $row['o']['value'] : null; $weight = $this->_getWeight($termsArray, $row['resourceUri']['value'], $object); if (isset($tempResult[$row['resourceUri']['value']])) { if ($weight > $tempResult[$row['resourceUri']['value']]) { $tempResult[$row['resourceUri']['value']] = $weight; } } else { $tempResult[$row['resourceUri']['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; // create different source description strings if (count($classes) > 0) { $sourceString = $translate->_('Local Search') . ' (' . $translate->_('recommended') . ')'; } else { $sourceString = $translate->_('Local Search'); } $result = array(); foreach ($tempResult as $uri => $w) { $title = $titleHelper->getTitle($uri); if (null !== $title) { $result[$uri] = str_replace('|', 'Ι', $title) . '|' . $uri . '|' . $sourceString; } else { $result[$uri] = OntoWiki_Utils::compactUri($uri) . $uri . '|' . $sourceString; } } return $result; }
/** * Moves resource to new URI * renaming all occurences of the resource. * * @param string $oldUri The URI that identifies the resource. * @param string $newUri The URI to move resource to. * * @return void */ public function renameResource($oldUri, $newUri) { $query = new Erfurt_Sparql_Query2(); $query->setDistinct(true); $vars = array(); foreach (array('s', 'p', 'o') as $varName) { $vars[$varName] = new Erfurt_Sparql_Query2_Var($varName); $query->addProjectionVar($vars[$varName]); } $oldUriRef = new Erfurt_Sparql_Query2_IriRef($oldUri); $union = new Erfurt_Sparql_Query2_GroupOrUnionGraphPattern(); foreach ($vars as $var) { $group = new Erfurt_Sparql_Query2_GroupGraphPattern(); $group->addTriple($vars['s'], $vars['p'], $vars['o']); $group->addFilter(new Erfurt_Sparql_Query2_sameTerm($var, $oldUriRef)); $union->addElement($group); } $query->addElement($union); $result = $this->sparqlQuery($query, array('result_format' => 'extended')); $removed = array(); $added = array(); foreach ($result['results']['bindings'] as $s) { // result format from sparqlQuery // isn't the same as format for delete/addMultipleStatements if (array_key_exists('xml:lang', $s['o'])) { $s['o']['lang'] = $s['o']['xml:lang']; unset($s['o']['xml:lang']); } $removed[$s['s']['value']][$s['p']['value']][] = $s['o']; foreach (array('s', 'p', 'o') as $varName) { if ($s[$varName]['type'] === 'uri' && $s[$varName]['value'] === $oldUri) { $s[$varName]['value'] = $newUri; } } $added[$s['s']['value']][$s['p']['value']][] = $s['o']; } $this->deleteMultipleStatements($removed); $this->addMultipleStatements($added); }