MediaWiki
REL1_19
|
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 00062 00069 function queryRedirect() { 00070 if ( $this->showRedirects ) { 00071 return ''; 00072 } else { 00073 return 'AND page_is_redirect=0'; 00074 } 00075 } 00076 00083 function queryNamespaces() { 00084 $namespaces = implode( ',', $this->namespaces ); 00085 if ( $namespaces == '' ) { 00086 $namespaces = '0'; 00087 } 00088 return 'AND page_namespace IN (' . $namespaces . ')'; 00089 } 00090 00098 function queryLimit( $sql ) { 00099 return $this->db->limitResult( $sql, $this->limit, $this->offset ); 00100 } 00101 00108 function queryRanking( $filteredTerm, $fulltext ) { 00109 return ' ORDER BY ftindex.[RANK] DESC'; // return ' ORDER BY score(1)'; 00110 } 00111 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 00155 function parseQuery( $filteredText, $fulltext ) { 00156 global $wgContLang; 00157 $lc = SearchEngine::legalSearchChars(); 00158 $this->searchTerms = array(); 00159 00160 # @todo FIXME: This doesn't handle parenthetical expressions. 00161 $m = array(); 00162 $q = array(); 00163 00164 if ( preg_match_all( '/([-+<>~]?)(([' . $lc . ']+)(\*?)|"[^"]*")/', 00165 $filteredText, $m, PREG_SET_ORDER ) ) { 00166 foreach ( $m as $terms ) { 00167 $q[] = $terms[1] . $wgContLang->normalizeForSearch( $terms[2] ); 00168 00169 if ( !empty( $terms[3] ) ) { 00170 $regexp = preg_quote( $terms[3], '/' ); 00171 if ( $terms[4] ) 00172 $regexp .= "[0-9A-Za-z_]+"; 00173 } else { 00174 $regexp = preg_quote( str_replace( '"', '', $terms[2] ), '/' ); 00175 } 00176 $this->searchTerms[] = $regexp; 00177 } 00178 } 00179 00180 $searchon = $this->db->strencode( join( ',', $q ) ); 00181 $field = $this->getIndexField( $fulltext ); 00182 return "$field, '$searchon'"; 00183 } 00184 00193 function update( $id, $title, $text ) { 00194 // We store the column data as UTF-8 byte order marked binary stream 00195 // because we are invoking the plain text IFilter on it so that, and we want it 00196 // to properly decode the stream as UTF-8. SQL doesn't support UTF8 as a data type 00197 // but the indexer will correctly handle it by this method. Since all we are doing 00198 // is passing this data to the indexer and never retrieving it via PHP, this will save space 00199 $table = $this->db->tableName( 'searchindex' ); 00200 $utf8bom = '0xEFBBBF'; 00201 $si_title = $utf8bom . bin2hex( $title ); 00202 $si_text = $utf8bom . bin2hex( $text ); 00203 $sql = "DELETE FROM $table WHERE si_page = $id;"; 00204 $sql .= "INSERT INTO $table (si_page, si_title, si_text) VALUES ($id, $si_title, $si_text)"; 00205 return $this->db->query( $sql, 'SearchMssql::update' ); 00206 } 00207 00215 function updateTitle( $id, $title ) { 00216 $table = $this->db->tableName( 'searchindex' ); 00217 00218 // see update for why we are using the utf8bom 00219 $utf8bom = '0xEFBBBF'; 00220 $si_title = $utf8bom . bin2hex( $title ); 00221 $sql = "DELETE FROM $table WHERE si_page = $id;"; 00222 $sql .= "INSERT INTO $table (si_page, si_title, si_text) VALUES ($id, $si_title, 0x00)"; 00223 return $this->db->query( $sql, 'SearchMssql::updateTitle' ); 00224 } 00225 } 00226 00230 class MssqlSearchResultSet extends SearchResultSet { 00231 function __construct( $resultSet, $terms ) { 00232 $this->mResultSet = $resultSet; 00233 $this->mTerms = $terms; 00234 } 00235 00236 function termMatches() { 00237 return $this->mTerms; 00238 } 00239 00240 function numRows() { 00241 return $this->mResultSet->numRows(); 00242 } 00243 00244 function next() { 00245 $row = $this->mResultSet->fetchObject(); 00246 if ( $row === false ) 00247 return false; 00248 return new SearchResult( $row ); 00249 } 00250 } 00251 00252