Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
78.57% |
11 / 14 |
|
72.73% |
8 / 11 |
CRAP | |
0.00% |
0 / 1 |
SearchQuery | |
78.57% |
11 / 14 |
|
72.73% |
8 / 11 |
12.19 | |
0.00% |
0 / 1 |
__construct | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
1 | |||
getId | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getQueryString | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getTaskType | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getTopics | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getSort | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
setSort | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getRescoreProfile | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
setRescoreProfile | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getDebugUrl | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
setDebugUrl | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 |
1 | <?php |
2 | |
3 | namespace GrowthExperiments\NewcomerTasks\TaskSuggester\SearchStrategy; |
4 | |
5 | use GrowthExperiments\NewcomerTasks\TaskSuggester\UnderlinkedFunctionScoreBuilder; |
6 | use GrowthExperiments\NewcomerTasks\TaskType\TaskType; |
7 | use GrowthExperiments\NewcomerTasks\Topic\Topic; |
8 | |
9 | /** |
10 | * A search query string with some associated metadata about what it represents. |
11 | * For now, a query is associated with a single task type and a single topic; this might |
12 | * change in the future (see T238171#5870744). |
13 | */ |
14 | class SearchQuery { |
15 | |
16 | /** Sort option with custom handling for prioritizing underlinked articles. */ |
17 | public const RESCORE_UNDERLINKED = 'growth_underlinked'; |
18 | |
19 | /** @var string */ |
20 | private $id; |
21 | |
22 | /** @var string */ |
23 | private $queryString; |
24 | |
25 | /** @var TaskType */ |
26 | private $taskType; |
27 | |
28 | /** @var Topic[] */ |
29 | private array $topics; |
30 | |
31 | /** @var string|null */ |
32 | private $sort; |
33 | |
34 | /** @var string|null */ |
35 | private $rescoreProfile; |
36 | |
37 | /** @var string|null */ |
38 | private $debugUrl; |
39 | |
40 | /** |
41 | * @param string $id Search ID. Used for internal purposes such as debugging or deduplication. |
42 | * @param string $queryString Search query string. |
43 | * @param TaskType $taskType Task type returned by the query. |
44 | * @param array{0:?Topic} $topics Topics associated to the query. |
45 | */ |
46 | public function __construct( string $id, string $queryString, TaskType $taskType, array $topics ) { |
47 | $this->id = $id; |
48 | $this->queryString = $queryString; |
49 | $this->taskType = $taskType; |
50 | $this->topics = array_filter( $topics ); |
51 | } |
52 | |
53 | /** |
54 | * Get a human-readable unique ID for this search query. This is used internally by the |
55 | * task suggester for deduplication and for debug logging. |
56 | * @return string |
57 | */ |
58 | public function getId(): string { |
59 | return $this->id; |
60 | } |
61 | |
62 | /** |
63 | * Get the search query string represented by this object. This is a string suitable for |
64 | * passing to SearchEngine::searchText() or the srsearch parameter of the search API. |
65 | * @return string |
66 | */ |
67 | public function getQueryString(): string { |
68 | return $this->queryString; |
69 | } |
70 | |
71 | /** |
72 | * Results from the search query will belong to this task type. |
73 | * @return TaskType |
74 | */ |
75 | public function getTaskType(): TaskType { |
76 | return $this->taskType; |
77 | } |
78 | |
79 | /** |
80 | * Results from the search query can belong to any of the topics requested. To get per-topic score |
81 | * or extra data from the search result T243478 needs to be resolved first. |
82 | * @return array Array of Topic(s) associated with the query |
83 | */ |
84 | public function getTopics(): array { |
85 | return $this->topics; |
86 | } |
87 | |
88 | /** |
89 | * Get the sort option to use for this query (for SearchEngine::setSort() / the srsort |
90 | * API parameter). |
91 | * @return string|null |
92 | */ |
93 | public function getSort(): ?string { |
94 | return $this->sort; |
95 | } |
96 | |
97 | /** |
98 | * @param string|null $sort |
99 | * @see ::getSort |
100 | */ |
101 | public function setSort( ?string $sort ): void { |
102 | $this->sort = $sort; |
103 | } |
104 | |
105 | /** |
106 | * Get the custom rescore profile to use. |
107 | * @return string|null |
108 | * @see UnderlinkedFunctionScoreBuilder |
109 | */ |
110 | public function getRescoreProfile(): ?string { |
111 | return $this->rescoreProfile; |
112 | } |
113 | |
114 | /** |
115 | * @param string $rescoreProfile |
116 | * @see ::getRescoreProfile |
117 | */ |
118 | public function setRescoreProfile( string $rescoreProfile ) { |
119 | $this->rescoreProfile = $rescoreProfile; |
120 | } |
121 | |
122 | /** |
123 | * Get debug URL for this query. This is an URL that will give detailed information about |
124 | * the search results and how they were scored. |
125 | * Note: this is only set after the search was performed, and it depends on the suggester |
126 | * and its debug settings whether it is set at all. |
127 | * @return string|null |
128 | */ |
129 | public function getDebugUrl(): ?string { |
130 | return $this->debugUrl; |
131 | } |
132 | |
133 | /** |
134 | * @param string|null $debugUrl |
135 | * @see ::getDebugUrl |
136 | */ |
137 | public function setDebugUrl( ?string $debugUrl ): void { |
138 | $this->debugUrl = $debugUrl; |
139 | } |
140 | |
141 | } |