Severely refactored the SearchResultSet descendants system, consolidating most od...
[lhc/web/wiklou.git] / includes / search / SearchMySQL.php
1 <?php
2 # Copyright (C) 2004 Brion Vibber <brion@pobox.com>
3 # http://www.mediawiki.org/
4 #
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 2 of the License, or
8 # (at your option) any later version.
9 #
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License along
16 # with this program; if not, write to the Free Software Foundation, Inc.,
17 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 # http://www.gnu.org/copyleft/gpl.html
19
20 /**
21 * @file
22 * @ingroup Search
23 */
24
25 /**
26 * Search engine hook for MySQL 4+
27 * @ingroup Search
28 */
29 class SearchMySQL extends SearchEngine {
30 var $strictMatching = true;
31
32 /** @todo document */
33 function __construct( $db ) {
34 $this->db = $db;
35 }
36
37 /**
38 * Parse the user's query and transform it into an SQL fragment which will
39 * become part of a WHERE clause
40 */
41 function parseQuery( $filteredText, $fulltext ) {
42 global $wgContLang;
43 $lc = SearchEngine::legalSearchChars(); // Minus format chars
44 $searchon = '';
45 $this->searchTerms = array();
46
47 # FIXME: This doesn't handle parenthetical expressions.
48 $m = array();
49 if( preg_match_all( '/([-+<>~]?)(([' . $lc . ']+)(\*?)|"[^"]*")/',
50 $filteredText, $m, PREG_SET_ORDER ) ) {
51 foreach( $m as $bits ) {
52 @list( /* all */, $modifier, $term, $nonQuoted, $wildcard ) = $bits;
53
54 if( $nonQuoted != '' ) {
55 $term = $nonQuoted;
56 $quote = '';
57 } else {
58 $term = str_replace( '"', '', $term );
59 $quote = '"';
60 }
61
62 if( $searchon !== '' ) $searchon .= ' ';
63 if( $this->strictMatching && ($modifier == '') ) {
64 // If we leave this out, boolean op defaults to OR which is rarely helpful.
65 $modifier = '+';
66 }
67
68 // Some languages such as Serbian store the input form in the search index,
69 // so we may need to search for matches in multiple writing system variants.
70 $convertedVariants = $wgContLang->autoConvertToAllVariants( $term );
71 if( is_array( $convertedVariants ) ) {
72 $variants = array_unique( array_values( $convertedVariants ) );
73 } else {
74 $variants = array( $term );
75 }
76
77 // The low-level search index does some processing on input to work
78 // around problems with minimum lengths and encoding in MySQL's
79 // fulltext engine.
80 // For Chinese this also inserts spaces between adjacent Han characters.
81 $strippedVariants = array_map(
82 array( $wgContLang, 'stripForSearch' ),
83 $variants );
84
85 // Some languages such as Chinese force all variants to a canonical
86 // form when stripping to the low-level search index, so to be sure
87 // let's check our variants list for unique items after stripping.
88 $strippedVariants = array_unique( $strippedVariants );
89
90 $searchon .= $modifier;
91 if( count( $strippedVariants) > 1 )
92 $searchon .= '(';
93 foreach( $strippedVariants as $stripped ) {
94 if( $nonQuoted && strpos( $stripped, ' ' ) !== false ) {
95 // Hack for Chinese: we need to toss in quotes for
96 // multiple-character phrases since stripForSearch()
97 // added spaces between them to make word breaks.
98 $stripped = '"' . trim( $stripped ) . '"';
99 }
100 $searchon .= "$quote$stripped$quote$wildcard ";
101 }
102 if( count( $strippedVariants) > 1 )
103 $searchon .= ')';
104
105 // Match individual terms or quoted phrase in result highlighting...
106 // Note that variants will be introduced in a later stage for highlighting!
107 $regexp = $this->regexTerm( $term, $wildcard );
108 $this->searchTerms[] = $regexp;
109 }
110 wfDebug( __METHOD__ . ": Would search with '$searchon'\n" );
111 wfDebug( __METHOD__ . ': Match with /' . implode( '|', $this->searchTerms ) . "/\n" );
112 } else {
113 wfDebug( __METHOD__ . ": Can't understand search query '{$filteredText}'\n" );
114 }
115
116 $searchon = $this->db->strencode( $searchon );
117 $field = $this->getIndexField( $fulltext );
118 return " MATCH($field) AGAINST('$searchon' IN BOOLEAN MODE) ";
119 }
120
121 function regexTerm( $string, $wildcard ) {
122 global $wgContLang;
123
124 $regex = preg_quote( $string, '/' );
125 if( $wgContLang->hasWordBreaks() ) {
126 if( $wildcard ) {
127 // Don't cut off the final bit!
128 $regex = "\b$regex";
129 } else {
130 $regex = "\b$regex\b";
131 }
132 } else {
133 // For Chinese, words may legitimately abut other words in the text literal.
134 // Don't add \b boundary checks... note this could cause false positives
135 // for latin chars.
136 }
137 return $regex;
138 }
139
140 public static function legalSearchChars() {
141 return "\"*" . parent::legalSearchChars();
142 }
143
144 /**
145 * Perform a full text search query and return a result set.
146 *
147 * @param $term String: raw search term
148 * @return MySQLSearchResultSet
149 */
150 function searchText( $term ) {
151 return $this->searchInternal( $term, true );
152 }
153
154 /**
155 * Perform a title-only search query and return a result set.
156 *
157 * @param $term String: raw search term
158 * @return MySQLSearchResultSet
159 */
160 function searchTitle( $term ) {
161 return $this->searchInternal( $term, false );
162 }
163
164 protected function searchInternal( $term, $fulltext ) {
165 global $wgSearchMySQLTotalHits;
166
167 $filteredTerm = $this->filter( $term );
168 $resultSet = $this->db->query( $this->getQuery( $filteredTerm, $fulltext ) );
169
170 $total = null;
171 if( $wgSearchMySQLTotalHits ) {
172 $totalResult = $this->db->query( $this->getCountQuery( $filteredTerm, $fulltext ) );
173 $row = $totalResult->fetchObject();
174 if( $row ) {
175 $total = intval( $row->c );
176 }
177 $totalResult->free();
178 }
179
180 return new MySQLSearchResultSet( $resultSet, $this->searchTerms, $total );
181 }
182
183
184 /**
185 * Return a partial WHERE clause to exclude redirects, if so set
186 * @return String
187 */
188 function queryRedirect() {
189 if( $this->showRedirects ) {
190 return '';
191 } else {
192 return 'AND page_is_redirect=0';
193 }
194 }
195
196 /**
197 * Return a partial WHERE clause to limit the search to the given namespaces
198 * @return String
199 */
200 function queryNamespaces() {
201 if( is_null($this->namespaces) )
202 return ''; # search all
203 if ( !count( $this->namespaces ) ) {
204 $namespaces = '0';
205 } else {
206 $namespaces = $this->db->makeList( $this->namespaces );
207 }
208 return 'AND page_namespace IN (' . $namespaces . ')';
209 }
210
211 /**
212 * Return a LIMIT clause to limit results on the query.
213 * @return String
214 */
215 function queryLimit() {
216 return $this->db->limitResult( '', $this->limit, $this->offset );
217 }
218
219 /**
220 * Does not do anything for generic search engine
221 * subclasses may define this though
222 * @return String
223 */
224 function queryRanking( $filteredTerm, $fulltext ) {
225 return '';
226 }
227
228 /**
229 * Construct the full SQL query to do the search.
230 * The guts shoulds be constructed in queryMain()
231 * @param $filteredTerm String
232 * @param $fulltext Boolean
233 */
234 function getQuery( $filteredTerm, $fulltext ) {
235 return $this->queryMain( $filteredTerm, $fulltext ) . ' ' .
236 $this->queryRedirect() . ' ' .
237 $this->queryNamespaces() . ' ' .
238 $this->queryRanking( $filteredTerm, $fulltext ) . ' ' .
239 $this->queryLimit();
240 }
241
242 /**
243 * Picks which field to index on, depending on what type of query.
244 * @param $fulltext Boolean
245 * @return String
246 */
247 function getIndexField( $fulltext ) {
248 return $fulltext ? 'si_text' : 'si_title';
249 }
250
251 /**
252 * Get the base part of the search query.
253 * The actual match syntax will depend on the server
254 * version; MySQL 3 and MySQL 4 have different capabilities
255 * in their fulltext search indexes.
256 *
257 * @param $filteredTerm String
258 * @param $fulltext Boolean
259 * @return String
260 */
261 function queryMain( $filteredTerm, $fulltext ) {
262 $match = $this->parseQuery( $filteredTerm, $fulltext );
263 $page = $this->db->tableName( 'page' );
264 $searchindex = $this->db->tableName( 'searchindex' );
265 return 'SELECT page_id, page_namespace, page_title ' .
266 "FROM $page,$searchindex " .
267 'WHERE page_id=si_page AND ' . $match;
268 }
269
270 function getCountQuery( $filteredTerm, $fulltext ) {
271 $match = $this->parseQuery( $filteredTerm, $fulltext );
272 $page = $this->db->tableName( 'page' );
273 $searchindex = $this->db->tableName( 'searchindex' );
274 return "SELECT COUNT(*) AS c " .
275 "FROM $page,$searchindex " .
276 'WHERE page_id=si_page AND ' . $match .
277 $this->queryRedirect() . ' ' .
278 $this->queryNamespaces();
279 }
280
281 /**
282 * Create or update the search index record for the given page.
283 * Title and text should be pre-processed.
284 *
285 * @param $id Integer
286 * @param $title String
287 * @param $text String
288 */
289 function update( $id, $title, $text ) {
290 $dbw = wfGetDB( DB_MASTER );
291 $dbw->replace( 'searchindex',
292 array( 'si_page' ),
293 array(
294 'si_page' => $id,
295 'si_title' => $title,
296 'si_text' => $text
297 ), __METHOD__ );
298 }
299
300 /**
301 * Update a search index record's title only.
302 * Title should be pre-processed.
303 *
304 * @param $id Integer
305 * @param $title String
306 */
307 function updateTitle( $id, $title ) {
308 $dbw = wfGetDB( DB_MASTER );
309
310 $dbw->update( 'searchindex',
311 array( 'si_title' => $title ),
312 array( 'si_page' => $id ),
313 __METHOD__,
314 array( $dbw->lowPriorityOption() ) );
315 }
316 }
317
318 /**
319 * @ingroup Search
320 */
321 class MySQLSearchResultSet extends SqlSearchResultSet {
322 function MySQLSearchResultSet( $resultSet, $terms, $totalHits=null ) {
323 parent::__construct( $resultSet, $terms );
324 $this->mTotalHits = $totalHits;
325 }
326
327 function getTotalHits() {
328 return $this->mTotalHits;
329 }
330 }