Merge "Maintenance: Add an easy way to access Config instances"
[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 * @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 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 * @private
127 */
128 function queryMain( $filteredTerm, $fulltext ) {
129 $match = $this->parseQuery( $filteredTerm, $fulltext );
130 $page = $this->db->tableName( 'page' );
131 $searchindex = $this->db->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 function parseQuery( $filteredText, $fulltext ) {
144 global $wgContLang;
145 $lc = $this->legalSearchChars();
146 $this->searchTerms = array();
147
148 # @todo FIXME: This doesn't handle parenthetical expressions.
149 $m = array();
150 $q = array();
151
152 if ( preg_match_all( '/([-+<>~]?)(([' . $lc . ']+)(\*?)|"[^"]*")/',
153 $filteredText, $m, PREG_SET_ORDER ) ) {
154 foreach ( $m as $terms ) {
155 $q[] = $terms[1] . $wgContLang->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 $searchon = $this->db->addQuotes( join( ',', $q ) );
170 $field = $this->getIndexField( $fulltext );
171 return "$field, $searchon";
172 }
173
174 /**
175 * Create or update the search index record for the given page.
176 * Title and text should be pre-processed.
177 *
178 * @param int $id
179 * @param string $title
180 * @param string $text
181 * @return bool|ResultWrapper
182 */
183 function update( $id, $title, $text ) {
184 // We store the column data as UTF-8 byte order marked binary stream
185 // because we are invoking the plain text IFilter on it so that, and we want it
186 // to properly decode the stream as UTF-8. SQL doesn't support UTF8 as a data type
187 // but the indexer will correctly handle it by this method. Since all we are doing
188 // is passing this data to the indexer and never retrieving it via PHP, this will save space
189 $table = $this->db->tableName( 'searchindex' );
190 $utf8bom = '0xEFBBBF';
191 $si_title = $utf8bom . bin2hex( $title );
192 $si_text = $utf8bom . bin2hex( $text );
193 $sql = "DELETE FROM $table WHERE si_page = $id;";
194 $sql .= "INSERT INTO $table (si_page, si_title, si_text) VALUES ($id, $si_title, $si_text)";
195 return $this->db->query( $sql, 'SearchMssql::update' );
196 }
197
198 /**
199 * Update a search index record's title only.
200 * Title should be pre-processed.
201 *
202 * @param int $id
203 * @param string $title
204 * @return bool|ResultWrapper
205 */
206 function updateTitle( $id, $title ) {
207 $table = $this->db->tableName( 'searchindex' );
208
209 // see update for why we are using the utf8bom
210 $utf8bom = '0xEFBBBF';
211 $si_title = $utf8bom . bin2hex( $title );
212 $sql = "DELETE FROM $table WHERE si_page = $id;";
213 $sql .= "INSERT INTO $table (si_page, si_title, si_text) VALUES ($id, $si_title, 0x00)";
214 return $this->db->query( $sql, 'SearchMssql::updateTitle' );
215 }
216 }