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