* Fixed notice
[lhc/web/wiklou.git] / includes / SearchIBM_DB2.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 IBM DB2
27 * @ingroup Search
28 */
29 class SearchIBM_DB2 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 IBM_DB2SearchResultSet
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 IBM_DB2SearchResultSet($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 IBM_DB2SearchResultSet
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 // requires Net Search Extender or equivalent
104 // return ' ORDER BY score(1)';
105 return '';
106 }
107
108 /**
109 * Construct the full SQL query to do the search.
110 * The guts shoulds be constructed in queryMain()
111 * @param string $filteredTerm
112 * @param bool $fulltext
113 * @private
114 */
115 function getQuery( $filteredTerm, $fulltext ) {
116 return $this->queryLimit($this->queryMain($filteredTerm, $fulltext) . ' ' .
117 $this->queryRedirect() . ' ' .
118 $this->queryNamespaces() . ' ' .
119 $this->queryRanking( $filteredTerm, $fulltext ) . ' ');
120 }
121
122
123 /**
124 * Picks which field to index on, depending on what type of query.
125 * @param bool $fulltext
126 * @return string
127 */
128 function getIndexField($fulltext) {
129 return $fulltext ? 'si_text' : 'si_title';
130 }
131
132 /**
133 * Get the base part of the search query.
134 *
135 * @param string $filteredTerm
136 * @param bool $fulltext
137 * @return string
138 * @private
139 */
140 function queryMain( $filteredTerm, $fulltext ) {
141 $match = $this->parseQuery($filteredTerm, $fulltext);
142 $page = $this->db->tableName('page');
143 $searchindex = $this->db->tableName('searchindex');
144 return 'SELECT page_id, page_namespace, page_title ' .
145 "FROM $page,$searchindex " .
146 'WHERE page_id=si_page AND ' . $match;
147 }
148
149 /** @todo document */
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->strencode(join(',', $q));
176 $field = $this->getIndexField($fulltext);
177
178 // requires Net Search Extender or equivalent
179 //return " CONTAINS($field, '$searchon') > 0 ";
180
181 return " lcase($field) LIKE lcase('%$searchon%')";
182 }
183
184 /**
185 * Create or update the search index record for the given page.
186 * Title and text should be pre-processed.
187 *
188 * @param int $id
189 * @param string $title
190 * @param string $text
191 */
192 function update($id, $title, $text) {
193 $dbw = wfGetDB(DB_MASTER);
194 $dbw->replace('searchindex',
195 array('si_page'),
196 array(
197 'si_page' => $id,
198 'si_title' => $title,
199 'si_text' => $text
200 ), 'SearchIBM_DB2::update' );
201 // ?
202 //$dbw->query("CALL ctx_ddl.sync_index('si_text_idx')");
203 //$dbw->query("CALL ctx_ddl.sync_index('si_title_idx')");
204 }
205
206 /**
207 * Update a search index record's title only.
208 * Title should be pre-processed.
209 *
210 * @param int $id
211 * @param string $title
212 */
213 function updateTitle($id, $title) {
214 $dbw = wfGetDB(DB_MASTER);
215
216 $dbw->update('searchindex',
217 array('si_title' => $title),
218 array('si_page' => $id),
219 'SearchIBM_DB2::updateTitle',
220 array());
221 }
222 }
223
224 /**
225 * @ingroup Search
226 */
227 class IBM_DB2SearchResultSet extends SearchResultSet {
228 function __construct($resultSet, $terms) {
229 $this->mResultSet = $resultSet;
230 $this->mTerms = $terms;
231 }
232
233 function termMatches() {
234 return $this->mTerms;
235 }
236
237 function numRows() {
238 return $this->mResultSet->numRows();
239 }
240
241 function next() {
242 $row = $this->mResultSet->fetchObject();
243 if ($row === false)
244 return false;
245 return new SearchResult($row);
246 }
247 }