Merge "Suppress section edit links with action=render"
[lhc/web/wiklou.git] / includes / search / SearchOracle.php
1 <?php
2 /**
3 * Oracle search engine
4 *
5 * Copyright © 2004 Brion Vibber <brion@pobox.com>
6 * https://www.mediawiki.org/
7 *
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.
12 *
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.
17 *
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
22 *
23 * @file
24 * @ingroup Search
25 */
26
27 /**
28 * Search engine hook base class for Oracle (ConText).
29 * @ingroup Search
30 */
31 class SearchOracle extends SearchDatabase {
32
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,
60 );
61
62 /**
63 * Perform a full text search query and return a result set.
64 *
65 * @param string $term raw search term
66 * @return SqlSearchResultSet
67 */
68 function searchText( $term ) {
69 if ( $term == '' ) {
70 return new SqlSearchResultSet( false, '' );
71 }
72
73 $resultSet = $this->db->resultObject( $this->db->query( $this->getQuery( $this->filter( $term ), true ) ) );
74 return new SqlSearchResultSet( $resultSet, $this->searchTerms );
75 }
76
77 /**
78 * Perform a title-only search query and return a result set.
79 *
80 * @param string $term raw search term
81 * @return SqlSearchResultSet
82 */
83 function searchTitle( $term ) {
84 if ( $term == '' ) {
85 return new SqlSearchResultSet( false, '' );
86 }
87
88 $resultSet = $this->db->resultObject( $this->db->query( $this->getQuery( $this->filter( $term ), false ) ) );
89 return new MySQLSearchResultSet( $resultSet, $this->searchTerms );
90 }
91
92 /**
93 * Return a partial WHERE clause to limit the search to the given namespaces
94 * @return String
95 */
96 function queryNamespaces() {
97 if ( is_null( $this->namespaces ) ) {
98 return '';
99 }
100 if ( !count( $this->namespaces ) ) {
101 $namespaces = '0';
102 } else {
103 $namespaces = $this->db->makeList( $this->namespaces );
104 }
105 return 'AND page_namespace IN (' . $namespaces . ')';
106 }
107
108 /**
109 * Return a LIMIT clause to limit results on the query.
110 *
111 * @param $sql string
112 *
113 * @return String
114 */
115 function queryLimit( $sql ) {
116 return $this->db->limitResult( $sql, $this->limit, $this->offset );
117 }
118
119 /**
120 * Does not do anything for generic search engine
121 * subclasses may define this though
122 *
123 * @return String
124 */
125 function queryRanking( $filteredTerm, $fulltext ) {
126 return ' ORDER BY score(1)';
127 }
128
129 /**
130 * Construct the full SQL query to do the search.
131 * The guts shoulds be constructed in queryMain()
132 * @param $filteredTerm String
133 * @param $fulltext Boolean
134 * @return String
135 */
136 function getQuery( $filteredTerm, $fulltext ) {
137 return $this->queryLimit( $this->queryMain( $filteredTerm, $fulltext ) . ' ' .
138 $this->queryNamespaces() . ' ' .
139 $this->queryRanking( $filteredTerm, $fulltext ) . ' ' );
140 }
141
142 /**
143 * Picks which field to index on, depending on what type of query.
144 * @param $fulltext Boolean
145 * @return String
146 */
147 function getIndexField( $fulltext ) {
148 return $fulltext ? 'si_text' : 'si_title';
149 }
150
151 /**
152 * Get the base part of the search query.
153 *
154 * @param $filteredTerm String
155 * @param $fulltext Boolean
156 * @return String
157 */
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;
165 }
166
167 /**
168 * Parse a user input search string, and return an SQL fragment to be used
169 * as part of a WHERE clause
170 * @return string
171 */
172 function parseQuery( $filteredText, $fulltext ) {
173 global $wgContLang;
174 $lc = SearchEngine::legalSearchChars();
175 $this->searchTerms = array();
176
177 # @todo FIXME: This doesn't handle parenthetical expressions.
178 $m = array();
179 $searchon = '';
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 );
190 }
191 }
192 else {
193 $searchon .= ( $terms[1] == '-' ? ' ~' : ' & ' ) . $this->escapeTerm( $terms[2] );
194 }
195 if ( !empty( $terms[3] ) ) {
196 $regexp = preg_quote( $terms[3], '/' );
197 if ( $terms[4] ) {
198 $regexp .= "[0-9A-Za-z_]+";
199 }
200 } else {
201 $regexp = preg_quote( str_replace( '"', '', $terms[2] ), '/' );
202 }
203 $this->searchTerms[] = $regexp;
204 }
205 }
206
207 $searchon = $this->db->addQuotes( ltrim( $searchon, ' &' ) );
208 $field = $this->getIndexField( $fulltext );
209 return " CONTAINS($field, $searchon, 1) > 0 ";
210 }
211
212 private function escapeTerm( $t ) {
213 global $wgContLang;
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 );
218 return $t;
219 }
220
221 /**
222 * Create or update the search index record for the given page.
223 * Title and text should be pre-processed.
224 *
225 * @param $id Integer
226 * @param $title String
227 * @param $text String
228 */
229 function update( $id, $title, $text ) {
230 $dbw = wfGetDB( DB_MASTER );
231 $dbw->replace( 'searchindex',
232 array( 'si_page' ),
233 array(
234 'si_page' => $id,
235 'si_title' => $title,
236 'si_text' => $text
237 ), 'SearchOracle::update' );
238
239 // Sync the index
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 = ...
243 // was used.
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' ) ) . ")" );
248 }
249
250 /**
251 * Update a search index record's title only.
252 * Title should be pre-processed.
253 *
254 * @param $id Integer
255 * @param $title String
256 */
257 function updateTitle( $id, $title ) {
258 $dbw = wfGetDB( DB_MASTER );
259
260 $dbw->update( 'searchindex',
261 array( 'si_title' => $title ),
262 array( 'si_page' => $id ),
263 'SearchOracle::updateTitle',
264 array() );
265 }
266
267 public static function legalSearchChars() {
268 return "\"" . parent::legalSearchChars();
269 }
270 }