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