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
25 class SearchSuggestion
{
27 * @var string the suggestion
32 * @var string the suggestion URL
37 * @var Title|null the suggested title
39 private $suggestedTitle;
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;
49 * @var float|null The suggestion score
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;
64 if ( $suggestedTitle ) {
65 $this->setSuggestedTitle( $suggestedTitle );
67 $this->suggestedTitleID
= $suggestedTitleID;
74 public function getText() {
79 * Set the suggestion text.
81 * @param bool $setTitle Should we also update the title?
83 public function setText( $text, $setTitle = true ) {
85 if ( $setTitle && $text !== '' && $text !== null ) {
86 $this->setSuggestedTitle( Title
::makeTitle( 0, $text ) );
91 * Title object in the case this suggestion is based on a title.
92 * May return null if the suggestion is not a Title.
95 public function getSuggestedTitle() {
96 return $this->suggestedTitle
;
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.
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;
129 * @return float Suggestion score
131 public function getScore() {
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() {
153 * Set the suggestion URL
156 public function setURL( $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 );
180 $suggestion->setSuggestedTitle( Title
::makeTitle( 0, $text ) );