Deprecate overriding SearchEngine::search*
[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 SqlSearchResultSet
34 */
35 protected function doSearchText( $term ) {
36 $resultSet = $this->db->query( $this->getQuery( $this->filter( $term ), true ) );
37 return new SqlSearchResultSet( $resultSet, $this->searchTerms );
38 }
39
40 /**
41 * Perform a title-only search query and return a result set.
42 *
43 * @param string $term Raw search term
44 * @return SqlSearchResultSet
45 */
46 protected function doSearchTitle( $term ) {
47 $resultSet = $this->db->query( $this->getQuery( $this->filter( $term ), false ) );
48 return new SqlSearchResultSet( $resultSet, $this->searchTerms );
49 }
50
51 /**
52 * Return a partial WHERE clause to limit the search to the given namespaces
53 *
54 * @return string
55 * @private
56 */
57 function queryNamespaces() {
58 $namespaces = implode( ',', $this->namespaces );
59 if ( $namespaces == '' ) {
60 $namespaces = '0';
61 }
62 return 'AND page_namespace IN (' . $namespaces . ')';
63 }
64
65 /**
66 * Return a LIMIT clause to limit results on the query.
67 *
68 * @param string $sql
69 *
70 * @return string
71 */
72 function queryLimit( $sql ) {
73 return $this->db->limitResult( $sql, $this->limit, $this->offset );
74 }
75
76 /**
77 * Does not do anything for generic search engine
78 * subclasses may define this though
79 *
80 * @param string $filteredTerm
81 * @param bool $fulltext
82 * @return string
83 */
84 function queryRanking( $filteredTerm, $fulltext ) {
85 return ' ORDER BY ftindex.[RANK] DESC'; // return ' ORDER BY score(1)';
86 }
87
88 /**
89 * Construct the full SQL query to do the search.
90 * The guts shoulds be constructed in queryMain()
91 *
92 * @param string $filteredTerm
93 * @param bool $fulltext
94 * @return string
95 */
96 function getQuery( $filteredTerm, $fulltext ) {
97 return $this->queryLimit( $this->queryMain( $filteredTerm, $fulltext ) . ' ' .
98 $this->queryNamespaces() . ' ' .
99 $this->queryRanking( $filteredTerm, $fulltext ) . ' ' );
100 }
101
102 /**
103 * Picks which field to index on, depending on what type of query.
104 *
105 * @param bool $fulltext
106 * @return string
107 */
108 function getIndexField( $fulltext ) {
109 return $fulltext ? 'si_text' : 'si_title';
110 }
111
112 /**
113 * Get the base part of the search query.
114 *
115 * @param string $filteredTerm
116 * @param bool $fulltext
117 * @return string
118 * @private
119 */
120 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 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|ResultWrapper
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|ResultWrapper
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 }