public function parseQuery(SearchQuery $obj, $searchedQuery) { $queryString = ''; $queryTokens = array(); // Parsing the query splitting on " ", to recognize // a searched word and a token with his own parameters. $searchedQuery = explode(' ', $searchedQuery); // Analizying each parsed value, to set the $queryString and // the $queryTokens variables. foreach ($searchedQuery as $key => $value) { // cheking if ":" char is present or not. $finded = strpos($value, ':'); // if it is, means that we are looking at a token. if ($finded) { // matching the token from the query. $token = substr($searchedQuery[$key], 0, $finded); // matching the parameters list from the query. $parameters = substr($searchedQuery[$key], $finded + 1, strlen($searchedQuery[$key]) - 1); // if the parameters list is not empty, we can build it. if (!empty($parameters)) { $parameters = explode(',', $parameters); // creating an array with the token as key, and the // parameters list as values. $queryTokens = array_merge((array) $queryTokens, array($token => $parameters)); } // and unset his value on the $searchedQuery struct. unset($searchedQuery[$key]); } else { // in this case, we just merge the $parsedString with // the new searched word finded. if (empty($queryString)) { $queryString .= $value; } else { $queryString .= ' ' . $value; } // and unset his value on the $searchedQuery struct. unset($searchedQuery[$key]); } } // setting the $queryString and $queryTokens class variables. $obj->setQueryString($queryString); $obj->setQueryTokens($queryTokens); }
public function testSearchFileRecollPlugin() { $query = "filename:*foo* filename:*lol* type:pdf OR type:xml AND ext:html OR ext:xml"; $searchQuery = new SearchQuery(); $searchQuery->setQueryString($this->query); $searchQuery->setQueryTokens($this->tokens); // we want to check if the SearchManager is well working, // means if it is able to load all the required library. $searchManager = new SearchManager(); $searchManager->search($searchQuery); // we also want to check if the parsing process // gives us a well formatted string. $recollPlugin = new SearchFileRecollPlugin(); $recollPlugin->search($searchQuery); $this->assertEquals($query, $recollPlugin->getSearchQuery()); // FileSearchListener::fileCreated() $this->assertFalse(is_file($this->fixture_file_path)); $this->assertTrue($this->fixture_file->createNewFile()); $this->assertTrue(is_file($this->fixture_file_path)); $searchQuery->setQueryString('testFile'); $searchQuery->setQueryTokens(null); $recollPlugin->resetSearchQuery(); $recollPlugin->search($searchQuery); $results = $recollPlugin->getResults(); $this->assertEquals($results[0]['name'], 'testFile.txt'); // FileSearchListener::fileDeleted() $this->assertTrue(is_file($this->fixture_file_path)); $this->assertTrue($this->fixture_file->delete()); $this->assertFalse(is_file($this->fixture_file_path)); $searchQuery->setQueryString('testFile'); $searchQuery->setQueryTokens(null); $recollPlugin->resetSearchQuery(); $recollPlugin->search($searchQuery); $results = $recollPlugin->getResults(); $this->assertTrue(empty($results)); // FileSearchListener::directoryCreated() $this->assertFalse(is_dir($this->fixture_dir_path)); $this->assertTrue($this->fixture_dir->mkdir()); $this->assertTrue(is_dir($this->fixture_dir_path)); $searchQuery->setQueryString('testDir'); $searchQuery->setQueryTokens(null); $recollPlugin->resetSearchQuery(); $recollPlugin->search($searchQuery); $results = $recollPlugin->getResults(); $this->assertEquals($results[0]['name'], 'testDir'); // FileSearchListener::fileMoved() $this->assertFalse(is_file($this->fixture_file_path)); $this->assertTrue($this->fixture_file->createNewFile()); $this->assertTrue(is_file($this->fixture_file_path)); $newFile = FSI::getFile('home://~fakeUser/testDir/testFile2.txt'); $this->assertTrue($this->fixture_file->moveTo($newFile)); $this->assertTrue(is_file($this->fixture_newFile_path)); $this->assertFalse(is_file($this->fixture_file_path)); $searchQuery->setQueryString('testFile2'); $searchQuery->setQueryTokens(null); $recollPlugin->resetSearchQuery(); $recollPlugin->search($searchQuery); $results = $recollPlugin->getResults(); $this->assertEquals($results[0]['name'], 'testFile2.txt'); $searchQuery->setQueryString('testFile'); $searchQuery->setQueryTokens(null); $recollPlugin->resetSearchQuery(); $recollPlugin->search($searchQuery); $results = $recollPlugin->getResults(); $this->assertNotEquals($results[0]['name'], 'testFile.txt'); $newFile->delete(); // FileSearchListener::fileRenamed() $this->assertFalse(is_file($this->fixture_file_path)); $this->assertTrue($this->fixture_file->createNewFile()); $this->assertTrue(is_file($this->fixture_file_path)); $this->assertTrue($this->fixture_file->renameTo('testFileRenamed.txt')); $searchQuery->setQueryString('testFile'); $searchQuery->setQueryTokens(null); $recollPlugin->resetSearchQuery(); $recollPlugin->search($searchQuery); $results = $recollPlugin->getResults(); $this->assertNotEquals($results[0]['name'], 'testFile.txt'); $this->assertEquals($results[0]['name'], 'testFileRenamed.txt'); // FileSearchListener::fileWritten() $this->fixture_file->putContents('take it easy!'); // TODO: check if the content's file is up to date. }