Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 24
0.00% covered (danger)
0.00%
0 / 13
CRAP
0.00% covered (danger)
0.00%
0 / 1
SearchSuggestion
0.00% covered (danger)
0.00%
0 / 24
0.00% covered (danger)
0.00%
0 / 13
380
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
6
 getText
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 setText
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
20
 getSuggestedTitle
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 setSuggestedTitle
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
6
 getSuggestedTitleID
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 setSuggestedTitleID
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getScore
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 setScore
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getURL
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 setURL
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 fromTitle
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 fromText
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
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
22use MediaWiki\Title\Title;
23
24/**
25 * A search suggestion
26 */
27class SearchSuggestion {
28    /**
29     * @var string the suggestion
30     */
31    private $text;
32
33    /**
34     * @var string the suggestion URL
35     */
36    private $url;
37
38    /**
39     * @var Title|null
40     */
41    private $suggestedTitle;
42
43    /**
44     * NOTE: even if suggestedTitle is a redirect suggestedTitleID
45     * is the ID of the target page.
46     * @var int|null the suggested title ID
47     */
48    private $suggestedTitleID;
49
50    /**
51     * @var float|null The suggestion score
52     */
53    private $score;
54
55    /**
56     * @param float $score the suggestion score
57     * @param string|null $text the suggestion text
58     * @param Title|null $suggestedTitle
59     * @param int|null $suggestedTitleID
60     */
61    public function __construct( $score, $text = null, Title $suggestedTitle = null,
62            $suggestedTitleID = null ) {
63        $this->score = $score;
64        $this->text = $text;
65        if ( $suggestedTitle ) {
66            $this->setSuggestedTitle( $suggestedTitle );
67        }
68        $this->suggestedTitleID = $suggestedTitleID;
69    }
70
71    /**
72     * The suggestion text
73     * @return string
74     */
75    public function getText() {
76        return $this->text;
77    }
78
79    /**
80     * Set the suggestion text.
81     * @param string $text
82     * @param bool $setTitle Should we also update the title?
83     */
84    public function setText( $text, $setTitle = true ) {
85        $this->text = $text;
86        if ( $setTitle && $text !== '' && $text !== null ) {
87            $this->setSuggestedTitle( Title::makeTitle( 0, $text ) );
88        }
89    }
90
91    /**
92     * Title object in the case this suggestion is based on a title.
93     * May return null if the suggestion is not a Title.
94     * @return Title|null
95     */
96    public function getSuggestedTitle() {
97        return $this->suggestedTitle;
98    }
99
100    /**
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     * @param int|null $suggestedTitleID
121     */
122    public function setSuggestedTitleID( $suggestedTitleID = null ) {
123        $this->suggestedTitleID = $suggestedTitleID;
124    }
125
126    /**
127     * Suggestion score
128     * @return float Suggestion score
129     */
130    public function getScore() {
131        return $this->score;
132    }
133
134    /**
135     * Set the suggestion score
136     * @param float $score
137     */
138    public function setScore( $score ) {
139        $this->score = $score;
140    }
141
142    /**
143     * Suggestion URL, can be the link to the Title or maybe in the
144     * future a link to the search results for this search suggestion.
145     * @return string Suggestion URL
146     */
147    public function getURL() {
148        return $this->url;
149    }
150
151    /**
152     * Set the suggestion URL
153     * @param string $url
154     */
155    public function setURL( $url ) {
156        $this->url = $url;
157    }
158
159    /**
160     * Create suggestion from Title
161     * @param float $score Suggestions score
162     * @param Title $title
163     * @return SearchSuggestion
164     */
165    public static function fromTitle( $score, Title $title ) {
166        return new self( $score, $title->getPrefixedText(), $title, $title->getArticleID() );
167    }
168
169    /**
170     * Create suggestion from text
171     * Will also create a title if text if not empty.
172     * @param float $score Suggestions score
173     * @param string $text
174     * @return SearchSuggestion
175     */
176    public static function fromText( $score, $text ) {
177        $suggestion = new self( $score, $text );
178        if ( $text ) {
179            $suggestion->setSuggestedTitle( Title::newFromText( $text ) );
180        }
181        return $suggestion;
182    }
183
184}