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 ('ABOUT' => 1,
61 * Creates an instance of this class
62 * @param $db DatabasePostgres: database object
64 function __construct( $db ) {
65 parent
::__construct( $db );
69 * Perform a full text search query and return a result set.
71 * @param string $term raw search term
72 * @return SqlSearchResultSet
74 function searchText( $term ) {
76 return new SqlSearchResultSet( false, '' );
78 $resultSet = $this->db
->resultObject( $this->db
->query( $this->getQuery( $this->filter( $term ), true ) ) );
79 return new SqlSearchResultSet( $resultSet, $this->searchTerms
);
83 * Perform a title-only search query and return a result set.
85 * @param string $term raw search term
86 * @return SqlSearchResultSet
88 function searchTitle( $term ) {
90 return new SqlSearchResultSet( false, '' );
92 $resultSet = $this->db
->resultObject( $this->db
->query( $this->getQuery( $this->filter( $term ), false ) ) );
93 return new MySQLSearchResultSet( $resultSet, $this->searchTerms
);
97 * Return a partial WHERE clause to exclude redirects, if so set
100 function queryRedirect() {
101 if ( $this->showRedirects
) {
104 return 'AND page_is_redirect=0';
109 * Return a partial WHERE clause to limit the search to the given namespaces
112 function queryNamespaces() {
113 if( is_null( $this->namespaces
) )
115 if ( !count( $this->namespaces
) ) {
118 $namespaces = $this->db
->makeList( $this->namespaces
);
120 return 'AND page_namespace IN (' . $namespaces . ')';
124 * Return a LIMIT clause to limit results on the query.
130 function queryLimit( $sql ) {
131 return $this->db
->limitResult( $sql, $this->limit
, $this->offset
);
135 * Does not do anything for generic search engine
136 * subclasses may define this though
140 function queryRanking( $filteredTerm, $fulltext ) {
141 return ' ORDER BY score(1)';
145 * Construct the full SQL query to do the search.
146 * The guts shoulds be constructed in queryMain()
147 * @param $filteredTerm String
148 * @param $fulltext Boolean
151 function getQuery( $filteredTerm, $fulltext ) {
152 return $this->queryLimit( $this->queryMain( $filteredTerm, $fulltext ) . ' ' .
153 $this->queryRedirect() . ' ' .
154 $this->queryNamespaces() . ' ' .
155 $this->queryRanking( $filteredTerm, $fulltext ) . ' ' );
159 * Picks which field to index on, depending on what type of query.
160 * @param $fulltext Boolean
163 function getIndexField( $fulltext ) {
164 return $fulltext ?
'si_text' : 'si_title';
168 * Get the base part of the search query.
170 * @param $filteredTerm String
171 * @param $fulltext Boolean
174 function queryMain( $filteredTerm, $fulltext ) {
175 $match = $this->parseQuery( $filteredTerm, $fulltext );
176 $page = $this->db
->tableName( 'page' );
177 $searchindex = $this->db
->tableName( 'searchindex' );
178 return 'SELECT page_id, page_namespace, page_title ' .
179 "FROM $page,$searchindex " .
180 'WHERE page_id=si_page AND ' . $match;
184 * Parse a user input search string, and return an SQL fragment to be used
185 * as part of a WHERE clause
188 function parseQuery( $filteredText, $fulltext ) {
190 $lc = SearchEngine
::legalSearchChars();
191 $this->searchTerms
= array();
193 # @todo FIXME: This doesn't handle parenthetical expressions.
196 if ( preg_match_all( '/([-+<>~]?)(([' . $lc . ']+)(\*?)|"[^"]*")/',
197 $filteredText, $m, PREG_SET_ORDER
) ) {
198 foreach( $m as $terms ) {
199 // Search terms in all variant forms, only
200 // apply on wiki with LanguageConverter
201 $temp_terms = $wgContLang->autoConvertToAllVariants( $terms[2] );
202 if( is_array( $temp_terms )) {
203 $temp_terms = array_unique( array_values( $temp_terms ));
204 foreach( $temp_terms as $t ) {
205 $searchon .= ($terms[1] == '-' ?
' ~' : ' & ') . $this->escapeTerm( $t );
209 $searchon .= ($terms[1] == '-' ?
' ~' : ' & ') . $this->escapeTerm( $terms[2] );
211 if ( !empty( $terms[3] ) ) {
212 $regexp = preg_quote( $terms[3], '/' );
214 $regexp .= "[0-9A-Za-z_]+";
216 $regexp = preg_quote( str_replace( '"', '', $terms[2] ), '/' );
218 $this->searchTerms
[] = $regexp;
222 $searchon = $this->db
->addQuotes( ltrim( $searchon, ' &' ) );
223 $field = $this->getIndexField( $fulltext );
224 return " CONTAINS($field, $searchon, 1) > 0 ";
227 private function escapeTerm( $t ) {
229 $t = $wgContLang->normalizeForSearch( $t );
230 $t = isset( $this->reservedWords
[strtoupper( $t )] ) ?
'{'.$t.'}' : $t;
231 $t = preg_replace('/^"(.*)"$/', '($1)', $t);
232 $t = preg_replace('/([-&|])/', '\\\\$1', $t);
236 * Create or update the search index record for the given page.
237 * Title and text should be pre-processed.
240 * @param $title String
241 * @param $text String
243 function update( $id, $title, $text ) {
244 $dbw = wfGetDB( DB_MASTER
);
245 $dbw->replace( 'searchindex',
249 'si_title' => $title,
251 ), 'SearchOracle::update' );
254 // We need to specify the DB name (i.e. user/schema) here so that
255 // it can work from the installer, where
256 // ALTER SESSION SET CURRENT_SCHEMA = ...
258 $dbw->query( "CALL ctx_ddl.sync_index(" .
259 $dbw->addQuotes( $dbw->getDBname() . '.' . $dbw->tableName( 'si_text_idx', 'raw' ) ) . ")" );
260 $dbw->query( "CALL ctx_ddl.sync_index(" .
261 $dbw->addQuotes( $dbw->getDBname() . '.' . $dbw->tableName( 'si_title_idx', 'raw' ) ) . ")" );
265 * Update a search index record's title only.
266 * Title should be pre-processed.
269 * @param $title String
271 function updateTitle( $id, $title ) {
272 $dbw = wfGetDB( DB_MASTER
);
274 $dbw->update( 'searchindex',
275 array( 'si_title' => $title ),
276 array( 'si_page' => $id ),
277 'SearchOracle::updateTitle',
281 public static function legalSearchChars() {
282 return "\"" . parent
::legalSearchChars();