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
->resultObject(
73 $this->db
->query( $this->getQuery( $this->filter( $term ), true ) )
76 return new SqlSearchResultSet( $resultSet, $this->searchTerms
);
80 * Perform a title-only search query and return a result set.
82 * @param string $term Raw search term
83 * @return SqlSearchResultSet
85 function searchTitle( $term ) {
87 return new SqlSearchResultSet( false, '' );
90 $resultSet = $this->db
->resultObject(
91 $this->db
->query( $this->getQuery( $this->filter( $term ), false ) )
94 return new SqlSearchResultSet( $resultSet, $this->searchTerms
);
98 * Return a partial WHERE clause to limit the search to the given namespaces
101 function queryNamespaces() {
102 if ( is_null( $this->namespaces
) ) {
105 if ( !count( $this->namespaces
) ) {
108 $namespaces = $this->db
->makeList( $this->namespaces
);
110 return 'AND page_namespace IN (' . $namespaces . ')';
114 * Return a LIMIT clause to limit results on the query.
120 function queryLimit( $sql ) {
121 return $this->db
->limitResult( $sql, $this->limit
, $this->offset
);
125 * Does not do anything for generic search engine
126 * subclasses may define this though
130 function queryRanking( $filteredTerm, $fulltext ) {
131 return ' ORDER BY score(1)';
135 * Construct the full SQL query to do the search.
136 * The guts shoulds be constructed in queryMain()
137 * @param string $filteredTerm
138 * @param bool $fulltext
141 function getQuery( $filteredTerm, $fulltext ) {
142 return $this->queryLimit( $this->queryMain( $filteredTerm, $fulltext ) . ' ' .
143 $this->queryNamespaces() . ' ' .
144 $this->queryRanking( $filteredTerm, $fulltext ) . ' ' );
148 * Picks which field to index on, depending on what type of query.
149 * @param bool $fulltext
152 function getIndexField( $fulltext ) {
153 return $fulltext ?
'si_text' : 'si_title';
157 * Get the base part of the search query.
159 * @param string $filteredTerm
160 * @param bool $fulltext
163 function queryMain( $filteredTerm, $fulltext ) {
164 $match = $this->parseQuery( $filteredTerm, $fulltext );
165 $page = $this->db
->tableName( 'page' );
166 $searchindex = $this->db
->tableName( 'searchindex' );
167 return 'SELECT page_id, page_namespace, page_title ' .
168 "FROM $page,$searchindex " .
169 'WHERE page_id=si_page AND ' . $match;
173 * Parse a user input search string, and return an SQL fragment to be used
174 * as part of a WHERE clause
177 function parseQuery( $filteredText, $fulltext ) {
179 $lc = $this->legalSearchChars();
180 $this->searchTerms
= array();
182 # @todo FIXME: This doesn't handle parenthetical expressions.
185 if ( preg_match_all( '/([-+<>~]?)(([' . $lc . ']+)(\*?)|"[^"]*")/',
186 $filteredText, $m, PREG_SET_ORDER
) ) {
187 foreach ( $m as $terms ) {
188 // Search terms in all variant forms, only
189 // apply on wiki with LanguageConverter
190 $temp_terms = $wgContLang->autoConvertToAllVariants( $terms[2] );
191 if ( is_array( $temp_terms ) ) {
192 $temp_terms = array_unique( array_values( $temp_terms ) );
193 foreach ( $temp_terms as $t ) {
194 $searchon .= ( $terms[1] == '-' ?
' ~' : ' & ' ) . $this->escapeTerm( $t );
198 $searchon .= ( $terms[1] == '-' ?
' ~' : ' & ' ) . $this->escapeTerm( $terms[2] );
200 if ( !empty( $terms[3] ) ) {
201 $regexp = preg_quote( $terms[3], '/' );
203 $regexp .= "[0-9A-Za-z_]+";
206 $regexp = preg_quote( str_replace( '"', '', $terms[2] ), '/' );
208 $this->searchTerms
[] = $regexp;
212 $searchon = $this->db
->addQuotes( ltrim( $searchon, ' &' ) );
213 $field = $this->getIndexField( $fulltext );
214 return " CONTAINS($field, $searchon, 1) > 0 ";
217 private function escapeTerm( $t ) {
219 $t = $wgContLang->normalizeForSearch( $t );
220 $t = isset( $this->reservedWords
[strtoupper( $t )] ) ?
'{' . $t . '}' : $t;
221 $t = preg_replace( '/^"(.*)"$/', '($1)', $t );
222 $t = preg_replace( '/([-&|])/', '\\\\$1', $t );
227 * Create or update the search index record for the given page.
228 * Title and text should be pre-processed.
231 * @param string $title
232 * @param string $text
234 function update( $id, $title, $text ) {
235 $dbw = wfGetDB( DB_MASTER
);
236 $dbw->replace( 'searchindex',
240 'si_title' => $title,
242 ), 'SearchOracle::update' );
245 // We need to specify the DB name (i.e. user/schema) here so that
246 // it can work from the installer, where
247 // ALTER SESSION SET CURRENT_SCHEMA = ...
249 $dbw->query( "CALL ctx_ddl.sync_index(" .
250 $dbw->addQuotes( $dbw->getDBname() . '.' . $dbw->tableName( 'si_text_idx', 'raw' ) ) . ")" );
251 $dbw->query( "CALL ctx_ddl.sync_index(" .
252 $dbw->addQuotes( $dbw->getDBname() . '.' . $dbw->tableName( 'si_title_idx', 'raw' ) ) . ")" );
256 * Update a search index record's title only.
257 * Title should be pre-processed.
260 * @param string $title
262 function updateTitle( $id, $title ) {
263 $dbw = wfGetDB( DB_MASTER
);
265 $dbw->update( 'searchindex',
266 array( 'si_title' => $title ),
267 array( 'si_page' => $id ),
268 'SearchOracle::updateTitle',
272 public static function legalSearchChars() {
273 return "\"" . parent
::legalSearchChars();