wfMsgForContentNoTrans() was removed
[mediawiki.git] / includes / search / SearchMssql.php
blob0d7970de38c2002f42c69c452af94ce5e419b01d
1 <?php
2 /**
3 * Mssql search engine
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
20 * @file
21 * @ingroup Search
24 /**
25 * Search engine hook base class for Mssql (ConText).
26 * @ingroup Search
28 class SearchMssql extends SearchDatabase {
29 /**
30 * Perform a full text search query and return a result set.
32 * @param string $term Raw search term
33 * @return SqlSearchResultSet
34 * @access public
36 function searchText( $term ) {
37 $resultSet = $this->db->query( $this->getQuery( $this->filter( $term ), true ) );
38 return new SqlSearchResultSet( $resultSet, $this->searchTerms );
41 /**
42 * Perform a title-only search query and return a result set.
44 * @param string $term Raw search term
45 * @return SqlSearchResultSet
46 * @access public
48 function searchTitle( $term ) {
49 $resultSet = $this->db->query( $this->getQuery( $this->filter( $term ), false ) );
50 return new SqlSearchResultSet( $resultSet, $this->searchTerms );
53 /**
54 * Return a partial WHERE clause to limit the search to the given namespaces
56 * @return string
57 * @private
59 function queryNamespaces() {
60 $namespaces = implode( ',', $this->namespaces );
61 if ( $namespaces == '' ) {
62 $namespaces = '0';
64 return 'AND page_namespace IN (' . $namespaces . ')';
67 /**
68 * Return a LIMIT clause to limit results on the query.
70 * @param string $sql
72 * @return string
74 function queryLimit( $sql ) {
75 return $this->db->limitResult( $sql, $this->limit, $this->offset );
78 /**
79 * Does not do anything for generic search engine
80 * subclasses may define this though
82 * @param string $filteredTerm
83 * @param bool $fulltext
84 * @return string
86 function queryRanking( $filteredTerm, $fulltext ) {
87 return ' ORDER BY ftindex.[RANK] DESC'; // return ' ORDER BY score(1)';
90 /**
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
96 * @return string
98 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
108 * @return string
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
119 * @return string
120 * @private
122 function queryMain( $filteredTerm, $fulltext ) {
123 $match = $this->parseQuery( $filteredTerm, $fulltext );
124 $page = $this->db->tableName( 'page' );
125 $searchindex = $this->db->tableName( 'searchindex' );
127 return 'SELECT page_id, page_namespace, page_title, ftindex.[RANK]' .
128 "FROM $page,FREETEXTTABLE($searchindex , $match, LANGUAGE 'English') as ftindex " .
129 'WHERE page_id=ftindex.[KEY] ';
132 /** @todo document
133 * @param string $filteredText
134 * @param bool $fulltext
135 * @return string
137 function parseQuery( $filteredText, $fulltext ) {
138 global $wgContLang;
139 $lc = $this->legalSearchChars();
140 $this->searchTerms = array();
142 # @todo FIXME: This doesn't handle parenthetical expressions.
143 $m = array();
144 $q = array();
146 if ( preg_match_all( '/([-+<>~]?)(([' . $lc . ']+)(\*?)|"[^"]*")/',
147 $filteredText, $m, PREG_SET_ORDER ) ) {
148 foreach ( $m as $terms ) {
149 $q[] = $terms[1] . $wgContLang->normalizeForSearch( $terms[2] );
151 if ( !empty( $terms[3] ) ) {
152 $regexp = preg_quote( $terms[3], '/' );
153 if ( $terms[4] ) {
154 $regexp .= "[0-9A-Za-z_]+";
156 } else {
157 $regexp = preg_quote( str_replace( '"', '', $terms[2] ), '/' );
159 $this->searchTerms[] = $regexp;
163 $searchon = $this->db->addQuotes( join( ',', $q ) );
164 $field = $this->getIndexField( $fulltext );
165 return "$field, $searchon";
169 * Create or update the search index record for the given page.
170 * Title and text should be pre-processed.
172 * @param int $id
173 * @param string $title
174 * @param string $text
175 * @return bool|ResultWrapper
177 function update( $id, $title, $text ) {
178 // We store the column data as UTF-8 byte order marked binary stream
179 // because we are invoking the plain text IFilter on it so that, and we want it
180 // to properly decode the stream as UTF-8. SQL doesn't support UTF8 as a data type
181 // but the indexer will correctly handle it by this method. Since all we are doing
182 // is passing this data to the indexer and never retrieving it via PHP, this will save space
183 $table = $this->db->tableName( 'searchindex' );
184 $utf8bom = '0xEFBBBF';
185 $si_title = $utf8bom . bin2hex( $title );
186 $si_text = $utf8bom . bin2hex( $text );
187 $sql = "DELETE FROM $table WHERE si_page = $id;";
188 $sql .= "INSERT INTO $table (si_page, si_title, si_text) VALUES ($id, $si_title, $si_text)";
189 return $this->db->query( $sql, 'SearchMssql::update' );
193 * Update a search index record's title only.
194 * Title should be pre-processed.
196 * @param int $id
197 * @param string $title
198 * @return bool|ResultWrapper
200 function updateTitle( $id, $title ) {
201 $table = $this->db->tableName( 'searchindex' );
203 // see update for why we are using the utf8bom
204 $utf8bom = '0xEFBBBF';
205 $si_title = $utf8bom . bin2hex( $title );
206 $sql = "DELETE FROM $table WHERE si_page = $id;";
207 $sql .= "INSERT INTO $table (si_page, si_title, si_text) VALUES ($id, $si_title, 0x00)";
208 return $this->db->query( $sql, 'SearchMssql::updateTitle' );