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 $searchon .= $terms[1] . $wgContLang->stripForSearch( $terms[2] );
57 if( !empty( $terms[3] ) ) {
58 // Match individual terms in result highlighting...
59 $regexp = preg_quote( $terms[3], '/' );
61 $regexp = "\b$regexp"; // foo*
63 $regexp = "\b$regexp\b";
66 // Match the quoted term in result highlighting...
67 $regexp = preg_quote( str_replace( '"', '', $terms[2] ), '/' );
69 $this->searchTerms
[] = $regexp;
71 wfDebug( "Would search with '$searchon'\n" );
72 wfDebug( 'Match with /' . implode( '|', $this->searchTerms
) . "/\n" );
74 wfDebug( "Can't understand search query '{$filteredText}'\n" );
77 $searchon = $this->db
->strencode( $searchon );
78 $field = $this->getIndexField( $fulltext );
79 return " MATCH($field) AGAINST('$searchon' IN BOOLEAN MODE) ";
82 public static function legalSearchChars() {
83 return "\"*" . parent
::legalSearchChars();
87 * Perform a full text search query and return a result set.
89 * @param string $term - Raw search term
90 * @return MySQLSearchResultSet
93 function searchText( $term ) {
94 $resultSet = $this->db
->resultObject( $this->db
->query( $this->getQuery( $this->filter( $term ), true ) ) );
95 return new MySQLSearchResultSet( $resultSet, $this->searchTerms
);
99 * Perform a title-only search query and return a result set.
101 * @param string $term - Raw search term
102 * @return MySQLSearchResultSet
105 function searchTitle( $term ) {
106 $resultSet = $this->db
->resultObject( $this->db
->query( $this->getQuery( $this->filter( $term ), false ) ) );
107 return new MySQLSearchResultSet( $resultSet, $this->searchTerms
);
112 * Return a partial WHERE clause to exclude redirects, if so set
116 function queryRedirect() {
117 if( $this->showRedirects
) {
120 return 'AND page_is_redirect=0';
125 * Return a partial WHERE clause to limit the search to the given namespaces
129 function queryNamespaces() {
130 if( is_null($this->namespaces
) )
131 return ''; # search all
132 if ( !count( $this->namespaces
) ) {
135 $namespaces = $this->db
->makeList( $this->namespaces
);
137 return 'AND page_namespace IN (' . $namespaces . ')';
141 * Return a LIMIT clause to limit results on the query.
145 function queryLimit() {
146 return $this->db
->limitResult( '', $this->limit
, $this->offset
);
150 * Does not do anything for generic search engine
151 * subclasses may define this though
155 function queryRanking( $filteredTerm, $fulltext ) {
160 * Construct the full SQL query to do the search.
161 * The guts shoulds be constructed in queryMain()
162 * @param string $filteredTerm
163 * @param bool $fulltext
166 function getQuery( $filteredTerm, $fulltext ) {
167 return $this->queryMain( $filteredTerm, $fulltext ) . ' ' .
168 $this->queryRedirect() . ' ' .
169 $this->queryNamespaces() . ' ' .
170 $this->queryRanking( $filteredTerm, $fulltext ) . ' ' .
176 * Picks which field to index on, depending on what type of query.
177 * @param bool $fulltext
180 function getIndexField( $fulltext ) {
181 return $fulltext ?
'si_text' : 'si_title';
185 * Get the base part of the search query.
186 * The actual match syntax will depend on the server
187 * version; MySQL 3 and MySQL 4 have different capabilities
188 * in their fulltext search indexes.
190 * @param string $filteredTerm
191 * @param bool $fulltext
195 function queryMain( $filteredTerm, $fulltext ) {
196 $match = $this->parseQuery( $filteredTerm, $fulltext );
197 $page = $this->db
->tableName( 'page' );
198 $searchindex = $this->db
->tableName( 'searchindex' );
199 return 'SELECT page_id, page_namespace, page_title ' .
200 "FROM $page,$searchindex " .
201 'WHERE page_id=si_page AND ' . $match;
205 * Create or update the search index record for the given page.
206 * Title and text should be pre-processed.
209 * @param string $title
210 * @param string $text
212 function update( $id, $title, $text ) {
213 $dbw = wfGetDB( DB_MASTER
);
214 $dbw->replace( 'searchindex',
218 'si_title' => $title,
224 * Update a search index record's title only.
225 * Title should be pre-processed.
228 * @param string $title
230 function updateTitle( $id, $title ) {
231 $dbw = wfGetDB( DB_MASTER
);
233 $dbw->update( 'searchindex',
234 array( 'si_title' => $title ),
235 array( 'si_page' => $id ),
237 array( $dbw->lowPriorityOption() ) );
244 class MySQLSearchResultSet
extends SearchResultSet
{
245 function MySQLSearchResultSet( $resultSet, $terms ) {
246 $this->mResultSet
= $resultSet;
247 $this->mTerms
= $terms;
250 function termMatches() {
251 return $this->mTerms
;
255 return $this->mResultSet
->numRows();
259 $row = $this->mResultSet
->fetchObject();
260 if( $row === false ) {
263 return new SearchResult( $row );
268 $this->mResultSet
->free();