Localisation updates from https://translatewiki.net.
[mediawiki.git] / includes / search / SearchOracle.php
blob4cfcec39888c617f46a078b45f6db7dbae7be9b5
1 <?php
2 /**
3 * Oracle search engine
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
23 * @file
24 * @ingroup Search
27 /**
28 * Search engine hook base class for Oracle (ConText).
29 * @ingroup Search
31 class SearchOracle extends SearchDatabase {
32 private $reservedWords = array(
33 'ABOUT' => 1,
34 'ACCUM' => 1,
35 'AND' => 1,
36 'BT' => 1,
37 'BTG' => 1,
38 'BTI' => 1,
39 'BTP' => 1,
40 'FUZZY' => 1,
41 'HASPATH' => 1,
42 'INPATH' => 1,
43 'MINUS' => 1,
44 'NEAR' => 1,
45 'NOT' => 1,
46 'NT' => 1,
47 'NTG' => 1,
48 'NTI' => 1,
49 'NTP' => 1,
50 'OR' => 1,
51 'PT' => 1,
52 'RT' => 1,
53 'SQE' => 1,
54 'SYN' => 1,
55 'TR' => 1,
56 'TRSYN' => 1,
57 'TT' => 1,
58 'WITHIN' => 1,
61 /**
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 ) {
68 if ( $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 );
79 /**
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 ) {
86 if ( $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 );
97 /**
98 * Return a partial WHERE clause to limit the search to the given namespaces
99 * @return string
101 function queryNamespaces() {
102 if ( is_null( $this->namespaces ) ) {
103 return '';
105 if ( !count( $this->namespaces ) ) {
106 $namespaces = '0';
107 } else {
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.
116 * @param string $sql
118 * @return string
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
128 * @return string
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
139 * @return string
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
150 * @return string
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
161 * @return string
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
175 * @return string
177 function parseQuery( $filteredText, $fulltext ) {
178 global $wgContLang;
179 $lc = $this->legalSearchChars();
180 $this->searchTerms = array();
182 # @todo FIXME: This doesn't handle parenthetical expressions.
183 $m = array();
184 $searchon = '';
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 );
197 else {
198 $searchon .= ( $terms[1] == '-' ? ' ~' : ' & ' ) . $this->escapeTerm( $terms[2] );
200 if ( !empty( $terms[3] ) ) {
201 $regexp = preg_quote( $terms[3], '/' );
202 if ( $terms[4] ) {
203 $regexp .= "[0-9A-Za-z_]+";
205 } else {
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 ) {
218 global $wgContLang;
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 );
223 return $t;
227 * Create or update the search index record for the given page.
228 * Title and text should be pre-processed.
230 * @param int $id
231 * @param string $title
232 * @param string $text
234 function update( $id, $title, $text ) {
235 $dbw = wfGetDB( DB_MASTER );
236 $dbw->replace( 'searchindex',
237 array( 'si_page' ),
238 array(
239 'si_page' => $id,
240 'si_title' => $title,
241 'si_text' => $text
242 ), 'SearchOracle::update' );
244 // Sync the index
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 = ...
248 // was used.
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.
259 * @param int $id
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',
269 array() );
272 public static function legalSearchChars() {
273 return "\"" . parent::legalSearchChars();