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