Correct empty case for r49149
[mediawiki.git] / includes / SearchIBM_DB2.php
blob57813a73de9dd221ca01e74e5f2c3b6ab965f975
1 <?php
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
20 /**
21 * @file
22 * @ingroup Search
25 /**
26 * Search engine hook base class for IBM DB2
27 * @ingroup Search
29 class SearchIBM_DB2 extends SearchEngine {
30 function __construct($db) {
31 $this->db = $db;
34 /**
35 * Perform a full text search query and return a result set.
37 * @param string $term - Raw search term
38 * @return IBM_DB2SearchResultSet
39 * @access public
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);
46 /**
47 * Perform a title-only search query and return a result set.
49 * @param string $term - Raw search term
50 * @return IBM_DB2SearchResultSet
51 * @access public
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);
59 /**
60 * Return a partial WHERE clause to exclude redirects, if so set
61 * @return string
62 * @private
64 function queryRedirect() {
65 if ($this->showRedirects) {
66 return '';
67 } else {
68 return 'AND page_is_redirect=0';
72 /**
73 * Return a partial WHERE clause to limit the search to the given namespaces
74 * @return string
75 * @private
77 function queryNamespaces() {
78 if( is_null($this->namespaces) )
79 return '';
80 $namespaces = implode(',', $this->namespaces);
81 if ($namespaces == '') {
82 $namespaces = '0';
84 return 'AND page_namespace IN (' . $namespaces . ')';
87 /**
88 * Return a LIMIT clause to limit results on the query.
89 * @return string
90 * @private
92 function queryLimit($sql) {
93 return $this->db->limitResult($sql, $this->limit, $this->offset);
96 /**
97 * Does not do anything for generic search engine
98 * subclasses may define this though
99 * @return string
100 * @private
102 function queryRanking($filteredTerm, $fulltext) {
103 // requires Net Search Extender or equivalent
104 // return ' ORDER BY score(1)';
105 return '';
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
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
126 * @return string
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
137 * @return string
138 * @private
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) {
151 global $wgContLang;
152 $lc = SearchEngine::legalSearchChars();
153 $this->searchTerms = array();
155 # FIXME: This doesn't handle parenthetical expressions.
156 $m = array();
157 $q = array();
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], '/' );
166 if ($terms[4])
167 $regexp .= "[0-9A-Za-z_]+";
168 } else {
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.
188 * @param int $id
189 * @param string $title
190 * @param string $text
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')");
207 * Update a search index record's title only.
208 * Title should be pre-processed.
210 * @param int $id
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',
220 array());
225 * @ingroup Search
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;
237 function numRows() {
238 return $this->mResultSet->numRows();
241 function next() {
242 $row = $this->mResultSet->fetchObject();
243 if ($row === false)
244 return false;
245 return new SearchResult($row);