Merge "Clear the DBO_TRX flag for sanity in ExternalStore"
[mediawiki.git] / includes / search / SearchMssql.php
blobde701f7355ed2225888f44661fac55d889748737
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 SearchDatabase {
29 /**
30 * Perform a full text search query and return a result set.
32 * @param string $term Raw search term
33 * @return SqlSearchResultSet
34 * @access public
36 function searchText( $term ) {
37 $resultSet = $this->db->resultObject(
38 $this->db->query( $this->getQuery( $this->filter( $term ), true ) )
41 return new SqlSearchResultSet( $resultSet, $this->searchTerms );
44 /**
45 * Perform a title-only search query and return a result set.
47 * @param string $term Raw search term
48 * @return SqlSearchResultSet
49 * @access public
51 function searchTitle( $term ) {
52 $resultSet = $this->db->resultObject(
53 $this->db->query( $this->getQuery( $this->filter( $term ), false ) )
56 return new SqlSearchResultSet( $resultSet, $this->searchTerms );
59 /**
60 * Return a partial WHERE clause to limit the search to the given namespaces
62 * @return string
63 * @private
65 function queryNamespaces() {
66 $namespaces = implode( ',', $this->namespaces );
67 if ( $namespaces == '' ) {
68 $namespaces = '0';
70 return 'AND page_namespace IN (' . $namespaces . ')';
73 /**
74 * Return a LIMIT clause to limit results on the query.
76 * @param string $sql
78 * @return string
80 function queryLimit( $sql ) {
81 return $this->db->limitResult( $sql, $this->limit, $this->offset );
84 /**
85 * Does not do anything for generic search engine
86 * subclasses may define this though
88 * @param string $filteredTerm
89 * @param bool $fulltext
90 * @return string
92 function queryRanking( $filteredTerm, $fulltext ) {
93 return ' ORDER BY ftindex.[RANK] DESC'; // return ' ORDER BY score(1)';
96 /**
97 * Construct the full SQL query to do the search.
98 * The guts shoulds be constructed in queryMain()
100 * @param string $filteredTerm
101 * @param bool $fulltext
102 * @return string
104 function getQuery( $filteredTerm, $fulltext ) {
105 return $this->queryLimit( $this->queryMain( $filteredTerm, $fulltext ) . ' ' .
106 $this->queryNamespaces() . ' ' .
107 $this->queryRanking( $filteredTerm, $fulltext ) . ' ' );
111 * Picks which field to index on, depending on what type of query.
113 * @param bool $fulltext
114 * @return string
116 function getIndexField( $fulltext ) {
117 return $fulltext ? 'si_text' : 'si_title';
121 * Get the base part of the search query.
123 * @param string $filteredTerm
124 * @param bool $fulltext
125 * @return string
126 * @private
128 function queryMain( $filteredTerm, $fulltext ) {
129 $match = $this->parseQuery( $filteredTerm, $fulltext );
130 $page = $this->db->tableName( 'page' );
131 $searchindex = $this->db->tableName( 'searchindex' );
133 return 'SELECT page_id, page_namespace, page_title, ftindex.[RANK]' .
134 "FROM $page,FREETEXTTABLE($searchindex , $match, LANGUAGE 'English') as ftindex " .
135 'WHERE page_id=ftindex.[KEY] ';
138 /** @todo document
139 * @param string $filteredText
140 * @param bool $fulltext
141 * @return string
143 function parseQuery( $filteredText, $fulltext ) {
144 global $wgContLang;
145 $lc = $this->legalSearchChars();
146 $this->searchTerms = array();
148 # @todo FIXME: This doesn't handle parenthetical expressions.
149 $m = array();
150 $q = array();
152 if ( preg_match_all( '/([-+<>~]?)(([' . $lc . ']+)(\*?)|"[^"]*")/',
153 $filteredText, $m, PREG_SET_ORDER ) ) {
154 foreach ( $m as $terms ) {
155 $q[] = $terms[1] . $wgContLang->normalizeForSearch( $terms[2] );
157 if ( !empty( $terms[3] ) ) {
158 $regexp = preg_quote( $terms[3], '/' );
159 if ( $terms[4] ) {
160 $regexp .= "[0-9A-Za-z_]+";
162 } else {
163 $regexp = preg_quote( str_replace( '"', '', $terms[2] ), '/' );
165 $this->searchTerms[] = $regexp;
169 $searchon = $this->db->addQuotes( join( ',', $q ) );
170 $field = $this->getIndexField( $fulltext );
171 return "$field, $searchon";
175 * Create or update the search index record for the given page.
176 * Title and text should be pre-processed.
178 * @param int $id
179 * @param string $title
180 * @param string $text
181 * @return bool|ResultWrapper
183 function update( $id, $title, $text ) {
184 // We store the column data as UTF-8 byte order marked binary stream
185 // because we are invoking the plain text IFilter on it so that, and we want it
186 // to properly decode the stream as UTF-8. SQL doesn't support UTF8 as a data type
187 // but the indexer will correctly handle it by this method. Since all we are doing
188 // is passing this data to the indexer and never retrieving it via PHP, this will save space
189 $table = $this->db->tableName( 'searchindex' );
190 $utf8bom = '0xEFBBBF';
191 $si_title = $utf8bom . bin2hex( $title );
192 $si_text = $utf8bom . bin2hex( $text );
193 $sql = "DELETE FROM $table WHERE si_page = $id;";
194 $sql .= "INSERT INTO $table (si_page, si_title, si_text) VALUES ($id, $si_title, $si_text)";
195 return $this->db->query( $sql, 'SearchMssql::update' );
199 * Update a search index record's title only.
200 * Title should be pre-processed.
202 * @param int $id
203 * @param string $title
204 * @return bool|ResultWrapper
206 function updateTitle( $id, $title ) {
207 $table = $this->db->tableName( 'searchindex' );
209 // see update for why we are using the utf8bom
210 $utf8bom = '0xEFBBBF';
211 $si_title = $utf8bom . bin2hex( $title );
212 $sql = "DELETE FROM $table WHERE si_page = $id;";
213 $sql .= "INSERT INTO $table (si_page, si_title, si_text) VALUES ($id, $si_title, 0x00)";
214 return $this->db->query( $sql, 'SearchMssql::updateTitle' );