2 # Copyright (C) 2004 Brion Vibber <brion@pobox.com>
3 # http://www.mediawiki.org/
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
21 * Search engine hook base class for Mssql (ConText).
24 class SearchMssql
extends SearchEngine
{
26 function __construct( $db ) {
31 * Perform a full text search query and return a result set.
33 * @param string $term - Raw search term
34 * @return MssqlSearchResultSet
37 function searchText( $term ) {
38 $resultSet = $this->db
->resultObject( $this->db
->query( $this->getQuery( $this->filter( $term ), true ) ) );
39 return new MssqlSearchResultSet( $resultSet, $this->searchTerms
);
43 * Perform a title-only search query and return a result set.
45 * @param string $term - Raw search term
46 * @return MssqlSearchResultSet
49 function searchTitle( $term ) {
50 $resultSet = $this->db
->resultObject( $this->db
->query( $this->getQuery( $this->filter( $term ), false ) ) );
51 return new MssqlSearchResultSet( $resultSet, $this->searchTerms
);
56 * Return a partial WHERE clause to exclude redirects, if so set
60 function queryRedirect() {
61 if ( $this->showRedirects
) {
64 return 'AND page_is_redirect=0';
69 * 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.
86 function queryLimit( $sql ) {
87 return $this->db
->limitResult( $sql, $this->limit
, $this->offset
);
91 * Does not do anything for generic search engine
92 * subclasses may define this though
96 function queryRanking( $filteredTerm, $fulltext ) {
97 return ' ORDER BY ftindex.[RANK] DESC'; // return ' ORDER BY score(1)';
101 * Construct the full SQL query to do the search.
102 * The guts shoulds be constructed in queryMain()
103 * @param string $filteredTerm
104 * @param bool $fulltext
107 function getQuery( $filteredTerm, $fulltext ) {
108 return $this->queryLimit( $this->queryMain( $filteredTerm, $fulltext ) . ' ' .
109 $this->queryRedirect() . ' ' .
110 $this->queryNamespaces() . ' ' .
111 $this->queryRanking( $filteredTerm, $fulltext ) . ' ' );
116 * Picks which field to index on, depending on what type of query.
117 * @param bool $fulltext
120 function getIndexField( $fulltext ) {
121 return $fulltext ?
'si_text' : 'si_title';
125 * Get the base part of the search query.
127 * @param string $filteredTerm
128 * @param bool $fulltext
132 function queryMain( $filteredTerm, $fulltext ) {
133 $match = $this->parseQuery( $filteredTerm, $fulltext );
134 $page = $this->db
->tableName( 'page' );
135 $searchindex = $this->db
->tableName( 'searchindex' );
137 return 'SELECT page_id, page_namespace, page_title, ftindex.[RANK]' .
138 "FROM $page,FREETEXTTABLE($searchindex , $match, LANGUAGE 'English') as ftindex " .
139 'WHERE page_id=ftindex.[KEY] ';
142 /** @todo document */
143 function parseQuery( $filteredText, $fulltext ) {
145 $lc = SearchEngine
::legalSearchChars();
146 $this->searchTerms
= array();
148 # FIXME: This doesn't handle parenthetical expressions.
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] );
157 if ( !empty( $terms[3] ) ) {
158 $regexp = preg_quote( $terms[3], '/' );
160 $regexp .= "[0-9A-Za-z_]+";
162 $regexp = preg_quote( str_replace( '"', '', $terms[2] ), '/' );
164 $this->searchTerms
[] = $regexp;
168 $searchon = $this->db
->strencode( join( ',', $q ) );
169 $field = $this->getIndexField( $fulltext );
170 return "$field, '$searchon'";
174 * Create or update the search index record for the given page.
175 * Title and text should be pre-processed.
178 * @param string $title
179 * @param string $text
181 function update( $id, $title, $text ) {
182 // We store the column data as UTF-8 byte order marked binary stream
183 // because we are invoking the plain text IFilter on it so that, and we want it
184 // to properly decode the stream as UTF-8. SQL doesn't support UTF8 as a data type
185 // but the indexer will correctly handle it by this method. Since all we are doing
186 // is passing this data to the indexer and never retrieving it via PHP, this will save space
187 $table = $this->db
->tableName( 'searchindex' );
188 $utf8bom = '0xEFBBBF';
189 $si_title = $utf8bom . bin2hex( $title );
190 $si_text = $utf8bom . bin2hex( $text );
191 $sql = "DELETE FROM $table WHERE si_page = $id;";
192 $sql .= "INSERT INTO $table (si_page, si_title, si_text) VALUES ($id, $si_title, $si_text)";
193 return $this->db
->query( $sql, 'SearchMssql::update' );
197 * Update a search index record's title only.
198 * Title should be pre-processed.
201 * @param string $title
203 function updateTitle( $id, $title ) {
204 $table = $this->db
->tableName( 'searchindex' );
206 // see update for why we are using the utf8bom
207 $utf8bom = '0xEFBBBF';
208 $si_title = $utf8bom . bin2hex( $title );
209 $sql = "DELETE FROM $table WHERE si_page = $id;";
210 $sql .= "INSERT INTO $table (si_page, si_title, si_text) VALUES ($id, $si_title, 0x00)";
211 return $this->db
->query( $sql, 'SearchMssql::updateTitle' );
218 class MssqlSearchResultSet
extends SearchResultSet
{
219 function __construct( $resultSet, $terms ) {
220 $this->mResultSet
= $resultSet;
221 $this->mTerms
= $terms;
224 function termMatches() {
225 return $this->mTerms
;
229 return $this->mResultSet
->numRows();
233 $row = $this->mResultSet
->fetchObject();
234 if ( $row === false )
236 return new SearchResult( $row );