Add support for Number grouping(commafy) based on CLDR number grouping patterns like...
[lhc/web/wiklou.git] / includes / search / SearchMssql.php
1 <?php
2 /**
3 * Mssql search engine
4 *
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.
9 *
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.
14 *
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
19 *
20 * @file
21 * @ingroup Search
22 */
23
24 /**
25 * Search engine hook base class for Mssql (ConText).
26 * @ingroup Search
27 */
28 class SearchMssql extends SearchEngine {
29
30 /**
31 * Creates an instance of this class
32 * @param $db DatabaseMssql: database object
33 */
34 function __construct( $db ) {
35 parent::__construct( $db );
36 }
37
38 /**
39 * Perform a full text search query and return a result set.
40 *
41 * @param $term String: raw search term
42 * @return MssqlSearchResultSet
43 * @access public
44 */
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 );
48 }
49
50 /**
51 * Perform a title-only search query and return a result set.
52 *
53 * @param $term String: raw search term
54 * @return MssqlSearchResultSet
55 * @access public
56 */
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 );
60 }
61
62
63 /**
64 * Return a partial WHERE clause to exclude redirects, if so set
65 *
66 * @return String
67 * @private
68 */
69 function queryRedirect() {
70 if ( $this->showRedirects ) {
71 return '';
72 } else {
73 return 'AND page_is_redirect=0';
74 }
75 }
76
77 /**
78 * Return a partial WHERE clause to limit the search to the given namespaces
79 *
80 * @return String
81 * @private
82 */
83 function queryNamespaces() {
84 $namespaces = implode( ',', $this->namespaces );
85 if ( $namespaces == '' ) {
86 $namespaces = '0';
87 }
88 return 'AND page_namespace IN (' . $namespaces . ')';
89 }
90
91 /**
92 * Return a LIMIT clause to limit results on the query.
93 *
94 * @param $sql string
95 *
96 * @return String
97 */
98 function queryLimit( $sql ) {
99 return $this->db->limitResult( $sql, $this->limit, $this->offset );
100 }
101
102 /**
103 * Does not do anything for generic search engine
104 * subclasses may define this though
105 *
106 * @return String
107 */
108 function queryRanking( $filteredTerm, $fulltext ) {
109 return ' ORDER BY ftindex.[RANK] DESC'; // return ' ORDER BY score(1)';
110 }
111
112 /**
113 * Construct the full SQL query to do the search.
114 * The guts shoulds be constructed in queryMain()
115 *
116 * @param $filteredTerm String
117 * @param $fulltext Boolean
118 */
119 function getQuery( $filteredTerm, $fulltext ) {
120 return $this->queryLimit( $this->queryMain( $filteredTerm, $fulltext ) . ' ' .
121 $this->queryRedirect() . ' ' .
122 $this->queryNamespaces() . ' ' .
123 $this->queryRanking( $filteredTerm, $fulltext ) . ' ' );
124 }
125
126 /**
127 * Picks which field to index on, depending on what type of query.
128 *
129 * @param $fulltext Boolean
130 * @return string
131 */
132 function getIndexField( $fulltext ) {
133 return $fulltext ? 'si_text' : 'si_title';
134 }
135
136 /**
137 * Get the base part of the search query.
138 *
139 * @param $filteredTerm String
140 * @param $fulltext Boolean
141 * @return String
142 * @private
143 */
144 function queryMain( $filteredTerm, $fulltext ) {
145 $match = $this->parseQuery( $filteredTerm, $fulltext );
146 $page = $this->db->tableName( 'page' );
147 $searchindex = $this->db->tableName( 'searchindex' );
148
149 return 'SELECT page_id, page_namespace, page_title, ftindex.[RANK]' .
150 "FROM $page,FREETEXTTABLE($searchindex , $match, LANGUAGE 'English') as ftindex " .
151 'WHERE page_id=ftindex.[KEY] ';
152 }
153
154 /** @todo document */
155 function parseQuery( $filteredText, $fulltext ) {
156 global $wgContLang;
157 $lc = SearchEngine::legalSearchChars();
158 $this->searchTerms = array();
159
160 # @todo FIXME: This doesn't handle parenthetical expressions.
161 $m = array();
162 $q = array();
163
164 if ( preg_match_all( '/([-+<>~]?)(([' . $lc . ']+)(\*?)|"[^"]*")/',
165 $filteredText, $m, PREG_SET_ORDER ) ) {
166 foreach ( $m as $terms ) {
167 $q[] = $terms[1] . $wgContLang->normalizeForSearch( $terms[2] );
168
169 if ( !empty( $terms[3] ) ) {
170 $regexp = preg_quote( $terms[3], '/' );
171 if ( $terms[4] )
172 $regexp .= "[0-9A-Za-z_]+";
173 } else {
174 $regexp = preg_quote( str_replace( '"', '', $terms[2] ), '/' );
175 }
176 $this->searchTerms[] = $regexp;
177 }
178 }
179
180 $searchon = $this->db->strencode( join( ',', $q ) );
181 $field = $this->getIndexField( $fulltext );
182 return "$field, '$searchon'";
183 }
184
185 /**
186 * Create or update the search index record for the given page.
187 * Title and text should be pre-processed.
188 *
189 * @param $id Integer
190 * @param $title String
191 * @param $text String
192 */
193 function update( $id, $title, $text ) {
194 // We store the column data as UTF-8 byte order marked binary stream
195 // because we are invoking the plain text IFilter on it so that, and we want it
196 // to properly decode the stream as UTF-8. SQL doesn't support UTF8 as a data type
197 // but the indexer will correctly handle it by this method. Since all we are doing
198 // is passing this data to the indexer and never retrieving it via PHP, this will save space
199 $table = $this->db->tableName( 'searchindex' );
200 $utf8bom = '0xEFBBBF';
201 $si_title = $utf8bom . bin2hex( $title );
202 $si_text = $utf8bom . bin2hex( $text );
203 $sql = "DELETE FROM $table WHERE si_page = $id;";
204 $sql .= "INSERT INTO $table (si_page, si_title, si_text) VALUES ($id, $si_title, $si_text)";
205 return $this->db->query( $sql, 'SearchMssql::update' );
206 }
207
208 /**
209 * Update a search index record's title only.
210 * Title should be pre-processed.
211 *
212 * @param $id Integer
213 * @param $title String
214 */
215 function updateTitle( $id, $title ) {
216 $table = $this->db->tableName( 'searchindex' );
217
218 // see update for why we are using the utf8bom
219 $utf8bom = '0xEFBBBF';
220 $si_title = $utf8bom . bin2hex( $title );
221 $sql = "DELETE FROM $table WHERE si_page = $id;";
222 $sql .= "INSERT INTO $table (si_page, si_title, si_text) VALUES ($id, $si_title, 0x00)";
223 return $this->db->query( $sql, 'SearchMssql::updateTitle' );
224 }
225 }
226
227 /**
228 * @ingroup Search
229 */
230 class MssqlSearchResultSet extends SearchResultSet {
231 function __construct( $resultSet, $terms ) {
232 $this->mResultSet = $resultSet;
233 $this->mTerms = $terms;
234 }
235
236 function termMatches() {
237 return $this->mTerms;
238 }
239
240 function numRows() {
241 return $this->mResultSet->numRows();
242 }
243
244 function next() {
245 $row = $this->mResultSet->fetchObject();
246 if ( $row === false )
247 return false;
248 return new SearchResult( $row );
249 }
250 }
251
252