Update set of files cleaned up after parserTests
[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 /**
28 * Search engine hook base class for Oracle (ConText).
29 * @ingroup Search
30 */
31 class SearchOracle extends SearchDatabase {
32
33 private $reservedWords = array(
34 'ABOUT' => 1,
35 'ACCUM' => 1,
36 'AND' => 1,
37 'BT' => 1,
38 'BTG' => 1,
39 'BTI' => 1,
40 'BTP' => 1,
41 'FUZZY' => 1,
42 'HASPATH' => 1,
43 'INPATH' => 1,
44 'MINUS' => 1,
45 'NEAR' => 1,
46 'NOT' => 1,
47 'NT' => 1,
48 'NTG' => 1,
49 'NTI' => 1,
50 'NTP' => 1,
51 'OR' => 1,
52 'PT' => 1,
53 'RT' => 1,
54 'SQE' => 1,
55 'SYN' => 1,
56 'TR' => 1,
57 'TRSYN' => 1,
58 'TT' => 1,
59 'WITHIN' => 1,
60 );
61
62 /**
63 * Perform a full text search query and return a result set.
64 *
65 * @param string $term raw search term
66 * @return SqlSearchResultSet
67 */
68 function searchText( $term ) {
69 if ( $term == '' ) {
70 return new SqlSearchResultSet( false, '' );
71 }
72
73 $resultSet = $this->db->resultObject( $this->db->query( $this->getQuery( $this->filter( $term ), true ) ) );
74 return new SqlSearchResultSet( $resultSet, $this->searchTerms );
75 }
76
77 /**
78 * Perform a title-only search query and return a result set.
79 *
80 * @param string $term raw search term
81 * @return SqlSearchResultSet
82 */
83 function searchTitle( $term ) {
84 if ( $term == '' ) {
85 return new SqlSearchResultSet( false, '' );
86 }
87
88 $resultSet = $this->db->resultObject( $this->db->query( $this->getQuery( $this->filter( $term ), false ) ) );
89 return new MySQLSearchResultSet( $resultSet, $this->searchTerms );
90 }
91
92 /**
93 * Return a partial WHERE clause to exclude redirects, if so set
94 * @return String
95 */
96 function queryRedirect() {
97 if ( $this->showRedirects ) {
98 return '';
99 } else {
100 return 'AND page_is_redirect=0';
101 }
102 }
103
104 /**
105 * Return a partial WHERE clause to limit the search to the given namespaces
106 * @return String
107 */
108 function queryNamespaces() {
109 if ( is_null( $this->namespaces ) ) {
110 return '';
111 }
112 if ( !count( $this->namespaces ) ) {
113 $namespaces = '0';
114 } else {
115 $namespaces = $this->db->makeList( $this->namespaces );
116 }
117 return 'AND page_namespace IN (' . $namespaces . ')';
118 }
119
120 /**
121 * Return a LIMIT clause to limit results on the query.
122 *
123 * @param $sql string
124 *
125 * @return String
126 */
127 function queryLimit( $sql ) {
128 return $this->db->limitResult( $sql, $this->limit, $this->offset );
129 }
130
131 /**
132 * Does not do anything for generic search engine
133 * subclasses may define this though
134 *
135 * @return String
136 */
137 function queryRanking( $filteredTerm, $fulltext ) {
138 return ' ORDER BY score(1)';
139 }
140
141 /**
142 * Construct the full SQL query to do the search.
143 * The guts shoulds be constructed in queryMain()
144 * @param $filteredTerm String
145 * @param $fulltext Boolean
146 * @return String
147 */
148 function getQuery( $filteredTerm, $fulltext ) {
149 return $this->queryLimit( $this->queryMain( $filteredTerm, $fulltext ) . ' ' .
150 $this->queryRedirect() . ' ' .
151 $this->queryNamespaces() . ' ' .
152 $this->queryRanking( $filteredTerm, $fulltext ) . ' ' );
153 }
154
155 /**
156 * Picks which field to index on, depending on what type of query.
157 * @param $fulltext Boolean
158 * @return String
159 */
160 function getIndexField( $fulltext ) {
161 return $fulltext ? 'si_text' : 'si_title';
162 }
163
164 /**
165 * Get the base part of the search query.
166 *
167 * @param $filteredTerm String
168 * @param $fulltext Boolean
169 * @return String
170 */
171 function queryMain( $filteredTerm, $fulltext ) {
172 $match = $this->parseQuery( $filteredTerm, $fulltext );
173 $page = $this->db->tableName( 'page' );
174 $searchindex = $this->db->tableName( 'searchindex' );
175 return 'SELECT page_id, page_namespace, page_title ' .
176 "FROM $page,$searchindex " .
177 'WHERE page_id=si_page AND ' . $match;
178 }
179
180 /**
181 * Parse a user input search string, and return an SQL fragment to be used
182 * as part of a WHERE clause
183 * @return string
184 */
185 function parseQuery( $filteredText, $fulltext ) {
186 global $wgContLang;
187 $lc = SearchEngine::legalSearchChars();
188 $this->searchTerms = array();
189
190 # @todo FIXME: This doesn't handle parenthetical expressions.
191 $m = array();
192 $searchon = '';
193 if ( preg_match_all( '/([-+<>~]?)(([' . $lc . ']+)(\*?)|"[^"]*")/',
194 $filteredText, $m, PREG_SET_ORDER ) ) {
195 foreach ( $m as $terms ) {
196 // Search terms in all variant forms, only
197 // apply on wiki with LanguageConverter
198 $temp_terms = $wgContLang->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 }
205 else {
206 $searchon .= ( $terms[1] == '-' ? ' ~' : ' & ' ) . $this->escapeTerm( $terms[2] );
207 }
208 if ( !empty( $terms[3] ) ) {
209 $regexp = preg_quote( $terms[3], '/' );
210 if ( $terms[4] ) {
211 $regexp .= "[0-9A-Za-z_]+";
212 }
213 } else {
214 $regexp = preg_quote( str_replace( '"', '', $terms[2] ), '/' );
215 }
216 $this->searchTerms[] = $regexp;
217 }
218 }
219
220 $searchon = $this->db->addQuotes( ltrim( $searchon, ' &' ) );
221 $field = $this->getIndexField( $fulltext );
222 return " CONTAINS($field, $searchon, 1) > 0 ";
223 }
224
225 private function escapeTerm( $t ) {
226 global $wgContLang;
227 $t = $wgContLang->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 $id Integer
239 * @param $title String
240 * @param $text String
241 */
242 function update( $id, $title, $text ) {
243 $dbw = wfGetDB( DB_MASTER );
244 $dbw->replace( 'searchindex',
245 array( 'si_page' ),
246 array(
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 $id Integer
268 * @param $title String
269 */
270 function updateTitle( $id, $title ) {
271 $dbw = wfGetDB( DB_MASTER );
272
273 $dbw->update( 'searchindex',
274 array( 'si_title' => $title ),
275 array( 'si_page' => $id ),
276 'SearchOracle::updateTitle',
277 array() );
278 }
279
280 public static function legalSearchChars() {
281 return "\"" . parent::legalSearchChars();
282 }
283 }