From 7b832c1fa33ad8917a1a5389710ac5feba808d6d Mon Sep 17 00:00:00 2001 From: daniel Date: Thu, 6 May 2021 10:33:45 +0200 Subject: [PATCH] Remove SearchResultPageIdentity interface SearchResultPageIdentity and SearchResultPageIdentityValue were introduced as placeholders for PageIdentity and PageIdentityValues. They can now become aliases. Bug: T282091 Depends-On: I9b1ab02e0acf12ace107361ea2c443aa543c4880 Depends-On: Ie405ea9539cd18e15b0abb6db6df64ec0135825d Change-Id: I6eb55c78a0a72be66814f8bc854ba663e7c6f62b --- autoload.php | 2 + .../Rest/Entity/SearchResultPageIdentity.php | 40 ---------------- .../Entity/SearchResultPageIdentityValue.php | 46 ------------------- includes/Rest/Handler/SearchHandler.php | 20 ++++---- includes/page/PageIdentityValue.php | 6 +++ includes/page/ProperPageIdentity.php | 6 +++ tests/phpunit/mocks/MockTitleTrait.php | 29 ++++++++++++ 7 files changed, 55 insertions(+), 94 deletions(-) delete mode 100644 includes/Rest/Entity/SearchResultPageIdentity.php delete mode 100644 includes/Rest/Entity/SearchResultPageIdentityValue.php diff --git a/autoload.php b/autoload.php index 026c754de90..03db83ec01f 100644 --- a/autoload.php +++ b/autoload.php @@ -1096,6 +1096,8 @@ $wgAutoloadLocalClasses = [ 'MediaWiki\\Parser\\ParserCacheMetadata' => __DIR__ . '/includes/parser/ParserCacheMetadata.php', 'MediaWiki\\Parser\\RevisionOutputCache' => __DIR__ . '/includes/parser/RevisionOutputCache.php', 'MediaWiki\\ProcOpenError' => __DIR__ . '/includes/exception/ProcOpenError.php', + 'MediaWiki\\Rest\\Entity\\SearchResultPageIdentity' => __DIR__ . '/includes/page/ProperPageIdentity.php', + 'MediaWiki\\Rest\\Entity\\SearchResultPageIdentityValue' => __DIR__ . '/includes/page/PageIdentityValue.php', 'MediaWiki\\ShellDisabledError' => __DIR__ . '/includes/exception/ShellDisabledError.php', 'MediaWiki\\Skins\\Hook\\SkinAfterPortletHook' => __DIR__ . '/includes/skins/Hook/SkinAfterPortletHook.php', 'MediaWiki\\Skins\\Hook\\SkinPageReadyConfigHook' => __DIR__ . '/includes/skins/Hook/SkinPageReadyConfigHook.php', diff --git a/includes/Rest/Entity/SearchResultPageIdentity.php b/includes/Rest/Entity/SearchResultPageIdentity.php deleted file mode 100644 index 65436c0b904..00000000000 --- a/includes/Rest/Entity/SearchResultPageIdentity.php +++ /dev/null @@ -1,40 +0,0 @@ -id = $id; - $this->namespace = $namespace; - $this->dbKey = $dbKey; - } - - public function getId(): int { - return $this->id; - } - - public function getNamespace(): int { - return $this->namespace; - } - - public function getDBkey(): string { - return $this->dbKey; - } - -} diff --git a/includes/Rest/Handler/SearchHandler.php b/includes/Rest/Handler/SearchHandler.php index 2b8303ad74a..90ad13dcbb5 100644 --- a/includes/Rest/Handler/SearchHandler.php +++ b/includes/Rest/Handler/SearchHandler.php @@ -5,7 +5,7 @@ namespace MediaWiki\Rest\Handler; use Config; use InvalidArgumentException; use ISearchResultSet; -use MediaWiki\Rest\Entity\SearchResultPageIdentityValue; +use MediaWiki\Page\ProperPageIdentity; use MediaWiki\Rest\Handler; use MediaWiki\Rest\LocalizedHttpException; use MediaWiki\Rest\Response; @@ -16,6 +16,7 @@ use SearchEngineFactory; use SearchResult; use SearchSuggestion; use Status; +use Title; use Wikimedia\Message\MessageValue; use Wikimedia\ParamValidator\ParamValidator; use Wikimedia\ParamValidator\TypeDef\IntegerDef; @@ -267,7 +268,7 @@ class SearchHandler extends Handler { * The information about description should be provided by extension by implementing * 'SearchResultProvideDescription' hook. Description is set to null if no extensions * implement the hook. - * @param array $pageIdentities + * @param ProperPageIdentity[] $pageIdentities * * @return array */ @@ -288,7 +289,7 @@ class SearchHandler extends Handler { * 'SearchResultProvideThumbnail' hook. Thumbnail is set to null if no extensions implement * the hook. * - * @param array $pageIdentities + * @param ProperPageIdentity[] $pageIdentities * * @return array */ @@ -309,15 +310,18 @@ class SearchHandler extends Handler { public function execute() { $searchEngine = $this->createSearchEngine(); $pageInfos = $this->doSearch( $searchEngine ); + + /** @var ProperPageIdentity[] $pageIdentities */ $pageIdentities = array_map( static function ( $pageInfo ) { + /** @var Title $title */ list( $title ) = $pageInfo; - return new SearchResultPageIdentityValue( - $title->getArticleID(), - $title->getNamespace(), - $title->getDBkey() - ); + return $title->exists() ? $title->toPageIdentity() : null; }, $pageInfos ); + // Remove empty entries resulting from non-proper pages like e.g. special pages + // in the search result. + $pageIdentities = array_filter( $pageIdentities ); + $result = array_map( "array_merge", $this->buildResultFromPageInfos( $pageInfos ), $this->buildDescriptionsFromPageIdentities( $pageIdentities ), diff --git a/includes/page/PageIdentityValue.php b/includes/page/PageIdentityValue.php index b259b44991c..3d9dc8481b0 100644 --- a/includes/page/PageIdentityValue.php +++ b/includes/page/PageIdentityValue.php @@ -88,3 +88,9 @@ class PageIdentityValue extends PageReferenceValue implements ProperPageIdentity } } + +// Retain compatibility with usage in Wikibase and PageImages +// along with SearchResultProvideDescriptionHook. +// Note that the class was marked @unstable, so the alias can be removed +// once the extensions have been updated. +class_alias( PageIdentityValue::class, 'MediaWiki\Rest\Entity\SearchResultPageIdentityValue' ); diff --git a/includes/page/ProperPageIdentity.php b/includes/page/ProperPageIdentity.php index 0210e3a79e3..0c9cbb1e2ae 100644 --- a/includes/page/ProperPageIdentity.php +++ b/includes/page/ProperPageIdentity.php @@ -74,3 +74,9 @@ interface ProperPageIdentity extends PageIdentity { public function canExist(): bool; } + +// Retain compatibility with hook interface signature as +// of SearchResultProvideDescriptionHook as used in Wikibase and PageImages. +// Note that the interface was marked @unstable, so the alias can be removed +// once the extensions have been updated. +class_alias( ProperPageIdentity::class, 'MediaWiki\Rest\Entity\SearchResultPageIdentity' ); diff --git a/tests/phpunit/mocks/MockTitleTrait.php b/tests/phpunit/mocks/MockTitleTrait.php index fc6d423d8f5..bd9fd35e5d2 100644 --- a/tests/phpunit/mocks/MockTitleTrait.php +++ b/tests/phpunit/mocks/MockTitleTrait.php @@ -1,5 +1,8 @@ method( '__toString' )->willReturn( "MockTitle:{$preText}" ); + $title->method( 'toPageIdentity' )->willReturnCallback( static function () use ( $title ) { + return new PageIdentityValue( + $title->getId(), + $title->getNamespace(), + $title->getDBkey(), + PageIdentity::LOCAL + ); + } ); + + $title->method( 'toPageRecord' )->willReturnCallback( static function () use ( $title ) { + return new PageStoreRecord( + (object)[ + 'page_id' => $title->getArticleID(), + 'page_namespace' => $title->getNamespace(), + 'page_title' => $title->getDBkey(), + 'page_wiki_id' => $title->getWikiId(), + 'page_latest' => $title->getLatestRevID(), + 'page_is_new' => $title->isNewPage(), + 'page_is_redirect' => $title->isRedirect(), + 'page_touched' => $title->getTouched(), + 'page_lang' => $title->getPageLanguage() ?: null, + ], + PageIdentity::LOCAL + ); + } ); + return $title; } }