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); }
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); }
private function assertSetsEquals($source, $expect, $in, $arg) { $out = $source->getInformation(Search_ResultSet::create($in), $arg); $this->assertEquals(Search_ResultSet::create($expect), $out); }
function testSpecifyDataSource() { $searchResult = array(array('object_type' => 'wiki page', 'object_id' => 'HomePage'), array('object_type' => 'wiki page', 'object_id' => 'SomePage')); $withData = array(array('object_type' => 'wiki page', 'object_id' => 'HomePage', 'description' => 'ABC'), array('object_type' => 'wiki page', 'object_id' => 'SomePage', 'description' => 'DEF')); $source = $this->getMock('Search_Formatter_DataSource_Interface'); $source->expects($this->once())->method('getInformation')->with($this->equalTo(Search_ResultSet::create($searchResult)), $this->equalTo(array('object_id', 'description')))->will($this->returnValue(Search_ResultSet::create($withData))); $plugin = new Search_Formatter_Plugin_WikiTemplate("* {display name=object_id} ({display name=description})\n"); $formatter = new Search_Formatter($plugin); $formatter->setDataSource($source); $output = $formatter->format($searchResult); $expect = <<<OUT * HomePage (ABC) * SomePage (DEF) OUT; $this->assertEquals($expect, $output); }
function testSpecifyDataSource() { $searchResult = Search_ResultSet::create(array(array('object_type' => 'wiki page', 'object_id' => 'HomePage'), array('object_type' => 'wiki page', 'object_id' => 'SomePage'))); $withData = array(array('object_type' => 'wiki page', 'object_id' => 'HomePage', 'description' => 'ABC'), array('object_type' => 'wiki page', 'object_id' => 'SomePage', 'description' => 'DEF')); $source = $this->getMock('Search_Formatter_DataSource_Interface'); $source->expects($this->any())->method('getData')->will($this->returnCallback(function ($entry, $field) use(&$withData) { $this->assertEquals('description', $field); return array_shift($withData); })); $plugin = new Search_Formatter_Plugin_WikiTemplate("* {display name=object_id} ({display name=description})\n"); $formatter = new Search_Formatter($plugin); $searchResult->applyTransform(new Search_Formatter_Transform_DynamicLoader($source)); $output = $formatter->format($searchResult); $expect = <<<OUT * HomePage (ABC) * SomePage (DEF) OUT; $this->assertEquals($expect, $output); }
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; }