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 $resultSet = $this->db
->query( $this->getQuery( $this->filter( $term ), true ) );
40 return new SqlSearchResultSet( $resultSet, $this->searchTerms
);
44 * Perform a title-only search query and return a result set.
46 * @param string $term Raw search term
47 * @return SqlSearchResultSet
49 protected function doSearchTitleInDB( $term ) {
50 $resultSet = $this->db
->query( $this->getQuery( $this->filter( $term ), false ) );
51 return new SqlSearchResultSet( $resultSet, $this->searchTerms
);
55 * Return a partial WHERE clause to limit the search to the given namespaces
59 private function queryNamespaces() {
60 $namespaces = implode( ',', $this->namespaces
);
61 if ( $namespaces == '' ) {
64 return 'AND page_namespace IN (' . $namespaces . ')';
68 * Return a LIMIT clause to limit results on the query.
74 private function queryLimit( $sql ) {
75 return $this->db
->limitResult( $sql, $this->limit
, $this->offset
);
79 * Does not do anything for generic search engine
80 * subclasses may define this though
82 * @param string $filteredTerm
83 * @param bool $fulltext
86 function queryRanking( $filteredTerm, $fulltext ) {
87 return ' ORDER BY ftindex.[RANK] DESC'; // return ' ORDER BY score(1)';
91 * Construct the full SQL query to do the search.
92 * The guts shoulds be constructed in queryMain()
94 * @param string $filteredTerm
95 * @param bool $fulltext
98 private function getQuery( $filteredTerm, $fulltext ) {
99 return $this->queryLimit( $this->queryMain( $filteredTerm, $fulltext ) . ' ' .
100 $this->queryNamespaces() . ' ' .
101 $this->queryRanking( $filteredTerm, $fulltext ) . ' ' );
105 * Picks which field to index on, depending on what type of query.
107 * @param bool $fulltext
110 function getIndexField( $fulltext ) {
111 return $fulltext ?
'si_text' : 'si_title';
115 * Get the base part of the search query.
117 * @param string $filteredTerm
118 * @param bool $fulltext
121 private function queryMain( $filteredTerm, $fulltext ) {
122 $match = $this->parseQuery( $filteredTerm, $fulltext );
123 $page = $this->db
->tableName( 'page' );
124 $searchindex = $this->db
->tableName( 'searchindex' );
126 return 'SELECT page_id, page_namespace, page_title, ftindex.[RANK]' .
127 "FROM $page,FREETEXTTABLE($searchindex , $match, LANGUAGE 'English') as ftindex " .
128 'WHERE page_id=ftindex.[KEY] ';
132 * @param string $filteredText
133 * @param bool $fulltext
136 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] . MediaWikiServices
::getInstance()->getContentLanguage()->
148 normalizeForSearch( $terms[2] );
150 if ( !empty( $terms[3] ) ) {
151 $regexp = preg_quote( $terms[3], '/' );
153 $regexp .= "[0-9A-Za-z_]+";
156 $regexp = preg_quote( str_replace( '"', '', $terms[2] ), '/' );
158 $this->searchTerms
[] = $regexp;
162 $searchon = $this->db
->addQuotes( implode( ',', $q ) );
163 $field = $this->getIndexField( $fulltext );
164 return "$field, $searchon";
168 * Create or update the search index record for the given page.
169 * Title and text should be pre-processed.
172 * @param string $title
173 * @param string $text
174 * @return bool|IResultWrapper
176 function update( $id, $title, $text ) {
177 // We store the column data as UTF-8 byte order marked binary stream
178 // because we are invoking the plain text IFilter on it so that, and we want it
179 // to properly decode the stream as UTF-8. SQL doesn't support UTF8 as a data type
180 // but the indexer will correctly handle it by this method. Since all we are doing
181 // is passing this data to the indexer and never retrieving it via PHP, this will save space
182 $table = $this->db
->tableName( 'searchindex' );
183 $utf8bom = '0xEFBBBF';
184 $si_title = $utf8bom . bin2hex( $title );
185 $si_text = $utf8bom . bin2hex( $text );
186 $sql = "DELETE FROM $table WHERE si_page = $id;";
187 $sql .= "INSERT INTO $table (si_page, si_title, si_text) VALUES ($id, $si_title, $si_text)";
188 return $this->db
->query( $sql, 'SearchMssql::update' );
192 * Update a search index record's title only.
193 * Title should be pre-processed.
196 * @param string $title
197 * @return bool|IResultWrapper
199 function updateTitle( $id, $title ) {
200 $table = $this->db
->tableName( 'searchindex' );
202 // see update for why we are using the utf8bom
203 $utf8bom = '0xEFBBBF';
204 $si_title = $utf8bom . bin2hex( $title );
205 $sql = "DELETE FROM $table WHERE si_page = $id;";
206 $sql .= "INSERT INTO $table (si_page, si_title, si_text) VALUES ($id, $si_title, 0x00)";
207 return $this->db
->query( $sql, 'SearchMssql::updateTitle' );