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