summaryrefslogtreecommitdiff
path: root/www/wiki/includes/search/SearchSuggestion.php
blob: 7f433db4aed32f1dcc2258e2c0dfae4c59a0feeb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
<?php

/**
 * Search suggestion
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 * http://www.gnu.org/copyleft/gpl.html
 */

/**
 * A search suggestion
 */
class SearchSuggestion {
	/**
	 * @var string the suggestion
	 */
	private $text;

	/**
	 * @var string the suggestion URL
	 */
	private $url;

	/**
	 * @var Title|null the suggested title
	 */
	private $suggestedTitle;

	/**
	 * NOTE: even if suggestedTitle is a redirect suggestedTitleID
	 * is the ID of the target page.
	 * @var int|null the suggested title ID
	 */
	private $suggestedTitleID;

	/**
	 * @var float|null The suggestion score
	 */
	private $score;

	/**
	 * Construct a new suggestion
	 * @param float $score the suggestion score
	 * @param string|null $text the suggestion text
	 * @param Title|null $suggestedTitle the suggested title
	 * @param int|null $suggestedTitleID the suggested title ID
	 */
	public function __construct( $score, $text = null, Title $suggestedTitle = null,
			$suggestedTitleID = null ) {
		$this->score = $score;
		$this->text = $text;
		if ( $suggestedTitle ) {
			$this->setSuggestedTitle( $suggestedTitle );
		}
		$this->suggestedTitleID = $suggestedTitleID;
	}

	/**
	 * The suggestion text
	 * @return string
	 */
	public function getText() {
		return $this->text;
	}

	/**
	 * Set the suggestion text.
	 * @param string $text
	 * @param bool $setTitle Should we also update the title?
	 */
	public function setText( $text, $setTitle = true ) {
		$this->text = $text;
		if ( $setTitle && $text !== '' && $text !== null ) {
			$this->setSuggestedTitle( Title::makeTitle( 0, $text ) );
		}
	}

	/**
	 * Title object in the case this suggestion is based on a title.
	 * May return null if the suggestion is not a Title.
	 * @return Title|null
	 */
	public function getSuggestedTitle() {
		return $this->suggestedTitle;
	}

	/**
	 * Set the suggested title
	 * @param Title|null $title
	 */
	public function setSuggestedTitle( Title $title = null ) {
		$this->suggestedTitle = $title;
		if ( $title !== null ) {
			$this->url = wfExpandUrl( $title->getFullURL(), PROTO_CURRENT );
		}
	}

	/**
	 * Title ID in the case this suggestion is based on a title.
	 * May return null if the suggestion is not a Title.
	 * @return int|null
	 */
	public function getSuggestedTitleID() {
		return $this->suggestedTitleID;
	}

	/**
	 * Set the suggested title ID
	 * @param int|null $suggestedTitleID
	 */
	public function setSuggestedTitleID( $suggestedTitleID = null ) {
		$this->suggestedTitleID = $suggestedTitleID;
	}

	/**
	 * Suggestion score
	 * @return float Suggestion score
	 */
	public function getScore() {
		return $this->score;
	}

	/**
	 * Set the suggestion score
	 * @param float $score
	 */
	public function setScore( $score ) {
		$this->score = $score;
	}

	/**
	 * Suggestion URL, can be the link to the Title or maybe in the
	 * future a link to the search results for this search suggestion.
	 * @return string Suggestion URL
	 */
	public function getURL() {
		return $this->url;
	}

	/**
	 * Set the suggestion URL
	 * @param string $url
	 */
	public function setURL( $url ) {
		$this->url = $url;
	}

	/**
	 * Create suggestion from Title
	 * @param float $score Suggestions score
	 * @param Title $title
	 * @return SearchSuggestion
	 */
	public static function fromTitle( $score, Title $title ) {
		return new self( $score, $title->getPrefixedText(), $title, $title->getArticleID() );
	}

	/**
	 * Create suggestion from text
	 * Will also create a title if text if not empty.
	 * @param float $score Suggestions score
	 * @param string $text
	 * @return SearchSuggestion
	 */
	public static function fromText( $score, $text ) {
		$suggestion = new self( $score, $text );
		if ( $text ) {
			$suggestion->setSuggestedTitle( Title::makeTitle( 0, $text ) );
		}
		return $suggestion;
	}

}