2 # Copyright (C) 2004 Brion Vibber <brion@pobox.com>
3 # http://www.mediawiki.org/
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.
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.
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
26 * Search engine hook for MySQL 4+
29 class SearchMySQL
extends SearchEngine
{
30 var $strictMatching = true;
33 function __construct( $db ) {
38 * Parse the user's query and transform it into an SQL fragment which will
39 * become part of a WHERE clause
41 function parseQuery( $filteredText, $fulltext ) {
43 $lc = SearchEngine
::legalSearchChars(); // Minus format chars
45 $this->searchTerms
= array();
47 # FIXME: This doesn't handle parenthetical expressions.
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] == '') ) {
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 ) . ' ';
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], '/' );
70 $regexp = "\b$regexp"; // foo*
72 $regexp = "\b$regexp\b";
75 // Match the quoted term in result highlighting...
76 $regexp = preg_quote( str_replace( '"', '', $terms[2] ), '/' );
78 $this->searchTerms
[] = $regexp;
80 wfDebug( "Would search with '$searchon'\n" );
81 wfDebug( 'Match with /' . implode( '|', $this->searchTerms
) . "/\n" );
83 wfDebug( "Can't understand search query '{$filteredText}'\n" );
86 $searchon = $this->db
->strencode( $searchon );
87 $field = $this->getIndexField( $fulltext );
88 return " MATCH($field) AGAINST('$searchon' IN BOOLEAN MODE) ";
91 public static function legalSearchChars() {
92 return "\"*" . parent
::legalSearchChars();
96 * Perform a full text search query and return a result set.
98 * @param $term String: raw search term
99 * @return MySQLSearchResultSet
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
);
107 * Perform a title-only search query and return a result set.
109 * @param $term String: raw search term
110 * @return MySQLSearchResultSet
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
);
119 * Return a partial WHERE clause to exclude redirects, if so set
122 function queryRedirect() {
123 if( $this->showRedirects
) {
126 return 'AND page_is_redirect=0';
131 * Return a partial WHERE clause to limit the search to the given namespaces
134 function queryNamespaces() {
135 if( is_null($this->namespaces
) )
136 return ''; # search all
137 if ( !count( $this->namespaces
) ) {
140 $namespaces = $this->db
->makeList( $this->namespaces
);
142 return 'AND page_namespace IN (' . $namespaces . ')';
146 * Return a LIMIT clause to limit results on the query.
149 function queryLimit() {
150 return $this->db
->limitResult( '', $this->limit
, $this->offset
);
154 * Does not do anything for generic search engine
155 * subclasses may define this though
158 function queryRanking( $filteredTerm, $fulltext ) {
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
168 function getQuery( $filteredTerm, $fulltext ) {
169 return $this->queryMain( $filteredTerm, $fulltext ) . ' ' .
170 $this->queryRedirect() . ' ' .
171 $this->queryNamespaces() . ' ' .
172 $this->queryRanking( $filteredTerm, $fulltext ) . ' ' .
178 * Picks which field to index on, depending on what type of query.
179 * @param $fulltext Boolean
182 function getIndexField( $fulltext ) {
183 return $fulltext ?
'si_text' : 'si_title';
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.
192 * @param $filteredTerm String
193 * @param $fulltext Boolean
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;
206 * Create or update the search index record for the given page.
207 * Title and text should be pre-processed.
210 * @param $title String
211 * @param $text String
213 function update( $id, $title, $text ) {
214 $dbw = wfGetDB( DB_MASTER
);
215 $dbw->replace( 'searchindex',
219 'si_title' => $title,
225 * Update a search index record's title only.
226 * Title should be pre-processed.
229 * @param $title String
231 function updateTitle( $id, $title ) {
232 $dbw = wfGetDB( DB_MASTER
);
234 $dbw->update( 'searchindex',
235 array( 'si_title' => $title ),
236 array( 'si_page' => $id ),
238 array( $dbw->lowPriorityOption() ) );
245 class MySQLSearchResultSet
extends SearchResultSet
{
246 function MySQLSearchResultSet( $resultSet, $terms ) {
247 $this->mResultSet
= $resultSet;
248 $this->mTerms
= $terms;
251 function termMatches() {
252 return $this->mTerms
;
256 return $this->mResultSet
->numRows();
260 $row = $this->mResultSet
->fetchObject();
261 if( $row === false ) {
264 return new SearchResult( $row );
269 $this->mResultSet
->free();