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.
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.
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
25 * Search engine hook base class for Mssql (ConText).
28 class SearchMssql
extends SearchDatabase
{
30 * Perform a full text search query and return a result set.
32 * @param string $term raw search term
33 * @return MssqlSearchResultSet
36 function searchText( $term ) {
37 $resultSet = $this->db
->resultObject( $this->db
->query( $this->getQuery( $this->filter( $term ), true ) ) );
38 return new MssqlSearchResultSet( $resultSet, $this->searchTerms
);
42 * Perform a title-only search query and return a result set.
44 * @param string $term raw search term
45 * @return MssqlSearchResultSet
48 function searchTitle( $term ) {
49 $resultSet = $this->db
->resultObject( $this->db
->query( $this->getQuery( $this->filter( $term ), false ) ) );
50 return new MssqlSearchResultSet( $resultSet, $this->searchTerms
);
54 * Return a partial WHERE clause to exclude redirects, if so set
59 function queryRedirect() {
60 if ( $this->showRedirects
) {
63 return 'AND page_is_redirect=0';
68 * Return a partial WHERE clause to limit the search to the given namespaces
73 function queryNamespaces() {
74 $namespaces = implode( ',', $this->namespaces
);
75 if ( $namespaces == '' ) {
78 return 'AND page_namespace IN (' . $namespaces . ')';
82 * Return a LIMIT clause to limit results on the query.
88 function queryLimit( $sql ) {
89 return $this->db
->limitResult( $sql, $this->limit
, $this->offset
);
93 * Does not do anything for generic search engine
94 * subclasses may define this though
98 function queryRanking( $filteredTerm, $fulltext ) {
99 return ' ORDER BY ftindex.[RANK] DESC'; // return ' ORDER BY score(1)';
103 * Construct the full SQL query to do the search.
104 * The guts shoulds be constructed in queryMain()
106 * @param $filteredTerm String
107 * @param $fulltext Boolean
110 function getQuery( $filteredTerm, $fulltext ) {
111 return $this->queryLimit( $this->queryMain( $filteredTerm, $fulltext ) . ' ' .
112 $this->queryRedirect() . ' ' .
113 $this->queryNamespaces() . ' ' .
114 $this->queryRanking( $filteredTerm, $fulltext ) . ' ' );
118 * Picks which field to index on, depending on what type of query.
120 * @param $fulltext Boolean
123 function getIndexField( $fulltext ) {
124 return $fulltext ?
'si_text' : 'si_title';
128 * Get the base part of the search query.
130 * @param $filteredTerm String
131 * @param $fulltext Boolean
135 function queryMain( $filteredTerm, $fulltext ) {
136 $match = $this->parseQuery( $filteredTerm, $fulltext );
137 $page = $this->db
->tableName( 'page' );
138 $searchindex = $this->db
->tableName( 'searchindex' );
140 return 'SELECT page_id, page_namespace, page_title, ftindex.[RANK]' .
141 "FROM $page,FREETEXTTABLE($searchindex , $match, LANGUAGE 'English') as ftindex " .
142 'WHERE page_id=ftindex.[KEY] ';
148 function parseQuery( $filteredText, $fulltext ) {
150 $lc = SearchEngine
::legalSearchChars();
151 $this->searchTerms
= array();
153 # @todo FIXME: This doesn't handle parenthetical expressions.
157 if ( preg_match_all( '/([-+<>~]?)(([' . $lc . ']+)(\*?)|"[^"]*")/',
158 $filteredText, $m, PREG_SET_ORDER
) ) {
159 foreach ( $m as $terms ) {
160 $q[] = $terms[1] . $wgContLang->normalizeForSearch( $terms[2] );
162 if ( !empty( $terms[3] ) ) {
163 $regexp = preg_quote( $terms[3], '/' );
165 $regexp .= "[0-9A-Za-z_]+";
168 $regexp = preg_quote( str_replace( '"', '', $terms[2] ), '/' );
170 $this->searchTerms
[] = $regexp;
174 $searchon = $this->db
->strencode( join( ',', $q ) );
175 $field = $this->getIndexField( $fulltext );
176 return "$field, '$searchon'";
180 * Create or update the search index record for the given page.
181 * Title and text should be pre-processed.
184 * @param $title String
185 * @param $text String
186 * @return bool|ResultWrapper
188 function update( $id, $title, $text ) {
189 // We store the column data as UTF-8 byte order marked binary stream
190 // because we are invoking the plain text IFilter on it so that, and we want it
191 // to properly decode the stream as UTF-8. SQL doesn't support UTF8 as a data type
192 // but the indexer will correctly handle it by this method. Since all we are doing
193 // is passing this data to the indexer and never retrieving it via PHP, this will save space
194 $table = $this->db
->tableName( 'searchindex' );
195 $utf8bom = '0xEFBBBF';
196 $si_title = $utf8bom . bin2hex( $title );
197 $si_text = $utf8bom . bin2hex( $text );
198 $sql = "DELETE FROM $table WHERE si_page = $id;";
199 $sql .= "INSERT INTO $table (si_page, si_title, si_text) VALUES ($id, $si_title, $si_text)";
200 return $this->db
->query( $sql, 'SearchMssql::update' );
204 * Update a search index record's title only.
205 * Title should be pre-processed.
208 * @param $title String
209 * @return bool|ResultWrapper
211 function updateTitle( $id, $title ) {
212 $table = $this->db
->tableName( 'searchindex' );
214 // see update for why we are using the utf8bom
215 $utf8bom = '0xEFBBBF';
216 $si_title = $utf8bom . bin2hex( $title );
217 $sql = "DELETE FROM $table WHERE si_page = $id;";
218 $sql .= "INSERT INTO $table (si_page, si_title, si_text) VALUES ($id, $si_title, 0x00)";
219 return $this->db
->query( $sql, 'SearchMssql::updateTitle' );
226 class MssqlSearchResultSet
extends SearchResultSet
{
227 function __construct( $resultSet, $terms ) {
228 $this->mResultSet
= $resultSet;
229 $this->mTerms
= $terms;
232 function termMatches() {
233 return $this->mTerms
;
237 return $this->mResultSet
->numRows();
241 $row = $this->mResultSet
->fetchObject();
242 if ( $row === false ) {
245 return new SearchResult( $row );