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