MediaWiki
REL1_19
|
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 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 00139 function queryMain( $filteredTerm, $fulltext ) { 00140 $match = $this->parseQuery($filteredTerm, $fulltext); 00141 $page = $this->db->tableName('page'); 00142 $searchindex = $this->db->tableName('searchindex'); 00143 return 'SELECT page_id, page_namespace, page_title ' . 00144 "FROM $page,$searchindex " . 00145 'WHERE page_id=si_page AND ' . $match; 00146 } 00147 00149 function parseQuery($filteredText, $fulltext) { 00150 global $wgContLang; 00151 $lc = SearchEngine::legalSearchChars(); 00152 $this->searchTerms = array(); 00153 00154 # @todo FIXME: This doesn't handle parenthetical expressions. 00155 $m = array(); 00156 $q = array(); 00157 00158 if (preg_match_all('/([-+<>~]?)(([' . $lc . ']+)(\*?)|"[^"]*")/', 00159 $filteredText, $m, PREG_SET_ORDER)) { 00160 foreach($m as $terms) { 00161 00162 // Search terms in all variant forms, only 00163 // apply on wiki with LanguageConverter 00164 $temp_terms = $wgContLang->autoConvertToAllVariants( $terms[2] ); 00165 if( is_array( $temp_terms )) { 00166 $temp_terms = array_unique( array_values( $temp_terms )); 00167 foreach( $temp_terms as $t ) 00168 $q[] = $terms[1] . $wgContLang->normalizeForSearch( $t ); 00169 } 00170 else 00171 $q[] = $terms[1] . $wgContLang->normalizeForSearch( $terms[2] ); 00172 00173 if (!empty($terms[3])) { 00174 $regexp = preg_quote( $terms[3], '/' ); 00175 if ($terms[4]) 00176 $regexp .= "[0-9A-Za-z_]+"; 00177 } else { 00178 $regexp = preg_quote(str_replace('"', '', $terms[2]), '/'); 00179 } 00180 $this->searchTerms[] = $regexp; 00181 } 00182 } 00183 00184 $searchon = $this->db->strencode(join(',', $q)); 00185 $field = $this->getIndexField($fulltext); 00186 00187 // requires Net Search Extender or equivalent 00188 //return " CONTAINS($field, '$searchon') > 0 "; 00189 00190 return " lcase($field) LIKE lcase('%$searchon%')"; 00191 } 00192 00201 function update($id, $title, $text) { 00202 $dbw = wfGetDB(DB_MASTER); 00203 $dbw->replace('searchindex', 00204 array('si_page'), 00205 array( 00206 'si_page' => $id, 00207 'si_title' => $title, 00208 'si_text' => $text 00209 ), 'SearchIBM_DB2::update' ); 00210 // ? 00211 //$dbw->query("CALL ctx_ddl.sync_index('si_text_idx')"); 00212 //$dbw->query("CALL ctx_ddl.sync_index('si_title_idx')"); 00213 } 00214 00222 function updateTitle($id, $title) { 00223 $dbw = wfGetDB(DB_MASTER); 00224 00225 $dbw->update('searchindex', 00226 array('si_title' => $title), 00227 array('si_page' => $id), 00228 'SearchIBM_DB2::updateTitle', 00229 array()); 00230 } 00231 }