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 use Wikimedia\Rdbms\IResultWrapper
;
27 * Search engine hook base class for Mssql (ConText).
30 class SearchMssql
extends SearchDatabase
{
32 * Perform a full text search query and return a result set.
34 * @param string $term Raw search term
35 * @return SqlSearchResultSet
37 protected function doSearchTextInDB( $term ) {
38 $resultSet = $this->db
->query( $this->getQuery( $this->filter( $term ), true ) );
39 return new SqlSearchResultSet( $resultSet, $this->searchTerms
);
43 * Perform a title-only search query and return a result set.
45 * @param string $term Raw search term
46 * @return SqlSearchResultSet
48 protected function doSearchTitleInDB( $term ) {
49 $resultSet = $this->db
->query( $this->getQuery( $this->filter( $term ), false ) );
50 return new SqlSearchResultSet( $resultSet, $this->searchTerms
);
54 * Return a partial WHERE clause to limit the search to the given namespaces
58 private function queryNamespaces() {
59 $namespaces = implode( ',', $this->namespaces
);
60 if ( $namespaces == '' ) {
63 return 'AND page_namespace IN (' . $namespaces . ')';
67 * Return a LIMIT clause to limit results on the query.
73 private function queryLimit( $sql ) {
74 return $this->db
->limitResult( $sql, $this->limit
, $this->offset
);
78 * Does not do anything for generic search engine
79 * subclasses may define this though
81 * @param string $filteredTerm
82 * @param bool $fulltext
85 function queryRanking( $filteredTerm, $fulltext ) {
86 return ' ORDER BY ftindex.[RANK] DESC'; // return ' ORDER BY score(1)';
90 * Construct the full SQL query to do the search.
91 * The guts shoulds be constructed in queryMain()
93 * @param string $filteredTerm
94 * @param bool $fulltext
97 private function getQuery( $filteredTerm, $fulltext ) {
98 return $this->queryLimit( $this->queryMain( $filteredTerm, $fulltext ) . ' ' .
99 $this->queryNamespaces() . ' ' .
100 $this->queryRanking( $filteredTerm, $fulltext ) . ' ' );
104 * Picks which field to index on, depending on what type of query.
106 * @param bool $fulltext
109 function getIndexField( $fulltext ) {
110 return $fulltext ?
'si_text' : 'si_title';
114 * Get the base part of the search query.
116 * @param string $filteredTerm
117 * @param bool $fulltext
120 private function queryMain( $filteredTerm, $fulltext ) {
121 $match = $this->parseQuery( $filteredTerm, $fulltext );
122 $page = $this->db
->tableName( 'page' );
123 $searchindex = $this->db
->tableName( 'searchindex' );
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] ';
131 * @param string $filteredText
132 * @param bool $fulltext
135 private function parseQuery( $filteredText, $fulltext ) {
137 $lc = $this->legalSearchChars( self
::CHARS_NO_SYNTAX
);
138 $this->searchTerms
= [];
140 # @todo FIXME: This doesn't handle parenthetical expressions.
144 if ( preg_match_all( '/([-+<>~]?)(([' . $lc . ']+)(\*?)|"[^"]*")/',
145 $filteredText, $m, PREG_SET_ORDER
) ) {
146 foreach ( $m as $terms ) {
147 $q[] = $terms[1] . $wgContLang->normalizeForSearch( $terms[2] );
149 if ( !empty( $terms[3] ) ) {
150 $regexp = preg_quote( $terms[3], '/' );
152 $regexp .= "[0-9A-Za-z_]+";
155 $regexp = preg_quote( str_replace( '"', '', $terms[2] ), '/' );
157 $this->searchTerms
[] = $regexp;
161 $searchon = $this->db
->addQuotes( implode( ',', $q ) );
162 $field = $this->getIndexField( $fulltext );
163 return "$field, $searchon";
167 * Create or update the search index record for the given page.
168 * Title and text should be pre-processed.
171 * @param string $title
172 * @param string $text
173 * @return bool|IResultWrapper
175 function update( $id, $title, $text ) {
176 // We store the column data as UTF-8 byte order marked binary stream
177 // because we are invoking the plain text IFilter on it so that, and we want it
178 // to properly decode the stream as UTF-8. SQL doesn't support UTF8 as a data type
179 // but the indexer will correctly handle it by this method. Since all we are doing
180 // is passing this data to the indexer and never retrieving it via PHP, this will save space
181 $table = $this->db
->tableName( 'searchindex' );
182 $utf8bom = '0xEFBBBF';
183 $si_title = $utf8bom . bin2hex( $title );
184 $si_text = $utf8bom . bin2hex( $text );
185 $sql = "DELETE FROM $table WHERE si_page = $id;";
186 $sql .= "INSERT INTO $table (si_page, si_title, si_text) VALUES ($id, $si_title, $si_text)";
187 return $this->db
->query( $sql, 'SearchMssql::update' );
191 * Update a search index record's title only.
192 * Title should be pre-processed.
195 * @param string $title
196 * @return bool|IResultWrapper
198 function updateTitle( $id, $title ) {
199 $table = $this->db
->tableName( 'searchindex' );
201 // see update for why we are using the utf8bom
202 $utf8bom = '0xEFBBBF';
203 $si_title = $utf8bom . bin2hex( $title );
204 $sql = "DELETE FROM $table WHERE si_page = $id;";
205 $sql .= "INSERT INTO $table (si_page, si_title, si_text) VALUES ($id, $si_title, 0x00)";
206 return $this->db
->query( $sql, 'SearchMssql::updateTitle' );