5 * Copyright © 2004 Brion Vibber <brion@pobox.com>
6 * http://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 SearchEngine
{
33 private $reservedWords = array(
63 * Creates an instance of this class
64 * @param $db DatabasePostgres: database object
66 function __construct( $db ) {
67 parent
::__construct( $db );
71 * Perform a full text search query and return a result set.
73 * @param string $term raw search term
74 * @return SqlSearchResultSet
76 function searchText( $term ) {
78 return new SqlSearchResultSet( false, '' );
81 $resultSet = $this->db
->resultObject( $this->db
->query( $this->getQuery( $this->filter( $term ), true ) ) );
82 return new SqlSearchResultSet( $resultSet, $this->searchTerms
);
86 * Perform a title-only search query and return a result set.
88 * @param string $term raw search term
89 * @return SqlSearchResultSet
91 function searchTitle( $term ) {
93 return new SqlSearchResultSet( false, '' );
96 $resultSet = $this->db
->resultObject( $this->db
->query( $this->getQuery( $this->filter( $term ), false ) ) );
97 return new MySQLSearchResultSet( $resultSet, $this->searchTerms
);
101 * Return a partial WHERE clause to exclude redirects, if so set
104 function queryRedirect() {
105 if ( $this->showRedirects
) {
108 return 'AND page_is_redirect=0';
113 * Return a partial WHERE clause to limit the search to the given namespaces
116 function queryNamespaces() {
117 if ( is_null( $this->namespaces
) ) {
120 if ( !count( $this->namespaces
) ) {
123 $namespaces = $this->db
->makeList( $this->namespaces
);
125 return 'AND page_namespace IN (' . $namespaces . ')';
129 * Return a LIMIT clause to limit results on the query.
135 function queryLimit( $sql ) {
136 return $this->db
->limitResult( $sql, $this->limit
, $this->offset
);
140 * Does not do anything for generic search engine
141 * subclasses may define this though
145 function queryRanking( $filteredTerm, $fulltext ) {
146 return ' ORDER BY score(1)';
150 * Construct the full SQL query to do the search.
151 * The guts shoulds be constructed in queryMain()
152 * @param $filteredTerm String
153 * @param $fulltext Boolean
156 function getQuery( $filteredTerm, $fulltext ) {
157 return $this->queryLimit( $this->queryMain( $filteredTerm, $fulltext ) . ' ' .
158 $this->queryRedirect() . ' ' .
159 $this->queryNamespaces() . ' ' .
160 $this->queryRanking( $filteredTerm, $fulltext ) . ' ' );
164 * Picks which field to index on, depending on what type of query.
165 * @param $fulltext Boolean
168 function getIndexField( $fulltext ) {
169 return $fulltext ?
'si_text' : 'si_title';
173 * Get the base part of the search query.
175 * @param $filteredTerm String
176 * @param $fulltext Boolean
179 function queryMain( $filteredTerm, $fulltext ) {
180 $match = $this->parseQuery( $filteredTerm, $fulltext );
181 $page = $this->db
->tableName( 'page' );
182 $searchindex = $this->db
->tableName( 'searchindex' );
183 return 'SELECT page_id, page_namespace, page_title ' .
184 "FROM $page,$searchindex " .
185 'WHERE page_id=si_page AND ' . $match;
189 * Parse a user input search string, and return an SQL fragment to be used
190 * as part of a WHERE clause
193 function parseQuery( $filteredText, $fulltext ) {
195 $lc = SearchEngine
::legalSearchChars();
196 $this->searchTerms
= array();
198 # @todo FIXME: This doesn't handle parenthetical expressions.
201 if ( preg_match_all( '/([-+<>~]?)(([' . $lc . ']+)(\*?)|"[^"]*")/',
202 $filteredText, $m, PREG_SET_ORDER
) ) {
203 foreach ( $m as $terms ) {
204 // Search terms in all variant forms, only
205 // apply on wiki with LanguageConverter
206 $temp_terms = $wgContLang->autoConvertToAllVariants( $terms[2] );
207 if ( is_array( $temp_terms ) ) {
208 $temp_terms = array_unique( array_values( $temp_terms ) );
209 foreach ( $temp_terms as $t ) {
210 $searchon .= ( $terms[1] == '-' ?
' ~' : ' & ' ) . $this->escapeTerm( $t );
214 $searchon .= ( $terms[1] == '-' ?
' ~' : ' & ' ) . $this->escapeTerm( $terms[2] );
216 if ( !empty( $terms[3] ) ) {
217 $regexp = preg_quote( $terms[3], '/' );
219 $regexp .= "[0-9A-Za-z_]+";
222 $regexp = preg_quote( str_replace( '"', '', $terms[2] ), '/' );
224 $this->searchTerms
[] = $regexp;
228 $searchon = $this->db
->addQuotes( ltrim( $searchon, ' &' ) );
229 $field = $this->getIndexField( $fulltext );
230 return " CONTAINS($field, $searchon, 1) > 0 ";
233 private function escapeTerm( $t ) {
235 $t = $wgContLang->normalizeForSearch( $t );
236 $t = isset( $this->reservedWords
[strtoupper( $t )] ) ?
'{' . $t . '}' : $t;
237 $t = preg_replace( '/^"(.*)"$/', '($1)', $t );
238 $t = preg_replace( '/([-&|])/', '\\\\$1', $t );
242 * Create or update the search index record for the given page.
243 * Title and text should be pre-processed.
246 * @param $title String
247 * @param $text String
249 function update( $id, $title, $text ) {
250 $dbw = wfGetDB( DB_MASTER
);
251 $dbw->replace( 'searchindex',
255 'si_title' => $title,
257 ), 'SearchOracle::update' );
260 // We need to specify the DB name (i.e. user/schema) here so that
261 // it can work from the installer, where
262 // ALTER SESSION SET CURRENT_SCHEMA = ...
264 $dbw->query( "CALL ctx_ddl.sync_index(" .
265 $dbw->addQuotes( $dbw->getDBname() . '.' . $dbw->tableName( 'si_text_idx', 'raw' ) ) . ")" );
266 $dbw->query( "CALL ctx_ddl.sync_index(" .
267 $dbw->addQuotes( $dbw->getDBname() . '.' . $dbw->tableName( 'si_title_idx', 'raw' ) ) . ")" );
271 * Update a search index record's title only.
272 * Title should be pre-processed.
275 * @param $title String
277 function updateTitle( $id, $title ) {
278 $dbw = wfGetDB( DB_MASTER
);
280 $dbw->update( 'searchindex',
281 array( 'si_title' => $title ),
282 array( 'si_page' => $id ),
283 'SearchOracle::updateTitle',
287 public static function legalSearchChars() {
288 return "\"" . parent
::legalSearchChars();