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;
}
/**
* 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() {
}
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];
+ }
}