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