5 * Copyright © 2004 Brion Vibber <brion@pobox.com>
6 * https://www.mediawiki.org/
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 * http://www.gnu.org/copyleft/gpl.html
28 * Search engine hook base class for Oracle (ConText).
31 class SearchOracle
extends SearchDatabase
{
32 private $reservedWords = array(
62 * Perform a full text search query and return a result set.
64 * @param string $term Raw search term
65 * @return SqlSearchResultSet
67 function searchText( $term ) {
69 return new SqlSearchResultSet( false, '' );
72 $resultSet = $this->db
->query( $this->getQuery( $this->filter( $term ), true ) );
73 return new SqlSearchResultSet( $resultSet, $this->searchTerms
);
77 * Perform a title-only search query and return a result set.
79 * @param string $term Raw search term
80 * @return SqlSearchResultSet
82 function searchTitle( $term ) {
84 return new SqlSearchResultSet( false, '' );
87 $resultSet = $this->db
->query( $this->getQuery( $this->filter( $term ), false ) );
88 return new SqlSearchResultSet( $resultSet, $this->searchTerms
);
92 * Return a partial WHERE clause to limit the search to the given namespaces
95 function queryNamespaces() {
96 if ( is_null( $this->namespaces
) ) {
99 if ( !count( $this->namespaces
) ) {
102 $namespaces = $this->db
->makeList( $this->namespaces
);
104 return 'AND page_namespace IN (' . $namespaces . ')';
108 * Return a LIMIT clause to limit results on the query.
114 function queryLimit( $sql ) {
115 return $this->db
->limitResult( $sql, $this->limit
, $this->offset
);
119 * Does not do anything for generic search engine
120 * subclasses may define this though
122 * @param string $filteredTerm
123 * @param bool $fulltext
126 function queryRanking( $filteredTerm, $fulltext ) {
127 return ' ORDER BY score(1)';
131 * Construct the full SQL query to do the search.
132 * The guts shoulds be constructed in queryMain()
133 * @param string $filteredTerm
134 * @param bool $fulltext
137 function getQuery( $filteredTerm, $fulltext ) {
138 return $this->queryLimit( $this->queryMain( $filteredTerm, $fulltext ) . ' ' .
139 $this->queryNamespaces() . ' ' .
140 $this->queryRanking( $filteredTerm, $fulltext ) . ' ' );
144 * Picks which field to index on, depending on what type of query.
145 * @param bool $fulltext
148 function getIndexField( $fulltext ) {
149 return $fulltext ?
'si_text' : 'si_title';
153 * Get the base part of the search query.
155 * @param string $filteredTerm
156 * @param bool $fulltext
159 function queryMain( $filteredTerm, $fulltext ) {
160 $match = $this->parseQuery( $filteredTerm, $fulltext );
161 $page = $this->db
->tableName( 'page' );
162 $searchindex = $this->db
->tableName( 'searchindex' );
163 return 'SELECT page_id, page_namespace, page_title ' .
164 "FROM $page,$searchindex " .
165 'WHERE page_id=si_page AND ' . $match;
169 * Parse a user input search string, and return an SQL fragment to be used
170 * as part of a WHERE clause
171 * @param string $filteredText
172 * @param bool $fulltext
175 function parseQuery( $filteredText, $fulltext ) {
177 $lc = $this->legalSearchChars();
178 $this->searchTerms
= array();
180 # @todo FIXME: This doesn't handle parenthetical expressions.
183 if ( preg_match_all( '/([-+<>~]?)(([' . $lc . ']+)(\*?)|"[^"]*")/',
184 $filteredText, $m, PREG_SET_ORDER
) ) {
185 foreach ( $m as $terms ) {
186 // Search terms in all variant forms, only
187 // apply on wiki with LanguageConverter
188 $temp_terms = $wgContLang->autoConvertToAllVariants( $terms[2] );
189 if ( is_array( $temp_terms ) ) {
190 $temp_terms = array_unique( array_values( $temp_terms ) );
191 foreach ( $temp_terms as $t ) {
192 $searchon .= ( $terms[1] == '-' ?
' ~' : ' & ' ) . $this->escapeTerm( $t );
195 $searchon .= ( $terms[1] == '-' ?
' ~' : ' & ' ) . $this->escapeTerm( $terms[2] );
197 if ( !empty( $terms[3] ) ) {
198 $regexp = preg_quote( $terms[3], '/' );
200 $regexp .= "[0-9A-Za-z_]+";
203 $regexp = preg_quote( str_replace( '"', '', $terms[2] ), '/' );
205 $this->searchTerms
[] = $regexp;
209 $searchon = $this->db
->addQuotes( ltrim( $searchon, ' &' ) );
210 $field = $this->getIndexField( $fulltext );
211 return " CONTAINS($field, $searchon, 1) > 0 ";
214 private function escapeTerm( $t ) {
216 $t = $wgContLang->normalizeForSearch( $t );
217 $t = isset( $this->reservedWords
[strtoupper( $t )] ) ?
'{' . $t . '}' : $t;
218 $t = preg_replace( '/^"(.*)"$/', '($1)', $t );
219 $t = preg_replace( '/([-&|])/', '\\\\$1', $t );
224 * Create or update the search index record for the given page.
225 * Title and text should be pre-processed.
228 * @param string $title
229 * @param string $text
231 function update( $id, $title, $text ) {
232 $dbw = wfGetDB( DB_MASTER
);
233 $dbw->replace( 'searchindex',
237 'si_title' => $title,
239 ), 'SearchOracle::update' );
242 // We need to specify the DB name (i.e. user/schema) here so that
243 // it can work from the installer, where
244 // ALTER SESSION SET CURRENT_SCHEMA = ...
246 $dbw->query( "CALL ctx_ddl.sync_index(" .
247 $dbw->addQuotes( $dbw->getDBname() . '.' . $dbw->tableName( 'si_text_idx', 'raw' ) ) . ")" );
248 $dbw->query( "CALL ctx_ddl.sync_index(" .
249 $dbw->addQuotes( $dbw->getDBname() . '.' . $dbw->tableName( 'si_title_idx', 'raw' ) ) . ")" );
253 * Update a search index record's title only.
254 * Title should be pre-processed.
257 * @param string $title
259 function updateTitle( $id, $title ) {
260 $dbw = wfGetDB( DB_MASTER
);
262 $dbw->update( 'searchindex',
263 array( 'si_title' => $title ),
264 array( 'si_page' => $id ),
265 'SearchOracle::updateTitle',
269 public static function legalSearchChars() {
270 return "\"" . parent
::legalSearchChars();