2 # Copyright (C) 2004 Brion Vibber <brion@pobox.com>
3 # http://www.mediawiki.org/
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.
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.
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
26 * Search engine hook base class for IBM DB2
29 class SearchIBM_DB2
extends SearchEngine
{
30 function __construct($db) {
35 * Perform a full text search query and return a result set.
37 * @param string $term - Raw search term
38 * @return IBM_DB2SearchResultSet
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
);
47 * Perform a title-only search query and return a result set.
49 * @param string $term - Raw search term
50 * @return IBM_DB2SearchResultSet
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
);
60 * Return a partial WHERE clause to exclude redirects, if so set
64 function queryRedirect() {
65 if ($this->showRedirects
) {
68 return 'AND page_is_redirect=0';
73 * Return a partial WHERE clause to limit the search to the given namespaces
77 function queryNamespaces() {
78 if( is_null($this->namespaces
) )
80 $namespaces = implode(',', $this->namespaces
);
81 if ($namespaces == '') {
84 return 'AND page_namespace IN (' . $namespaces . ')';
88 * Return a LIMIT clause to limit results on the query.
92 function queryLimit($sql) {
93 return $this->db
->limitResult($sql, $this->limit
, $this->offset
);
97 * Does not do anything for generic search engine
98 * subclasses may define this though
102 function queryRanking($filteredTerm, $fulltext) {
103 // requires Net Search Extender or equivalent
104 // return ' ORDER BY score(1)';
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
115 function getQuery( $filteredTerm, $fulltext ) {
116 return $this->queryLimit($this->queryMain($filteredTerm, $fulltext) . ' ' .
117 $this->queryRedirect() . ' ' .
118 $this->queryNamespaces() . ' ' .
119 $this->queryRanking( $filteredTerm, $fulltext ) . ' ');
124 * Picks which field to index on, depending on what type of query.
125 * @param bool $fulltext
128 function getIndexField($fulltext) {
129 return $fulltext ?
'si_text' : 'si_title';
133 * Get the base part of the search query.
135 * @param string $filteredTerm
136 * @param bool $fulltext
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;
149 /** @todo document */
150 function parseQuery($filteredText, $fulltext) {
152 $lc = SearchEngine
::legalSearchChars();
153 $this->searchTerms
= array();
155 # FIXME: This doesn't handle parenthetical expressions.
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]);
164 if (!empty($terms[3])) {
165 $regexp = preg_quote( $terms[3], '/' );
167 $regexp .= "[0-9A-Za-z_]+";
169 $regexp = preg_quote(str_replace('"', '', $terms[2]), '/');
171 $this->searchTerms
[] = $regexp;
175 $searchon = $this->db
->strencode(join(',', $q));
176 $field = $this->getIndexField($fulltext);
178 // requires Net Search Extender or equivalent
179 //return " CONTAINS($field, '$searchon') > 0 ";
181 return " lcase($field) LIKE lcase('%$searchon%')";
185 * Create or update the search index record for the given page.
186 * Title and text should be pre-processed.
189 * @param string $title
190 * @param string $text
192 function update($id, $title, $text) {
193 $dbw = wfGetDB(DB_MASTER
);
194 $dbw->replace('searchindex',
198 'si_title' => $title,
200 ), 'SearchIBM_DB2::update' );
202 //$dbw->query("CALL ctx_ddl.sync_index('si_text_idx')");
203 //$dbw->query("CALL ctx_ddl.sync_index('si_title_idx')");
207 * Update a search index record's title only.
208 * Title should be pre-processed.
211 * @param string $title
213 function updateTitle($id, $title) {
214 $dbw = wfGetDB(DB_MASTER
);
216 $dbw->update('searchindex',
217 array('si_title' => $title),
218 array('si_page' => $id),
219 'SearchIBM_DB2::updateTitle',
227 class IBM_DB2SearchResultSet
extends SearchResultSet
{
228 function __construct($resultSet, $terms) {
229 $this->mResultSet
= $resultSet;
230 $this->mTerms
= $terms;
233 function termMatches() {
234 return $this->mTerms
;
238 return $this->mResultSet
->numRows();
242 $row = $this->mResultSet
->fetchObject();
245 return new SearchResult($row);