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