Merge "Make statsd sampling rates configurable"
[lhc/web/wiklou.git] / includes / search / SearchResult.php
1 <?php
2 /**
3 * Search engine result
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 * @ingroup Search
22 */
23
24 use MediaWiki\MediaWikiServices;
25
26 /**
27 * @todo FIXME: This class is horribly factored. It would probably be better to
28 * have a useful base class to which you pass some standard information, then
29 * let the fancy self-highlighters extend that.
30 * @ingroup Search
31 */
32 class SearchResult {
33
34 /**
35 * @var Revision
36 */
37 protected $mRevision = null;
38
39 /**
40 * @var File
41 */
42 protected $mImage = null;
43
44 /**
45 * @var Title
46 */
47 protected $mTitle;
48
49 /**
50 * @var string
51 */
52 protected $mText;
53
54 /**
55 * @var SearchEngine
56 */
57 protected $searchEngine;
58
59 /**
60 * Return a new SearchResult and initializes it with a title.
61 *
62 * @param Title $title
63 * @return SearchResult
64 */
65 public static function newFromTitle( $title ) {
66 $result = new static();
67 $result->initFromTitle( $title );
68 return $result;
69 }
70
71 /**
72 * Initialize from a Title and if possible initializes a corresponding
73 * Revision and File.
74 *
75 * @param Title $title
76 */
77 protected function initFromTitle( $title ) {
78 $this->mTitle = $title;
79 if ( !is_null( $this->mTitle ) ) {
80 $id = false;
81 Hooks::run( 'SearchResultInitFromTitle', [ $title, &$id ] );
82 $this->mRevision = Revision::newFromTitle(
83 $this->mTitle, $id, Revision::READ_NORMAL );
84 if ( $this->mTitle->getNamespace() === NS_FILE ) {
85 $this->mImage = wfFindFile( $this->mTitle );
86 }
87 }
88 $this->searchEngine = MediaWikiServices::getInstance()->newSearchEngine();
89 }
90
91 /**
92 * Check if this is result points to an invalid title
93 *
94 * @return bool
95 */
96 function isBrokenTitle() {
97 return is_null( $this->mTitle );
98 }
99
100 /**
101 * Check if target page is missing, happens when index is out of date
102 *
103 * @return bool
104 */
105 function isMissingRevision() {
106 return !$this->mRevision && !$this->mImage;
107 }
108
109 /**
110 * @return Title
111 */
112 function getTitle() {
113 return $this->mTitle;
114 }
115
116 /**
117 * Get the file for this page, if one exists
118 * @return File|null
119 */
120 function getFile() {
121 return $this->mImage;
122 }
123
124 /**
125 * Lazy initialization of article text from DB
126 */
127 protected function initText() {
128 if ( !isset( $this->mText ) ) {
129 if ( $this->mRevision != null ) {
130 $this->mText = $this->searchEngine->getTextFromContent(
131 $this->mTitle, $this->mRevision->getContent() );
132 } else { // TODO: can we fetch raw wikitext for commons images?
133 $this->mText = '';
134 }
135 }
136 }
137
138 /**
139 * @param array $terms Terms to highlight
140 * @return string Highlighted text snippet, null (and not '') if not supported
141 */
142 function getTextSnippet( $terms ) {
143 global $wgAdvancedSearchHighlighting;
144 $this->initText();
145
146 // TODO: make highliter take a content object. Make ContentHandler a factory for SearchHighliter.
147 list( $contextlines, $contextchars ) = $this->searchEngine->userHighlightPrefs();
148
149 $h = new SearchHighlighter();
150 if ( count( $terms ) > 0 ) {
151 if ( $wgAdvancedSearchHighlighting ) {
152 return $h->highlightText( $this->mText, $terms, $contextlines, $contextchars );
153 } else {
154 return $h->highlightSimple( $this->mText, $terms, $contextlines, $contextchars );
155 }
156 } else {
157 return $h->highlightNone( $this->mText, $contextlines, $contextchars );
158 }
159 }
160
161 /**
162 * @return string Highlighted title, '' if not supported
163 */
164 function getTitleSnippet() {
165 return '';
166 }
167
168 /**
169 * @return string Highlighted redirect name (redirect to this page), '' if none or not supported
170 */
171 function getRedirectSnippet() {
172 return '';
173 }
174
175 /**
176 * @return Title|null Title object for the redirect to this page, null if none or not supported
177 */
178 function getRedirectTitle() {
179 return null;
180 }
181
182 /**
183 * @return string Highlighted relevant section name, null if none or not supported
184 */
185 function getSectionSnippet() {
186 return '';
187 }
188
189 /**
190 * @return Title|null Title object (pagename+fragment) for the section,
191 * null if none or not supported
192 */
193 function getSectionTitle() {
194 return null;
195 }
196
197 /**
198 * @return string Highlighted relevant category name or '' if none or not supported
199 */
200 public function getCategorySnippet() {
201 return '';
202 }
203
204 /**
205 * @return string Timestamp
206 */
207 function getTimestamp() {
208 if ( $this->mRevision ) {
209 return $this->mRevision->getTimestamp();
210 } elseif ( $this->mImage ) {
211 return $this->mImage->getTimestamp();
212 }
213 return '';
214 }
215
216 /**
217 * @return int Number of words
218 */
219 function getWordCount() {
220 $this->initText();
221 return str_word_count( $this->mText );
222 }
223
224 /**
225 * @return int Size in bytes
226 */
227 function getByteSize() {
228 $this->initText();
229 return strlen( $this->mText );
230 }
231
232 /**
233 * @return string Interwiki prefix of the title (return iw even if title is broken)
234 */
235 function getInterwikiPrefix() {
236 return '';
237 }
238
239 /**
240 * @return string Interwiki namespace of the title (since we likely can't resolve it locally)
241 */
242 function getInterwikiNamespaceText() {
243 return '';
244 }
245
246 /**
247 * Did this match file contents (eg: PDF/DJVU)?
248 * @return bool
249 */
250 function isFileMatch() {
251 return false;
252 }
253 }