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