Localisation updates for core and extension messages from translatewiki.net (2010...
[mediawiki.git] / includes / search / SearchMssql.php
blob4177775d5c4451f7faf8264646a4be7cd8b5109c
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 function __construct( $db ) {
31 $this->db = $db;
34 /**
35 * Perform a full text search query and return a result set.
37 * @param $term String: raw search term
38 * @return MssqlSearchResultSet
39 * @access public
41 function searchText( $term ) {
42 $resultSet = $this->db->resultObject( $this->db->query( $this->getQuery( $this->filter( $term ), true ) ) );
43 return new MssqlSearchResultSet( $resultSet, $this->searchTerms );
46 /**
47 * Perform a title-only search query and return a result set.
49 * @param $term String: raw search term
50 * @return MssqlSearchResultSet
51 * @access public
53 function searchTitle( $term ) {
54 $resultSet = $this->db->resultObject( $this->db->query( $this->getQuery( $this->filter( $term ), false ) ) );
55 return new MssqlSearchResultSet( $resultSet, $this->searchTerms );
59 /**
60 * Return a partial WHERE clause to exclude redirects, if so set
62 * @return String
63 * @private
65 function queryRedirect() {
66 if ( $this->showRedirects ) {
67 return '';
68 } else {
69 return 'AND page_is_redirect=0';
73 /**
74 * Return a partial WHERE clause to limit the search to the given namespaces
76 * @return String
77 * @private
79 function queryNamespaces() {
80 $namespaces = implode( ',', $this->namespaces );
81 if ( $namespaces == '' ) {
82 $namespaces = '0';
84 return 'AND page_namespace IN (' . $namespaces . ')';
87 /**
88 * Return a LIMIT clause to limit results on the query.
90 * @return String
91 * @private
93 function queryLimit( $sql ) {
94 return $this->db->limitResult( $sql, $this->limit, $this->offset );
97 /**
98 * Does not do anything for generic search engine
99 * subclasses may define this though
101 * @return String
102 * @private
104 function queryRanking( $filteredTerm, $fulltext ) {
105 return ' ORDER BY ftindex.[RANK] DESC'; // return ' ORDER BY score(1)';
109 * Construct the full SQL query to do the search.
110 * The guts shoulds be constructed in queryMain()
112 * @param $filteredTerm String
113 * @param $fulltext Boolean
114 * @private
116 function getQuery( $filteredTerm, $fulltext ) {
117 return $this->queryLimit( $this->queryMain( $filteredTerm, $fulltext ) . ' ' .
118 $this->queryRedirect() . ' ' .
119 $this->queryNamespaces() . ' ' .
120 $this->queryRanking( $filteredTerm, $fulltext ) . ' ' );
125 * Picks which field to index on, depending on what type of query.
127 * @param $fulltext Boolean
128 * @return string
130 function getIndexField( $fulltext ) {
131 return $fulltext ? 'si_text' : 'si_title';
135 * Get the base part of the search query.
137 * @param $filteredTerm String
138 * @param $fulltext Boolean
139 * @return String
140 * @private
142 function queryMain( $filteredTerm, $fulltext ) {
143 $match = $this->parseQuery( $filteredTerm, $fulltext );
144 $page = $this->db->tableName( 'page' );
145 $searchindex = $this->db->tableName( 'searchindex' );
147 return 'SELECT page_id, page_namespace, page_title, ftindex.[RANK]' .
148 "FROM $page,FREETEXTTABLE($searchindex , $match, LANGUAGE 'English') as ftindex " .
149 'WHERE page_id=ftindex.[KEY] ';
152 /** @todo document */
153 function parseQuery( $filteredText, $fulltext ) {
154 global $wgContLang;
155 $lc = SearchEngine::legalSearchChars();
156 $this->searchTerms = array();
158 # FIXME: This doesn't handle parenthetical expressions.
159 $m = array();
160 $q = array();
162 if ( preg_match_all( '/([-+<>~]?)(([' . $lc . ']+)(\*?)|"[^"]*")/',
163 $filteredText, $m, PREG_SET_ORDER ) ) {
164 foreach ( $m as $terms ) {
165 $q[] = $terms[1] . $wgContLang->normalizeForSearch( $terms[2] );
167 if ( !empty( $terms[3] ) ) {
168 $regexp = preg_quote( $terms[3], '/' );
169 if ( $terms[4] )
170 $regexp .= "[0-9A-Za-z_]+";
171 } else {
172 $regexp = preg_quote( str_replace( '"', '', $terms[2] ), '/' );
174 $this->searchTerms[] = $regexp;
178 $searchon = $this->db->strencode( join( ',', $q ) );
179 $field = $this->getIndexField( $fulltext );
180 return "$field, '$searchon'";
184 * Create or update the search index record for the given page.
185 * Title and text should be pre-processed.
187 * @param $id Integer
188 * @param $title String
189 * @param $text String
191 function update( $id, $title, $text ) {
192 // We store the column data as UTF-8 byte order marked binary stream
193 // because we are invoking the plain text IFilter on it so that, and we want it
194 // to properly decode the stream as UTF-8. SQL doesn't support UTF8 as a data type
195 // but the indexer will correctly handle it by this method. Since all we are doing
196 // is passing this data to the indexer and never retrieving it via PHP, this will save space
197 $table = $this->db->tableName( 'searchindex' );
198 $utf8bom = '0xEFBBBF';
199 $si_title = $utf8bom . bin2hex( $title );
200 $si_text = $utf8bom . bin2hex( $text );
201 $sql = "DELETE FROM $table WHERE si_page = $id;";
202 $sql .= "INSERT INTO $table (si_page, si_title, si_text) VALUES ($id, $si_title, $si_text)";
203 return $this->db->query( $sql, 'SearchMssql::update' );
207 * Update a search index record's title only.
208 * Title should be pre-processed.
210 * @param $id Integer
211 * @param $title String
213 function updateTitle( $id, $title ) {
214 $table = $this->db->tableName( 'searchindex' );
216 // see update for why we are using the utf8bom
217 $utf8bom = '0xEFBBBF';
218 $si_title = $utf8bom . bin2hex( $title );
219 $sql = "DELETE FROM $table WHERE si_page = $id;";
220 $sql .= "INSERT INTO $table (si_page, si_title, si_text) VALUES ($id, $si_title, 0x00)";
221 return $this->db->query( $sql, 'SearchMssql::updateTitle' );
226 * @ingroup Search
228 class MssqlSearchResultSet extends SearchResultSet {
229 function __construct( $resultSet, $terms ) {
230 $this->mResultSet = $resultSet;
231 $this->mTerms = $terms;
234 function termMatches() {
235 return $this->mTerms;
238 function numRows() {
239 return $this->mResultSet->numRows();
242 function next() {
243 $row = $this->mResultSet->fetchObject();
244 if ( $row === false )
245 return false;
246 return new SearchResult( $row );