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
{
34 * Creates an instance of this class
35 * @param $db DatabaseIbm_db2: database object
37 function __construct($db) {
38 parent
::__construct( $db );
42 * Perform a full text search query and return a result set.
44 * @param $term String: raw search term
45 * @return SqlSearchResultSet
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
);
53 * Perform a title-only search query and return a result set.
55 * @param $term String: taw search term
56 * @return SqlSearchResultSet
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
);
65 * Return a partial WHERE clause to exclude redirects, if so set
68 function queryRedirect() {
69 if ($this->showRedirects
) {
72 return 'AND page_is_redirect=0';
77 * Return a partial WHERE clause to limit the search to the given namespaces
80 function queryNamespaces() {
81 if( is_null($this->namespaces
) )
83 $namespaces = implode(',', $this->namespaces
);
84 if ($namespaces == '') {
87 return 'AND page_namespace IN (' . $namespaces . ')';
91 * Return a LIMIT clause to limit results on the query.
94 function queryLimit( $sql ) {
95 return $this->db
->limitResult($sql, $this->limit
, $this->offset
);
99 * Does not do anything for generic search engine
100 * subclasses may define this though
103 function queryRanking($filteredTerm, $fulltext) {
104 // requires Net Search Extender or equivalent
105 // return ' ORDER BY score(1)';
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
116 function getQuery( $filteredTerm, $fulltext ) {
117 return $this->queryLimit($this->queryMain($filteredTerm, $fulltext) . ' ' .
118 $this->queryRedirect() . ' ' .
119 $this->queryNamespaces() . ' ' .
120 $this->queryRanking( $filteredTerm, $fulltext ) . ' ');
125 * Picks which field to index on, depending on what type of query.
126 * @param $fulltext Boolean
129 function getIndexField($fulltext) {
130 return $fulltext ?
'si_text' : 'si_title';
134 * Get the base part of the search query.
136 * @param $filteredTerm String
137 * @param $fulltext Boolean
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;
152 function parseQuery($filteredText, $fulltext) {
154 $lc = SearchEngine
::legalSearchChars();
155 $this->searchTerms
= array();
157 # @todo FIXME: This doesn't handle parenthetical expressions.
161 if (preg_match_all('/([-+<>~]?)(([' . $lc . ']+)(\*?)|"[^"]*")/',
162 $filteredText, $m, PREG_SET_ORDER
)) {
163 foreach($m as $terms) {
165 // Search terms in all variant forms, only
166 // apply on wiki with LanguageConverter
167 $temp_terms = $wgContLang->autoConvertToAllVariants( $terms[2] );
168 if( is_array( $temp_terms )) {
169 $temp_terms = array_unique( array_values( $temp_terms ));
170 foreach( $temp_terms as $t )
171 $q[] = $terms[1] . $wgContLang->normalizeForSearch( $t );
174 $q[] = $terms[1] . $wgContLang->normalizeForSearch( $terms[2] );
176 if (!empty($terms[3])) {
177 $regexp = preg_quote( $terms[3], '/' );
179 $regexp .= "[0-9A-Za-z_]+";
181 $regexp = preg_quote(str_replace('"', '', $terms[2]), '/');
183 $this->searchTerms
[] = $regexp;
187 $searchon = $this->db
->strencode(join(',', $q));
188 $field = $this->getIndexField($fulltext);
190 // requires Net Search Extender or equivalent
191 //return " CONTAINS($field, '$searchon') > 0 ";
193 return " lcase($field) LIKE lcase('%$searchon%')";
197 * Create or update the search index record for the given page.
198 * Title and text should be pre-processed.
201 * @param $title String
202 * @param $text String
204 function update($id, $title, $text) {
205 $dbw = wfGetDB(DB_MASTER
);
206 $dbw->replace('searchindex',
210 'si_title' => $title,
212 ), 'SearchIBM_DB2::update' );
214 //$dbw->query("CALL ctx_ddl.sync_index('si_text_idx')");
215 //$dbw->query("CALL ctx_ddl.sync_index('si_title_idx')");
219 * Update a search index record's title only.
220 * Title should be pre-processed.
223 * @param $title String
225 function updateTitle($id, $title) {
226 $dbw = wfGetDB(DB_MASTER
);
228 $dbw->update('searchindex',
229 array('si_title' => $title),
230 array('si_page' => $id),
231 'SearchIBM_DB2::updateTitle',