MockSearchEngine.php 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. <?php
  2. use MediaWiki\MediaWikiServices;
  3. class MockSearchEngine extends SearchEngine {
  4. /** @var SearchResult[][] */
  5. private static $results = [];
  6. /** @var SearchResultSet[][] */
  7. private static $interwikiResults = [];
  8. public static function clearMockResults() {
  9. self::$results = [];
  10. self::$interwikiResults = [];
  11. }
  12. /**
  13. * @param string $query The query searched for *after* initial
  14. * transformations have been applied.
  15. * @param SearchResult[] $results The results to return for $query
  16. */
  17. public static function addMockResults( $query, array $results ) {
  18. $lc = MediaWikiServices::getInstance()->getLinkCache();
  19. foreach ( $results as &$result ) {
  20. // Resolve deferred results; needed to work around T203279
  21. if ( is_callable( $result ) ) {
  22. $result = $result();
  23. }
  24. // TODO: better page ids? Does it matter?
  25. $lc->addGoodLinkObj( mt_rand(), $result->getTitle() );
  26. }
  27. self::$results[$query] = $results;
  28. }
  29. /**
  30. * @param SearchResultSet[][] $interwikiResults
  31. */
  32. public static function setMockInterwikiResults( array $interwikiResults ) {
  33. self::$interwikiResults = $interwikiResults;
  34. }
  35. protected function doSearchText( $term ) {
  36. if ( isset( self::$results[ $term ] ) ) {
  37. $results = array_slice( self::$results[ $term ], $this->offset, $this->limit );
  38. } else {
  39. $results = [];
  40. }
  41. return new MockSearchResultSet( $results, self::$interwikiResults );
  42. }
  43. }