Merge "Selenium: replace UserLoginPage with BlankPage where possible"
[lhc/web/wiklou.git] / includes / search / SearchOracle.php
1 <?php
2 /**
3 * Oracle search engine
4 *
5 * Copyright © 2004 Brion Vibber <brion@pobox.com>
6 * https://www.mediawiki.org/
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 * http://www.gnu.org/copyleft/gpl.html
22 *
23 * @file
24 * @ingroup Search
25 */
26
27 use MediaWiki\MediaWikiServices;
28
29 /**
30 * Search engine hook base class for Oracle (ConText).
31 * @ingroup Search
32 */
33 class SearchOracle extends SearchDatabase {
34 private $reservedWords = [
35 'ABOUT' => 1,
36 'ACCUM' => 1,
37 'AND' => 1,
38 'BT' => 1,
39 'BTG' => 1,
40 'BTI' => 1,
41 'BTP' => 1,
42 'FUZZY' => 1,
43 'HASPATH' => 1,
44 'INPATH' => 1,
45 'MINUS' => 1,
46 'NEAR' => 1,
47 'NOT' => 1,
48 'NT' => 1,
49 'NTG' => 1,
50 'NTI' => 1,
51 'NTP' => 1,
52 'OR' => 1,
53 'PT' => 1,
54 'RT' => 1,
55 'SQE' => 1,
56 'SYN' => 1,
57 'TR' => 1,
58 'TRSYN' => 1,
59 'TT' => 1,
60 'WITHIN' => 1,
61 ];
62
63 /**
64 * Perform a full text search query and return a result set.
65 *
66 * @param string $term Raw search term
67 * @return SqlSearchResultSet
68 */
69 protected function doSearchTextInDB( $term ) {
70 if ( $term == '' ) {
71 return new SqlSearchResultSet( false, '' );
72 }
73
74 $dbr = $this->lb->getConnectionRef( DB_REPLICA );
75 $resultSet = $dbr->query( $this->getQuery( $this->filter( $term ), true ) );
76 return new SqlSearchResultSet( $resultSet, $this->searchTerms );
77 }
78
79 /**
80 * Perform a title-only search query and return a result set.
81 *
82 * @param string $term Raw search term
83 * @return SqlSearchResultSet
84 */
85 protected function doSearchTitleInDB( $term ) {
86 if ( $term == '' ) {
87 return new SqlSearchResultSet( false, '' );
88 }
89
90 $dbr = $this->lb->getConnectionRef( DB_REPLICA );
91 $resultSet = $dbr->query( $this->getQuery( $this->filter( $term ), false ) );
92 return new SqlSearchResultSet( $resultSet, $this->searchTerms );
93 }
94
95 /**
96 * Return a partial WHERE clause to limit the search to the given namespaces
97 * @return string
98 */
99 private function queryNamespaces() {
100 if ( is_null( $this->namespaces ) ) {
101 return '';
102 }
103 if ( $this->namespaces === [] ) {
104 $namespaces = '0';
105 } else {
106 $dbr = $this->lb->getConnectionRef( DB_REPLICA );
107 $namespaces = $dbr->makeList( $this->namespaces );
108 }
109 return 'AND page_namespace IN (' . $namespaces . ')';
110 }
111
112 /**
113 * Return a LIMIT clause to limit results on the query.
114 *
115 * @param string $sql
116 *
117 * @return string
118 */
119 private function queryLimit( $sql ) {
120 $dbr = $this->lb->getConnectionRef( DB_REPLICA );
121
122 return $dbr->limitResult( $sql, $this->limit, $this->offset );
123 }
124
125 /**
126 * Does not do anything for generic search engine
127 * subclasses may define this though
128 *
129 * @param string $filteredTerm
130 * @param bool $fulltext
131 * @return string
132 */
133 function queryRanking( $filteredTerm, $fulltext ) {
134 return ' ORDER BY score(1)';
135 }
136
137 /**
138 * Construct the full SQL query to do the search.
139 * The guts shoulds be constructed in queryMain()
140 * @param string $filteredTerm
141 * @param bool $fulltext
142 * @return string
143 */
144 private function getQuery( $filteredTerm, $fulltext ) {
145 return $this->queryLimit( $this->queryMain( $filteredTerm, $fulltext ) . ' ' .
146 $this->queryNamespaces() . ' ' .
147 $this->queryRanking( $filteredTerm, $fulltext ) . ' ' );
148 }
149
150 /**
151 * Picks which field to index on, depending on what type of query.
152 * @param bool $fulltext
153 * @return string
154 */
155 private function getIndexField( $fulltext ) {
156 return $fulltext ? 'si_text' : 'si_title';
157 }
158
159 /**
160 * Get the base part of the search query.
161 *
162 * @param string $filteredTerm
163 * @param bool $fulltext
164 * @return string
165 */
166 function queryMain( $filteredTerm, $fulltext ) {
167 $match = $this->parseQuery( $filteredTerm, $fulltext );
168
169 $dbr = $this->lb->getMaintenanceConnectionRef( DB_REPLICA );
170 $page = $dbr->tableName( 'page' );
171 $searchindex = $dbr->tableName( 'searchindex' );
172
173 return 'SELECT page_id, page_namespace, page_title ' .
174 "FROM $page,$searchindex " .
175 'WHERE page_id=si_page AND ' . $match;
176 }
177
178 /**
179 * Parse a user input search string, and return an SQL fragment to be used
180 * as part of a WHERE clause
181 * @param string $filteredText
182 * @param bool $fulltext
183 * @return string
184 */
185 private function parseQuery( $filteredText, $fulltext ) {
186 $lc = $this->legalSearchChars( self::CHARS_NO_SYNTAX );
187 $this->searchTerms = [];
188
189 # @todo FIXME: This doesn't handle parenthetical expressions.
190 $m = [];
191 $searchon = '';
192 if ( preg_match_all( '/([-+<>~]?)(([' . $lc . ']+)(\*?)|"[^"]*")/',
193 $filteredText, $m, PREG_SET_ORDER ) ) {
194 foreach ( $m as $terms ) {
195 // Search terms in all variant forms, only
196 // apply on wiki with LanguageConverter
197 $temp_terms = MediaWikiServices::getInstance()->getContentLanguage()->
198 autoConvertToAllVariants( $terms[2] );
199 if ( is_array( $temp_terms ) ) {
200 $temp_terms = array_unique( array_values( $temp_terms ) );
201 foreach ( $temp_terms as $t ) {
202 $searchon .= ( $terms[1] == '-' ? ' ~' : ' & ' ) . $this->escapeTerm( $t );
203 }
204 } else {
205 $searchon .= ( $terms[1] == '-' ? ' ~' : ' & ' ) . $this->escapeTerm( $terms[2] );
206 }
207 if ( !empty( $terms[3] ) ) {
208 $regexp = preg_quote( $terms[3], '/' );
209 if ( $terms[4] ) {
210 $regexp .= "[0-9A-Za-z_]+";
211 }
212 } else {
213 $regexp = preg_quote( str_replace( '"', '', $terms[2] ), '/' );
214 }
215 $this->searchTerms[] = $regexp;
216 }
217 }
218
219 $dbr = $this->lb->getConnectionRef( DB_REPLICA );
220 $searchon = $dbr->addQuotes( ltrim( $searchon, ' &' ) );
221 $field = $this->getIndexField( $fulltext );
222
223 return " CONTAINS($field, $searchon, 1) > 0 ";
224 }
225
226 private function escapeTerm( $t ) {
227 $t = MediaWikiServices::getInstance()->getContentLanguage()->normalizeForSearch( $t );
228 $t = isset( $this->reservedWords[strtoupper( $t )] ) ? '{' . $t . '}' : $t;
229 $t = preg_replace( '/^"(.*)"$/', '($1)', $t );
230 $t = preg_replace( '/([-&|])/', '\\\\$1', $t );
231 return $t;
232 }
233
234 /**
235 * Create or update the search index record for the given page.
236 * Title and text should be pre-processed.
237 *
238 * @param int $id
239 * @param string $title
240 * @param string $text
241 */
242 function update( $id, $title, $text ) {
243 $dbw = $this->lb->getConnection( DB_MASTER );
244 $dbw->replace( 'searchindex',
245 [ 'si_page' ],
246 [
247 'si_page' => $id,
248 'si_title' => $title,
249 'si_text' => $text
250 ], 'SearchOracle::update' );
251
252 // Sync the index
253 // We need to specify the DB name (i.e. user/schema) here so that
254 // it can work from the installer, where
255 // ALTER SESSION SET CURRENT_SCHEMA = ...
256 // was used.
257 $dbw->query( "CALL ctx_ddl.sync_index(" .
258 $dbw->addQuotes( $dbw->getDBname() . '.' . $dbw->tableName( 'si_text_idx', 'raw' ) ) . ")" );
259 $dbw->query( "CALL ctx_ddl.sync_index(" .
260 $dbw->addQuotes( $dbw->getDBname() . '.' . $dbw->tableName( 'si_title_idx', 'raw' ) ) . ")" );
261 }
262
263 /**
264 * Update a search index record's title only.
265 * Title should be pre-processed.
266 *
267 * @param int $id
268 * @param string $title
269 */
270 function updateTitle( $id, $title ) {
271 $dbw = $this->lb->getConnectionRef( DB_MASTER );
272 $dbw->update( 'searchindex',
273 [ 'si_title' => $title ],
274 [ 'si_page' => $id ],
275 'SearchOracle::updateTitle',
276 [] );
277 }
278
279 public function legalSearchChars( $type = self::CHARS_ALL ) {
280 $searchChars = parent::legalSearchChars( $type );
281 if ( $type === self::CHARS_ALL ) {
282 $searchChars = "\"" . $searchChars;
283 }
284 return $searchChars;
285 }
286 }