0cbb41c4711b17e4f6d05506b0239312f3ff170d
[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 $resultSet = $this->db->query( $this->getQuery( $this->filter( $term ), true ) );
75 return new SqlSearchResultSet( $resultSet, $this->searchTerms );
76 }
77
78 /**
79 * Perform a title-only search query and return a result set.
80 *
81 * @param string $term Raw search term
82 * @return SqlSearchResultSet
83 */
84 protected function doSearchTitleInDB( $term ) {
85 if ( $term == '' ) {
86 return new SqlSearchResultSet( false, '' );
87 }
88
89 $resultSet = $this->db->query( $this->getQuery( $this->filter( $term ), false ) );
90 return new SqlSearchResultSet( $resultSet, $this->searchTerms );
91 }
92
93 /**
94 * Return a partial WHERE clause to limit the search to the given namespaces
95 * @return string
96 */
97 private function queryNamespaces() {
98 if ( is_null( $this->namespaces ) ) {
99 return '';
100 }
101 if ( $this->namespaces === [] ) {
102 $namespaces = '0';
103 } else {
104 $namespaces = $this->db->makeList( $this->namespaces );
105 }
106 return 'AND page_namespace IN (' . $namespaces . ')';
107 }
108
109 /**
110 * Return a LIMIT clause to limit results on the query.
111 *
112 * @param string $sql
113 *
114 * @return string
115 */
116 private function queryLimit( $sql ) {
117 return $this->db->limitResult( $sql, $this->limit, $this->offset );
118 }
119
120 /**
121 * Does not do anything for generic search engine
122 * subclasses may define this though
123 *
124 * @param string $filteredTerm
125 * @param bool $fulltext
126 * @return string
127 */
128 function queryRanking( $filteredTerm, $fulltext ) {
129 return ' ORDER BY score(1)';
130 }
131
132 /**
133 * Construct the full SQL query to do the search.
134 * The guts shoulds be constructed in queryMain()
135 * @param string $filteredTerm
136 * @param bool $fulltext
137 * @return string
138 */
139 private function getQuery( $filteredTerm, $fulltext ) {
140 return $this->queryLimit( $this->queryMain( $filteredTerm, $fulltext ) . ' ' .
141 $this->queryNamespaces() . ' ' .
142 $this->queryRanking( $filteredTerm, $fulltext ) . ' ' );
143 }
144
145 /**
146 * Picks which field to index on, depending on what type of query.
147 * @param bool $fulltext
148 * @return string
149 */
150 private function getIndexField( $fulltext ) {
151 return $fulltext ? 'si_text' : 'si_title';
152 }
153
154 /**
155 * Get the base part of the search query.
156 *
157 * @param string $filteredTerm
158 * @param bool $fulltext
159 * @return string
160 */
161 function queryMain( $filteredTerm, $fulltext ) {
162 $match = $this->parseQuery( $filteredTerm, $fulltext );
163 $page = $this->db->tableName( 'page' );
164 $searchindex = $this->db->tableName( 'searchindex' );
165 return 'SELECT page_id, page_namespace, page_title ' .
166 "FROM $page,$searchindex " .
167 'WHERE page_id=si_page AND ' . $match;
168 }
169
170 /**
171 * Parse a user input search string, and return an SQL fragment to be used
172 * as part of a WHERE clause
173 * @param string $filteredText
174 * @param bool $fulltext
175 * @return string
176 */
177 private function parseQuery( $filteredText, $fulltext ) {
178 $lc = $this->legalSearchChars( self::CHARS_NO_SYNTAX );
179 $this->searchTerms = [];
180
181 # @todo FIXME: This doesn't handle parenthetical expressions.
182 $m = [];
183 $searchon = '';
184 if ( preg_match_all( '/([-+<>~]?)(([' . $lc . ']+)(\*?)|"[^"]*")/',
185 $filteredText, $m, PREG_SET_ORDER ) ) {
186 foreach ( $m as $terms ) {
187 // Search terms in all variant forms, only
188 // apply on wiki with LanguageConverter
189 $temp_terms = MediaWikiServices::getInstance()->getContentLanguage()->
190 autoConvertToAllVariants( $terms[2] );
191 if ( is_array( $temp_terms ) ) {
192 $temp_terms = array_unique( array_values( $temp_terms ) );
193 foreach ( $temp_terms as $t ) {
194 $searchon .= ( $terms[1] == '-' ? ' ~' : ' & ' ) . $this->escapeTerm( $t );
195 }
196 } else {
197 $searchon .= ( $terms[1] == '-' ? ' ~' : ' & ' ) . $this->escapeTerm( $terms[2] );
198 }
199 if ( !empty( $terms[3] ) ) {
200 $regexp = preg_quote( $terms[3], '/' );
201 if ( $terms[4] ) {
202 $regexp .= "[0-9A-Za-z_]+";
203 }
204 } else {
205 $regexp = preg_quote( str_replace( '"', '', $terms[2] ), '/' );
206 }
207 $this->searchTerms[] = $regexp;
208 }
209 }
210
211 $searchon = $this->db->addQuotes( ltrim( $searchon, ' &' ) );
212 $field = $this->getIndexField( $fulltext );
213 return " CONTAINS($field, $searchon, 1) > 0 ";
214 }
215
216 private function escapeTerm( $t ) {
217 $t = MediaWikiServices::getInstance()->getContentLanguage()->normalizeForSearch( $t );
218 $t = isset( $this->reservedWords[strtoupper( $t )] ) ? '{' . $t . '}' : $t;
219 $t = preg_replace( '/^"(.*)"$/', '($1)', $t );
220 $t = preg_replace( '/([-&|])/', '\\\\$1', $t );
221 return $t;
222 }
223
224 /**
225 * Create or update the search index record for the given page.
226 * Title and text should be pre-processed.
227 *
228 * @param int $id
229 * @param string $title
230 * @param string $text
231 */
232 function update( $id, $title, $text ) {
233 $dbw = wfGetDB( DB_MASTER );
234 $dbw->replace( 'searchindex',
235 [ 'si_page' ],
236 [
237 'si_page' => $id,
238 'si_title' => $title,
239 'si_text' => $text
240 ], 'SearchOracle::update' );
241
242 // Sync the index
243 // We need to specify the DB name (i.e. user/schema) here so that
244 // it can work from the installer, where
245 // ALTER SESSION SET CURRENT_SCHEMA = ...
246 // was used.
247 $dbw->query( "CALL ctx_ddl.sync_index(" .
248 $dbw->addQuotes( $dbw->getDBname() . '.' . $dbw->tableName( 'si_text_idx', 'raw' ) ) . ")" );
249 $dbw->query( "CALL ctx_ddl.sync_index(" .
250 $dbw->addQuotes( $dbw->getDBname() . '.' . $dbw->tableName( 'si_title_idx', 'raw' ) ) . ")" );
251 }
252
253 /**
254 * Update a search index record's title only.
255 * Title should be pre-processed.
256 *
257 * @param int $id
258 * @param string $title
259 */
260 function updateTitle( $id, $title ) {
261 $dbw = wfGetDB( DB_MASTER );
262
263 $dbw->update( 'searchindex',
264 [ 'si_title' => $title ],
265 [ 'si_page' => $id ],
266 'SearchOracle::updateTitle',
267 [] );
268 }
269
270 public static function legalSearchChars( $type = self::CHARS_ALL ) {
271 $searchChars = parent::legalSearchChars( $type );
272 if ( $type === self::CHARS_ALL ) {
273 $searchChars = "\"" . $searchChars;
274 }
275 return $searchChars;
276 }
277 }