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