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