BaseSearchResultSet.php 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. <?php
  2. /**
  3. * BaseSearchResultSet is the base class that must be extended by SearchEngine
  4. * search result set implementations.
  5. *
  6. * This base class is meant to hold B/C behaviors and to be useful it must never:
  7. * - be used as type hints (ISearchResultSet must be used for this)
  8. * - implement a constructor
  9. * - declare utility methods
  10. *
  11. * @ingroup Search
  12. */
  13. abstract class BaseSearchResultSet implements ISearchResultSet {
  14. /**
  15. * @var ArrayIterator|null Iterator supporting BC iteration methods
  16. */
  17. private $bcIterator;
  18. /**
  19. * Fetches next search result, or false.
  20. * @deprecated since 1.32; Use self::extractResults() or foreach
  21. * @return SearchResult|false
  22. */
  23. public function next() {
  24. wfDeprecated( __METHOD__, '1.32' );
  25. $it = $this->bcIterator();
  26. $searchResult = $it->current();
  27. $it->next();
  28. return $searchResult ?? false;
  29. }
  30. /**
  31. * Rewind result set back to beginning
  32. * @deprecated since 1.32; Use self::extractResults() or foreach
  33. */
  34. public function rewind() {
  35. wfDeprecated( __METHOD__, '1.32' );
  36. $this->bcIterator()->rewind();
  37. }
  38. private function bcIterator() {
  39. if ( $this->bcIterator === null ) {
  40. $this->bcIterator = 'RECURSION';
  41. $this->bcIterator = $this->getIterator();
  42. } elseif ( $this->bcIterator === 'RECURSION' ) {
  43. // Either next/rewind or extractResults must be implemented. This
  44. // class was potentially instantiated directly. It should be
  45. // abstract with abstract methods to enforce this but that's a
  46. // breaking change...
  47. wfDeprecated( static::class . ' without implementing extractResults', '1.32' );
  48. $this->bcIterator = new ArrayIterator( [] );
  49. }
  50. return $this->bcIterator;
  51. }
  52. /**
  53. * Fetch an array of regular expression fragments for matching
  54. * the search terms as parsed by this engine in a text extract.
  55. * STUB
  56. *
  57. * @return string[]
  58. * @deprecated since 1.34 (use SqlSearchResult)
  59. */
  60. public function termMatches() {
  61. return [];
  62. }
  63. /**
  64. * Frees the result set, if applicable.
  65. * @deprecated noop since 1.34
  66. */
  67. public function free() {
  68. }
  69. }