3 * IBM DB2 search engine
5 * Copyright © 2004 Brion Vibber <brion@pobox.com>
6 * http://www.mediawiki.org/
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.
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.
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
28 * Search engine hook base class for IBM DB2
31 class SearchIBM_DB2
extends SearchEngine
{
32 function __construct($db) {
37 * Perform a full text search query and return a result set.
39 * @param $term String: raw search term
40 * @return SqlSearchResultSet
42 function searchText( $term ) {
43 $resultSet = $this->db
->resultObject($this->db
->query($this->getQuery($this->filter($term), true)));
44 return new SqlSearchResultSet($resultSet, $this->searchTerms
);
48 * Perform a title-only search query and return a result set.
50 * @param $term String: taw search term
51 * @return SqlSearchResultSet
53 function searchTitle($term) {
54 $resultSet = $this->db
->resultObject($this->db
->query($this->getQuery($this->filter($term), false)));
55 return new SqlSearchResultSet($resultSet, $this->searchTerms
);
60 * Return a partial WHERE clause to exclude redirects, if so set
63 function queryRedirect() {
64 if ($this->showRedirects
) {
67 return 'AND page_is_redirect=0';
72 * Return a partial WHERE clause to limit the search to the given namespaces
75 function queryNamespaces() {
76 if( is_null($this->namespaces
) )
78 $namespaces = implode(',', $this->namespaces
);
79 if ($namespaces == '') {
82 return 'AND page_namespace IN (' . $namespaces . ')';
86 * Return a LIMIT clause to limit results on the query.
89 function queryLimit($sql) {
90 return $this->db
->limitResult($sql, $this->limit
, $this->offset
);
94 * Does not do anything for generic search engine
95 * subclasses may define this though
98 function queryRanking($filteredTerm, $fulltext) {
99 // requires Net Search Extender or equivalent
100 // return ' ORDER BY score(1)';
105 * Construct the full SQL query to do the search.
106 * The guts shoulds be constructed in queryMain()
107 * @param $filteredTerm String
108 * @param $fulltext Boolean
110 function getQuery( $filteredTerm, $fulltext ) {
111 return $this->queryLimit($this->queryMain($filteredTerm, $fulltext) . ' ' .
112 $this->queryRedirect() . ' ' .
113 $this->queryNamespaces() . ' ' .
114 $this->queryRanking( $filteredTerm, $fulltext ) . ' ');
119 * Picks which field to index on, depending on what type of query.
120 * @param $fulltext Boolean
123 function getIndexField($fulltext) {
124 return $fulltext ?
'si_text' : 'si_title';
128 * Get the base part of the search query.
130 * @param $filteredTerm String
131 * @param $fulltext Boolean
134 function queryMain( $filteredTerm, $fulltext ) {
135 $match = $this->parseQuery($filteredTerm, $fulltext);
136 $page = $this->db
->tableName('page');
137 $searchindex = $this->db
->tableName('searchindex');
138 return 'SELECT page_id, page_namespace, page_title ' .
139 "FROM $page,$searchindex " .
140 'WHERE page_id=si_page AND ' . $match;
143 /** @todo document */
144 function parseQuery($filteredText, $fulltext) {
146 $lc = SearchEngine
::legalSearchChars();
147 $this->searchTerms
= array();
149 # FIXME: This doesn't handle parenthetical expressions.
153 if (preg_match_all('/([-+<>~]?)(([' . $lc . ']+)(\*?)|"[^"]*")/',
154 $filteredText, $m, PREG_SET_ORDER
)) {
155 foreach($m as $terms) {
157 // Search terms in all variant forms, only
158 // apply on wiki with LanguageConverter
159 $temp_terms = $wgContLang->autoConvertToAllVariants( $terms[2] );
160 if( is_array( $temp_terms )) {
161 $temp_terms = array_unique( array_values( $temp_terms ));
162 foreach( $temp_terms as $t )
163 $q[] = $terms[1] . $wgContLang->normalizeForSearch( $t );
166 $q[] = $terms[1] . $wgContLang->normalizeForSearch( $terms[2] );
168 if (!empty($terms[3])) {
169 $regexp = preg_quote( $terms[3], '/' );
171 $regexp .= "[0-9A-Za-z_]+";
173 $regexp = preg_quote(str_replace('"', '', $terms[2]), '/');
175 $this->searchTerms
[] = $regexp;
179 $searchon = $this->db
->strencode(join(',', $q));
180 $field = $this->getIndexField($fulltext);
182 // requires Net Search Extender or equivalent
183 //return " CONTAINS($field, '$searchon') > 0 ";
185 return " lcase($field) LIKE lcase('%$searchon%')";
189 * Create or update the search index record for the given page.
190 * Title and text should be pre-processed.
193 * @param $title String
194 * @param $text String
196 function update($id, $title, $text) {
197 $dbw = wfGetDB(DB_MASTER
);
198 $dbw->replace('searchindex',
202 'si_title' => $title,
204 ), 'SearchIBM_DB2::update' );
206 //$dbw->query("CALL ctx_ddl.sync_index('si_text_idx')");
207 //$dbw->query("CALL ctx_ddl.sync_index('si_title_idx')");
211 * Update a search index record's title only.
212 * Title should be pre-processed.
215 * @param $title String
217 function updateTitle($id, $title) {
218 $dbw = wfGetDB(DB_MASTER
);
220 $dbw->update('searchindex',
221 array('si_title' => $title),
222 array('si_page' => $id),
223 'SearchIBM_DB2::updateTitle',