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