Merge "Special:BookSources: Remove link to PriceSCAN"
[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( $this->db->query( $this->getQuery( $this->filter( $term ), true ) ) );
38 return new SqlSearchResultSet( $resultSet, $this->searchTerms );
39 }
40
41 /**
42 * Perform a title-only search query and return a result set.
43 *
44 * @param string $term Raw search term
45 * @return SqlSearchResultSet
46 * @access public
47 */
48 function searchTitle( $term ) {
49 $resultSet = $this->db->resultObject( $this->db->query( $this->getQuery( $this->filter( $term ), false ) ) );
50 return new SqlSearchResultSet( $resultSet, $this->searchTerms );
51 }
52
53 /**
54 * Return a partial WHERE clause to limit the search to the given namespaces
55 *
56 * @return string
57 * @private
58 */
59 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 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 * @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 * @return string
132 */
133 function parseQuery( $filteredText, $fulltext ) {
134 global $wgContLang;
135 $lc = $this->legalSearchChars();
136 $this->searchTerms = array();
137
138 # @todo FIXME: This doesn't handle parenthetical expressions.
139 $m = array();
140 $q = array();
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->strencode( join( ',', $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 }