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 MediaWiki\MediaWikiServices
;
25 use Wikimedia\Rdbms\IResultWrapper
;
28 * Search engine hook base class for Mssql (ConText).
31 class SearchMssql
extends SearchDatabase
{
33 * Perform a full text search query and return a result set.
35 * @param string $term Raw search term
36 * @return SqlSearchResultSet
38 protected function doSearchTextInDB( $term ) {
39 $dbr = $this->lb
->getConnectionRef( DB_REPLICA
);
40 $resultSet = $dbr->query( $this->getQuery( $this->filter( $term ), true ) );
42 return new SqlSearchResultSet( $resultSet, $this->searchTerms
);
46 * Perform a title-only search query and return a result set.
48 * @param string $term Raw search term
49 * @return SqlSearchResultSet
51 protected function doSearchTitleInDB( $term ) {
52 $dbr = $this->lb
->getConnectionRef( DB_REPLICA
);
53 $resultSet = $dbr->query( $this->getQuery( $this->filter( $term ), false ) );
55 return new SqlSearchResultSet( $resultSet, $this->searchTerms
);
59 * Return a partial WHERE clause to limit the search to the given namespaces
63 private function queryNamespaces() {
64 $namespaces = implode( ',', $this->namespaces
);
65 if ( $namespaces == '' ) {
68 return 'AND page_namespace IN (' . $namespaces . ')';
72 * Return a LIMIT clause to limit results on the query.
78 private function queryLimit( $sql ) {
79 $dbr = $this->lb
->getConnectionRef( DB_REPLICA
);
81 return $dbr->limitResult( $sql, $this->limit
, $this->offset
);
85 * Does not do anything for generic search engine
86 * subclasses may define this though
88 * @param string $filteredTerm
89 * @param bool $fulltext
92 function queryRanking( $filteredTerm, $fulltext ) {
93 return ' ORDER BY ftindex.[RANK] DESC'; // return ' ORDER BY score(1)';
97 * Construct the full SQL query to do the search.
98 * The guts shoulds be constructed in queryMain()
100 * @param string $filteredTerm
101 * @param bool $fulltext
104 private function getQuery( $filteredTerm, $fulltext ) {
105 return $this->queryLimit( $this->queryMain( $filteredTerm, $fulltext ) . ' ' .
106 $this->queryNamespaces() . ' ' .
107 $this->queryRanking( $filteredTerm, $fulltext ) . ' ' );
111 * Picks which field to index on, depending on what type of query.
113 * @param bool $fulltext
116 function getIndexField( $fulltext ) {
117 return $fulltext ?
'si_text' : 'si_title';
121 * Get the base part of the search query.
123 * @param string $filteredTerm
124 * @param bool $fulltext
127 private function queryMain( $filteredTerm, $fulltext ) {
128 $match = $this->parseQuery( $filteredTerm, $fulltext );
129 $dbr = $this->lb
->getMaintenanceConnectionRef( DB_REPLICA
);
130 $page = $dbr->tableName( 'page' );
131 $searchindex = $dbr->tableName( 'searchindex' );
133 return 'SELECT page_id, page_namespace, page_title, ftindex.[RANK]' .
134 "FROM $page,FREETEXTTABLE($searchindex , $match, LANGUAGE 'English') as ftindex " .
135 'WHERE page_id=ftindex.[KEY] ';
139 * @param string $filteredText
140 * @param bool $fulltext
143 private function parseQuery( $filteredText, $fulltext ) {
144 $lc = $this->legalSearchChars( self
::CHARS_NO_SYNTAX
);
145 $this->searchTerms
= [];
147 # @todo FIXME: This doesn't handle parenthetical expressions.
151 if ( preg_match_all( '/([-+<>~]?)(([' . $lc . ']+)(\*?)|"[^"]*")/',
152 $filteredText, $m, PREG_SET_ORDER
) ) {
153 foreach ( $m as $terms ) {
154 $q[] = $terms[1] . MediaWikiServices
::getInstance()->getContentLanguage()->
155 normalizeForSearch( $terms[2] );
157 if ( !empty( $terms[3] ) ) {
158 $regexp = preg_quote( $terms[3], '/' );
160 $regexp .= "[0-9A-Za-z_]+";
163 $regexp = preg_quote( str_replace( '"', '', $terms[2] ), '/' );
165 $this->searchTerms
[] = $regexp;
169 $dbr = $this->lb
->getConnectionRef( DB_REPLICA
);
170 $searchon = $dbr->addQuotes( implode( ',', $q ) );
171 $field = $this->getIndexField( $fulltext );
173 return "$field, $searchon";
177 * Create or update the search index record for the given page.
178 * Title and text should be pre-processed.
181 * @param string $title
182 * @param string $text
183 * @return bool|IResultWrapper
185 function update( $id, $title, $text ) {
186 // We store the column data as UTF-8 byte order marked binary stream
187 // because we are invoking the plain text IFilter on it so that, and we want it
188 // to properly decode the stream as UTF-8. SQL doesn't support UTF8 as a data type
189 // but the indexer will correctly handle it by this method. Since all we are doing
190 // is passing this data to the indexer and never retrieving it via PHP, this will save space
191 $dbr = $this->lb
->getMaintenanceConnectionRef( DB_MASTER
);
192 $table = $dbr->tableName( 'searchindex' );
193 $utf8bom = '0xEFBBBF';
194 $si_title = $utf8bom . bin2hex( $title );
195 $si_text = $utf8bom . bin2hex( $text );
196 $sql = "DELETE FROM $table WHERE si_page = $id;";
197 $sql .= "INSERT INTO $table (si_page, si_title, si_text) VALUES ($id, $si_title, $si_text)";
198 return $dbr->query( $sql, 'SearchMssql::update' );
202 * Update a search index record's title only.
203 * Title should be pre-processed.
206 * @param string $title
207 * @return bool|IResultWrapper
209 function updateTitle( $id, $title ) {
210 $dbr = $this->lb
->getMaintenanceConnectionRef( DB_MASTER
);
211 $table = $dbr->tableName( 'searchindex' );
213 // see update for why we are using the utf8bom
214 $utf8bom = '0xEFBBBF';
215 $si_title = $utf8bom . bin2hex( $title );
216 $sql = "DELETE FROM $table WHERE si_page = $id;";
217 $sql .= "INSERT INTO $table (si_page, si_title, si_text) VALUES ($id, $si_title, 0x00)";
218 return $dbr->query( $sql, 'SearchMssql::updateTitle' );