summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/SemanticMediaWiki/src/MediaWiki/Search/SearchResultSet.php
blob: 6843891b802ee6b5077c2f078aa9efd19291c24b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
<?php

namespace SMW\MediaWiki\Search;

use SMW\DIWikiPage;
use SMW\Utils\CharExaminer;

/**
 * @ingroup SMW
 *
 * @licence GNU GPL v2+
 * @since   2.1
 *
 * @author  Stephan Gambke
 */
class SearchResultSet extends \SearchResultSet {

	/**
	 * @var DIWikiPage[]|[]
	 */
	private $pages;

	/**
	 * @var QueryToken
	 */
	private $queryToken;

	/**
	 * @var Excerpts
	 */
	private $excerpts;

	private $count = null;

	public function __construct( \SMWQueryResult $result, $count = null ) {
		$this->pages = $result->getResults();
		$this->queryToken = $result->getQuery()->getQueryToken();
		$this->excerpts = $result->getExcerpts();
		$this->count = $count;
	}

	/**
	 * Return number of rows included in this result set.
	 *
	 * @return int|void
	 */
	public function numRows() {
		return count( $this->pages );
	}

	/**
	 * Return true if results are included in this result set.
	 *
	 * @return bool
	 */
	public function hasResults() {
		return $this->numRows() > 0;
	}

	/**
	 * Fetches next search result, or false.
	 *
	 * @return SearchResult
	 */
	public function next() {

		$page = current( $this->pages );
		$searchResult = false;

		if ( $page instanceof DIWikiPage ) {
			$searchResult = SearchResult::newFromTitle( $page->getTitle() );
		}

		// Attempt to use excerpts available from a different back-end
		if ( $searchResult && $this->excerpts !== null ) {
			if ( ( $excerpt = $this->excerpts->getExcerpt( $page ) ) !== false ) {
				$searchResult->setExcerpt( $excerpt, $this->excerpts->hasHighlight() );
			}
		}

		next( $this->pages );

		return $searchResult;
	}

	/**
	 * @since 3.0
	 *
	 * @return SearchSuggestionSet
	 */
	public function newSearchSuggestionSet() {

		$suggestions = [];
		$hasMoreResults = false;
		$score = count( $this->pages );

		foreach ( $this->pages as $page ) {
			if ( ( $title = $page->getTitle() ) && $title->exists() ) {
				$suggestions[] = \SearchSuggestion::fromTitle( $score--, $title );
			}
		}

		return new \SearchSuggestionSet( $suggestions, $hasMoreResults );
	}

	/**
	 * @see SearchResultSet::extractResults
	 *
	 * @since 3.0
	 */
	public function extractResults() {

		// #3204
		// https://github.com/wikimedia/mediawiki/commit/720fdfa7901cbba93b5695ed5f00f982272ced27
		//
		// MW 1.32+:
		// - Remove SearchResultSet::next, SearchResultSet::numRows
		// - Move QueryResult::getResults, QueryResult::getExcerpts into this
		//   method to avoid constructor work

		if ( $this->pages === [] ) {
			return $this->results = [];
		}

		foreach ( $this->pages as $page ) {

			if ( $page instanceof DIWikiPage ) {
				$searchResult = SearchResult::newFromTitle( $page->getTitle() );
			}

			// Attempt to use excerpts available from a different back-end
			if ( $searchResult && $this->excerpts !== null ) {
				if ( ( $excerpt = $this->excerpts->getExcerpt( $page ) ) !== false ) {
					$searchResult->setExcerpt( $excerpt, $this->excerpts->hasHighlight() );
				}
			}

			$this->results[] = $searchResult;
		}

		return $this->results;
	}

	/**
	 * Returns true, so Special:Search won't offer the user a link to a create
	 * a page named by the search string because the name would contain the
	 * search syntax, i.e. the SMW query.
	 *
	 * @return bool
	 */
	public function searchContainedSyntax() {
		return true;
	}

	public function getTotalHits() {
		return $this->count;
	}

	/**
	 * Return an array of regular expression fragments for matching
	 * the search terms as parsed by the engine in a text extract.
	 *
	 * This is a temporary hack for MW versions that can not cope
	 * with no search term being returned (<1.24).
	 *
	 * @deprecated remove once min supported MW version has \SearchHighlighter::highlightNone()
	 *
	 * @return string[]
	 */
	public function termMatches() {

		if ( ( $tokens = $this->getTokens() ) !== [] ) {
			return $tokens;
		}

		if ( method_exists( '\SearchHighlighter', 'highlightNone' ) ) {
			return [];
		}

		// Will cause the highlighter to match every line start, thus returning the first few lines of found pages.
		return [ '^' ];
	}

	private function getTokens() {

		$tokens = [];

		if ( $this->queryToken === null ) {
			return $tokens;
		}

		// Use tokens gathered from a query context [[in:Foo]] (~~*Foo*), a filter context
		// such as [[Category:Foo]] is not considered eligible to provide a
		// token.
		foreach ( $this->queryToken->getTokens() as $key => $value ) {
			// Avoid add \b boundary checks for CJK where whitespace is not used
			// as word break
			$tokens[] = CharExaminer::isCJK( $key ) ? "$key" : "\b$key\b";
		}

		return $tokens;
	}

}