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