Merge "Special:Upload should not crash on failing previews"
[mediawiki.git] / includes / search / SearchSuggestion.php
blob4e7c782e84dec30c9e514028246256fcbcfc2310
1 <?php
3 /**
4 * Search suggestion
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.
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.
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
22 /**
23 * A search suggestion
25 class SearchSuggestion {
26 /**
27 * @var string the suggestion
29 private $text;
31 /**
32 * @var string the suggestion URL
34 private $url;
36 /**
37 * @var Title|null the suggested title
39 private $suggestedTitle;
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
46 private $suggestedTitleID;
48 /**
49 * @var float|null The suggestion score
51 private $score;
53 /**
54 * Construct a new suggestion
55 * @param float $score the suggestion score
56 * @param string $text|null the suggestion text
57 * @param Title|null $suggestedTitle the suggested title
58 * @param int|null $suggestedTitleID the suggested title ID
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 );
67 $this->suggestedTitleID = $suggestedTitleID;
70 /**
71 * The suggestion text
72 * @return string
74 public function getText() {
75 return $this->text;
78 /**
79 * Set the suggestion text.
80 * @param string $text
81 * @param bool $setTitle Should we also update the title?
83 public function setText( $text, $setTitle = true ) {
84 $this->text = $text;
85 if ( $setTitle && $text !== '' && $text !== null ) {
86 $this->setSuggestedTitle( Title::makeTitle( 0, $text ) );
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
95 public function getSuggestedTitle() {
96 return $this->suggestedTitle;
99 /**
100 * Set the suggested title
101 * @param Title|null $title
103 public function setSuggestedTitle( Title $title = null ) {
104 $this->suggestedTitle = $title;
105 if ( $title !== null ) {
106 $this->url = wfExpandUrl( $title->getFullURL(), PROTO_CURRENT );
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
115 public function getSuggestedTitleID() {
116 return $this->suggestedTitleID;
120 * Set the suggested title ID
121 * @param int|null $suggestedTitleID
123 public function setSuggestedTitleID( $suggestedTitleID = null ) {
124 $this->suggestedTitleID = $suggestedTitleID;
128 * Suggestion score
129 * @return float Suggestion score
131 public function getScore() {
132 return $this->score;
136 * Set the suggestion score
137 * @param float $score
139 public function setScore( $score ) {
140 $this->score = $score;
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
148 public function getURL() {
149 return $this->url;
153 * Set the suggestion URL
154 * @param string $url
156 public function setURL( $url ) {
157 $this->url = $url;
161 * Create suggestion from Title
162 * @param float $score Suggestions score
163 * @param Title $title
164 * @return SearchSuggestion
166 public static function fromTitle( $score, Title $title ) {
167 return new self( $score, $title->getPrefixedText(), $title, $title->getArticleID() );
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
177 public static function fromText( $score, $text ) {
178 $suggestion = new self( $score, $text );
179 if ( $text ) {
180 $suggestion->setSuggestedTitle( Title::makeTitle( 0, $text ) );
182 return $suggestion;