Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
24 / 24 |
|
100.00% |
7 / 7 |
CRAP | |
100.00% |
1 / 1 |
FallbackStatus | |
100.00% |
24 / 24 |
|
100.00% |
7 / 7 |
7 | |
100.00% |
1 / 1 |
__construct | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
apply | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
getAction | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
suggestQuery | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
1 | |||
replaceLocalResults | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
1 | |||
addInterwikiResults | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
1 | |||
noSuggestion | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 |
1 | <?php |
2 | |
3 | namespace CirrusSearch\Fallbacks; |
4 | |
5 | use CirrusSearch\Search\CirrusSearchResultSet; |
6 | use Closure; |
7 | use HtmlArmor; |
8 | |
9 | /** |
10 | * Representation of the result of running a FallbackMethod. |
11 | * |
12 | * Describes the change that the method has decided to apply to the search |
13 | * result set. This primarily allows wrapping the changes into atomic units |
14 | * of execution so the execution trace can be logged. |
15 | */ |
16 | class FallbackStatus { |
17 | public const NO_ACTION = 'noSuggestion'; |
18 | public const ACTION_SUGGEST_QUERY = 'suggestQuery'; |
19 | public const ACTION_REPLACE_LOCAL_RESULTS = 'replaceLocalResults'; |
20 | public const ACTION_ADD_INTERWIKI_RESULTS = 'addInterwikiResults'; |
21 | |
22 | /** @var string */ |
23 | private $actionName; |
24 | |
25 | /** @var Closure */ |
26 | private $fn; |
27 | |
28 | /** |
29 | * @param string $actionName An ACTION_* class constant, or NO_ACTION. Describes the action |
30 | * to perform. |
31 | * @param Closure $fn Function accepting a CirrusSearchResultSet and returning a CirrusSearchResultSet. |
32 | * Called to apply the chosen action to an existing result set. Implementations may return |
33 | * the provided result set, or a completely different one if desired. |
34 | */ |
35 | private function __construct( string $actionName, Closure $fn ) { |
36 | $this->actionName = $actionName; |
37 | $this->fn = $fn; |
38 | } |
39 | |
40 | public function apply( CirrusSearchResultSet $currentSet ): CirrusSearchResultSet { |
41 | $fn = $this->fn; |
42 | return $fn( $currentSet ); |
43 | } |
44 | |
45 | /** |
46 | * @return string The fallback action to perform |
47 | */ |
48 | public function getAction(): string { |
49 | return $this->actionName; |
50 | } |
51 | |
52 | /** |
53 | * @param string $query |
54 | * @param HtmlArmor|string|null $snippet |
55 | * @return FallbackStatus |
56 | */ |
57 | public static function suggestQuery( string $query, $snippet = null ): FallbackStatus { |
58 | return new self( self::ACTION_SUGGEST_QUERY, static function ( CirrusSearchResultSet $currentSet ) use ( $query, $snippet ) { |
59 | $currentSet->setSuggestionQuery( $query, $snippet ); |
60 | return $currentSet; |
61 | } ); |
62 | } |
63 | |
64 | /** |
65 | * @param CirrusSearchResultSet $rewrittenResults New result set to replace existing results with |
66 | * @param string $query The search query performed in the new result set. |
67 | * @param HtmlArmor|string|null $snippet A highlighted snippet showing the changes in $query. |
68 | * @return FallbackStatus |
69 | */ |
70 | public static function replaceLocalResults( CirrusSearchResultSet $rewrittenResults, string $query, $snippet = null ): FallbackStatus { |
71 | return new self( |
72 | self::ACTION_REPLACE_LOCAL_RESULTS, |
73 | static function ( CirrusSearchResultSet $currentSet ) use ( $rewrittenResults, $query, $snippet ) { |
74 | $rewrittenResults->setRewrittenQuery( $query, $snippet ); |
75 | return $rewrittenResults; |
76 | } ); |
77 | } |
78 | |
79 | /** |
80 | * @param CirrusSearchResultSet $results Interwiki results to add to current result set |
81 | * @param string $wikiId The wiki these results come from |
82 | * @return FallbackStatus |
83 | */ |
84 | public static function addInterwikiResults( CirrusSearchResultSet $results, string $wikiId ): FallbackStatus { |
85 | return new self( self::ACTION_ADD_INTERWIKI_RESULTS, static function ( |
86 | CirrusSearchResultSet $currentSet |
87 | ) use ( $results, $wikiId ) { |
88 | $currentSet->addInterwikiResults( $results, \SearchResultSet::INLINE_RESULTS, $wikiId ); |
89 | return $currentSet; |
90 | } ); |
91 | } |
92 | |
93 | public static function noSuggestion(): FallbackStatus { |
94 | return new self( self::NO_ACTION, static function ( CirrusSearchResultSet $currentSet ) { |
95 | return $currentSet; |
96 | } ); |
97 | } |
98 | } |