Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 25 |
|
0.00% |
0 / 13 |
CRAP | |
0.00% |
0 / 1 |
SearchSuggestion | |
0.00% |
0 / 25 |
|
0.00% |
0 / 13 |
380 | |
0.00% |
0 / 1 |
__construct | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
6 | |||
getText | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
setText | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
20 | |||
getSuggestedTitle | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
setSuggestedTitle | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
6 | |||
getSuggestedTitleID | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
setSuggestedTitleID | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getScore | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
setScore | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getURL | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
setURL | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
fromTitle | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
fromText | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
6 |
1 | <?php |
2 | |
3 | /** |
4 | * Search suggestion |
5 | * |
6 | * This program is free software; you can redistribute it and/or modify |
7 | * it under the terms of the GNU General Public License as published by |
8 | * the Free Software Foundation; either version 2 of the License, or |
9 | * (at your option) any later version. |
10 | * |
11 | * This program is distributed in the hope that it will be useful, |
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 | * GNU General Public License for more details. |
15 | * |
16 | * You should have received a copy of the GNU General Public License along |
17 | * with this program; if not, write to the Free Software Foundation, Inc., |
18 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
19 | * http://www.gnu.org/copyleft/gpl.html |
20 | */ |
21 | |
22 | use MediaWiki\MediaWikiServices; |
23 | use MediaWiki\Title\Title; |
24 | |
25 | /** |
26 | * A search suggestion |
27 | */ |
28 | class SearchSuggestion { |
29 | /** |
30 | * @var string the suggestion |
31 | */ |
32 | private $text; |
33 | |
34 | /** |
35 | * @var string the suggestion URL |
36 | */ |
37 | private $url; |
38 | |
39 | /** |
40 | * @var Title|null |
41 | */ |
42 | private $suggestedTitle; |
43 | |
44 | /** |
45 | * NOTE: even if suggestedTitle is a redirect suggestedTitleID |
46 | * is the ID of the target page. |
47 | * @var int|null the suggested title ID |
48 | */ |
49 | private $suggestedTitleID; |
50 | |
51 | /** |
52 | * @var float|null The suggestion score |
53 | */ |
54 | private $score; |
55 | |
56 | /** |
57 | * @param float $score the suggestion score |
58 | * @param string|null $text the suggestion text |
59 | * @param Title|null $suggestedTitle |
60 | * @param int|null $suggestedTitleID |
61 | */ |
62 | public function __construct( $score, $text = null, ?Title $suggestedTitle = null, |
63 | $suggestedTitleID = null ) { |
64 | $this->score = $score; |
65 | $this->text = $text; |
66 | if ( $suggestedTitle ) { |
67 | $this->setSuggestedTitle( $suggestedTitle ); |
68 | } |
69 | $this->suggestedTitleID = $suggestedTitleID; |
70 | } |
71 | |
72 | /** |
73 | * The suggestion text |
74 | * @return string |
75 | */ |
76 | public function getText() { |
77 | return $this->text; |
78 | } |
79 | |
80 | /** |
81 | * Set the suggestion text. |
82 | * @param string $text |
83 | * @param bool $setTitle Should we also update the title? |
84 | */ |
85 | public function setText( $text, $setTitle = true ) { |
86 | $this->text = $text; |
87 | if ( $setTitle && $text !== '' && $text !== null ) { |
88 | $this->setSuggestedTitle( Title::makeTitle( 0, $text ) ); |
89 | } |
90 | } |
91 | |
92 | /** |
93 | * Title object in the case this suggestion is based on a title. |
94 | * May return null if the suggestion is not a Title. |
95 | * @return Title|null |
96 | */ |
97 | public function getSuggestedTitle() { |
98 | return $this->suggestedTitle; |
99 | } |
100 | |
101 | /** |
102 | * @param Title|null $title |
103 | */ |
104 | public function setSuggestedTitle( ?Title $title = null ) { |
105 | $this->suggestedTitle = $title; |
106 | if ( $title !== null ) { |
107 | $urlUtils = MediaWikiServices::getInstance()->getUrlUtils(); |
108 | $this->url = $urlUtils->expand( $title->getFullURL(), PROTO_CURRENT ) ?? false; |
109 | } |
110 | } |
111 | |
112 | /** |
113 | * Title ID in the case this suggestion is based on a title. |
114 | * May return null if the suggestion is not a Title. |
115 | * @return int|null |
116 | */ |
117 | public function getSuggestedTitleID() { |
118 | return $this->suggestedTitleID; |
119 | } |
120 | |
121 | /** |
122 | * @param int|null $suggestedTitleID |
123 | */ |
124 | public function setSuggestedTitleID( $suggestedTitleID = null ) { |
125 | $this->suggestedTitleID = $suggestedTitleID; |
126 | } |
127 | |
128 | /** |
129 | * Suggestion score |
130 | * @return float Suggestion score |
131 | */ |
132 | public function getScore() { |
133 | return $this->score; |
134 | } |
135 | |
136 | /** |
137 | * Set the suggestion score |
138 | * @param float $score |
139 | */ |
140 | public function setScore( $score ) { |
141 | $this->score = $score; |
142 | } |
143 | |
144 | /** |
145 | * Suggestion URL, can be the link to the Title or maybe in the |
146 | * future a link to the search results for this search suggestion. |
147 | * @return string Suggestion URL |
148 | */ |
149 | public function getURL() { |
150 | return $this->url; |
151 | } |
152 | |
153 | /** |
154 | * Set the suggestion URL |
155 | * @param string $url |
156 | */ |
157 | public function setURL( $url ) { |
158 | $this->url = $url; |
159 | } |
160 | |
161 | /** |
162 | * Create suggestion from Title |
163 | * @param float $score Suggestions score |
164 | * @param Title $title |
165 | * @return SearchSuggestion |
166 | */ |
167 | public static function fromTitle( $score, Title $title ) { |
168 | return new self( $score, $title->getPrefixedText(), $title, $title->getArticleID() ); |
169 | } |
170 | |
171 | /** |
172 | * Create suggestion from text |
173 | * Will also create a title if text if not empty. |
174 | * @param float $score Suggestions score |
175 | * @param string $text |
176 | * @return SearchSuggestion |
177 | */ |
178 | public static function fromText( $score, $text ) { |
179 | $suggestion = new self( $score, $text ); |
180 | if ( $text ) { |
181 | $suggestion->setSuggestedTitle( Title::newFromText( $text ) ); |
182 | } |
183 | return $suggestion; |
184 | } |
185 | |
186 | } |