MediaWiki
REL1_20
|
00001 <?php 00031 class SearchIBM_DB2 extends SearchEngine { 00032 00037 function __construct($db) { 00038 parent::__construct( $db ); 00039 } 00040 00047 function searchText( $term ) { 00048 $resultSet = $this->db->resultObject($this->db->query($this->getQuery($this->filter($term), true))); 00049 return new SqlSearchResultSet($resultSet, $this->searchTerms); 00050 } 00051 00058 function searchTitle($term) { 00059 $resultSet = $this->db->resultObject($this->db->query($this->getQuery($this->filter($term), false))); 00060 return new SqlSearchResultSet($resultSet, $this->searchTerms); 00061 } 00062 00063 00068 function queryRedirect() { 00069 if ($this->showRedirects) { 00070 return ''; 00071 } else { 00072 return 'AND page_is_redirect=0'; 00073 } 00074 } 00075 00080 function queryNamespaces() { 00081 if( is_null($this->namespaces) ) 00082 return ''; 00083 $namespaces = implode(',', $this->namespaces); 00084 if ($namespaces == '') { 00085 $namespaces = '0'; 00086 } 00087 return 'AND page_namespace IN (' . $namespaces . ')'; 00088 } 00089 00094 function queryLimit( $sql ) { 00095 return $this->db->limitResult($sql, $this->limit, $this->offset); 00096 } 00097 00103 function queryRanking($filteredTerm, $fulltext) { 00104 // requires Net Search Extender or equivalent 00105 // return ' ORDER BY score(1)'; 00106 return ''; 00107 } 00108 00116 function getQuery( $filteredTerm, $fulltext ) { 00117 return $this->queryLimit($this->queryMain($filteredTerm, $fulltext) . ' ' . 00118 $this->queryRedirect() . ' ' . 00119 $this->queryNamespaces() . ' ' . 00120 $this->queryRanking( $filteredTerm, $fulltext ) . ' '); 00121 } 00122 00123 00129 function getIndexField($fulltext) { 00130 return $fulltext ? 'si_text' : 'si_title'; 00131 } 00132 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 00152 function parseQuery($filteredText, $fulltext) { 00153 global $wgContLang; 00154 $lc = SearchEngine::legalSearchChars(); 00155 $this->searchTerms = array(); 00156 00157 # @todo FIXME: This doesn't handle parenthetical expressions. 00158 $m = array(); 00159 $q = array(); 00160 00161 if (preg_match_all('/([-+<>~]?)(([' . $lc . ']+)(\*?)|"[^"]*")/', 00162 $filteredText, $m, PREG_SET_ORDER)) { 00163 foreach($m as $terms) { 00164 00165 // Search terms in all variant forms, only 00166 // apply on wiki with LanguageConverter 00167 $temp_terms = $wgContLang->autoConvertToAllVariants( $terms[2] ); 00168 if( is_array( $temp_terms )) { 00169 $temp_terms = array_unique( array_values( $temp_terms )); 00170 foreach( $temp_terms as $t ) 00171 $q[] = $terms[1] . $wgContLang->normalizeForSearch( $t ); 00172 } 00173 else 00174 $q[] = $terms[1] . $wgContLang->normalizeForSearch( $terms[2] ); 00175 00176 if (!empty($terms[3])) { 00177 $regexp = preg_quote( $terms[3], '/' ); 00178 if ($terms[4]) 00179 $regexp .= "[0-9A-Za-z_]+"; 00180 } else { 00181 $regexp = preg_quote(str_replace('"', '', $terms[2]), '/'); 00182 } 00183 $this->searchTerms[] = $regexp; 00184 } 00185 } 00186 00187 $searchon = $this->db->strencode(join(',', $q)); 00188 $field = $this->getIndexField($fulltext); 00189 00190 // requires Net Search Extender or equivalent 00191 //return " CONTAINS($field, '$searchon') > 0 "; 00192 00193 return " lcase($field) LIKE lcase('%$searchon%')"; 00194 } 00195 00204 function update($id, $title, $text) { 00205 $dbw = wfGetDB(DB_MASTER); 00206 $dbw->replace('searchindex', 00207 array('si_page'), 00208 array( 00209 'si_page' => $id, 00210 'si_title' => $title, 00211 'si_text' => $text 00212 ), 'SearchIBM_DB2::update' ); 00213 // ? 00214 //$dbw->query("CALL ctx_ddl.sync_index('si_text_idx')"); 00215 //$dbw->query("CALL ctx_ddl.sync_index('si_title_idx')"); 00216 } 00217 00225 function updateTitle($id, $title) { 00226 $dbw = wfGetDB(DB_MASTER); 00227 00228 $dbw->update('searchindex', 00229 array('si_title' => $title), 00230 array('si_page' => $id), 00231 'SearchIBM_DB2::updateTitle', 00232 array()); 00233 } 00234 }