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], '/' );
58 $regexp = "\b$regexp"; // foo*
60 $regexp = "\b$regexp\b";
63 // Match the quoted term in result highlighting...
64 $regexp = preg_quote( str_replace( '"', '', $terms[2] ), '/' );
66 $this->searchTerms
[] = $regexp;
68 wfDebug( "Would search with '$searchon'\n" );
69 wfDebug( 'Match with /' . implode( '|', $this->searchTerms
) . "/\n" );
71 wfDebug( "Can't understand search query '{$filteredText}'\n" );
74 $searchon = $this->db
->strencode( $searchon );
75 $field = $this->getIndexField( $fulltext );
76 return " MATCH($field) AGAINST('$searchon' IN BOOLEAN MODE) ";
79 public static function legalSearchChars() {
80 return "\"*" . parent
::legalSearchChars();
84 * Perform a full text search query and return a result set.
86 * @param string $term - Raw search term
87 * @return MySQLSearchResultSet
90 function searchText( $term ) {
91 $resultSet = $this->db
->resultObject( $this->db
->query( $this->getQuery( $this->filter( $term ), true ) ) );
92 return new MySQLSearchResultSet( $resultSet, $this->searchTerms
);
96 * Perform a title-only search query and return a result set.
98 * @param string $term - Raw search term
99 * @return MySQLSearchResultSet
102 function searchTitle( $term ) {
103 $resultSet = $this->db
->resultObject( $this->db
->query( $this->getQuery( $this->filter( $term ), false ) ) );
104 return new MySQLSearchResultSet( $resultSet, $this->searchTerms
);
109 * Return a partial WHERE clause to exclude redirects, if so set
113 function queryRedirect() {
114 if( $this->showRedirects
) {
117 return 'AND page_is_redirect=0';
122 * Return a partial WHERE clause to limit the search to the given namespaces
126 function queryNamespaces() {
127 if( is_null($this->namespaces
) )
128 return ''; # search all
129 $namespaces = implode( ',', $this->namespaces
);
130 if ($namespaces == '') {
133 return 'AND page_namespace IN (' . $namespaces . ')';
137 * Return a LIMIT clause to limit results on the query.
141 function queryLimit() {
142 return $this->db
->limitResult( '', $this->limit
, $this->offset
);
146 * Does not do anything for generic search engine
147 * subclasses may define this though
151 function queryRanking( $filteredTerm, $fulltext ) {
156 * Construct the full SQL query to do the search.
157 * The guts shoulds be constructed in queryMain()
158 * @param string $filteredTerm
159 * @param bool $fulltext
162 function getQuery( $filteredTerm, $fulltext ) {
163 return $this->queryMain( $filteredTerm, $fulltext ) . ' ' .
164 $this->queryRedirect() . ' ' .
165 $this->queryNamespaces() . ' ' .
166 $this->queryRanking( $filteredTerm, $fulltext ) . ' ' .
172 * Picks which field to index on, depending on what type of query.
173 * @param bool $fulltext
176 function getIndexField( $fulltext ) {
177 return $fulltext ?
'si_text' : 'si_title';
181 * Get the base part of the search query.
182 * The actual match syntax will depend on the server
183 * version; MySQL 3 and MySQL 4 have different capabilities
184 * in their fulltext search indexes.
186 * @param string $filteredTerm
187 * @param bool $fulltext
191 function queryMain( $filteredTerm, $fulltext ) {
192 $match = $this->parseQuery( $filteredTerm, $fulltext );
193 $page = $this->db
->tableName( 'page' );
194 $searchindex = $this->db
->tableName( 'searchindex' );
195 return 'SELECT page_id, page_namespace, page_title ' .
196 "FROM $page,$searchindex " .
197 'WHERE page_id=si_page AND ' . $match;
201 * Create or update the search index record for the given page.
202 * Title and text should be pre-processed.
205 * @param string $title
206 * @param string $text
208 function update( $id, $title, $text ) {
209 $dbw = wfGetDB( DB_MASTER
);
210 $dbw->replace( 'searchindex',
214 'si_title' => $title,
220 * Update a search index record's title only.
221 * Title should be pre-processed.
224 * @param string $title
226 function updateTitle( $id, $title ) {
227 $dbw = wfGetDB( DB_MASTER
);
229 $dbw->update( 'searchindex',
230 array( 'si_title' => $title ),
231 array( 'si_page' => $id ),
233 array( $dbw->lowPriorityOption() ) );
240 class MySQLSearchResultSet
extends SearchResultSet
{
241 function MySQLSearchResultSet( $resultSet, $terms ) {
242 $this->mResultSet
= $resultSet;
243 $this->mTerms
= $terms;
246 function termMatches() {
247 return $this->mTerms
;
251 return $this->mResultSet
->numRows();
255 $row = $this->mResultSet
->fetchObject();
256 if( $row === false ) {
259 return new SearchResult( $row );
264 $this->mResultSet
->free();