* replace use of deprecated makeLinkObj() by link() in core
[lhc/web/wiklou.git] / includes / 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 function __construct($db) {
31 $this->db = $db;
32 }
33
34 /**
35 * Perform a full text search query and return a result set.
36 *
37 * @param $term String: raw search term
38 * @return OracleSearchResultSet
39 */
40 function searchText( $term ) {
41 if ($term == '')
42 return new OracleSearchResultSet(false, '');
43
44 $resultSet = $this->db->resultObject($this->db->query($this->getQuery($this->filter($term), true)));
45 return new OracleSearchResultSet($resultSet, $this->searchTerms);
46 }
47
48 /**
49 * Perform a title-only search query and return a result set.
50 *
51 * @param $term String: raw search term
52 * @return ORacleSearchResultSet
53 */
54 function searchTitle($term) {
55 if ($term == '')
56 return new OracleSearchResultSet(false, '');
57
58 $resultSet = $this->db->resultObject($this->db->query($this->getQuery($this->filter($term), false)));
59 return new MySQLSearchResultSet($resultSet, $this->searchTerms);
60 }
61
62
63 /**
64 * Return a partial WHERE clause to exclude redirects, if so set
65 * @return String
66 */
67 function queryRedirect() {
68 if ($this->showRedirects) {
69 return '';
70 } else {
71 return 'AND page_is_redirect=0';
72 }
73 }
74
75 /**
76 * Return a partial WHERE clause to limit the search to the given namespaces
77 * @return String
78 */
79 function queryNamespaces() {
80 if( is_null($this->namespaces) )
81 return '';
82 if ( !count( $this->namespaces ) ) {
83 $namespaces = '0';
84 } else {
85 $namespaces = $this->db->makeList( $this->namespaces );
86 }
87 return 'AND page_namespace IN (' . $namespaces . ')';
88 }
89
90 /**
91 * Return a LIMIT clause to limit results on the query.
92 * @return String
93 */
94 function queryLimit($sql) {
95 return $this->db->limitResult($sql, $this->limit, $this->offset);
96 }
97
98 /**
99 * Does not do anything for generic search engine
100 * subclasses may define this though
101 * @return String
102 */
103 function queryRanking($filteredTerm, $fulltext) {
104 return ' ORDER BY score(1)';
105 }
106
107 /**
108 * Construct the full SQL query to do the search.
109 * The guts shoulds be constructed in queryMain()
110 * @param $filteredTerm String
111 * @param $fulltext Boolean
112 */
113 function getQuery( $filteredTerm, $fulltext ) {
114 return $this->queryLimit($this->queryMain($filteredTerm, $fulltext) . ' ' .
115 $this->queryRedirect() . ' ' .
116 $this->queryNamespaces() . ' ' .
117 $this->queryRanking( $filteredTerm, $fulltext ) . ' ');
118 }
119
120
121 /**
122 * Picks which field to index on, depending on what type of query.
123 * @param $fulltext Boolean
124 * @return String
125 */
126 function getIndexField($fulltext) {
127 return $fulltext ? 'si_text' : 'si_title';
128 }
129
130 /**
131 * Get the base part of the search query.
132 *
133 * @param $filteredTerm String
134 * @param $fulltext Boolean
135 * @return String
136 */
137 function queryMain( $filteredTerm, $fulltext ) {
138 $match = $this->parseQuery($filteredTerm, $fulltext);
139 $page = $this->db->tableName('page');
140 $searchindex = $this->db->tableName('searchindex');
141 return 'SELECT page_id, page_namespace, page_title ' .
142 "FROM $page,$searchindex " .
143 'WHERE page_id=si_page AND ' . $match;
144 }
145
146 /**
147 * Parse a user input search string, and return an SQL fragment to be used
148 * as part of a WHERE clause
149 */
150 function parseQuery($filteredText, $fulltext) {
151 global $wgContLang;
152 $lc = SearchEngine::legalSearchChars();
153 $this->searchTerms = array();
154
155 # FIXME: This doesn't handle parenthetical expressions.
156 $m = array();
157 $q = array();
158
159 if (preg_match_all('/([-+<>~]?)(([' . $lc . ']+)(\*?)|"[^"]*")/',
160 $filteredText, $m, PREG_SET_ORDER)) {
161 foreach($m as $terms) {
162 $q[] = $terms[1] . $wgContLang->stripForSearch($terms[2]);
163
164 if (!empty($terms[3])) {
165 $regexp = preg_quote( $terms[3], '/' );
166 if ($terms[4])
167 $regexp .= "[0-9A-Za-z_]+";
168 } else {
169 $regexp = preg_quote(str_replace('"', '', $terms[2]), '/');
170 }
171 $this->searchTerms[] = $regexp;
172 }
173 }
174
175 $searchon = $this->db->addQuotes(join(',', $q));
176 $field = $this->getIndexField($fulltext);
177 return " CONTAINS($field, $searchon, 1) > 0 ";
178 }
179
180 /**
181 * Create or update the search index record for the given page.
182 * Title and text should be pre-processed.
183 *
184 * @param $id Integer
185 * @param $title String
186 * @param $text String
187 */
188 function update($id, $title, $text) {
189 $dbw = wfGetDB(DB_MASTER);
190 $dbw->replace('searchindex',
191 array('si_page'),
192 array(
193 'si_page' => $id,
194 'si_title' => $title,
195 'si_text' => $text
196 ), 'SearchOracle::update' );
197 $dbw->query("CALL ctx_ddl.sync_index('si_text_idx')");
198 $dbw->query("CALL ctx_ddl.sync_index('si_title_idx')");
199 }
200
201 /**
202 * Update a search index record's title only.
203 * Title should be pre-processed.
204 *
205 * @param int $id
206 * @param string $title
207 */
208 function updateTitle($id, $title) {
209 $dbw = wfGetDB(DB_MASTER);
210
211 $dbw->update('searchindex',
212 array('si_title' => $title),
213 array('si_page' => $id),
214 'SearchOracle::updateTitle',
215 array());
216 }
217 }
218
219 /**
220 * @ingroup Search
221 */
222 class OracleSearchResultSet extends SearchResultSet {
223
224 function __construct($resultSet, $terms) {
225 $this->mResultSet = $resultSet;
226 $this->mTerms = $terms;
227 }
228
229 function termMatches() {
230 return $this->mTerms;
231 }
232
233 function numRows() {
234 if ($this->mResultSet === false )
235 return 0;
236 else
237 return $this->mResultSet->numRows();
238 }
239
240 function next() {
241 if ($this->mResultSet === false )
242 return false;
243
244 $row = $this->mResultSet->fetchObject();
245 if ($row === false)
246 return false;
247 return new SearchResult($row);
248 }
249 }