Reverted r49019, unnecessary use of the ellipsis character, per CR
[mediawiki.git] / includes / SearchMySQL.php
blob59e1f0e41be2702936f493993218fc9597abd069
1 <?php
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
20 /**
21 * @file
22 * @ingroup Search
25 /**
26 * Search engine hook for MySQL 4+
27 * @ingroup Search
29 class SearchMySQL extends SearchEngine {
30 var $strictMatching = true;
32 /** @todo document */
33 function __construct( $db ) {
34 $this->db = $db;
37 /**
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 ) {
42 global $wgContLang;
43 $lc = SearchEngine::legalSearchChars(); // Minus format chars
44 $searchon = '';
45 $this->searchTerms = array();
47 # FIXME: This doesn't handle parenthetical expressions.
48 $m = array();
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] == '') ) {
54 $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], '/' );
60 if( $terms[4] ) {
61 $regexp = "\b$regexp"; // foo*
62 } else {
63 $regexp = "\b$regexp\b";
65 } else {
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" );
73 } else {
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();
86 /**
87 * Perform a full text search query and return a result set.
89 * @param $term String: raw search term
90 * @return MySQLSearchResultSet
92 function searchText( $term ) {
93 $resultSet = $this->db->resultObject( $this->db->query( $this->getQuery( $this->filter( $term ), true ) ) );
94 return new MySQLSearchResultSet( $resultSet, $this->searchTerms );
97 /**
98 * Perform a title-only search query and return a result set.
100 * @param $term String: raw search term
101 * @return MySQLSearchResultSet
103 function searchTitle( $term ) {
104 $resultSet = $this->db->resultObject( $this->db->query( $this->getQuery( $this->filter( $term ), false ) ) );
105 return new MySQLSearchResultSet( $resultSet, $this->searchTerms );
110 * Return a partial WHERE clause to exclude redirects, if so set
111 * @return String
113 function queryRedirect() {
114 if( $this->showRedirects ) {
115 return '';
116 } else {
117 return 'AND page_is_redirect=0';
122 * Return a partial WHERE clause to limit the search to the given namespaces
123 * @return String
125 function queryNamespaces() {
126 if( is_null($this->namespaces) )
127 return ''; # search all
128 if ( !count( $this->namespaces ) ) {
129 $namespaces = '0';
130 } else {
131 $namespaces = $this->db->makeList( $this->namespaces );
133 return 'AND page_namespace IN (' . $namespaces . ')';
137 * Return a LIMIT clause to limit results on the query.
138 * @return String
140 function queryLimit() {
141 return $this->db->limitResult( '', $this->limit, $this->offset );
145 * Does not do anything for generic search engine
146 * subclasses may define this though
147 * @return String
149 function queryRanking( $filteredTerm, $fulltext ) {
150 return '';
154 * Construct the full SQL query to do the search.
155 * The guts shoulds be constructed in queryMain()
156 * @param $filteredTerm String
157 * @param $fulltext Boolean
159 function getQuery( $filteredTerm, $fulltext ) {
160 return $this->queryMain( $filteredTerm, $fulltext ) . ' ' .
161 $this->queryRedirect() . ' ' .
162 $this->queryNamespaces() . ' ' .
163 $this->queryRanking( $filteredTerm, $fulltext ) . ' ' .
164 $this->queryLimit();
169 * Picks which field to index on, depending on what type of query.
170 * @param $fulltext Boolean
171 * @return String
173 function getIndexField( $fulltext ) {
174 return $fulltext ? 'si_text' : 'si_title';
178 * Get the base part of the search query.
179 * The actual match syntax will depend on the server
180 * version; MySQL 3 and MySQL 4 have different capabilities
181 * in their fulltext search indexes.
183 * @param $filteredTerm String
184 * @param $fulltext Boolean
185 * @return String
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.
200 * @param $id Integer
201 * @param $title String
202 * @param $text String
204 function update( $id, $title, $text ) {
205 $dbw = wfGetDB( DB_MASTER );
206 $dbw->replace( 'searchindex',
207 array( 'si_page' ),
208 array(
209 'si_page' => $id,
210 'si_title' => $title,
211 'si_text' => $text
212 ), __METHOD__ );
216 * Update a search index record's title only.
217 * Title should be pre-processed.
219 * @param $id Integer
220 * @param $title String
222 function updateTitle( $id, $title ) {
223 $dbw = wfGetDB( DB_MASTER );
225 $dbw->update( 'searchindex',
226 array( 'si_title' => $title ),
227 array( 'si_page' => $id ),
228 __METHOD__,
229 array( $dbw->lowPriorityOption() ) );
234 * @ingroup Search
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;
246 function numRows() {
247 return $this->mResultSet->numRows();
250 function next() {
251 $row = $this->mResultSet->fetchObject();
252 if( $row === false ) {
253 return false;
254 } else {
255 return new SearchResult( $row );
259 function free() {
260 $this->mResultSet->free();