4035fc3d54d58a35c401d9181d1efcbad947c2f7
[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 * http://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 SearchEngine {
32
33 private $reservedWords = array ('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);
59
60 /**
61 * Creates an instance of this class
62 * @param $db DatabasePostgres: database object
63 */
64 function __construct($db) {
65 parent::__construct( $db );
66 }
67
68 /**
69 * Perform a full text search query and return a result set.
70 *
71 * @param $term String: raw search term
72 * @return SqlSearchResultSet
73 */
74 function searchText( $term ) {
75 if ($term == '')
76 return new SqlSearchResultSet(false, '');
77
78 $resultSet = $this->db->resultObject($this->db->query($this->getQuery($this->filter($term), true)));
79 return new SqlSearchResultSet($resultSet, $this->searchTerms);
80 }
81
82 /**
83 * Perform a title-only search query and return a result set.
84 *
85 * @param $term String: raw search term
86 * @return SqlSearchResultSet
87 */
88 function searchTitle($term) {
89 if ($term == '')
90 return new SqlSearchResultSet(false, '');
91
92 $resultSet = $this->db->resultObject($this->db->query($this->getQuery($this->filter($term), false)));
93 return new MySQLSearchResultSet($resultSet, $this->searchTerms);
94 }
95
96
97 /**
98 * Return a partial WHERE clause to exclude redirects, if so set
99 * @return String
100 */
101 function queryRedirect() {
102 if ($this->showRedirects) {
103 return '';
104 } else {
105 return 'AND page_is_redirect=0';
106 }
107 }
108
109 /**
110 * Return a partial WHERE clause to limit the search to the given namespaces
111 * @return String
112 */
113 function queryNamespaces() {
114 if( is_null($this->namespaces) )
115 return '';
116 if ( !count( $this->namespaces ) ) {
117 $namespaces = '0';
118 } else {
119 $namespaces = $this->db->makeList( $this->namespaces );
120 }
121 return 'AND page_namespace IN (' . $namespaces . ')';
122 }
123
124 /**
125 * Return a LIMIT clause to limit results on the query.
126 * @return String
127 */
128 function queryLimit($sql) {
129 return $this->db->limitResult($sql, $this->limit, $this->offset);
130 }
131
132 /**
133 * Does not do anything for generic search engine
134 * subclasses may define this though
135 * @return String
136 */
137 function queryRanking($filteredTerm, $fulltext) {
138 return ' ORDER BY score(1)';
139 }
140
141 /**
142 * Construct the full SQL query to do the search.
143 * The guts shoulds be constructed in queryMain()
144 * @param $filteredTerm String
145 * @param $fulltext Boolean
146 */
147 function getQuery( $filteredTerm, $fulltext ) {
148 return $this->queryLimit($this->queryMain($filteredTerm, $fulltext) . ' ' .
149 $this->queryRedirect() . ' ' .
150 $this->queryNamespaces() . ' ' .
151 $this->queryRanking( $filteredTerm, $fulltext ) . ' ');
152 }
153
154
155 /**
156 * Picks which field to index on, depending on what type of query.
157 * @param $fulltext Boolean
158 * @return String
159 */
160 function getIndexField($fulltext) {
161 return $fulltext ? 'si_text' : 'si_title';
162 }
163
164 /**
165 * Get the base part of the search query.
166 *
167 * @param $filteredTerm String
168 * @param $fulltext Boolean
169 * @return String
170 */
171 function queryMain( $filteredTerm, $fulltext ) {
172 $match = $this->parseQuery($filteredTerm, $fulltext);
173 $page = $this->db->tableName('page');
174 $searchindex = $this->db->tableName('searchindex');
175 return 'SELECT page_id, page_namespace, page_title ' .
176 "FROM $page,$searchindex " .
177 'WHERE page_id=si_page AND ' . $match;
178 }
179
180 /**
181 * Parse a user input search string, and return an SQL fragment to be used
182 * as part of a WHERE clause
183 */
184 function parseQuery($filteredText, $fulltext) {
185 global $wgContLang;
186 $lc = SearchEngine::legalSearchChars();
187 $this->searchTerms = array();
188
189 # FIXME: This doesn't handle parenthetical expressions.
190 $m = array();
191 $searchon = '';
192 if (preg_match_all('/([-+<>~]?)(([' . $lc . ']+)(\*?)|"[^"]*")/',
193 $filteredText, $m, PREG_SET_ORDER)) {
194 foreach($m as $terms) {
195 // Search terms in all variant forms, only
196 // apply on wiki with LanguageConverter
197 $temp_terms = $wgContLang->autoConvertToAllVariants( $terms[2] );
198 if( is_array( $temp_terms )) {
199 $temp_terms = array_unique( array_values( $temp_terms ));
200 foreach( $temp_terms as $t ) {
201 $searchon .= ($terms[1] == '-' ? ' ~' : ' & ') . $this->escapeTerm( $t );
202 }
203 }
204 else {
205 $searchon .= ($terms[1] == '-' ? ' ~' : ' & ') . $this->escapeTerm( $terms[2] );
206 }
207 if (!empty($terms[3])) {
208 $regexp = preg_quote( $terms[3], '/' );
209 if ($terms[4])
210 $regexp .= "[0-9A-Za-z_]+";
211 } else {
212 $regexp = preg_quote(str_replace('"', '', $terms[2]), '/');
213 }
214 $this->searchTerms[] = $regexp;
215 }
216 }
217
218
219 $searchon = $this->db->addQuotes(ltrim($searchon, ' &'));
220 $field = $this->getIndexField($fulltext);
221 return " CONTAINS($field, $searchon, 1) > 0 ";
222 }
223
224 private function escapeTerm($t) {
225 global $wgContLang;
226 $t = $wgContLang->normalizeForSearch($t);
227 $t = isset($this->reservedWords[strtoupper($t)]) ? '{'.$t.'}' : $t;
228 $t = preg_replace('/^"(.*)"$/', '($1)', $t);
229 $t = preg_replace('/([-&|])/', '\\\\$1', $t);
230 return $t;
231 }
232 /**
233 * Create or update the search index record for the given page.
234 * Title and text should be pre-processed.
235 *
236 * @param $id Integer
237 * @param $title String
238 * @param $text String
239 */
240 function update($id, $title, $text) {
241 $dbw = wfGetDB(DB_MASTER);
242 $dbw->replace('searchindex',
243 array('si_page'),
244 array(
245 'si_page' => $id,
246 'si_title' => $title,
247 'si_text' => $text
248 ), 'SearchOracle::update' );
249
250 // Sync the index
251 // We need to specify the DB name (i.e. user/schema) here so that
252 // it can work from the installer, where
253 // ALTER SESSION SET CURRENT_SCHEMA = ...
254 // was used.
255 $dbw->query( "CALL ctx_ddl.sync_index(" .
256 $dbw->addQuotes( $dbw->getDBname() . '.si_text_idx' ) . ")" );
257 $dbw->query( "CALL ctx_ddl.sync_index(" .
258 $dbw->addQuotes( $dbw->getDBname() . '.si_title_idx' ) . ")" );
259 }
260
261 /**
262 * Update a search index record's title only.
263 * Title should be pre-processed.
264 *
265 * @param $id Integer
266 * @param $title String
267 */
268 function updateTitle($id, $title) {
269 $dbw = wfGetDB(DB_MASTER);
270
271 $dbw->update('searchindex',
272 array('si_title' => $title),
273 array('si_page' => $id),
274 'SearchOracle::updateTitle',
275 array());
276 }
277
278
279 public static function legalSearchChars() {
280 return "\"" . parent::legalSearchChars();
281 }
282 }