(bug 22647) Add category details in search results. Contributed by Lejonel.
[lhc/web/wiklou.git] / includes / search / SearchOracle.php
1 <?php
2 # Copyright (C) 2004 Brion Vibber <brion@pobox.com>
3 # http://www.mediawiki.org/
4 #
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 2 of the License, or
8 # (at your option) any later version.
9 #
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License along
16 # with this program; if not, write to the Free Software Foundation, Inc.,
17 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 # http://www.gnu.org/copyleft/gpl.html
19
20 /**
21 * @file
22 * @ingroup Search
23 */
24
25 /**
26 * Search engine hook base class for Oracle (ConText).
27 * @ingroup Search
28 */
29 class SearchOracle extends SearchEngine {
30
31 private $reservedWords = array ('ABOUT' => 1,
32 'ACCUM' => 1,
33 'AND' => 1,
34 'BT' => 1,
35 'BTG' => 1,
36 'BTI' => 1,
37 'BTP' => 1,
38 'FUZZY' => 1,
39 'HASPATH' => 1,
40 'INPATH' => 1,
41 'MINUS' => 1,
42 'NEAR' => 1,
43 'NOT' => 1,
44 'NT' => 1,
45 'NTG' => 1,
46 'NTI' => 1,
47 'NTP' => 1,
48 'OR' => 1,
49 'PT' => 1,
50 'RT' => 1,
51 'SQE' => 1,
52 'SYN' => 1,
53 'TR' => 1,
54 'TRSYN' => 1,
55 'TT' => 1,
56 'WITHIN' => 1);
57
58 function __construct($db) {
59 $this->db = $db;
60 }
61
62 /**
63 * Perform a full text search query and return a result set.
64 *
65 * @param $term String: raw search term
66 * @return SqlSearchResultSet
67 */
68 function searchText( $term ) {
69 if ($term == '')
70 return new SqlSearchResultSet(false, '');
71
72 $resultSet = $this->db->resultObject($this->db->query($this->getQuery($this->filter($term), true)));
73 return new SqlSearchResultSet($resultSet, $this->searchTerms);
74 }
75
76 /**
77 * Perform a title-only search query and return a result set.
78 *
79 * @param $term String: raw search term
80 * @return SqlSearchResultSet
81 */
82 function searchTitle($term) {
83 if ($term == '')
84 return new SqlSearchResultSet(false, '');
85
86 $resultSet = $this->db->resultObject($this->db->query($this->getQuery($this->filter($term), false)));
87 return new MySQLSearchResultSet($resultSet, $this->searchTerms);
88 }
89
90
91 /**
92 * Return a partial WHERE clause to exclude redirects, if so set
93 * @return String
94 */
95 function queryRedirect() {
96 if ($this->showRedirects) {
97 return '';
98 } else {
99 return 'AND page_is_redirect=0';
100 }
101 }
102
103 /**
104 * Return a partial WHERE clause to limit the search to the given namespaces
105 * @return String
106 */
107 function queryNamespaces() {
108 if( is_null($this->namespaces) )
109 return '';
110 if ( !count( $this->namespaces ) ) {
111 $namespaces = '0';
112 } else {
113 $namespaces = $this->db->makeList( $this->namespaces );
114 }
115 return 'AND page_namespace IN (' . $namespaces . ')';
116 }
117
118 /**
119 * Return a LIMIT clause to limit results on the query.
120 * @return String
121 */
122 function queryLimit($sql) {
123 return $this->db->limitResult($sql, $this->limit, $this->offset);
124 }
125
126 /**
127 * Does not do anything for generic search engine
128 * subclasses may define this though
129 * @return String
130 */
131 function queryRanking($filteredTerm, $fulltext) {
132 return ' ORDER BY score(1)';
133 }
134
135 /**
136 * Construct the full SQL query to do the search.
137 * The guts shoulds be constructed in queryMain()
138 * @param $filteredTerm String
139 * @param $fulltext Boolean
140 */
141 function getQuery( $filteredTerm, $fulltext ) {
142 return $this->queryLimit($this->queryMain($filteredTerm, $fulltext) . ' ' .
143 $this->queryRedirect() . ' ' .
144 $this->queryNamespaces() . ' ' .
145 $this->queryRanking( $filteredTerm, $fulltext ) . ' ');
146 }
147
148
149 /**
150 * Picks which field to index on, depending on what type of query.
151 * @param $fulltext Boolean
152 * @return String
153 */
154 function getIndexField($fulltext) {
155 return $fulltext ? 'si_text' : 'si_title';
156 }
157
158 /**
159 * Get the base part of the search query.
160 *
161 * @param $filteredTerm String
162 * @param $fulltext Boolean
163 * @return String
164 */
165 function queryMain( $filteredTerm, $fulltext ) {
166 $match = $this->parseQuery($filteredTerm, $fulltext);
167 $page = $this->db->tableName('page');
168 $searchindex = $this->db->tableName('searchindex');
169 return 'SELECT page_id, page_namespace, page_title ' .
170 "FROM $page,$searchindex " .
171 'WHERE page_id=si_page AND ' . $match;
172 }
173
174 /**
175 * Parse a user input search string, and return an SQL fragment to be used
176 * as part of a WHERE clause
177 */
178 function parseQuery($filteredText, $fulltext) {
179 global $wgContLang;
180 $lc = SearchEngine::legalSearchChars();
181 $this->searchTerms = array();
182
183 # FIXME: This doesn't handle parenthetical expressions.
184 $m = array();
185 $searchon = '';
186 if (preg_match_all('/([-+<>~]?)(([' . $lc . ']+)(\*?)|"[^"]*")/',
187 $filteredText, $m, PREG_SET_ORDER)) {
188 foreach($m as $terms) {
189 // Search terms in all variant forms, only
190 // apply on wiki with LanguageConverter
191 $temp_terms = $wgContLang->autoConvertToAllVariants( $terms[2] );
192 if( is_array( $temp_terms )) {
193 $temp_terms = array_unique( array_values( $temp_terms ));
194 foreach( $temp_terms as $t ) {
195 $searchon .= ($terms[1] == '-' ? ' ~' : ' & ') . $this->escapeTerm( $t );
196 }
197 }
198 else {
199 $searchon .= ($terms[1] == '-' ? ' ~' : ' & ') . $this->escapeTerm( $terms[2] );
200 }
201 if (!empty($terms[3])) {
202 $regexp = preg_quote( $terms[3], '/' );
203 if ($terms[4])
204 $regexp .= "[0-9A-Za-z_]+";
205 } else {
206 $regexp = preg_quote(str_replace('"', '', $terms[2]), '/');
207 }
208 $this->searchTerms[] = $regexp;
209 }
210 }
211
212
213 $searchon = $this->db->addQuotes(ltrim($searchon, ' &'));
214 $field = $this->getIndexField($fulltext);
215 return " CONTAINS($field, $searchon, 1) > 0 ";
216 }
217
218 private function escapeTerm($t) {
219 global $wgContLang;
220 $t = $wgContLang->normalizeForSearch($t);
221 $t = isset($this->reservedWords[strtoupper($t)]) ? '{'.$t.'}' : $t;
222 $t = preg_replace('/^"(.*)"$/', '($1)', $t);
223 $t = preg_replace('/([-&|])/', '\\\\$1', $t);
224 return $t;
225 }
226 /**
227 * Create or update the search index record for the given page.
228 * Title and text should be pre-processed.
229 *
230 * @param $id Integer
231 * @param $title String
232 * @param $text String
233 */
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' );
243 $dbw->query("CALL ctx_ddl.sync_index('si_text_idx')");
244 $dbw->query("CALL ctx_ddl.sync_index('si_title_idx')");
245 }
246
247 /**
248 * Update a search index record's title only.
249 * Title should be pre-processed.
250 *
251 * @param int $id
252 * @param string $title
253 */
254 function updateTitle($id, $title) {
255 $dbw = wfGetDB(DB_MASTER);
256
257 $dbw->update('searchindex',
258 array('si_title' => $title),
259 array('si_page' => $id),
260 'SearchOracle::updateTitle',
261 array());
262 }
263
264
265 public static function legalSearchChars() {
266 return "\"" . parent::legalSearchChars();
267 }
268 }