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, '' );
80 $resultSet = $this->db
->resultObject( $this->db
->query( $this->getQuery( $this->filter( $term ), true ) ) );
81 return new SqlSearchResultSet( $resultSet, $this->searchTerms
);
85 * Perform a title-only search query and return a result set.
87 * @param string $term raw search term
88 * @return SqlSearchResultSet
90 function searchTitle( $term ) {
92 return new SqlSearchResultSet( false, '' );
94 $resultSet = $this->db
->resultObject( $this->db
->query( $this->getQuery( $this->filter( $term ), false ) ) );
95 return new MySQLSearchResultSet( $resultSet, $this->searchTerms
);
99 * Return a partial WHERE clause to exclude redirects, if so set
102 function queryRedirect() {
103 if ( $this->showRedirects
) {
106 return 'AND page_is_redirect=0';
111 * Return a partial WHERE clause to limit the search to the given namespaces
114 function queryNamespaces() {
115 if( is_null( $this->namespaces
) )
117 if ( !count( $this->namespaces
) ) {
120 $namespaces = $this->db
->makeList( $this->namespaces
);
122 return 'AND page_namespace IN (' . $namespaces . ')';
126 * Return a LIMIT clause to limit results on the query.
132 function queryLimit( $sql ) {
133 return $this->db
->limitResult( $sql, $this->limit
, $this->offset
);
137 * Does not do anything for generic search engine
138 * subclasses may define this though
142 function queryRanking( $filteredTerm, $fulltext ) {
143 return ' ORDER BY score(1)';
147 * Construct the full SQL query to do the search.
148 * The guts shoulds be constructed in queryMain()
149 * @param $filteredTerm String
150 * @param $fulltext Boolean
153 function getQuery( $filteredTerm, $fulltext ) {
154 return $this->queryLimit( $this->queryMain( $filteredTerm, $fulltext ) . ' ' .
155 $this->queryRedirect() . ' ' .
156 $this->queryNamespaces() . ' ' .
157 $this->queryRanking( $filteredTerm, $fulltext ) . ' ' );
161 * Picks which field to index on, depending on what type of query.
162 * @param $fulltext Boolean
165 function getIndexField( $fulltext ) {
166 return $fulltext ?
'si_text' : 'si_title';
170 * Get the base part of the search query.
172 * @param $filteredTerm String
173 * @param $fulltext Boolean
176 function queryMain( $filteredTerm, $fulltext ) {
177 $match = $this->parseQuery( $filteredTerm, $fulltext );
178 $page = $this->db
->tableName( 'page' );
179 $searchindex = $this->db
->tableName( 'searchindex' );
180 return 'SELECT page_id, page_namespace, page_title ' .
181 "FROM $page,$searchindex " .
182 'WHERE page_id=si_page AND ' . $match;
186 * Parse a user input search string, and return an SQL fragment to be used
187 * as part of a WHERE clause
190 function parseQuery( $filteredText, $fulltext ) {
192 $lc = SearchEngine
::legalSearchChars();
193 $this->searchTerms
= array();
195 # @todo FIXME: This doesn't handle parenthetical expressions.
198 if ( preg_match_all( '/([-+<>~]?)(([' . $lc . ']+)(\*?)|"[^"]*")/',
199 $filteredText, $m, PREG_SET_ORDER
) ) {
200 foreach( $m as $terms ) {
201 // Search terms in all variant forms, only
202 // apply on wiki with LanguageConverter
203 $temp_terms = $wgContLang->autoConvertToAllVariants( $terms[2] );
204 if( is_array( $temp_terms ) ) {
205 $temp_terms = array_unique( array_values( $temp_terms ) );
206 foreach( $temp_terms as $t ) {
207 $searchon .= ($terms[1] == '-' ?
' ~' : ' & ') . $this->escapeTerm( $t );
211 $searchon .= ($terms[1] == '-' ?
' ~' : ' & ') . $this->escapeTerm( $terms[2] );
213 if ( !empty( $terms[3] ) ) {
214 $regexp = preg_quote( $terms[3], '/' );
216 $regexp .= "[0-9A-Za-z_]+";
218 $regexp = preg_quote( str_replace( '"', '', $terms[2] ), '/' );
220 $this->searchTerms
[] = $regexp;
224 $searchon = $this->db
->addQuotes( ltrim( $searchon, ' &' ) );
225 $field = $this->getIndexField( $fulltext );
226 return " CONTAINS($field, $searchon, 1) > 0 ";
229 private function escapeTerm( $t ) {
231 $t = $wgContLang->normalizeForSearch( $t );
232 $t = isset( $this->reservedWords
[strtoupper( $t )] ) ?
'{' . $t . '}' : $t;
233 $t = preg_replace( '/^"(.*)"$/', '($1)', $t );
234 $t = preg_replace( '/([-&|])/', '\\\\$1', $t );
238 * Create or update the search index record for the given page.
239 * Title and text should be pre-processed.
242 * @param $title String
243 * @param $text String
245 function update( $id, $title, $text ) {
246 $dbw = wfGetDB( DB_MASTER
);
247 $dbw->replace( 'searchindex',
251 'si_title' => $title,
253 ), 'SearchOracle::update' );
256 // We need to specify the DB name (i.e. user/schema) here so that
257 // it can work from the installer, where
258 // ALTER SESSION SET CURRENT_SCHEMA = ...
260 $dbw->query( "CALL ctx_ddl.sync_index(" .
261 $dbw->addQuotes( $dbw->getDBname() . '.' . $dbw->tableName( 'si_text_idx', 'raw' ) ) . ")" );
262 $dbw->query( "CALL ctx_ddl.sync_index(" .
263 $dbw->addQuotes( $dbw->getDBname() . '.' . $dbw->tableName( 'si_title_idx', 'raw' ) ) . ")" );
267 * Update a search index record's title only.
268 * Title should be pre-processed.
271 * @param $title String
273 function updateTitle( $id, $title ) {
274 $dbw = wfGetDB( DB_MASTER
);
276 $dbw->update( 'searchindex',
277 array( 'si_title' => $title ),
278 array( 'si_page' => $id ),
279 'SearchOracle::updateTitle',
283 public static function legalSearchChars() {
284 return "\"" . parent
::legalSearchChars();