Created
December 16, 2021 22:38
-
-
Save bpirkle/19e5bed7e33daccfcba790a0ec8a26ae to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use MediaWiki\HookContainer\HookRunner; | |
use MediaWiki\MediaWikiServices; | |
use MediaWiki\Rest\LocalizedHttpException; | |
use Wikimedia\Message\MessageValue; | |
$services = MediaWikiServices::getInstance(); | |
$searchEngineFactory = $services->getSearchEngineFactory(); | |
$searchEngineConfig = $services->getSearchEngineConfig(); | |
$searchEngine = $searchEngineFactory->create(); | |
$searchEngine->setNamespaces( $searchEngineConfig->defaultNamespaces() ); | |
$hookRunner = new HookRunner( $services->getHookContainer() ); | |
$query = 'File:Russian balls 12-13.jpg'; | |
function bdpGetSearchResultsOrThrow( $results ) { | |
if ( $results ) { | |
if ( $results instanceof Status ) { | |
$status = $results; | |
if ( !$status->isOK() ) { | |
list( $error ) = $status->splitByErrorType(); | |
if ( $error->getErrors() ) { // Only throw for errors, suppress warnings (for now) | |
$errorMessages = $error->getMessage(); | |
throw new LocalizedHttpException( | |
new MessageValue( "rest-search-error", [ $errorMessages->getKey() ] ) | |
); | |
} | |
} | |
$statusValue = $status->getValue(); | |
if ( $statusValue instanceof ISearchResultSet ) { | |
return $statusValue->extractResults(); | |
} | |
} else { | |
return $results->extractResults(); | |
} | |
} | |
return []; | |
} | |
$titleSearch = $searchEngine->searchTitle( $query ); | |
$textSearch = $searchEngine->searchText( $query ); | |
$titleSearchResults = bdpGetSearchResultsOrThrow( $titleSearch ); | |
$textSearchResults = bdpGetSearchResultsOrThrow( $textSearch ); | |
$mergedResults = array_merge( $titleSearchResults, $textSearchResults ); | |
$pageInfos = []; | |
foreach ( $mergedResults as $result ) { | |
if ( !$result->isBrokenTitle() && !$result->isMissingRevision() ) { | |
$title = $result->getTitle(); | |
$pageID = $title->getArticleID(); | |
if ( !isset( $pageInfos[$pageID] ) ) { | |
$pageInfos[$pageID] = [ $title, null, $result ]; | |
} | |
} | |
} | |
$pageIdentities = array_map( static function ( $pageInfo ) { | |
/** @var Title $title */ | |
list( $title ) = $pageInfo; | |
return $title->exists() ? $title->toPageIdentity() : null; | |
}, $pageInfos ); | |
$pageIdentities = array_filter( $pageIdentities ); | |
$r = array_map( static function ( $pageInfo ) { | |
list( $title, $sugg, $result ) = $pageInfo; | |
return [ | |
'id' => $title->getArticleID(), | |
'key' => $title->getPrefixedDBkey(), | |
'title' => $title->getPrefixedText(), | |
'excerpt' => ($sugg ? $sugg->getText() : $result->getTextSnippet()) ?: null, | |
]; | |
}, | |
$pageInfos ); | |
$descriptions = array_fill_keys( array_keys( $pageIdentities ), null ); | |
$hookRunner->onSearchResultProvideDescription( $pageIdentities, $descriptions ); | |
$d = array_map( static function ( $description ) { | |
return [ 'description' => $description ]; | |
}, $descriptions ); | |
$thumbnails = array_fill_keys( array_keys( $pageIdentities ), null ); | |
$hookRunner->onSearchResultProvideThumbnail( $pageIdentities, $thumbnails ); | |
$t = array_map( function ( $thumbnail ) { | |
return [ 'thumbnail' => $this->serializeThumbnail( $thumbnail ) ]; | |
}, $thumbnails ); | |
$result = array_map( "array_merge", | |
$r, | |
$d, | |
$t | |
); | |
print_r( $r ); | |
print_r( $d ); | |
print_r( $t ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment