00001 <?php
00002 # Copyright (C) 2004 Brion Vibber <brion@pobox.com>
00003 # http://www.mediawiki.org/
00004 #
00005 # This program is free software; you can redistribute it and/or modify
00006 # it under the terms of the GNU General Public License as published by
00007 # the Free Software Foundation; either version 2 of the License, or
00008 # (at your option) any later version.
00009 #
00010 # This program is distributed in the hope that it will be useful,
00011 # but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00013 # GNU General Public License for more details.
00014 #
00015 # You should have received a copy of the GNU General Public License along
00016 # with this program; if not, write to the Free Software Foundation, Inc.,
00017 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00018 # http://www.gnu.org/copyleft/gpl.html
00019
00029 class SearchIBM_DB2 extends SearchEngine {
00030 function __construct($db) {
00031 $this->db = $db;
00032 }
00033
00041 function searchText( $term ) {
00042 $resultSet = $this->db->resultObject($this->db->query($this->getQuery($this->filter($term), true)));
00043 return new IBM_DB2SearchResultSet($resultSet, $this->searchTerms);
00044 }
00045
00053 function searchTitle($term) {
00054 $resultSet = $this->db->resultObject($this->db->query($this->getQuery($this->filter($term), false)));
00055 return new MySQLSearchResultSet($resultSet, $this->searchTerms);
00056 }
00057
00058
00064 function queryRedirect() {
00065 if ($this->showRedirects) {
00066 return '';
00067 } else {
00068 return 'AND page_is_redirect=0';
00069 }
00070 }
00071
00077 function queryNamespaces() {
00078 if( is_null($this->namespaces) )
00079 return '';
00080 $namespaces = implode(',', $this->namespaces);
00081 if ($namespaces == '') {
00082 $namespaces = '0';
00083 }
00084 return 'AND page_namespace IN (' . $namespaces . ')';
00085 }
00086
00092 function queryLimit($sql) {
00093 return $this->db->limitResult($sql, $this->limit, $this->offset);
00094 }
00095
00102 function queryRanking($filteredTerm, $fulltext) {
00103
00104
00105 return '';
00106 }
00107
00115 function getQuery( $filteredTerm, $fulltext ) {
00116 return $this->queryLimit($this->queryMain($filteredTerm, $fulltext) . ' ' .
00117 $this->queryRedirect() . ' ' .
00118 $this->queryNamespaces() . ' ' .
00119 $this->queryRanking( $filteredTerm, $fulltext ) . ' ');
00120 }
00121
00122
00128 function getIndexField($fulltext) {
00129 return $fulltext ? 'si_text' : 'si_title';
00130 }
00131
00140 function queryMain( $filteredTerm, $fulltext ) {
00141 $match = $this->parseQuery($filteredTerm, $fulltext);
00142 $page = $this->db->tableName('page');
00143 $searchindex = $this->db->tableName('searchindex');
00144 return 'SELECT page_id, page_namespace, page_title ' .
00145 "FROM $page,$searchindex " .
00146 'WHERE page_id=si_page AND ' . $match;
00147 }
00148
00150 function parseQuery($filteredText, $fulltext) {
00151 global $wgContLang;
00152 $lc = SearchEngine::legalSearchChars();
00153 $this->searchTerms = array();
00154
00155 # FIXME: This doesn't handle parenthetical expressions.
00156 $m = array();
00157 $q = array();
00158
00159 if (preg_match_all('/([-+<>~]?)(([' . $lc . ']+)(\*?)|"[^"]*")/',
00160 $filteredText, $m, PREG_SET_ORDER)) {
00161 foreach($m as $terms) {
00162 $q[] = $terms[1] . $wgContLang->stripForSearch($terms[2]);
00163
00164 if (!empty($terms[3])) {
00165 $regexp = preg_quote( $terms[3], '/' );
00166 if ($terms[4])
00167 $regexp .= "[0-9A-Za-z_]+";
00168 } else {
00169 $regexp = preg_quote(str_replace('"', '', $terms[2]), '/');
00170 }
00171 $this->searchTerms[] = $regexp;
00172 }
00173 }
00174
00175 $searchon = $this->db->strencode(join(',', $q));
00176 $field = $this->getIndexField($fulltext);
00177
00178
00179
00180
00181 return " lcase($field) LIKE lcase('%$searchon%')";
00182 }
00183
00192 function update($id, $title, $text) {
00193 $dbw = wfGetDB(DB_MASTER);
00194 $dbw->replace('searchindex',
00195 array('si_page'),
00196 array(
00197 'si_page' => $id,
00198 'si_title' => $title,
00199 'si_text' => $text
00200 ), 'SearchIBM_DB2::update' );
00201
00202
00203
00204 }
00205
00213 function updateTitle($id, $title) {
00214 $dbw = wfGetDB(DB_MASTER);
00215
00216 $dbw->update('searchindex',
00217 array('si_title' => $title),
00218 array('si_page' => $id),
00219 'SearchIBM_DB2::updateTitle',
00220 array());
00221 }
00222 }
00223
00227 class IBM_DB2SearchResultSet extends SearchResultSet {
00228 function __construct($resultSet, $terms) {
00229 $this->mResultSet = $resultSet;
00230 $this->mTerms = $terms;
00231 }
00232
00233 function termMatches() {
00234 return $this->mTerms;
00235 }
00236
00237 function numRows() {
00238 return $this->mResultSet->numRows();
00239 }
00240
00241 function next() {
00242 $row = $this->mResultSet->fetchObject();
00243 if ($row === false)
00244 return false;
00245 return new SearchResult($row);
00246 }
00247 }