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 SqlSearchResultSet( $resultSet, $this->searchTerms
);
93 * Return a partial WHERE clause to limit the search to the given namespaces
96 function queryNamespaces() {
97 if ( is_null( $this->namespaces
) ) {
100 if ( !count( $this->namespaces
) ) {
103 $namespaces = $this->db
->makeList( $this->namespaces
);
105 return 'AND page_namespace IN (' . $namespaces . ')';
109 * Return a LIMIT clause to limit results on the query.
115 function queryLimit( $sql ) {
116 return $this->db
->limitResult( $sql, $this->limit
, $this->offset
);
120 * Does not do anything for generic search engine
121 * subclasses may define this though
125 function queryRanking( $filteredTerm, $fulltext ) {
126 return ' ORDER BY score(1)';
130 * Construct the full SQL query to do the search.
131 * The guts shoulds be constructed in queryMain()
132 * @param string $filteredTerm
133 * @param bool $fulltext
136 function getQuery( $filteredTerm, $fulltext ) {
137 return $this->queryLimit( $this->queryMain( $filteredTerm, $fulltext ) . ' ' .
138 $this->queryNamespaces() . ' ' .
139 $this->queryRanking( $filteredTerm, $fulltext ) . ' ' );
143 * Picks which field to index on, depending on what type of query.
144 * @param bool $fulltext
147 function getIndexField( $fulltext ) {
148 return $fulltext ?
'si_text' : 'si_title';
152 * Get the base part of the search query.
154 * @param string $filteredTerm
155 * @param bool $fulltext
158 function queryMain( $filteredTerm, $fulltext ) {
159 $match = $this->parseQuery( $filteredTerm, $fulltext );
160 $page = $this->db
->tableName( 'page' );
161 $searchindex = $this->db
->tableName( 'searchindex' );
162 return 'SELECT page_id, page_namespace, page_title ' .
163 "FROM $page,$searchindex " .
164 'WHERE page_id=si_page AND ' . $match;
168 * Parse a user input search string, and return an SQL fragment to be used
169 * as part of a WHERE clause
172 function parseQuery( $filteredText, $fulltext ) {
174 $lc = SearchEngine
::legalSearchChars();
175 $this->searchTerms
= array();
177 # @todo FIXME: This doesn't handle parenthetical expressions.
180 if ( preg_match_all( '/([-+<>~]?)(([' . $lc . ']+)(\*?)|"[^"]*")/',
181 $filteredText, $m, PREG_SET_ORDER
) ) {
182 foreach ( $m as $terms ) {
183 // Search terms in all variant forms, only
184 // apply on wiki with LanguageConverter
185 $temp_terms = $wgContLang->autoConvertToAllVariants( $terms[2] );
186 if ( is_array( $temp_terms ) ) {
187 $temp_terms = array_unique( array_values( $temp_terms ) );
188 foreach ( $temp_terms as $t ) {
189 $searchon .= ( $terms[1] == '-' ?
' ~' : ' & ' ) . $this->escapeTerm( $t );
193 $searchon .= ( $terms[1] == '-' ?
' ~' : ' & ' ) . $this->escapeTerm( $terms[2] );
195 if ( !empty( $terms[3] ) ) {
196 $regexp = preg_quote( $terms[3], '/' );
198 $regexp .= "[0-9A-Za-z_]+";
201 $regexp = preg_quote( str_replace( '"', '', $terms[2] ), '/' );
203 $this->searchTerms
[] = $regexp;
207 $searchon = $this->db
->addQuotes( ltrim( $searchon, ' &' ) );
208 $field = $this->getIndexField( $fulltext );
209 return " CONTAINS($field, $searchon, 1) > 0 ";
212 private function escapeTerm( $t ) {
214 $t = $wgContLang->normalizeForSearch( $t );
215 $t = isset( $this->reservedWords
[strtoupper( $t )] ) ?
'{' . $t . '}' : $t;
216 $t = preg_replace( '/^"(.*)"$/', '($1)', $t );
217 $t = preg_replace( '/([-&|])/', '\\\\$1', $t );
222 * Create or update the search index record for the given page.
223 * Title and text should be pre-processed.
226 * @param string $title
227 * @param string $text
229 function update( $id, $title, $text ) {
230 $dbw = wfGetDB( DB_MASTER
);
231 $dbw->replace( 'searchindex',
235 'si_title' => $title,
237 ), 'SearchOracle::update' );
240 // We need to specify the DB name (i.e. user/schema) here so that
241 // it can work from the installer, where
242 // ALTER SESSION SET CURRENT_SCHEMA = ...
244 $dbw->query( "CALL ctx_ddl.sync_index(" .
245 $dbw->addQuotes( $dbw->getDBname() . '.' . $dbw->tableName( 'si_text_idx', 'raw' ) ) . ")" );
246 $dbw->query( "CALL ctx_ddl.sync_index(" .
247 $dbw->addQuotes( $dbw->getDBname() . '.' . $dbw->tableName( 'si_title_idx', 'raw' ) ) . ")" );
251 * Update a search index record's title only.
252 * Title should be pre-processed.
255 * @param string $title
257 function updateTitle( $id, $title ) {
258 $dbw = wfGetDB( DB_MASTER
);
260 $dbw->update( 'searchindex',
261 array( 'si_title' => $title ),
262 array( 'si_page' => $id ),
263 'SearchOracle::updateTitle',
267 public static function legalSearchChars() {
268 return "\"" . parent
::legalSearchChars();