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
21 * Search engine hook for MySQL 4+
24 class SearchMySQL
extends SearchEngine
{
25 var $strictMatching = true;
28 function __construct( $db ) {
33 function parseQuery( $filteredText, $fulltext ) {
35 $lc = SearchEngine
::legalSearchChars(); // Minus format chars
37 $this->searchTerms
= array();
39 # FIXME: This doesn't handle parenthetical expressions.
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] == '') ) {
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_]+";
54 // Match the quoted term in result highlighting...
55 $regexp = preg_quote( str_replace( '"', '', $terms[2] ), '/' );
57 $this->searchTerms
[] = "\b$regexp\b";
59 wfDebug( "Would search with '$searchon'\n" );
60 wfDebug( 'Match with /\b' . implode( '\b|\b', $this->searchTerms
) . "\b/\n" );
62 wfDebug( "Can't understand search query '{$filteredText}'\n" );
65 $searchon = $this->db
->strencode( $searchon );
66 $field = $this->getIndexField( $fulltext );
67 return " MATCH($field) AGAINST('$searchon' IN BOOLEAN MODE) ";
70 public static function legalSearchChars() {
71 return "\"*" . parent
::legalSearchChars();
75 * Perform a full text search query and return a result set.
77 * @param string $term - Raw search term
78 * @return MySQLSearchResultSet
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
);
87 * Perform a title-only search query and return a result set.
89 * @param string $term - Raw search term
90 * @return MySQLSearchResultSet
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
);
100 * Return a partial WHERE clause to exclude redirects, if so set
104 function queryRedirect() {
105 if( $this->showRedirects
) {
108 return 'AND page_is_redirect=0';
113 * Return a partial WHERE clause to limit the search to the given namespaces
117 function queryNamespaces() {
118 $namespaces = implode( ',', $this->namespaces
);
119 if ($namespaces == '') {
122 return 'AND page_namespace IN (' . $namespaces . ')';
126 * Return a LIMIT clause to limit results on the query.
130 function queryLimit() {
131 return $this->db
->limitResult( '', $this->limit
, $this->offset
);
135 * Does not do anything for generic search engine
136 * subclasses may define this though
140 function queryRanking( $filteredTerm, $fulltext ) {
145 * Construct the full SQL query to do the search.
146 * The guts shoulds be constructed in queryMain()
147 * @param string $filteredTerm
148 * @param bool $fulltext
151 function getQuery( $filteredTerm, $fulltext ) {
152 return $this->queryMain( $filteredTerm, $fulltext ) . ' ' .
153 $this->queryRedirect() . ' ' .
154 $this->queryNamespaces() . ' ' .
155 $this->queryRanking( $filteredTerm, $fulltext ) . ' ' .
161 * Picks which field to index on, depending on what type of query.
162 * @param bool $fulltext
165 function getIndexField( $fulltext ) {
166 return $fulltext ?
'si_text' : 'si_title';
170 * Get the base part of the search query.
171 * The actual match syntax will depend on the server
172 * version; MySQL 3 and MySQL 4 have different capabilities
173 * in their fulltext search indexes.
175 * @param string $filteredTerm
176 * @param bool $fulltext
180 function queryMain( $filteredTerm, $fulltext ) {
181 $match = $this->parseQuery( $filteredTerm, $fulltext );
182 $page = $this->db
->tableName( 'page' );
183 $searchindex = $this->db
->tableName( 'searchindex' );
184 return 'SELECT page_id, page_namespace, page_title ' .
185 "FROM $page,$searchindex " .
186 'WHERE page_id=si_page AND ' . $match;
190 * Create or update the search index record for the given page.
191 * Title and text should be pre-processed.
194 * @param string $title
195 * @param string $text
197 function update( $id, $title, $text ) {
198 $dbw = wfGetDB( DB_MASTER
);
199 $dbw->replace( 'searchindex',
203 'si_title' => $title,
209 * Update a search index record's title only.
210 * Title should be pre-processed.
213 * @param string $title
215 function updateTitle( $id, $title ) {
216 $dbw = wfGetDB( DB_MASTER
);
218 $dbw->update( 'searchindex',
219 array( 'si_title' => $title ),
220 array( 'si_page' => $id ),
222 array( $dbw->lowPriorityOption() ) );
229 class MySQLSearchResultSet
extends SearchResultSet
{
230 function MySQLSearchResultSet( $resultSet, $terms ) {
231 $this->mResultSet
= $resultSet;
232 $this->mTerms
= $terms;
235 function termMatches() {
236 return $this->mTerms
;
240 return $this->mResultSet
->numRows();
244 $row = $this->mResultSet
->fetchObject();
245 if( $row === false ) {
248 return new SearchResult( $row );
253 $this->mResultSet
->free();