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 function parseQuery( $filteredText, $fulltext ) {
40 $lc = SearchEngine
::legalSearchChars(); // Minus format chars
42 $this->searchTerms
= array();
44 # FIXME: This doesn't handle parenthetical expressions.
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] == '') ) {
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_]+";
59 // Match the quoted term in result highlighting...
60 $regexp = preg_quote( str_replace( '"', '', $terms[2] ), '/' );
62 $this->searchTerms
[] = $regexp;
64 wfDebug( "Would search with '$searchon'\n" );
65 wfDebug( 'Match with /' . implode( '|', $this->searchTerms
) . "/\n" );
67 wfDebug( "Can't understand search query '{$filteredText}'\n" );
70 $searchon = $this->db
->strencode( $searchon );
71 $field = $this->getIndexField( $fulltext );
72 return " MATCH($field) AGAINST('$searchon' IN BOOLEAN MODE) ";
75 public static function legalSearchChars() {
76 return "\"*" . parent
::legalSearchChars();
80 * Perform a full text search query and return a result set.
82 * @param string $term - Raw search term
83 * @return MySQLSearchResultSet
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
);
92 * Perform a title-only search query and return a result set.
94 * @param string $term - Raw search term
95 * @return MySQLSearchResultSet
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
);
105 * Return a partial WHERE clause to exclude redirects, if so set
109 function queryRedirect() {
110 if( $this->showRedirects
) {
113 return 'AND page_is_redirect=0';
118 * Return a partial WHERE clause to limit the search to the given namespaces
122 function queryNamespaces() {
123 if( is_null($this->namespaces
) )
124 return ''; # search all
125 $namespaces = implode( ',', $this->namespaces
);
126 if ($namespaces == '') {
129 return 'AND page_namespace IN (' . $namespaces . ')';
133 * Return a LIMIT clause to limit results on the query.
137 function queryLimit() {
138 return $this->db
->limitResult( '', $this->limit
, $this->offset
);
142 * Does not do anything for generic search engine
143 * subclasses may define this though
147 function queryRanking( $filteredTerm, $fulltext ) {
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
158 function getQuery( $filteredTerm, $fulltext ) {
159 return $this->queryMain( $filteredTerm, $fulltext ) . ' ' .
160 $this->queryRedirect() . ' ' .
161 $this->queryNamespaces() . ' ' .
162 $this->queryRanking( $filteredTerm, $fulltext ) . ' ' .
168 * Picks which field to index on, depending on what type of query.
169 * @param bool $fulltext
172 function getIndexField( $fulltext ) {
173 return $fulltext ?
'si_text' : 'si_title';
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.
182 * @param string $filteredTerm
183 * @param bool $fulltext
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;
197 * Create or update the search index record for the given page.
198 * Title and text should be pre-processed.
201 * @param string $title
202 * @param string $text
204 function update( $id, $title, $text ) {
205 $dbw = wfGetDB( DB_MASTER
);
206 $dbw->replace( 'searchindex',
210 'si_title' => $title,
216 * Update a search index record's title only.
217 * Title should be pre-processed.
220 * @param string $title
222 function updateTitle( $id, $title ) {
223 $dbw = wfGetDB( DB_MASTER
);
225 $dbw->update( 'searchindex',
226 array( 'si_title' => $title ),
227 array( 'si_page' => $id ),
229 array( $dbw->lowPriorityOption() ) );
236 class MySQLSearchResultSet
extends SearchResultSet
{
237 function MySQLSearchResultSet( $resultSet, $terms ) {
238 $this->mResultSet
= $resultSet;
239 $this->mTerms
= $terms;
242 function termMatches() {
243 return $this->mTerms
;
247 return $this->mResultSet
->numRows();
251 $row = $this->mResultSet
->fetchObject();
252 if( $row === false ) {
255 return new SearchResult( $row );
260 $this->mResultSet
->free();