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
{
33 private $reservedWords = array(
63 * Perform a full text search query and return a result set.
65 * @param string $term raw search term
66 * @return SqlSearchResultSet
68 function searchText( $term ) {
70 return new SqlSearchResultSet( false, '' );
73 $resultSet = $this->db
->resultObject( $this->db
->query( $this->getQuery( $this->filter( $term ), true ) ) );
74 return new SqlSearchResultSet( $resultSet, $this->searchTerms
);
78 * Perform a title-only search query and return a result set.
80 * @param string $term raw search term
81 * @return SqlSearchResultSet
83 function searchTitle( $term ) {
85 return new SqlSearchResultSet( false, '' );
88 $resultSet = $this->db
->resultObject( $this->db
->query( $this->getQuery( $this->filter( $term ), false ) ) );
89 return new MySQLSearchResultSet( $resultSet, $this->searchTerms
);
93 * Return a partial WHERE clause to exclude redirects, if so set
96 function queryRedirect() {
97 if ( $this->showRedirects
) {
100 return 'AND page_is_redirect=0';
105 * Return a partial WHERE clause to limit the search to the given namespaces
108 function queryNamespaces() {
109 if ( is_null( $this->namespaces
) ) {
112 if ( !count( $this->namespaces
) ) {
115 $namespaces = $this->db
->makeList( $this->namespaces
);
117 return 'AND page_namespace IN (' . $namespaces . ')';
121 * Return a LIMIT clause to limit results on the query.
127 function queryLimit( $sql ) {
128 return $this->db
->limitResult( $sql, $this->limit
, $this->offset
);
132 * Does not do anything for generic search engine
133 * subclasses may define this though
137 function queryRanking( $filteredTerm, $fulltext ) {
138 return ' ORDER BY score(1)';
142 * Construct the full SQL query to do the search.
143 * The guts shoulds be constructed in queryMain()
144 * @param $filteredTerm String
145 * @param $fulltext Boolean
148 function getQuery( $filteredTerm, $fulltext ) {
149 return $this->queryLimit( $this->queryMain( $filteredTerm, $fulltext ) . ' ' .
150 $this->queryRedirect() . ' ' .
151 $this->queryNamespaces() . ' ' .
152 $this->queryRanking( $filteredTerm, $fulltext ) . ' ' );
156 * Picks which field to index on, depending on what type of query.
157 * @param $fulltext Boolean
160 function getIndexField( $fulltext ) {
161 return $fulltext ?
'si_text' : 'si_title';
165 * Get the base part of the search query.
167 * @param $filteredTerm String
168 * @param $fulltext Boolean
171 function queryMain( $filteredTerm, $fulltext ) {
172 $match = $this->parseQuery( $filteredTerm, $fulltext );
173 $page = $this->db
->tableName( 'page' );
174 $searchindex = $this->db
->tableName( 'searchindex' );
175 return 'SELECT page_id, page_namespace, page_title ' .
176 "FROM $page,$searchindex " .
177 'WHERE page_id=si_page AND ' . $match;
181 * Parse a user input search string, and return an SQL fragment to be used
182 * as part of a WHERE clause
185 function parseQuery( $filteredText, $fulltext ) {
187 $lc = SearchEngine
::legalSearchChars();
188 $this->searchTerms
= array();
190 # @todo FIXME: This doesn't handle parenthetical expressions.
193 if ( preg_match_all( '/([-+<>~]?)(([' . $lc . ']+)(\*?)|"[^"]*")/',
194 $filteredText, $m, PREG_SET_ORDER
) ) {
195 foreach ( $m as $terms ) {
196 // Search terms in all variant forms, only
197 // apply on wiki with LanguageConverter
198 $temp_terms = $wgContLang->autoConvertToAllVariants( $terms[2] );
199 if ( is_array( $temp_terms ) ) {
200 $temp_terms = array_unique( array_values( $temp_terms ) );
201 foreach ( $temp_terms as $t ) {
202 $searchon .= ( $terms[1] == '-' ?
' ~' : ' & ' ) . $this->escapeTerm( $t );
206 $searchon .= ( $terms[1] == '-' ?
' ~' : ' & ' ) . $this->escapeTerm( $terms[2] );
208 if ( !empty( $terms[3] ) ) {
209 $regexp = preg_quote( $terms[3], '/' );
211 $regexp .= "[0-9A-Za-z_]+";
214 $regexp = preg_quote( str_replace( '"', '', $terms[2] ), '/' );
216 $this->searchTerms
[] = $regexp;
220 $searchon = $this->db
->addQuotes( ltrim( $searchon, ' &' ) );
221 $field = $this->getIndexField( $fulltext );
222 return " CONTAINS($field, $searchon, 1) > 0 ";
225 private function escapeTerm( $t ) {
227 $t = $wgContLang->normalizeForSearch( $t );
228 $t = isset( $this->reservedWords
[strtoupper( $t )] ) ?
'{' . $t . '}' : $t;
229 $t = preg_replace( '/^"(.*)"$/', '($1)', $t );
230 $t = preg_replace( '/([-&|])/', '\\\\$1', $t );
235 * Create or update the search index record for the given page.
236 * Title and text should be pre-processed.
239 * @param $title String
240 * @param $text String
242 function update( $id, $title, $text ) {
243 $dbw = wfGetDB( DB_MASTER
);
244 $dbw->replace( 'searchindex',
248 'si_title' => $title,
250 ), 'SearchOracle::update' );
253 // We need to specify the DB name (i.e. user/schema) here so that
254 // it can work from the installer, where
255 // ALTER SESSION SET CURRENT_SCHEMA = ...
257 $dbw->query( "CALL ctx_ddl.sync_index(" .
258 $dbw->addQuotes( $dbw->getDBname() . '.' . $dbw->tableName( 'si_text_idx', 'raw' ) ) . ")" );
259 $dbw->query( "CALL ctx_ddl.sync_index(" .
260 $dbw->addQuotes( $dbw->getDBname() . '.' . $dbw->tableName( 'si_title_idx', 'raw' ) ) . ")" );
264 * Update a search index record's title only.
265 * Title should be pre-processed.
268 * @param $title String
270 function updateTitle( $id, $title ) {
271 $dbw = wfGetDB( DB_MASTER
);
273 $dbw->update( 'searchindex',
274 array( 'si_title' => $title ),
275 array( 'si_page' => $id ),
276 'SearchOracle::updateTitle',
280 public static function legalSearchChars() {
281 return "\"" . parent
::legalSearchChars();