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