From e97ff3524bf1ca66bece7cf902ad4e9d5a972608 Mon Sep 17 00:00:00 2001 From: David Causse Date: Thu, 25 Jul 2019 14:11:06 +0200 Subject: [PATCH] Add BaseSearchResultSet Meant to hold the B/C behaviors and be the base class implemented by extensions. Bug: T228626 Change-Id: I072a9bd0f94bd785d1ff51f0e6ee13790e37b417 --- autoload.php | 1 + includes/search/BaseSearchResultSet.php | 76 +++++++++++++++++++ includes/search/ISearchResultSet.php | 5 ++ includes/search/SearchEngine.php | 4 + includes/search/SearchResultSet.php | 72 +----------------- .../includes/search/SearchResultSetTest.php | 8 +- 6 files changed, 91 insertions(+), 75 deletions(-) create mode 100644 includes/search/BaseSearchResultSet.php diff --git a/autoload.php b/autoload.php index e6e650492f..88d77770bc 100644 --- a/autoload.php +++ b/autoload.php @@ -181,6 +181,7 @@ $wgAutoloadLocalClasses = [ 'BadTitleError' => __DIR__ . '/includes/exception/BadTitleError.php', 'BagOStuff' => __DIR__ . '/includes/libs/objectcache/BagOStuff.php', 'BaseDump' => __DIR__ . '/includes/export/BaseDump.php', + 'BaseSearchResultSet' => __DIR__ . '/includes/search/BaseSearchResultSet.php', 'BaseTemplate' => __DIR__ . '/includes/skins/BaseTemplate.php', 'BashkirUppercaseCollation' => __DIR__ . '/includes/collation/BashkirUppercaseCollation.php', 'BatchRowIterator' => __DIR__ . '/includes/utils/BatchRowIterator.php', diff --git a/includes/search/BaseSearchResultSet.php b/includes/search/BaseSearchResultSet.php new file mode 100644 index 0000000000..d8aed0ea8f --- /dev/null +++ b/includes/search/BaseSearchResultSet.php @@ -0,0 +1,76 @@ +bcIterator(); + $searchResult = $it->current(); + $it->next(); + return $searchResult ?? false; + } + + /** + * Rewind result set back to beginning + * @deprecated since 1.32; Use self::extractResults() or foreach + */ + public function rewind() { + wfDeprecated( __METHOD__, '1.32' ); + $this->bcIterator()->rewind(); + } + + private function bcIterator() { + if ( $this->bcIterator === null ) { + $this->bcIterator = 'RECURSION'; + $this->bcIterator = $this->getIterator(); + } elseif ( $this->bcIterator === 'RECURSION' ) { + // Either next/rewind or extractResults must be implemented. This + // class was potentially instantiated directly. It should be + // abstract with abstract methods to enforce this but that's a + // breaking change... + wfDeprecated( static::class . ' without implementing extractResults', '1.32' ); + $this->bcIterator = new ArrayIterator( [] ); + } + return $this->bcIterator; + } + + /** + * Fetch an array of regular expression fragments for matching + * the search terms as parsed by this engine in a text extract. + * STUB + * + * @return string[] + * @deprecated since 1.34 (use SqlSearchResult) + */ + public function termMatches() { + return []; + } + + /** + * Frees the result set, if applicable. + * @deprecated noop since 1.34 + */ + public function free() { + } +} diff --git a/includes/search/ISearchResultSet.php b/includes/search/ISearchResultSet.php index 0dd902b139..5faa4451cd 100644 --- a/includes/search/ISearchResultSet.php +++ b/includes/search/ISearchResultSet.php @@ -1,6 +1,11 @@ $augmentor ) { $data = $augmentor->augmentAll( $resultSet ); if ( $data ) { diff --git a/includes/search/SearchResultSet.php b/includes/search/SearchResultSet.php index c9686bcdff..b8e40d5c5b 100644 --- a/includes/search/SearchResultSet.php +++ b/includes/search/SearchResultSet.php @@ -24,7 +24,7 @@ /** * @ingroup Search */ -class SearchResultSet implements ISearchResultSet { +class SearchResultSet extends BaseSearchResultSet { protected $containedSyntax = false; @@ -42,25 +42,11 @@ class SearchResultSet implements ISearchResultSet { */ protected $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 = []; - /** * @var boolean True when there are more pages of search results available. */ private $hasMoreResults; - /** - * @var ArrayIterator|null Iterator supporting BC iteration methods - */ - private $bcIterator; - /** * @param bool $containedSyntax True when query is not requesting a simple * term match @@ -78,18 +64,6 @@ class SearchResultSet implements ISearchResultSet { $this->hasMoreResults = $hasMoreResults; } - /** - * Fetch an array of regular expression fragments for matching - * the search terms as parsed by this engine in a text extract. - * STUB - * - * @return string[] - * @deprecated since 1.34 (use SqlSearchResult) - */ - public function termMatches() { - return []; - } - public function numRows() { return $this->count(); } @@ -183,50 +157,6 @@ class SearchResultSet implements ISearchResultSet { return false; } - /** - * Fetches next search result, or false. - * @deprecated since 1.32; Use self::extractResults() or foreach - * @return SearchResult|false - */ - public function next() { - wfDeprecated( __METHOD__, '1.32' ); - $it = $this->bcIterator(); - $searchResult = $it->current(); - $it->next(); - return $searchResult ?? false; - } - - /** - * Rewind result set back to beginning - * @deprecated since 1.32; Use self::extractResults() or foreach - */ - public function rewind() { - wfDeprecated( __METHOD__, '1.32' ); - $this->bcIterator()->rewind(); - } - - private function bcIterator() { - if ( $this->bcIterator === null ) { - $this->bcIterator = 'RECURSION'; - $this->bcIterator = $this->getIterator(); - } elseif ( $this->bcIterator === 'RECURSION' ) { - // Either next/rewind or extractResults must be implemented. This - // class was potentially instantiated directly. It should be - // abstract with abstract methods to enforce this but that's a - // breaking change... - wfDeprecated( static::class . ' without implementing extractResults', '1.32' ); - $this->bcIterator = new ArrayIterator( [] ); - } - return $this->bcIterator; - } - - /** - * Frees the result set, if applicable. - * @deprecated noop since 1.34 - */ - public function free() { - } - /** * Did the search contain search syntax? If so, Special:Search won't offer * the user a link to a create a page named by the search string because the diff --git a/tests/phpunit/includes/search/SearchResultSetTest.php b/tests/phpunit/includes/search/SearchResultSetTest.php index 774e023738..2d00a0de17 100644 --- a/tests/phpunit/includes/search/SearchResultSetTest.php +++ b/tests/phpunit/includes/search/SearchResultSetTest.php @@ -3,8 +3,8 @@ class SearchResultSetTest extends MediaWikiTestCase { /** * @covers SearchResultSet::getIterator - * @covers SearchResultSet::next - * @covers SearchResultSet::rewind + * @covers BaseSearchResultSet::next + * @covers BaseSearchResultSet::rewind */ public function testIterate() { $result = SearchResult::newFromTitle( Title::newMainPage() ); @@ -17,8 +17,8 @@ class SearchResultSetTest extends MediaWikiTestCase { } $this->assertEquals( 1, $count ); - $this->hideDeprecated( 'SearchResultSet::rewind' ); - $this->hideDeprecated( 'SearchResultSet::next' ); + $this->hideDeprecated( 'BaseSearchResultSet::rewind' ); + $this->hideDeprecated( 'BaseSearchResultSet::next' ); $resultSet->rewind(); $count = 0; while ( ( $iterResult = $resultSet->next() ) !== false ) { -- 2.20.1