Source of file ResultList.php
Size: 7,636 Bytes - Last Modified: 2021-12-23T10:25:26+00:00
/var/www/docs.ssmods.com/process/src/src/Symbiote/Elastica/ResultList.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333 | <?php namespace Symbiote\Elastica; use Elastica\Query; /** * A list wrapper around the results from a query. Note that not all operations are implemented. */ class ResultList extends \ViewableData implements \SS_Limitable { /** * * @var \Elastica\Index */ private $index; /** * * @var \Elastica\Query */ private $query; protected $dataObjects; protected $totalResults = 0; /** * */ protected $results; /** * * @var \Elastica\ResultSet */ protected $resultSet; public function __construct(/*Index */$index, Query $query) { $this->index = $index; $this->query = $query; } public function __clone() { $this->query = clone $this->query; } /** * @return \Elastica\Index */ public function getIndex() { return $this->index; } /** * @return \Elastica\Query */ public function getQuery() { return $this->query; } /** * @return \Elastica\ResultSet */ public function getResultSet() { if (!$this->resultSet) { $this->resultSet = $this->index->search($this->query); } return $this->resultSet; } public function getIterator() { return new \ArrayIterator($this->toArray()); } public function limit($limit, $offset = 0) { $list = clone $this; $list->getQuery()->setSize($limit); $list->getQuery()->setFrom($offset); return $list; } public function getTotalResults() { return $this->getResultSet()->getTotalHits(); } public function getTimeTaken() { return $this->getResultSet()->getTotalTime(); } public function getAggregations() { return $this->getResultSet()->getAggregations(); } /** * The paginated result set that is rendered onto the search page. * * @return PaginatedList */ public function getDataObjects($limit = 0, $start = 0) { $pagination = \PaginatedList::create($this->toArrayList()) ->setPageLength($limit) ->setPageStart($start) ->setTotalItems($this->getTotalResults()) ->setLimitItems(false); return $pagination; } /** * Converts results of type {@link \Elastica\Result} * into their respective {@link DataObject} counterparts. * * @param boolean $evaluatePermissions * Should we eval permissions now? * @param boolean $loadDataObjects * Should data objects be loaded from the DB ? * * @return array DataObject[] */ public function toArray($evaluatePermissions = false, $loadDataObjects = true) { if ($this->dataObjects) { return $this->dataObjects; } $result = array(); /** @var $found \Elastica\Result[] */ $found = $this->getResultSet(); $needed = array(); $retrieved = array(); foreach ($found->getResults() as $item) { $data = $item->getData(); $type = isset($data['ClassName']) ? $data['ClassName'] : $item->getType(); $bits = explode('_', $item->getId()); $id = isset($data['ID']) ? $data['ID'] : $item->getId(); if (count($bits) == 3) { list($type, $id, $stage) = $bits; } else if (count($bits) == 2) { list($type, $id) = $bits; $stage = \Versioned::current_stage(); } else { $stage = \Versioned::current_stage(); } if (!$type || !$id) { error_log("Invalid elastic document ID {$item->getId()}"); continue; } // a double sanity check for the stage here. $source = $item->getSource(); $stages = isset($source['SS_Stage']) ? $source['SS_Stage'] : []; if ($currentStage = \Versioned::current_stage()) { if ($currentStage != $stage && !in_array($currentStage, $stages)) { continue; } } if (class_exists($type) && $loadDataObjects) { $object = \DataObject::get_by_id($type, $id); } else { $object = \ArrayData::create($source); } if ($object) { // check that the user has permission if ($item->getScore()) { $object->SearchScore = $item->getScore(); } $canAdd = true; if ($evaluatePermissions) { // check if we've got a way of evaluating perms if ($object->hasMethod('canView')) { $canAdd = $object->canView(); } } if (!$evaluatePermissions || $canAdd) { if ($object->hasMethod('canShowInSearch')) { if ($object->canShowInSearch()) { $result[] = $object; } } else { $result[] = $object; } } } else { error_log("Object {$item->getId()} is no longer in the system"); } } // // $this->totalResults = $documents->numFound; // // // update the dos with stats about this query // // $this->dataObjects = PaginatedList::create($this->dataObjects); // // $this->dataObjects->setPageLength($this->queryParameters->limit) // ->setPageStart($documents->start) // ->setTotalItems($documents->numFound) // ->setLimitItems(false); // if (!array_key_exists($type, $needed)) { // $needed[$type] = array($item->getId()); // $retrieved[$type] = array(); // } else { // $needed[$type][] = $item->getId(); // } // // foreach ($needed as $class => $ids) { // foreach ($class::get()->byIDs($ids) as $record) { // $retrieved[$class][$record->ID] = $record; // } // } // // foreach ($found as $item) { // // Safeguards against indexed items which might no longer be in the DB // if(array_key_exists($item->getId(), $retrieved[$item->getType()])) { // $result[] = $retrieved[$item->getType()][$item->getId()]; // } // } $this->dataObjects = $result; return $result; } public function toArrayList() { return new \ArrayList($this->toArray()); } public function toNestedArray() { $result = array(); foreach ($this as $record) { $result[] = $record->toMap(); } return $result; } public function first() { // TODO } public function last() { // TODO: Implement last() method. } public function map($key = 'ID', $title = 'Title') { return $this->toArrayList()->map($key, $title); } public function column($col = 'ID') { if($col == 'ID') { $ids = array(); foreach ($this->getResultSet()->getResults() as $result) { $ids[] = $result->getId(); } return $ids; } else { return $this->toArrayList()->column($col); } } public function each($callback) { return $this->toArrayList()->each($callback); } public function count() { return count($this->toArray()); } /** * @ignore */ public function offsetExists($offset) { throw new \Exception(); } /** * @ignore */ public function offsetGet($offset) { throw new \Exception(); } /** * @ignore */ public function offsetSet($offset, $value) { throw new \Exception(); } /** * @ignore */ public function offsetUnset($offset) { throw new \Exception(); } /** * @ignore */ public function add($item) { throw new \Exception(); } /** * @ignore */ public function remove($item) { throw new \Exception(); } /** * @ignore */ public function find($key, $value) { throw new \Exception(); } } |