X-Git-Url: http://git.cyclocoop.org/?a=blobdiff_plain;f=includes%2Fsearch%2FSearchResultSet.php;h=978db2707f9f73fb242a641d7b7bd055bc92e75a;hb=7471e1db1b613d035f981f489f8683a177acff7e;hp=69795e792b0b052333934ec89fff51c504faf4b1;hpb=4ce349cdbfe51da968c96ca2c72ae465538510cc;p=lhc%2Fweb%2Fwiklou.git diff --git a/includes/search/SearchResultSet.php b/includes/search/SearchResultSet.php index 69795e792b..978db2707f 100644 --- a/includes/search/SearchResultSet.php +++ b/includes/search/SearchResultSet.php @@ -42,6 +42,29 @@ class SearchResultSet { protected $containedSyntax = false; + /** + * Cache of titles. + * Lists titles of the result set, in the same order as results. + * @var Title[] + */ + private $titles; + + /** + * Cache of results - serialization of the result iterator + * as an array. + * @var SearchResult[] + */ + private $results; + + /** + * Set of result's extra data, indexed per result id + * and then per data item name. + * The structure is: + * PAGE_ID => [ augmentor name => data, ... ] + * @var array[] + */ + protected $extraData = []; + public function __construct( $containedSyntax = false ) { $this->containedSyntax = $containedSyntax; } @@ -147,15 +170,15 @@ class SearchResultSet { /** * Fetches next search result, or false. * STUB - * - * @return SearchResult + * FIXME: refactor as iterator, so we could use nicer interfaces. + * @return SearchResult|false */ function next() { return false; } /** - * Rewind result set back to begining + * Rewind result set back to beginning */ function rewind() { } @@ -176,4 +199,69 @@ class SearchResultSet { public function searchContainedSyntax() { return $this->containedSyntax; } + + /** + * Extract all the results in the result set as array. + * @return SearchResult[] + */ + public function extractResults() { + if ( is_null( $this->results ) ) { + $this->results = []; + if ( $this->numRows() == 0 ) { + // Don't bother if we've got empty result + return $this->results; + } + $this->rewind(); + while ( ( $result = $this->next() ) != false ) { + $this->results[] = $result; + } + $this->rewind(); + } + return $this->results; + } + + /** + * Extract all the titles in the result set. + * @return Title[] + */ + public function extractTitles() { + if ( is_null( $this->titles ) ) { + if ( $this->numRows() == 0 ) { + // Don't bother if we've got empty result + $this->titles = []; + } else { + $this->titles = array_map( + function ( SearchResult $result ) { + return $result->getTitle(); + }, + $this->extractResults() ); + } + } + return $this->titles; + } + + /** + * Sets augmented data for result set. + * @param string $name Extra data item name + * @param array[] $data Extra data as PAGEID => data + */ + public function setAugmentedData( $name, $data ) { + foreach ( $data as $id => $resultData ) { + $this->extraData[$id][$name] = $resultData; + } + } + + /** + * Returns extra data for specific result and store it in SearchResult object. + * @param SearchResult $result + * @return array|null List of data as name => value or null if none present. + */ + public function augmentResult( SearchResult $result ) { + $id = $result->getTitle()->getArticleID(); + if ( !$id || !isset( $this->extraData[$id] ) ) { + return null; + } + $result->setExtensionData( $this->extraData[$id] ); + return $this->extraData[$id]; + } }