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