Merge "Type hint against LinkTarget in WatchedItemStore"
[lhc/web/wiklou.git] / includes / search / SqlSearchResult.php
1 <?php
2
3 /**
4 * Search engine result issued from SearchData search engines.
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 * @file
22 * @ingroup Search
23 */
24
25 class SqlSearchResult extends SearchResult {
26 /** @var string[] */
27 private $terms;
28
29 /**
30 * SqlSearchResult constructor.
31 * @param Title $title
32 * @param string[] $terms list of parsed terms
33 */
34 public function __construct( Title $title, array $terms ) {
35 $this->initFromTitle( $title );
36 $this->terms = $terms;
37 }
38
39 /**
40 * return string[]
41 */
42 public function getTermMatches(): array {
43 return $this->terms;
44 }
45
46 /**
47 * @param array $terms Terms to highlight (this parameter is deprecated)
48 * @return string Highlighted text snippet, null (and not '') if not supported
49 */
50 function getTextSnippet( $terms = [] ) {
51 global $wgAdvancedSearchHighlighting;
52 $this->initText();
53
54 // TODO: make highliter take a content object. Make ContentHandler a factory for SearchHighliter.
55 list( $contextlines, $contextchars ) = $this->searchEngine->userHighlightPrefs();
56
57 $h = new SearchHighlighter();
58 if ( count( $this->terms ) > 0 ) {
59 if ( $wgAdvancedSearchHighlighting ) {
60 return $h->highlightText( $this->mText, $this->terms, $contextlines, $contextchars );
61 } else {
62 return $h->highlightSimple( $this->mText, $this->terms, $contextlines, $contextchars );
63 }
64 } else {
65 return $h->highlightNone( $this->mText, $contextlines, $contextchars );
66 }
67 }
68
69 }