API: Fix list=deletedrevs paging bug pointed out by Splarka on IRC
[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 $resultSet = $this->db->resultObject($this->db->query($this->getQuery($this->filter($term), true)));
42 return new OracleSearchResultSet($resultSet, $this->searchTerms);
43 }
44
45 /**
46 * Perform a title-only search query and return a result set.
47 *
48 * @param $term String: raw search term
49 * @return ORacleSearchResultSet
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 */
61 function queryRedirect() {
62 if ($this->showRedirects) {
63 return '';
64 } else {
65 return 'AND page_is_redirect=0';
66 }
67 }
68
69 /**
70 * Return a partial WHERE clause to limit the search to the given namespaces
71 * @return String
72 */
73 function queryNamespaces() {
74 if( is_null($this->namespaces) )
75 return '';
76 if ( !count( $this->namespaces ) ) {
77 $namespaces = '0';
78 } else {
79 $namespaces = $this->db->makeList( $this->namespaces );
80 }
81 return 'AND page_namespace IN (' . $namespaces . ')';
82 }
83
84 /**
85 * Return a LIMIT clause to limit results on the query.
86 * @return String
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 */
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 $filteredTerm String
105 * @param $fulltext Boolean
106 */
107 function getQuery( $filteredTerm, $fulltext ) {
108 return $this->queryLimit($this->queryMain($filteredTerm, $fulltext) . ' ' .
109 $this->queryRedirect() . ' ' .
110 $this->queryNamespaces() . ' ' .
111 $this->queryRanking( $filteredTerm, $fulltext ) . ' ');
112 }
113
114
115 /**
116 * Picks which field to index on, depending on what type of query.
117 * @param $fulltext Boolean
118 * @return String
119 */
120 function getIndexField($fulltext) {
121 return $fulltext ? 'si_text' : 'si_title';
122 }
123
124 /**
125 * Get the base part of the search query.
126 *
127 * @param $filteredTerm String
128 * @param $fulltext Boolean
129 * @return String
130 */
131 function queryMain( $filteredTerm, $fulltext ) {
132 $match = $this->parseQuery($filteredTerm, $fulltext);
133 $page = $this->db->tableName('page');
134 $searchindex = $this->db->tableName('searchindex');
135 return 'SELECT page_id, page_namespace, page_title ' .
136 "FROM $page,$searchindex " .
137 'WHERE page_id=si_page AND ' . $match;
138 }
139
140 /**
141 * Parse a user input search string, and return an SQL fragment to be used
142 * as part of a WHERE clause
143 */
144 function parseQuery($filteredText, $fulltext) {
145 global $wgContLang;
146 $lc = SearchEngine::legalSearchChars();
147 $this->searchTerms = array();
148
149 # FIXME: This doesn't handle parenthetical expressions.
150 $m = array();
151 $q = array();
152
153 if (preg_match_all('/([-+<>~]?)(([' . $lc . ']+)(\*?)|"[^"]*")/',
154 $filteredText, $m, PREG_SET_ORDER)) {
155 foreach($m as $terms) {
156 $q[] = $terms[1] . $wgContLang->stripForSearch($terms[2]);
157
158 if (!empty($terms[3])) {
159 $regexp = preg_quote( $terms[3], '/' );
160 if ($terms[4])
161 $regexp .= "[0-9A-Za-z_]+";
162 } else {
163 $regexp = preg_quote(str_replace('"', '', $terms[2]), '/');
164 }
165 $this->searchTerms[] = $regexp;
166 }
167 }
168
169 $searchon = $this->db->addQuotes(join(',', $q));
170 $field = $this->getIndexField($fulltext);
171 return " CONTAINS($field, $searchon, 1) > 0 ";
172 }
173
174 /**
175 * Create or update the search index record for the given page.
176 * Title and text should be pre-processed.
177 *
178 * @param $id Integer
179 * @param $title String
180 * @param $text String
181 */
182 function update($id, $title, $text) {
183 $dbw = wfGetDB(DB_MASTER);
184 $dbw->replace('searchindex',
185 array('si_page'),
186 array(
187 'si_page' => $id,
188 'si_title' => $title,
189 'si_text' => $text
190 ), 'SearchOracle::update' );
191 $dbw->query("CALL ctx_ddl.sync_index('si_text_idx')");
192 $dbw->query("CALL ctx_ddl.sync_index('si_title_idx')");
193 }
194
195 /**
196 * Update a search index record's title only.
197 * Title should be pre-processed.
198 *
199 * @param int $id
200 * @param string $title
201 */
202 function updateTitle($id, $title) {
203 $dbw = wfGetDB(DB_MASTER);
204
205 $dbw->update('searchindex',
206 array('si_title' => $title),
207 array('si_page' => $id),
208 'SearchOracle::updateTitle',
209 array());
210 }
211 }
212
213 /**
214 * @ingroup Search
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 return new SearchResult($row);
235 }
236 }