Merge "Added UserCache::getUserName() convenience function."
[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 $resultSet = $this->db->resultObject( $this->db->query( $this->getQuery( $this->filter( $term ), true ) ) );
81 return new SqlSearchResultSet( $resultSet, $this->searchTerms );
82 }
83
84 /**
85 * Perform a title-only search query and return a result set.
86 *
87 * @param string $term raw search term
88 * @return SqlSearchResultSet
89 */
90 function searchTitle( $term ) {
91 if ( $term == '' )
92 return new SqlSearchResultSet( false, '' );
93
94 $resultSet = $this->db->resultObject( $this->db->query( $this->getQuery( $this->filter( $term ), false ) ) );
95 return new MySQLSearchResultSet( $resultSet, $this->searchTerms );
96 }
97
98 /**
99 * Return a partial WHERE clause to exclude redirects, if so set
100 * @return String
101 */
102 function queryRedirect() {
103 if ( $this->showRedirects ) {
104 return '';
105 } else {
106 return 'AND page_is_redirect=0';
107 }
108 }
109
110 /**
111 * Return a partial WHERE clause to limit the search to the given namespaces
112 * @return String
113 */
114 function queryNamespaces() {
115 if( is_null( $this->namespaces ) )
116 return '';
117 if ( !count( $this->namespaces ) ) {
118 $namespaces = '0';
119 } else {
120 $namespaces = $this->db->makeList( $this->namespaces );
121 }
122 return 'AND page_namespace IN (' . $namespaces . ')';
123 }
124
125 /**
126 * Return a LIMIT clause to limit results on the query.
127 *
128 * @param $sql string
129 *
130 * @return String
131 */
132 function queryLimit( $sql ) {
133 return $this->db->limitResult( $sql, $this->limit, $this->offset );
134 }
135
136 /**
137 * Does not do anything for generic search engine
138 * subclasses may define this though
139 *
140 * @return String
141 */
142 function queryRanking( $filteredTerm, $fulltext ) {
143 return ' ORDER BY score(1)';
144 }
145
146 /**
147 * Construct the full SQL query to do the search.
148 * The guts shoulds be constructed in queryMain()
149 * @param $filteredTerm String
150 * @param $fulltext Boolean
151 * @return String
152 */
153 function getQuery( $filteredTerm, $fulltext ) {
154 return $this->queryLimit( $this->queryMain( $filteredTerm, $fulltext ) . ' ' .
155 $this->queryRedirect() . ' ' .
156 $this->queryNamespaces() . ' ' .
157 $this->queryRanking( $filteredTerm, $fulltext ) . ' ' );
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 $searchon = $this->db->addQuotes( ltrim( $searchon, ' &' ) );
225 $field = $this->getIndexField( $fulltext );
226 return " CONTAINS($field, $searchon, 1) > 0 ";
227 }
228
229 private function escapeTerm( $t ) {
230 global $wgContLang;
231 $t = $wgContLang->normalizeForSearch( $t );
232 $t = isset( $this->reservedWords[strtoupper( $t )] ) ? '{' . $t . '}' : $t;
233 $t = preg_replace( '/^"(.*)"$/', '($1)', $t );
234 $t = preg_replace( '/([-&|])/', '\\\\$1', $t );
235 return $t;
236 }
237 /**
238 * Create or update the search index record for the given page.
239 * Title and text should be pre-processed.
240 *
241 * @param $id Integer
242 * @param $title String
243 * @param $text String
244 */
245 function update( $id, $title, $text ) {
246 $dbw = wfGetDB( DB_MASTER );
247 $dbw->replace( 'searchindex',
248 array( 'si_page' ),
249 array(
250 'si_page' => $id,
251 'si_title' => $title,
252 'si_text' => $text
253 ), 'SearchOracle::update' );
254
255 // Sync the index
256 // We need to specify the DB name (i.e. user/schema) here so that
257 // it can work from the installer, where
258 // ALTER SESSION SET CURRENT_SCHEMA = ...
259 // was used.
260 $dbw->query( "CALL ctx_ddl.sync_index(" .
261 $dbw->addQuotes( $dbw->getDBname() . '.' . $dbw->tableName( 'si_text_idx', 'raw' ) ) . ")" );
262 $dbw->query( "CALL ctx_ddl.sync_index(" .
263 $dbw->addQuotes( $dbw->getDBname() . '.' . $dbw->tableName( 'si_title_idx', 'raw' ) ) . ")" );
264 }
265
266 /**
267 * Update a search index record's title only.
268 * Title should be pre-processed.
269 *
270 * @param $id Integer
271 * @param $title String
272 */
273 function updateTitle( $id, $title ) {
274 $dbw = wfGetDB( DB_MASTER );
275
276 $dbw->update( 'searchindex',
277 array( 'si_title' => $title ),
278 array( 'si_page' => $id ),
279 'SearchOracle::updateTitle',
280 array() );
281 }
282
283 public static function legalSearchChars() {
284 return "\"" . parent::legalSearchChars();
285 }
286 }