Fix fixme on r107328, attempting to use $this in a static method
[lhc/web/wiklou.git] / includes / search / SearchIBM_DB2.php
1 <?php
2 /**
3 * IBM DB2 search engine
4 *
5 * Copyright © 2004 Brion Vibber <brion@pobox.com>
6 * http://www.mediawiki.org/
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 * http://www.gnu.org/copyleft/gpl.html
22 *
23 * @file
24 * @ingroup Search
25 */
26
27 /**
28 * Search engine hook base class for IBM DB2
29 * @ingroup Search
30 */
31 class SearchIBM_DB2 extends SearchEngine {
32
33 /**
34 * Creates an instance of this class
35 * @param $db DatabaseIbm_db2: database object
36 */
37 function __construct($db) {
38 parent::__construct( $db );
39 }
40
41 /**
42 * Perform a full text search query and return a result set.
43 *
44 * @param $term String: raw search term
45 * @return SqlSearchResultSet
46 */
47 function searchText( $term ) {
48 $resultSet = $this->db->resultObject($this->db->query($this->getQuery($this->filter($term), true)));
49 return new SqlSearchResultSet($resultSet, $this->searchTerms);
50 }
51
52 /**
53 * Perform a title-only search query and return a result set.
54 *
55 * @param $term String: taw search term
56 * @return SqlSearchResultSet
57 */
58 function searchTitle($term) {
59 $resultSet = $this->db->resultObject($this->db->query($this->getQuery($this->filter($term), false)));
60 return new SqlSearchResultSet($resultSet, $this->searchTerms);
61 }
62
63
64 /**
65 * Return a partial WHERE clause to exclude redirects, if so set
66 * @return String
67 */
68 function queryRedirect() {
69 if ($this->showRedirects) {
70 return '';
71 } else {
72 return 'AND page_is_redirect=0';
73 }
74 }
75
76 /**
77 * Return a partial WHERE clause to limit the search to the given namespaces
78 * @return String
79 */
80 function queryNamespaces() {
81 if( is_null($this->namespaces) )
82 return '';
83 $namespaces = implode(',', $this->namespaces);
84 if ($namespaces == '') {
85 $namespaces = '0';
86 }
87 return 'AND page_namespace IN (' . $namespaces . ')';
88 }
89
90 /**
91 * Return a LIMIT clause to limit results on the query.
92 * @return String
93 */
94 function queryLimit( $sql ) {
95 return $this->db->limitResult($sql, $this->limit, $this->offset);
96 }
97
98 /**
99 * Does not do anything for generic search engine
100 * subclasses may define this though
101 * @return String
102 */
103 function queryRanking($filteredTerm, $fulltext) {
104 // requires Net Search Extender or equivalent
105 // return ' ORDER BY score(1)';
106 return '';
107 }
108
109 /**
110 * Construct the full SQL query to do the search.
111 * The guts shoulds be constructed in queryMain()
112 * @param $filteredTerm String
113 * @param $fulltext Boolean
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 $fulltext Boolean
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 $filteredTerm String
136 * @param $fulltext Boolean
137 * @return String
138 */
139 function queryMain( $filteredTerm, $fulltext ) {
140 $match = $this->parseQuery($filteredTerm, $fulltext);
141 $page = $this->db->tableName('page');
142 $searchindex = $this->db->tableName('searchindex');
143 return 'SELECT page_id, page_namespace, page_title ' .
144 "FROM $page,$searchindex " .
145 'WHERE page_id=si_page AND ' . $match;
146 }
147
148 /** @todo document */
149 function parseQuery($filteredText, $fulltext) {
150 global $wgContLang;
151 $lc = SearchEngine::legalSearchChars();
152 $this->searchTerms = array();
153
154 # @todo FIXME: This doesn't handle parenthetical expressions.
155 $m = array();
156 $q = array();
157
158 if (preg_match_all('/([-+<>~]?)(([' . $lc . ']+)(\*?)|"[^"]*")/',
159 $filteredText, $m, PREG_SET_ORDER)) {
160 foreach($m as $terms) {
161
162 // Search terms in all variant forms, only
163 // apply on wiki with LanguageConverter
164 $temp_terms = $wgContLang->autoConvertToAllVariants( $terms[2] );
165 if( is_array( $temp_terms )) {
166 $temp_terms = array_unique( array_values( $temp_terms ));
167 foreach( $temp_terms as $t )
168 $q[] = $terms[1] . $wgContLang->normalizeForSearch( $t );
169 }
170 else
171 $q[] = $terms[1] . $wgContLang->normalizeForSearch( $terms[2] );
172
173 if (!empty($terms[3])) {
174 $regexp = preg_quote( $terms[3], '/' );
175 if ($terms[4])
176 $regexp .= "[0-9A-Za-z_]+";
177 } else {
178 $regexp = preg_quote(str_replace('"', '', $terms[2]), '/');
179 }
180 $this->searchTerms[] = $regexp;
181 }
182 }
183
184 $searchon = $this->db->strencode(join(',', $q));
185 $field = $this->getIndexField($fulltext);
186
187 // requires Net Search Extender or equivalent
188 //return " CONTAINS($field, '$searchon') > 0 ";
189
190 return " lcase($field) LIKE lcase('%$searchon%')";
191 }
192
193 /**
194 * Create or update the search index record for the given page.
195 * Title and text should be pre-processed.
196 *
197 * @param $id Integer
198 * @param $title String
199 * @param $text String
200 */
201 function update($id, $title, $text) {
202 $dbw = wfGetDB(DB_MASTER);
203 $dbw->replace('searchindex',
204 array('si_page'),
205 array(
206 'si_page' => $id,
207 'si_title' => $title,
208 'si_text' => $text
209 ), 'SearchIBM_DB2::update' );
210 // ?
211 //$dbw->query("CALL ctx_ddl.sync_index('si_text_idx')");
212 //$dbw->query("CALL ctx_ddl.sync_index('si_title_idx')");
213 }
214
215 /**
216 * Update a search index record's title only.
217 * Title should be pre-processed.
218 *
219 * @param $id Integer
220 * @param $title String
221 */
222 function updateTitle($id, $title) {
223 $dbw = wfGetDB(DB_MASTER);
224
225 $dbw->update('searchindex',
226 array('si_title' => $title),
227 array('si_page' => $id),
228 'SearchIBM_DB2::updateTitle',
229 array());
230 }
231 }