Example #1
0
 function find(Search_Expr_Interface $query, Search_Query_Order $sortOrder, $resultStart, $resultCount)
 {
     $data = $this->internalFind($query, $sortOrder);
     $result = array_slice($data['result'], $resultStart, $resultCount);
     $resultSet = new Search_ResultSet($result, count($data['result']), $resultStart, $resultCount);
     $resultSet->setEstimate($data['count']);
     if ($this->highlight) {
         $resultSet->setHighlightHelper(new Search_Index_Lucene_HighlightHelper($query));
     } else {
         $resultSet->setHighlightHelper(new Search_ResultSet_SnippetHelper());
     }
     return $resultSet;
 }
Example #2
0
 function renderEntries(Search_ResultSet $entries)
 {
     if ($entries->getTsOn()) {
         return $this->parent->renderEntries($entries);
     }
     $smarty = TikiLib::lib('smarty');
     $smarty->loadPlugin('smarty_block_pagination_links');
     $arguments = $this->arguments;
     $arguments['resultset'] = $entries;
     $tmp = false;
     $pagination = smarty_block_pagination_links($arguments, '', $smarty, $tmp);
     if ($this->getFormat() == Search_Formatter_Plugin_Interface::FORMAT_WIKI) {
         $pagination = "~np~{$pagination}~/np~";
     }
     return $this->parent->renderEntries($entries) . $pagination;
 }
Example #3
0
 function format($list)
 {
     $list = Search_ResultSet::create($list);
     $defaultValues = $this->plugin->getFields();
     $fields = array_keys($defaultValues);
     $subDefault = array();
     foreach ($this->subFormatters as $key => $plugin) {
         $subDefault[$key] = $plugin->getFields();
         $fields = array_merge($fields, array_keys($subDefault[$key]));
     }
     if ($this->dataSource) {
         $list = $this->dataSource->getInformation($list, $fields);
     }
     if (in_array('highlight', $fields)) {
         foreach ($list as &$entry) {
             $entry['highlight'] = $list->highlight($entry);
         }
     }
     $data = array();
     foreach ($list as $row) {
         // Clear blank values so the defaults prevail
         $row = array_filter($row, array($this, 'is_empty_string'));
         $row = array_merge($defaultValues, $row);
         $subEntries = array();
         foreach ($this->subFormatters as $key => $plugin) {
             $subInput = new Search_Formatter_ValueFormatter(array_merge($subDefault[$key], $row));
             $subEntries[$key] = $this->render($plugin, Search_ResultSet::create(array($plugin->prepareEntry($subInput))), $this->plugin->getFormat(), $list);
         }
         $row = array_merge($row, $subEntries);
         $data[] = $this->plugin->prepareEntry(new Search_Formatter_ValueFormatter($row));
     }
     $list = $list->replaceEntries($data);
     return $this->render($this->plugin, $list, Search_Formatter_Plugin_Interface::FORMAT_WIKI);
 }
Example #4
0
 function getPopulatedList($list)
 {
     $list = Search_ResultSet::create($list);
     $defaultValues = $this->plugin->getFields();
     $fields = array_keys($defaultValues);
     $subDefault = array();
     foreach ($this->subFormatters as $key => $plugin) {
         $subDefault[$key] = $plugin->getFields();
         $fields = array_merge($fields, array_keys($subDefault[$key]));
     }
     $data = array();
     $enableHighlight = in_array('highlight', $fields);
     foreach ($list as $pre) {
         foreach ($fields as $f) {
             if (isset($pre[$f])) {
                 $pre[$f];
                 // Dynamic loading if applicable
             }
         }
         $row = array_filter($defaultValues, 'strlen');
         // Clear blank values so the defaults prevail
         foreach ($pre as $k => $value) {
             if ($value !== '' && $value !== null) {
                 $row[$k] = $value;
             }
         }
         if ($enableHighlight) {
             $row['highlight'] = $list->highlight($row);
         }
         $subEntries = array();
         foreach ($this->subFormatters as $key => $plugin) {
             $subInput = new Search_Formatter_ValueFormatter(array_merge($subDefault[$key], $row));
             $subEntries[$key] = $this->render($plugin, Search_ResultSet::create(array($plugin->prepareEntry($subInput))), $this->plugin->getFormat(), $list);
         }
         $row = array_merge($row, $subEntries);
         $data[] = $this->plugin->prepareEntry(new Search_Formatter_ValueFormatter($row));
     }
     return $list->replaceEntries($data);
 }
