(bug 14869) Allow access to QueryPage-based special pages via API
[lhc/web/wiklou.git] / includes / 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 $resultSet = $this->db->resultObject( $this->db->query( $this->getQuery( $this->filter( $term ), true ) ) );
152 return new MySQLSearchResultSet( $resultSet, $this->searchTerms );
153 }
154
155 /**
156 * Perform a title-only search query and return a result set.
157 *
158 * @param $term String: raw search term
159 * @return MySQLSearchResultSet
160 */
161 function searchTitle( $term ) {
162 $resultSet = $this->db->resultObject( $this->db->query( $this->getQuery( $this->filter( $term ), false ) ) );
163 return new MySQLSearchResultSet( $resultSet, $this->searchTerms );
164 }
165
166
167 /**
168 * Return a partial WHERE clause to exclude redirects, if so set
169 * @return String
170 */
171 function queryRedirect() {
172 if( $this->showRedirects ) {
173 return '';
174 } else {
175 return 'AND page_is_redirect=0';
176 }
177 }
178
179 /**
180 * Return a partial WHERE clause to limit the search to the given namespaces
181 * @return String
182 */
183 function queryNamespaces() {
184 if( is_null($this->namespaces) )
185 return ''; # search all
186 if ( !count( $this->namespaces ) ) {
187 $namespaces = '0';
188 } else {
189 $namespaces = $this->db->makeList( $this->namespaces );
190 }
191 return 'AND page_namespace IN (' . $namespaces . ')';
192 }
193
194 /**
195 * Return a LIMIT clause to limit results on the query.
196 * @return String
197 */
198 function queryLimit() {
199 return $this->db->limitResult( '', $this->limit, $this->offset );
200 }
201
202 /**
203 * Does not do anything for generic search engine
204 * subclasses may define this though
205 * @return String
206 */
207 function queryRanking( $filteredTerm, $fulltext ) {
208 return '';
209 }
210
211 /**
212 * Construct the full SQL query to do the search.
213 * The guts shoulds be constructed in queryMain()
214 * @param $filteredTerm String
215 * @param $fulltext Boolean
216 */
217 function getQuery( $filteredTerm, $fulltext ) {
218 return $this->queryMain( $filteredTerm, $fulltext ) . ' ' .
219 $this->queryRedirect() . ' ' .
220 $this->queryNamespaces() . ' ' .
221 $this->queryRanking( $filteredTerm, $fulltext ) . ' ' .
222 $this->queryLimit();
223 }
224
225
226 /**
227 * Picks which field to index on, depending on what type of query.
228 * @param $fulltext Boolean
229 * @return String
230 */
231 function getIndexField( $fulltext ) {
232 return $fulltext ? 'si_text' : 'si_title';
233 }
234
235 /**
236 * Get the base part of the search query.
237 * The actual match syntax will depend on the server
238 * version; MySQL 3 and MySQL 4 have different capabilities
239 * in their fulltext search indexes.
240 *
241 * @param $filteredTerm String
242 * @param $fulltext Boolean
243 * @return String
244 */
245 function queryMain( $filteredTerm, $fulltext ) {
246 $match = $this->parseQuery( $filteredTerm, $fulltext );
247 $page = $this->db->tableName( 'page' );
248 $searchindex = $this->db->tableName( 'searchindex' );
249 return 'SELECT page_id, page_namespace, page_title ' .
250 "FROM $page,$searchindex " .
251 'WHERE page_id=si_page AND ' . $match;
252 }
253
254 /**
255 * Create or update the search index record for the given page.
256 * Title and text should be pre-processed.
257 *
258 * @param $id Integer
259 * @param $title String
260 * @param $text String
261 */
262 function update( $id, $title, $text ) {
263 $dbw = wfGetDB( DB_MASTER );
264 $dbw->replace( 'searchindex',
265 array( 'si_page' ),
266 array(
267 'si_page' => $id,
268 'si_title' => $title,
269 'si_text' => $text
270 ), __METHOD__ );
271 }
272
273 /**
274 * Update a search index record's title only.
275 * Title should be pre-processed.
276 *
277 * @param $id Integer
278 * @param $title String
279 */
280 function updateTitle( $id, $title ) {
281 $dbw = wfGetDB( DB_MASTER );
282
283 $dbw->update( 'searchindex',
284 array( 'si_title' => $title ),
285 array( 'si_page' => $id ),
286 __METHOD__,
287 array( $dbw->lowPriorityOption() ) );
288 }
289 }
290
291 /**
292 * @ingroup Search
293 */
294 class MySQLSearchResultSet extends SearchResultSet {
295 function MySQLSearchResultSet( $resultSet, $terms ) {
296 $this->mResultSet = $resultSet;
297 $this->mTerms = $terms;
298 }
299
300 function termMatches() {
301 return $this->mTerms;
302 }
303
304 function numRows() {
305 return $this->mResultSet->numRows();
306 }
307
308 function next() {
309 $row = $this->mResultSet->fetchObject();
310 if( $row === false ) {
311 return false;
312 } else {
313 return new SearchResult( $row );
314 }
315 }
316
317 function free() {
318 $this->mResultSet->free();
319 }
320 }