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