protected function _buildCountQuery($uri, $setup) { $query = new Erfurt_Sparql_Query2(); $query->addProjectionVar(new Erfurt_Sparql_Query2_Var('resourceUri')); $query->setCountStar(true); $query->setDistinct(); $query->addElements(NavigationHelper::getInstancesTriples($uri, $setup)); // error logging $this->_owApp->logger->info('NavigationController: COUNTQUERY: ' . $query->__toString()); //*/ return $query; }
/** * Handles search requests */ public function searchAction() { $title = $this->_owApp->translate->_('Resource Search'); $this->view->placeholder('main.window.title')->set($title); OntoWiki::getInstance()->getNavigation()->disableNavigation(); $store = $this->_erfurt->getStore(); if (isset($this->_owApp->selectedModel) && null !== $this->_owApp->selectedModel) { $modelUri = $this->_owApp->selectedModel->getModelIri(); } else { $modelUri = null; } if ($this->_request->getParam('searchtext-input') !== null) { $searchText = trim($this->getParam('searchtext-input')); } $error = false; $errorMsg = ''; // check if search is already errorenous if (!$error) { // try sparql query pre search check (with limit to 1) $elements = $store->getSearchPattern($searchText, $modelUri); $query = new Erfurt_Sparql_Query2(); $query->addElements($elements); $query->setLimit(1); $query->addFrom($modelUri); try { $store->sparqlQuery($query); } catch (Exception $e) { // build error message $this->_owApp->appendMessage(new OntoWiki_Message($this->_owApp->translate->_('search failed'), OntoWiki_Message::ERROR)); $error = true; $errorMsg .= 'Message details: '; $errorMsg .= str_replace('LIMIT 1', '', $e->getMessage()); } } // if error occured set output for error page if ($error) { $this->view->errorMsg = $errorMsg; } else { // set redirect to effective search controller $url = new OntoWiki_Url(array('controller' => 'list'), array()); $url->setParam('s', $searchText); $url->setParam('init', '1'); $this->_redirect($url); } }
/** * 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; }