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
24 * Search engine hook base class for Mssql (ConText).
27 class SearchMssql
extends SearchEngine
{
29 function __construct( $db ) {
34 * Perform a full text search query and return a result set.
36 * @param $term String: raw search term
37 * @return MssqlSearchResultSet
40 function searchText( $term ) {
41 $resultSet = $this->db
->resultObject( $this->db
->query( $this->getQuery( $this->filter( $term ), true ) ) );
42 return new MssqlSearchResultSet( $resultSet, $this->searchTerms
);
46 * Perform a title-only search query and return a result set.
48 * @param $term String: raw search term
49 * @return MssqlSearchResultSet
52 function searchTitle( $term ) {
53 $resultSet = $this->db
->resultObject( $this->db
->query( $this->getQuery( $this->filter( $term ), false ) ) );
54 return new MssqlSearchResultSet( $resultSet, $this->searchTerms
);
59 * Return a partial WHERE clause to exclude redirects, if so set
64 function queryRedirect() {
65 if ( $this->showRedirects
) {
68 return 'AND page_is_redirect=0';
73 * Return a partial WHERE clause to limit the search to the given namespaces
78 function queryNamespaces() {
79 $namespaces = implode( ',', $this->namespaces
);
80 if ( $namespaces == '' ) {
83 return 'AND page_namespace IN (' . $namespaces . ')';
87 * Return a LIMIT clause to limit results on the query.
92 function queryLimit( $sql ) {
93 return $this->db
->limitResult( $sql, $this->limit
, $this->offset
);
97 * Does not do anything for generic search engine
98 * subclasses may define this though
103 function queryRanking( $filteredTerm, $fulltext ) {
104 return ' ORDER BY ftindex.[RANK] DESC'; // return ' ORDER BY score(1)';
108 * Construct the full SQL query to do the search.
109 * The guts shoulds be constructed in queryMain()
111 * @param $filteredTerm String
112 * @param $fulltext Boolean
115 function getQuery( $filteredTerm, $fulltext ) {
116 return $this->queryLimit( $this->queryMain( $filteredTerm, $fulltext ) . ' ' .
117 $this->queryRedirect() . ' ' .
118 $this->queryNamespaces() . ' ' .
119 $this->queryRanking( $filteredTerm, $fulltext ) . ' ' );
124 * Picks which field to index on, depending on what type of query.
126 * @param $fulltext Boolean
129 function getIndexField( $fulltext ) {
130 return $fulltext ?
'si_text' : 'si_title';
134 * Get the base part of the search query.
136 * @param $filteredTerm String
137 * @param $fulltext Boolean
141 function queryMain( $filteredTerm, $fulltext ) {
142 $match = $this->parseQuery( $filteredTerm, $fulltext );
143 $page = $this->db
->tableName( 'page' );
144 $searchindex = $this->db
->tableName( 'searchindex' );
146 return 'SELECT page_id, page_namespace, page_title, ftindex.[RANK]' .
147 "FROM $page,FREETEXTTABLE($searchindex , $match, LANGUAGE 'English') as ftindex " .
148 'WHERE page_id=ftindex.[KEY] ';
151 /** @todo document */
152 function parseQuery( $filteredText, $fulltext ) {
154 $lc = SearchEngine
::legalSearchChars();
155 $this->searchTerms
= array();
157 # FIXME: This doesn't handle parenthetical expressions.
161 if ( preg_match_all( '/([-+<>~]?)(([' . $lc . ']+)(\*?)|"[^"]*")/',
162 $filteredText, $m, PREG_SET_ORDER
) ) {
163 foreach ( $m as $terms ) {
164 $q[] = $terms[1] . $wgContLang->normalizeForSearch( $terms[2] );
166 if ( !empty( $terms[3] ) ) {
167 $regexp = preg_quote( $terms[3], '/' );
169 $regexp .= "[0-9A-Za-z_]+";
171 $regexp = preg_quote( str_replace( '"', '', $terms[2] ), '/' );
173 $this->searchTerms
[] = $regexp;
177 $searchon = $this->db
->strencode( join( ',', $q ) );
178 $field = $this->getIndexField( $fulltext );
179 return "$field, '$searchon'";
183 * Create or update the search index record for the given page.
184 * Title and text should be pre-processed.
187 * @param $title String
188 * @param $text String
190 function update( $id, $title, $text ) {
191 // We store the column data as UTF-8 byte order marked binary stream
192 // because we are invoking the plain text IFilter on it so that, and we want it
193 // to properly decode the stream as UTF-8. SQL doesn't support UTF8 as a data type
194 // but the indexer will correctly handle it by this method. Since all we are doing
195 // is passing this data to the indexer and never retrieving it via PHP, this will save space
196 $table = $this->db
->tableName( 'searchindex' );
197 $utf8bom = '0xEFBBBF';
198 $si_title = $utf8bom . bin2hex( $title );
199 $si_text = $utf8bom . bin2hex( $text );
200 $sql = "DELETE FROM $table WHERE si_page = $id;";
201 $sql .= "INSERT INTO $table (si_page, si_title, si_text) VALUES ($id, $si_title, $si_text)";
202 return $this->db
->query( $sql, 'SearchMssql::update' );
206 * Update a search index record's title only.
207 * Title should be pre-processed.
210 * @param $title String
212 function updateTitle( $id, $title ) {
213 $table = $this->db
->tableName( 'searchindex' );
215 // see update for why we are using the utf8bom
216 $utf8bom = '0xEFBBBF';
217 $si_title = $utf8bom . bin2hex( $title );
218 $sql = "DELETE FROM $table WHERE si_page = $id;";
219 $sql .= "INSERT INTO $table (si_page, si_title, si_text) VALUES ($id, $si_title, 0x00)";
220 return $this->db
->query( $sql, 'SearchMssql::updateTitle' );
227 class MssqlSearchResultSet
extends SearchResultSet
{
228 function __construct( $resultSet, $terms ) {
229 $this->mResultSet
= $resultSet;
230 $this->mTerms
= $terms;
233 function termMatches() {
234 return $this->mTerms
;
238 return $this->mResultSet
->numRows();
242 $row = $this->mResultSet
->fetchObject();
243 if ( $row === false )
245 return new SearchResult( $row );