MediaWiki
REL1_22
|
00001 <?php 00028 class SearchMssql extends SearchEngine { 00029 00034 function __construct( $db ) { 00035 parent::__construct( $db ); 00036 } 00037 00045 function searchText( $term ) { 00046 $resultSet = $this->db->resultObject( $this->db->query( $this->getQuery( $this->filter( $term ), true ) ) ); 00047 return new MssqlSearchResultSet( $resultSet, $this->searchTerms ); 00048 } 00049 00057 function searchTitle( $term ) { 00058 $resultSet = $this->db->resultObject( $this->db->query( $this->getQuery( $this->filter( $term ), false ) ) ); 00059 return new MssqlSearchResultSet( $resultSet, $this->searchTerms ); 00060 } 00061 00068 function queryRedirect() { 00069 if ( $this->showRedirects ) { 00070 return ''; 00071 } else { 00072 return 'AND page_is_redirect=0'; 00073 } 00074 } 00075 00082 function queryNamespaces() { 00083 $namespaces = implode( ',', $this->namespaces ); 00084 if ( $namespaces == '' ) { 00085 $namespaces = '0'; 00086 } 00087 return 'AND page_namespace IN (' . $namespaces . ')'; 00088 } 00089 00097 function queryLimit( $sql ) { 00098 return $this->db->limitResult( $sql, $this->limit, $this->offset ); 00099 } 00100 00107 function queryRanking( $filteredTerm, $fulltext ) { 00108 return ' ORDER BY ftindex.[RANK] DESC'; // return ' ORDER BY score(1)'; 00109 } 00110 00119 function getQuery( $filteredTerm, $fulltext ) { 00120 return $this->queryLimit( $this->queryMain( $filteredTerm, $fulltext ) . ' ' . 00121 $this->queryRedirect() . ' ' . 00122 $this->queryNamespaces() . ' ' . 00123 $this->queryRanking( $filteredTerm, $fulltext ) . ' ' ); 00124 } 00125 00132 function getIndexField( $fulltext ) { 00133 return $fulltext ? 'si_text' : 'si_title'; 00134 } 00135 00144 function queryMain( $filteredTerm, $fulltext ) { 00145 $match = $this->parseQuery( $filteredTerm, $fulltext ); 00146 $page = $this->db->tableName( 'page' ); 00147 $searchindex = $this->db->tableName( 'searchindex' ); 00148 00149 return 'SELECT page_id, page_namespace, page_title, ftindex.[RANK]' . 00150 "FROM $page,FREETEXTTABLE($searchindex , $match, LANGUAGE 'English') as ftindex " . 00151 'WHERE page_id=ftindex.[KEY] '; 00152 } 00153 00157 function parseQuery( $filteredText, $fulltext ) { 00158 global $wgContLang; 00159 $lc = SearchEngine::legalSearchChars(); 00160 $this->searchTerms = array(); 00161 00162 # @todo FIXME: This doesn't handle parenthetical expressions. 00163 $m = array(); 00164 $q = array(); 00165 00166 if ( preg_match_all( '/([-+<>~]?)(([' . $lc . ']+)(\*?)|"[^"]*")/', 00167 $filteredText, $m, PREG_SET_ORDER ) ) { 00168 foreach ( $m as $terms ) { 00169 $q[] = $terms[1] . $wgContLang->normalizeForSearch( $terms[2] ); 00170 00171 if ( !empty( $terms[3] ) ) { 00172 $regexp = preg_quote( $terms[3], '/' ); 00173 if ( $terms[4] ) { 00174 $regexp .= "[0-9A-Za-z_]+"; 00175 } 00176 } else { 00177 $regexp = preg_quote( str_replace( '"', '', $terms[2] ), '/' ); 00178 } 00179 $this->searchTerms[] = $regexp; 00180 } 00181 } 00182 00183 $searchon = $this->db->strencode( join( ',', $q ) ); 00184 $field = $this->getIndexField( $fulltext ); 00185 return "$field, '$searchon'"; 00186 } 00187 00197 function update( $id, $title, $text ) { 00198 // We store the column data as UTF-8 byte order marked binary stream 00199 // because we are invoking the plain text IFilter on it so that, and we want it 00200 // to properly decode the stream as UTF-8. SQL doesn't support UTF8 as a data type 00201 // but the indexer will correctly handle it by this method. Since all we are doing 00202 // is passing this data to the indexer and never retrieving it via PHP, this will save space 00203 $table = $this->db->tableName( 'searchindex' ); 00204 $utf8bom = '0xEFBBBF'; 00205 $si_title = $utf8bom . bin2hex( $title ); 00206 $si_text = $utf8bom . bin2hex( $text ); 00207 $sql = "DELETE FROM $table WHERE si_page = $id;"; 00208 $sql .= "INSERT INTO $table (si_page, si_title, si_text) VALUES ($id, $si_title, $si_text)"; 00209 return $this->db->query( $sql, 'SearchMssql::update' ); 00210 } 00211 00220 function updateTitle( $id, $title ) { 00221 $table = $this->db->tableName( 'searchindex' ); 00222 00223 // see update for why we are using the utf8bom 00224 $utf8bom = '0xEFBBBF'; 00225 $si_title = $utf8bom . bin2hex( $title ); 00226 $sql = "DELETE FROM $table WHERE si_page = $id;"; 00227 $sql .= "INSERT INTO $table (si_page, si_title, si_text) VALUES ($id, $si_title, 0x00)"; 00228 return $this->db->query( $sql, 'SearchMssql::updateTitle' ); 00229 } 00230 } 00231 00235 class MssqlSearchResultSet extends SearchResultSet { 00236 function __construct( $resultSet, $terms ) { 00237 $this->mResultSet = $resultSet; 00238 $this->mTerms = $terms; 00239 } 00240 00241 function termMatches() { 00242 return $this->mTerms; 00243 } 00244 00245 function numRows() { 00246 return $this->mResultSet->numRows(); 00247 } 00248 00249 function next() { 00250 $row = $this->mResultSet->fetchObject(); 00251 if ( $row === false ) { 00252 return false; 00253 } 00254 return new SearchResult( $row ); 00255 } 00256 }