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