Merge "Typo fix"
[mediawiki.git] / includes / search / SearchOracle.php
bloba84796542f08163ccbb764aa3e5a9d3f9907988a
1 <?php
2 /**
3 * Oracle search engine
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
23 * @file
24 * @ingroup Search
27 /**
28 * Search engine hook base class for Oracle (ConText).
29 * @ingroup Search
31 class SearchOracle extends SearchEngine {
33 private $reservedWords = array(
34 'ABOUT' => 1,
35 'ACCUM' => 1,
36 'AND' => 1,
37 'BT' => 1,
38 'BTG' => 1,
39 'BTI' => 1,
40 'BTP' => 1,
41 'FUZZY' => 1,
42 'HASPATH' => 1,
43 'INPATH' => 1,
44 'MINUS' => 1,
45 'NEAR' => 1,
46 'NOT' => 1,
47 'NT' => 1,
48 'NTG' => 1,
49 'NTI' => 1,
50 'NTP' => 1,
51 'OR' => 1,
52 'PT' => 1,
53 'RT' => 1,
54 'SQE' => 1,
55 'SYN' => 1,
56 'TR' => 1,
57 'TRSYN' => 1,
58 'TT' => 1,
59 'WITHIN' => 1,
62 /**
63 * Creates an instance of this class
64 * @param $db DatabasePostgres: database object
66 function __construct( $db ) {
67 parent::__construct( $db );
70 /**
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 ) {
77 if ( $term == '' ) {
78 return new SqlSearchResultSet( false, '' );
81 $resultSet = $this->db->resultObject( $this->db->query( $this->getQuery( $this->filter( $term ), true ) ) );
82 return new SqlSearchResultSet( $resultSet, $this->searchTerms );
85 /**
86 * Perform a title-only search query and return a result set.
88 * @param string $term raw search term
89 * @return SqlSearchResultSet
91 function searchTitle( $term ) {
92 if ( $term == '' ) {
93 return new SqlSearchResultSet( false, '' );
96 $resultSet = $this->db->resultObject( $this->db->query( $this->getQuery( $this->filter( $term ), false ) ) );
97 return new MySQLSearchResultSet( $resultSet, $this->searchTerms );
101 * Return a partial WHERE clause to exclude redirects, if so set
102 * @return String
104 function queryRedirect() {
105 if ( $this->showRedirects ) {
106 return '';
107 } else {
108 return 'AND page_is_redirect=0';
113 * Return a partial WHERE clause to limit the search to the given namespaces
114 * @return String
116 function queryNamespaces() {
117 if ( is_null( $this->namespaces ) ) {
118 return '';
120 if ( !count( $this->namespaces ) ) {
121 $namespaces = '0';
122 } else {
123 $namespaces = $this->db->makeList( $this->namespaces );
125 return 'AND page_namespace IN (' . $namespaces . ')';
129 * Return a LIMIT clause to limit results on the query.
131 * @param $sql string
133 * @return String
135 function queryLimit( $sql ) {
136 return $this->db->limitResult( $sql, $this->limit, $this->offset );
140 * Does not do anything for generic search engine
141 * subclasses may define this though
143 * @return String
145 function queryRanking( $filteredTerm, $fulltext ) {
146 return ' ORDER BY score(1)';
150 * Construct the full SQL query to do the search.
151 * The guts shoulds be constructed in queryMain()
152 * @param $filteredTerm String
153 * @param $fulltext Boolean
154 * @return String
156 function getQuery( $filteredTerm, $fulltext ) {
157 return $this->queryLimit( $this->queryMain( $filteredTerm, $fulltext ) . ' ' .
158 $this->queryRedirect() . ' ' .
159 $this->queryNamespaces() . ' ' .
160 $this->queryRanking( $filteredTerm, $fulltext ) . ' ' );
164 * Picks which field to index on, depending on what type of query.
165 * @param $fulltext Boolean
166 * @return String
168 function getIndexField( $fulltext ) {
169 return $fulltext ? 'si_text' : 'si_title';
173 * Get the base part of the search query.
175 * @param $filteredTerm String
176 * @param $fulltext Boolean
177 * @return String
179 function queryMain( $filteredTerm, $fulltext ) {
180 $match = $this->parseQuery( $filteredTerm, $fulltext );
181 $page = $this->db->tableName( 'page' );
182 $searchindex = $this->db->tableName( 'searchindex' );
183 return 'SELECT page_id, page_namespace, page_title ' .
184 "FROM $page,$searchindex " .
185 'WHERE page_id=si_page AND ' . $match;
189 * Parse a user input search string, and return an SQL fragment to be used
190 * as part of a WHERE clause
191 * @return string
193 function parseQuery( $filteredText, $fulltext ) {
194 global $wgContLang;
195 $lc = SearchEngine::legalSearchChars();
196 $this->searchTerms = array();
198 # @todo FIXME: This doesn't handle parenthetical expressions.
199 $m = array();
200 $searchon = '';
201 if ( preg_match_all( '/([-+<>~]?)(([' . $lc . ']+)(\*?)|"[^"]*")/',
202 $filteredText, $m, PREG_SET_ORDER ) ) {
203 foreach ( $m as $terms ) {
204 // Search terms in all variant forms, only
205 // apply on wiki with LanguageConverter
206 $temp_terms = $wgContLang->autoConvertToAllVariants( $terms[2] );
207 if ( is_array( $temp_terms ) ) {
208 $temp_terms = array_unique( array_values( $temp_terms ) );
209 foreach ( $temp_terms as $t ) {
210 $searchon .= ( $terms[1] == '-' ? ' ~' : ' & ' ) . $this->escapeTerm( $t );
213 else {
214 $searchon .= ( $terms[1] == '-' ? ' ~' : ' & ' ) . $this->escapeTerm( $terms[2] );
216 if ( !empty( $terms[3] ) ) {
217 $regexp = preg_quote( $terms[3], '/' );
218 if ( $terms[4] ) {
219 $regexp .= "[0-9A-Za-z_]+";
221 } else {
222 $regexp = preg_quote( str_replace( '"', '', $terms[2] ), '/' );
224 $this->searchTerms[] = $regexp;
228 $searchon = $this->db->addQuotes( ltrim( $searchon, ' &' ) );
229 $field = $this->getIndexField( $fulltext );
230 return " CONTAINS($field, $searchon, 1) > 0 ";
233 private function escapeTerm( $t ) {
234 global $wgContLang;
235 $t = $wgContLang->normalizeForSearch( $t );
236 $t = isset( $this->reservedWords[strtoupper( $t )] ) ? '{' . $t . '}' : $t;
237 $t = preg_replace( '/^"(.*)"$/', '($1)', $t );
238 $t = preg_replace( '/([-&|])/', '\\\\$1', $t );
239 return $t;
242 * Create or update the search index record for the given page.
243 * Title and text should be pre-processed.
245 * @param $id Integer
246 * @param $title String
247 * @param $text String
249 function update( $id, $title, $text ) {
250 $dbw = wfGetDB( DB_MASTER );
251 $dbw->replace( 'searchindex',
252 array( 'si_page' ),
253 array(
254 'si_page' => $id,
255 'si_title' => $title,
256 'si_text' => $text
257 ), 'SearchOracle::update' );
259 // Sync the index
260 // We need to specify the DB name (i.e. user/schema) here so that
261 // it can work from the installer, where
262 // ALTER SESSION SET CURRENT_SCHEMA = ...
263 // was used.
264 $dbw->query( "CALL ctx_ddl.sync_index(" .
265 $dbw->addQuotes( $dbw->getDBname() . '.' . $dbw->tableName( 'si_text_idx', 'raw' ) ) . ")" );
266 $dbw->query( "CALL ctx_ddl.sync_index(" .
267 $dbw->addQuotes( $dbw->getDBname() . '.' . $dbw->tableName( 'si_title_idx', 'raw' ) ) . ")" );
271 * Update a search index record's title only.
272 * Title should be pre-processed.
274 * @param $id Integer
275 * @param $title String
277 function updateTitle( $id, $title ) {
278 $dbw = wfGetDB( DB_MASTER );
280 $dbw->update( 'searchindex',
281 array( 'si_title' => $title ),
282 array( 'si_page' => $id ),
283 'SearchOracle::updateTitle',
284 array() );
287 public static function legalSearchChars() {
288 return "\"" . parent::legalSearchChars();