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