Pass __METHOD__ to DatabaseBase::commit() and DatabaseBase::rollback()
[mediawiki.git] / includes / search / SearchMssql.php
blob69c92ba32447af8eb2b097d28ec2c4a5eb0769d8
1 <?php
2 /**
3 * Mssql search engine
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 * @file
21 * @ingroup Search
24 /**
25 * Search engine hook base class for Mssql (ConText).
26 * @ingroup Search
28 class SearchMssql extends SearchEngine {
30 /**
31 * Creates an instance of this class
32 * @param $db DatabaseMssql: database object
34 function __construct( $db ) {
35 parent::__construct( $db );
38 /**
39 * Perform a full text search query and return a result set.
41 * @param $term String: raw search term
42 * @return MssqlSearchResultSet
43 * @access public
45 function searchText( $term ) {
46 $resultSet = $this->db->resultObject( $this->db->query( $this->getQuery( $this->filter( $term ), true ) ) );
47 return new MssqlSearchResultSet( $resultSet, $this->searchTerms );
50 /**
51 * Perform a title-only search query and return a result set.
53 * @param $term String: raw search term
54 * @return MssqlSearchResultSet
55 * @access public
57 function searchTitle( $term ) {
58 $resultSet = $this->db->resultObject( $this->db->query( $this->getQuery( $this->filter( $term ), false ) ) );
59 return new MssqlSearchResultSet( $resultSet, $this->searchTerms );
63 /**
64 * Return a partial WHERE clause to exclude redirects, if so set
66 * @return String
67 * @private
69 function queryRedirect() {
70 if ( $this->showRedirects ) {
71 return '';
72 } else {
73 return 'AND page_is_redirect=0';
77 /**
78 * Return a partial WHERE clause to limit the search to the given namespaces
80 * @return String
81 * @private
83 function queryNamespaces() {
84 $namespaces = implode( ',', $this->namespaces );
85 if ( $namespaces == '' ) {
86 $namespaces = '0';
88 return 'AND page_namespace IN (' . $namespaces . ')';
91 /**
92 * Return a LIMIT clause to limit results on the query.
94 * @param $sql string
96 * @return String
98 function queryLimit( $sql ) {
99 return $this->db->limitResult( $sql, $this->limit, $this->offset );
103 * Does not do anything for generic search engine
104 * subclasses may define this though
106 * @return String
108 function queryRanking( $filteredTerm, $fulltext ) {
109 return ' ORDER BY ftindex.[RANK] DESC'; // return ' ORDER BY score(1)';
113 * Construct the full SQL query to do the search.
114 * The guts shoulds be constructed in queryMain()
116 * @param $filteredTerm String
117 * @param $fulltext Boolean
118 * @return String
120 function getQuery( $filteredTerm, $fulltext ) {
121 return $this->queryLimit( $this->queryMain( $filteredTerm, $fulltext ) . ' ' .
122 $this->queryRedirect() . ' ' .
123 $this->queryNamespaces() . ' ' .
124 $this->queryRanking( $filteredTerm, $fulltext ) . ' ' );
128 * Picks which field to index on, depending on what type of query.
130 * @param $fulltext Boolean
131 * @return string
133 function getIndexField( $fulltext ) {
134 return $fulltext ? 'si_text' : 'si_title';
138 * Get the base part of the search query.
140 * @param $filteredTerm String
141 * @param $fulltext Boolean
142 * @return String
143 * @private
145 function queryMain( $filteredTerm, $fulltext ) {
146 $match = $this->parseQuery( $filteredTerm, $fulltext );
147 $page = $this->db->tableName( 'page' );
148 $searchindex = $this->db->tableName( 'searchindex' );
150 return 'SELECT page_id, page_namespace, page_title, ftindex.[RANK]' .
151 "FROM $page,FREETEXTTABLE($searchindex , $match, LANGUAGE 'English') as ftindex " .
152 'WHERE page_id=ftindex.[KEY] ';
155 /** @todo document
156 * @return string
158 function parseQuery( $filteredText, $fulltext ) {
159 global $wgContLang;
160 $lc = SearchEngine::legalSearchChars();
161 $this->searchTerms = array();
163 # @todo FIXME: This doesn't handle parenthetical expressions.
164 $m = array();
165 $q = array();
167 if ( preg_match_all( '/([-+<>~]?)(([' . $lc . ']+)(\*?)|"[^"]*")/',
168 $filteredText, $m, PREG_SET_ORDER ) ) {
169 foreach ( $m as $terms ) {
170 $q[] = $terms[1] . $wgContLang->normalizeForSearch( $terms[2] );
172 if ( !empty( $terms[3] ) ) {
173 $regexp = preg_quote( $terms[3], '/' );
174 if ( $terms[4] )
175 $regexp .= "[0-9A-Za-z_]+";
176 } else {
177 $regexp = preg_quote( str_replace( '"', '', $terms[2] ), '/' );
179 $this->searchTerms[] = $regexp;
183 $searchon = $this->db->strencode( join( ',', $q ) );
184 $field = $this->getIndexField( $fulltext );
185 return "$field, '$searchon'";
189 * Create or update the search index record for the given page.
190 * Title and text should be pre-processed.
192 * @param $id Integer
193 * @param $title String
194 * @param $text String
195 * @return bool|\ResultWrapper
197 function update( $id, $title, $text ) {
198 // We store the column data as UTF-8 byte order marked binary stream
199 // because we are invoking the plain text IFilter on it so that, and we want it
200 // to properly decode the stream as UTF-8. SQL doesn't support UTF8 as a data type
201 // but the indexer will correctly handle it by this method. Since all we are doing
202 // is passing this data to the indexer and never retrieving it via PHP, this will save space
203 $table = $this->db->tableName( 'searchindex' );
204 $utf8bom = '0xEFBBBF';
205 $si_title = $utf8bom . bin2hex( $title );
206 $si_text = $utf8bom . bin2hex( $text );
207 $sql = "DELETE FROM $table WHERE si_page = $id;";
208 $sql .= "INSERT INTO $table (si_page, si_title, si_text) VALUES ($id, $si_title, $si_text)";
209 return $this->db->query( $sql, 'SearchMssql::update' );
213 * Update a search index record's title only.
214 * Title should be pre-processed.
216 * @param $id Integer
217 * @param $title String
218 * @return bool|\ResultWrapper
220 function updateTitle( $id, $title ) {
221 $table = $this->db->tableName( 'searchindex' );
223 // see update for why we are using the utf8bom
224 $utf8bom = '0xEFBBBF';
225 $si_title = $utf8bom . bin2hex( $title );
226 $sql = "DELETE FROM $table WHERE si_page = $id;";
227 $sql .= "INSERT INTO $table (si_page, si_title, si_text) VALUES ($id, $si_title, 0x00)";
228 return $this->db->query( $sql, 'SearchMssql::updateTitle' );
233 * @ingroup Search
235 class MssqlSearchResultSet extends SearchResultSet {
236 function __construct( $resultSet, $terms ) {
237 $this->mResultSet = $resultSet;
238 $this->mTerms = $terms;
241 function termMatches() {
242 return $this->mTerms;
245 function numRows() {
246 return $this->mResultSet->numRows();
249 function next() {
250 $row = $this->mResultSet->fetchObject();
251 if ( $row === false )
252 return false;
253 return new SearchResult( $row );