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
00040 function searchText( $term ) {
00041 $resultSet = $this->db->resultObject($this->db->query($this->getQuery($this->filter($term), true)));
00042 return new SqlSearchResultSet($resultSet, $this->searchTerms);
00043 }
00044
00051 function searchTitle($term) {
00052 $resultSet = $this->db->resultObject($this->db->query($this->getQuery($this->filter($term), false)));
00053 return new SqlSearchResultSet($resultSet, $this->searchTerms);
00054 }
00055
00056
00061 function queryRedirect() {
00062 if ($this->showRedirects) {
00063 return '';
00064 } else {
00065 return 'AND page_is_redirect=0';
00066 }
00067 }
00068
00073 function queryNamespaces() {
00074 if( is_null($this->namespaces) )
00075 return '';
00076 $namespaces = implode(',', $this->namespaces);
00077 if ($namespaces == '') {
00078 $namespaces = '0';
00079 }
00080 return 'AND page_namespace IN (' . $namespaces . ')';
00081 }
00082
00087 function queryLimit($sql) {
00088 return $this->db->limitResult($sql, $this->limit, $this->offset);
00089 }
00090
00096 function queryRanking($filteredTerm, $fulltext) {
00097
00098
00099 return '';
00100 }
00101
00108 function getQuery( $filteredTerm, $fulltext ) {
00109 return $this->queryLimit($this->queryMain($filteredTerm, $fulltext) . ' ' .
00110 $this->queryRedirect() . ' ' .
00111 $this->queryNamespaces() . ' ' .
00112 $this->queryRanking( $filteredTerm, $fulltext ) . ' ');
00113 }
00114
00115
00121 function getIndexField($fulltext) {
00122 return $fulltext ? 'si_text' : 'si_title';
00123 }
00124
00132 function queryMain( $filteredTerm, $fulltext ) {
00133 $match = $this->parseQuery($filteredTerm, $fulltext);
00134 $page = $this->db->tableName('page');
00135 $searchindex = $this->db->tableName('searchindex');
00136 return 'SELECT page_id, page_namespace, page_title ' .
00137 "FROM $page,$searchindex " .
00138 'WHERE page_id=si_page AND ' . $match;
00139 }
00140
00142 function parseQuery($filteredText, $fulltext) {
00143 global $wgContLang;
00144 $lc = SearchEngine::legalSearchChars();
00145 $this->searchTerms = array();
00146
00147 # FIXME: This doesn't handle parenthetical expressions.
00148 $m = array();
00149 $q = array();
00150
00151 if (preg_match_all('/([-+<>~]?)(([' . $lc . ']+)(\*?)|"[^"]*")/',
00152 $filteredText, $m, PREG_SET_ORDER)) {
00153 foreach($m as $terms) {
00154
00155
00156
00157 $temp_terms = $wgContLang->autoConvertToAllVariants( $terms[2] );
00158 if( is_array( $temp_terms )) {
00159 $temp_terms = array_unique( array_values( $temp_terms ));
00160 foreach( $temp_terms as $t )
00161 $q[] = $terms[1] . $wgContLang->normalizeForSearch( $t );
00162 }
00163 else
00164 $q[] = $terms[1] . $wgContLang->normalizeForSearch( $terms[2] );
00165
00166 if (!empty($terms[3])) {
00167 $regexp = preg_quote( $terms[3], '/' );
00168 if ($terms[4])
00169 $regexp .= "[0-9A-Za-z_]+";
00170 } else {
00171 $regexp = preg_quote(str_replace('"', '', $terms[2]), '/');
00172 }
00173 $this->searchTerms[] = $regexp;
00174 }
00175 }
00176
00177 $searchon = $this->db->strencode(join(',', $q));
00178 $field = $this->getIndexField($fulltext);
00179
00180
00181
00182
00183 return " lcase($field) LIKE lcase('%$searchon%')";
00184 }
00185
00194 function update($id, $title, $text) {
00195 $dbw = wfGetDB(DB_MASTER);
00196 $dbw->replace('searchindex',
00197 array('si_page'),
00198 array(
00199 'si_page' => $id,
00200 'si_title' => $title,
00201 'si_text' => $text
00202 ), 'SearchIBM_DB2::update' );
00203
00204
00205
00206 }
00207
00215 function updateTitle($id, $title) {
00216 $dbw = wfGetDB(DB_MASTER);
00217
00218 $dbw->update('searchindex',
00219 array('si_title' => $title),
00220 array('si_page' => $id),
00221 'SearchIBM_DB2::updateTitle',
00222 array());
00223 }
00224 }