]> scripts.mit.edu Git - autoinstalls/mediawiki.git/blob - includes/search/SearchSuggestion.php
MediaWiki 1.30.2-scripts2
[autoinstalls/mediawiki.git] / includes / search / SearchSuggestion.php
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 /**
23  * A search suggestion
24  */
25 class SearchSuggestion {
26         /**
27          * @var string the suggestion
28          */
29         private $text;
30
31         /**
32          * @var string the suggestion URL
33          */
34         private $url;
35
36         /**
37          * @var Title|null the suggested title
38          */
39         private $suggestedTitle;
40
41         /**
42          * NOTE: even if suggestedTitle is a redirect suggestedTitleID
43          * is the ID of the target page.
44          * @var int|null the suggested title ID
45          */
46         private $suggestedTitleID;
47
48         /**
49          * @var float|null The suggestion score
50          */
51         private $score;
52
53         /**
54          * Construct a new suggestion
55          * @param float $score the suggestion score
56          * @param string|null $text the suggestion text
57          * @param Title|null $suggestedTitle the suggested title
58          * @param int|null $suggestedTitleID the suggested title ID
59          */
60         public function __construct( $score, $text = null, Title $suggestedTitle = null,
61                         $suggestedTitleID = null ) {
62                 $this->score = $score;
63                 $this->text = $text;
64                 if ( $suggestedTitle ) {
65                         $this->setSuggestedTitle( $suggestedTitle );
66                 }
67                 $this->suggestedTitleID = $suggestedTitleID;
68         }
69
70         /**
71          * The suggestion text
72          * @return string
73          */
74         public function getText() {
75                 return $this->text;
76         }
77
78         /**
79          * Set the suggestion text.
80          * @param string $text
81          * @param bool $setTitle Should we also update the title?
82          */
83         public function setText( $text, $setTitle = true ) {
84                 $this->text = $text;
85                 if ( $setTitle && $text !== '' && $text !== null ) {
86                         $this->setSuggestedTitle( Title::makeTitle( 0, $text ) );
87                 }
88         }
89
90         /**
91          * Title object in the case this suggestion is based on a title.
92          * May return null if the suggestion is not a Title.
93          * @return Title|null
94          */
95         public function getSuggestedTitle() {
96                 return $this->suggestedTitle;
97         }
98
99         /**
100          * Set the suggested title
101          * @param Title|null $title
102          */
103         public function setSuggestedTitle( Title $title = null ) {
104                 $this->suggestedTitle = $title;
105                 if ( $title !== null ) {
106                         $this->url = wfExpandUrl( $title->getFullURL(), PROTO_CURRENT );
107                 }
108         }
109
110         /**
111          * Title ID in the case this suggestion is based on a title.
112          * May return null if the suggestion is not a Title.
113          * @return int|null
114          */
115         public function getSuggestedTitleID() {
116                 return $this->suggestedTitleID;
117         }
118
119         /**
120          * Set the suggested title ID
121          * @param int|null $suggestedTitleID
122          */
123         public function setSuggestedTitleID( $suggestedTitleID = null ) {
124                 $this->suggestedTitleID = $suggestedTitleID;
125         }
126
127         /**
128          * Suggestion score
129          * @return float Suggestion score
130          */
131         public function getScore() {
132                 return $this->score;
133         }
134
135         /**
136          * Set the suggestion score
137          * @param float $score
138          */
139         public function setScore( $score ) {
140                 $this->score = $score;
141         }
142
143         /**
144          * Suggestion URL, can be the link to the Title or maybe in the
145          * future a link to the search results for this search suggestion.
146          * @return string Suggestion URL
147          */
148         public function getURL() {
149                 return $this->url;
150         }
151
152         /**
153          * Set the suggestion URL
154          * @param string $url
155          */
156         public function setURL( $url ) {
157                 $this->url = $url;
158         }
159
160         /**
161          * Create suggestion from Title
162          * @param float $score Suggestions score
163          * @param Title $title
164          * @return SearchSuggestion
165          */
166         public static function fromTitle( $score, Title $title ) {
167                 return new self( $score, $title->getPrefixedText(), $title, $title->getArticleID() );
168         }
169
170         /**
171          * Create suggestion from text
172          * Will also create a title if text if not empty.
173          * @param float $score Suggestions score
174          * @param string $text
175          * @return SearchSuggestion
176          */
177         public static function fromText( $score, $text ) {
178                 $suggestion = new self( $score, $text );
179                 if ( $text ) {
180                         $suggestion->setSuggestedTitle( Title::makeTitle( 0, $text ) );
181                 }
182                 return $suggestion;
183         }
184
185 }