Example #5
0
 private function assertSetsEquals($source, $expect, $in, $arg)
 {
     $out = $source->getInformation(Search_ResultSet::create($in), $arg);
     $this->assertEquals(Search_ResultSet::create($expect), $out);
 }
Example #6
0
 function testHighlightRequested()
 {
     $plugin = new Search_Formatter_Plugin_WikiTemplate('{display name=highlight}');
     $resultSet = new Search_ResultSet(array(array('object_type' => 'wiki page', 'object_id' => 'HomePage', 'content' => 'Hello World'), array('object_type' => 'wiki page', 'object_id' => 'SomePage', 'content' => 'Test')), 22, 20, 10);
     $resultSet->setHighlightHelper(new Search_FormatterTest_HighlightHelper());
     $formatter = new Search_Formatter($plugin);
     $output = $formatter->format($resultSet);
     $this->assertContains('<strong>Hello</strong>', $output);
 }
Example #7
0
 function find(Search_Query_Interface $query, $resultStart, $resultCount)
 {
     try {
         $words = $this->getWords($query->getExpr());
         $condition = $this->builder->build($query->getExpr());
         $conditions = empty($condition) ? array() : array($this->table->expr($condition));
         $scoreField = null;
         $indexes = $this->builder->getRequiredIndexes();
         foreach ($indexes as $index) {
             $this->table->ensureHasIndex($index['field'], $index['type']);
             if (!$scoreField && $index['type'] == 'fulltext') {
                 $scoreField = $index['field'];
             }
         }
         $this->table->flush();
         $order = $this->getOrderClause($query, (bool) $scoreField);
         $selectFields = $this->table->all();
         if ($scoreField) {
             $str = $this->db->qstr(implode(' ', $words));
             $selectFields['score'] = $this->table->expr("ROUND(MATCH(`{$scoreField}`) AGAINST ({$str}),2)");
         }
         $count = $this->table->fetchCount($conditions);
         $entries = $this->table->fetchAll($selectFields, $conditions, $resultCount, $resultStart, $order);
         $resultSet = new Search_ResultSet($entries, $count, $resultStart, $resultCount);
         $resultSet->setHighlightHelper(new Search_MySql_HighlightHelper($words));
         return $resultSet;
     } catch (Search_MySql_QueryException $e) {
         $resultSet = new Search_ResultSet(array(), 0, $resultStart, $resultCount);
         return $resultSet;
     }
 }
Example #8
0
 function search(Search_Index_Interface $index)
 {
     $this->finalize();
     try {
         $resultset = $index->find($this, $this->start, $this->count);
     } catch (Search_Elastic_SortException $e) {
         //on sort exception, try again without the sort field
         $this->sortOrder = null;
         $resultset = $index->find($this, $this->start, $this->count);
     } catch (Exception $e) {
         TikiLib::lib('errorreport')->report($e->getMessage());
         return Search_ResultSet::create([]);
     }
     $resultset->applyTransform(function ($entry) {
         if (!isset($entry['_index']) || !isset($this->foreignQueries[$entry['_index']])) {
             foreach ($this->transformations as $trans) {
                 $entry = $trans($entry);
             }
         }
         return $entry;
     });
     foreach ($this->foreignQueries as $indexName => $query) {
         $resultset->applyTransform(function ($entry) use($query, $indexName) {
             if (isset($entry['_index']) && $entry['_index'] == $indexName) {
                 foreach ($query->transformations as $trans) {
                     $entry = $trans($entry);
                 }
             }
             return $entry;
         });
     }
     return $resultset;
 }