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