Fix log type and name in revdeleted messages: suppressions are stored in the suppress...
[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 $terms ) {
52 if( $searchon !== '' ) $searchon .= ' ';
53 if( $this->strictMatching && ($terms[1] == '') ) {
54 $terms[1] = '+';
55 }
56 // Search terms in all variant forms, only
57 // apply on wiki with LanguageConverter
58 $temp_terms = $wgContLang->autoConvertToAllVariants( $terms[2] );
59 if( is_array( $temp_terms )) {
60 $temp_terms = array_unique( array_values( $temp_terms ));
61 foreach( $temp_terms as $t )
62 $searchon .= $terms[1] . $wgContLang->stripForSearch( $t ) . ' ';
63 }
64 else
65 $searchon .= $terms[1] . $wgContLang->stripForSearch( $terms[2] );
66 if( !empty( $terms[3] ) ) {
67 // Match individual terms in result highlighting...
68 $regexp = preg_quote( $terms[3], '/' );
69 if( $terms[4] ) {
70 $regexp = "\b$regexp"; // foo*
71 } else {
72 $regexp = "\b$regexp\b";
73 }
74 } else {
75 // Match the quoted term in result highlighting...
76 $regexp = preg_quote( str_replace( '"', '', $terms[2] ), '/' );
77 }
78 $this->searchTerms[] = $regexp;
79 }
80 wfDebug( "Would search with '$searchon'\n" );
81 wfDebug( 'Match with /' . implode( '|', $this->searchTerms ) . "/\n" );
82 } else {
83 wfDebug( "Can't understand search query '{$filteredText}'\n" );
84 }
85
86 $searchon = $this->db->strencode( $searchon );
87 $field = $this->getIndexField( $fulltext );
88 return " MATCH($field) AGAINST('$searchon' IN BOOLEAN MODE) ";
89 }
90
91 public static function legalSearchChars() {
92 return "\"*" . parent::legalSearchChars();
93 }
94
95 /**
96 * Perform a full text search query and return a result set.
97 *
98 * @param $term String: raw search term
99 * @return MySQLSearchResultSet
100 */
101 function searchText( $term ) {
102 $resultSet = $this->db->resultObject( $this->db->query( $this->getQuery( $this->filter( $term ), true ) ) );
103 return new MySQLSearchResultSet( $resultSet, $this->searchTerms );
104 }
105
106 /**
107 * Perform a title-only search query and return a result set.
108 *
109 * @param $term String: raw search term
110 * @return MySQLSearchResultSet
111 */
112 function searchTitle( $term ) {
113 $resultSet = $this->db->resultObject( $this->db->query( $this->getQuery( $this->filter( $term ), false ) ) );
114 return new MySQLSearchResultSet( $resultSet, $this->searchTerms );
115 }
116
117
118 /**
119 * Return a partial WHERE clause to exclude redirects, if so set
120 * @return String
121 */
122 function queryRedirect() {
123 if( $this->showRedirects ) {
124 return '';
125 } else {
126 return 'AND page_is_redirect=0';
127 }
128 }
129
130 /**
131 * Return a partial WHERE clause to limit the search to the given namespaces
132 * @return String
133 */
134 function queryNamespaces() {
135 if( is_null($this->namespaces) )
136 return ''; # search all
137 if ( !count( $this->namespaces ) ) {
138 $namespaces = '0';
139 } else {
140 $namespaces = $this->db->makeList( $this->namespaces );
141 }
142 return 'AND page_namespace IN (' . $namespaces . ')';
143 }
144
145 /**
146 * Return a LIMIT clause to limit results on the query.
147 * @return String
148 */
149 function queryLimit() {
150 return $this->db->limitResult( '', $this->limit, $this->offset );
151 }
152
153 /**
154 * Does not do anything for generic search engine
155 * subclasses may define this though
156 * @return String
157 */
158 function queryRanking( $filteredTerm, $fulltext ) {
159 return '';
160 }
161
162 /**
163 * Construct the full SQL query to do the search.
164 * The guts shoulds be constructed in queryMain()
165 * @param $filteredTerm String
166 * @param $fulltext Boolean
167 */
168 function getQuery( $filteredTerm, $fulltext ) {
169 return $this->queryMain( $filteredTerm, $fulltext ) . ' ' .
170 $this->queryRedirect() . ' ' .
171 $this->queryNamespaces() . ' ' .
172 $this->queryRanking( $filteredTerm, $fulltext ) . ' ' .
173 $this->queryLimit();
174 }
175
176
177 /**
178 * Picks which field to index on, depending on what type of query.
179 * @param $fulltext Boolean
180 * @return String
181 */
182 function getIndexField( $fulltext ) {
183 return $fulltext ? 'si_text' : 'si_title';
184 }
185
186 /**
187 * Get the base part of the search query.
188 * The actual match syntax will depend on the server
189 * version; MySQL 3 and MySQL 4 have different capabilities
190 * in their fulltext search indexes.
191 *
192 * @param $filteredTerm String
193 * @param $fulltext Boolean
194 * @return String
195 */
196 function queryMain( $filteredTerm, $fulltext ) {
197 $match = $this->parseQuery( $filteredTerm, $fulltext );
198 $page = $this->db->tableName( 'page' );
199 $searchindex = $this->db->tableName( 'searchindex' );
200 return 'SELECT page_id, page_namespace, page_title ' .
201 "FROM $page,$searchindex " .
202 'WHERE page_id=si_page AND ' . $match;
203 }
204
205 /**
206 * Create or update the search index record for the given page.
207 * Title and text should be pre-processed.
208 *
209 * @param $id Integer
210 * @param $title String
211 * @param $text String
212 */
213 function update( $id, $title, $text ) {
214 $dbw = wfGetDB( DB_MASTER );
215 $dbw->replace( 'searchindex',
216 array( 'si_page' ),
217 array(
218 'si_page' => $id,
219 'si_title' => $title,
220 'si_text' => $text
221 ), __METHOD__ );
222 }
223
224 /**
225 * Update a search index record's title only.
226 * Title should be pre-processed.
227 *
228 * @param $id Integer
229 * @param $title String
230 */
231 function updateTitle( $id, $title ) {
232 $dbw = wfGetDB( DB_MASTER );
233
234 $dbw->update( 'searchindex',
235 array( 'si_title' => $title ),
236 array( 'si_page' => $id ),
237 __METHOD__,
238 array( $dbw->lowPriorityOption() ) );
239 }
240 }
241
242 /**
243 * @ingroup Search
244 */
245 class MySQLSearchResultSet extends SearchResultSet {
246 function MySQLSearchResultSet( $resultSet, $terms ) {
247 $this->mResultSet = $resultSet;
248 $this->mTerms = $terms;
249 }
250
251 function termMatches() {
252 return $this->mTerms;
253 }
254
255 function numRows() {
256 return $this->mResultSet->numRows();
257 }
258
259 function next() {
260 $row = $this->mResultSet->fetchObject();
261 if( $row === false ) {
262 return false;
263 } else {
264 return new SearchResult( $row );
265 }
266 }
267
268 function free() {
269 $this->mResultSet->free();
270 }
271 }