From 16e2491a73ea59510c55a8291a2921db2911d64b Mon Sep 17 00:00:00 2001 From: dcausse Date: Tue, 20 Sep 2016 12:07:35 +0200 Subject: [PATCH] Pass User to SearchEngine::getProfiles Useful for search engines that allow users to customize search profiles. Depends-On: Icd577c8ebc6e162befe30bde4fe276e633d2e434 Change-Id: I471cd090730d2a25cb70d622ec3bebbe9583118c --- includes/api/SearchApi.php | 8 +++++++- includes/search/SearchEngine.php | 5 +++-- tests/phpunit/includes/api/ApiOpenSearchTest.php | 4 ++-- 3 files changed, 12 insertions(+), 5 deletions(-) diff --git a/includes/api/SearchApi.php b/includes/api/SearchApi.php index 8ae1192837..fb9c4e67d9 100644 --- a/includes/api/SearchApi.php +++ b/includes/api/SearchApi.php @@ -104,7 +104,8 @@ trait SearchApi { $searchEngine = MediaWikiServices::getInstance()->newSearchEngine(); $params = []; foreach ( $configs as $paramName => $paramConfig ) { - $profiles = $searchEngine->getProfiles( $paramConfig['profile-type'] ); + $profiles = $searchEngine->getProfiles( $paramConfig['profile-type'], + $this->getContext()->getUser() ); if ( !$profiles ) { continue; } @@ -188,4 +189,9 @@ trait SearchApi { * containing 'help-message' and 'profile-type' keys. */ abstract public function getSearchProfileParams(); + + /** + * @return IContextSource + */ + abstract public function getContext(); } diff --git a/includes/search/SearchEngine.php b/includes/search/SearchEngine.php index 1eba14119e..da93a07fd6 100644 --- a/includes/search/SearchEngine.php +++ b/includes/search/SearchEngine.php @@ -648,10 +648,11 @@ abstract class SearchEngine { * - default: set to true if this profile is the default * * @since 1.28 - * @param $profileType the type of profiles + * @param string $profileType the type of profiles + * @param User|null $user the user requesting the list of profiles * @return array|null the list of profiles or null if none available */ - public function getProfiles( $profileType ) { + public function getProfiles( $profileType, User $user = null ) { return null; } diff --git a/tests/phpunit/includes/api/ApiOpenSearchTest.php b/tests/phpunit/includes/api/ApiOpenSearchTest.php index 39e90c2e33..5358f294c6 100644 --- a/tests/phpunit/includes/api/ApiOpenSearchTest.php +++ b/tests/phpunit/includes/api/ApiOpenSearchTest.php @@ -9,11 +9,12 @@ class ApiOpenSearchTest extends MediaWikiTestCase { ->method( 'getSearchTypes' ) ->will( $this->returnValue( [ 'the one ring' ] ) ); + $api = $this->createApi(); $engine = $this->replaceSearchEngine(); $engine->expects( $this->any() ) ->method( 'getProfiles' ) ->will( $this->returnValueMap( [ - [ SearchEngine::COMPLETION_PROFILE_TYPE, [ + [ SearchEngine::COMPLETION_PROFILE_TYPE, $api->getUser(), [ [ 'name' => 'normal', 'desc-message' => 'normal-message', @@ -26,7 +27,6 @@ class ApiOpenSearchTest extends MediaWikiTestCase { ] ], ] ) ); - $api = $this->createApi(); $params = $api->getAllowedParams(); $this->assertArrayNotHasKey( 'offset', $params ); -- 2.20.1