MediaWiki
REL1_23
|
00001 <?php 00028 class SearchMssql extends SearchDatabase { 00036 function searchText( $term ) { 00037 $resultSet = $this->db->resultObject( $this->db->query( $this->getQuery( $this->filter( $term ), true ) ) ); 00038 return new MssqlSearchResultSet( $resultSet, $this->searchTerms ); 00039 } 00040 00048 function searchTitle( $term ) { 00049 $resultSet = $this->db->resultObject( $this->db->query( $this->getQuery( $this->filter( $term ), false ) ) ); 00050 return new MssqlSearchResultSet( $resultSet, $this->searchTerms ); 00051 } 00052 00059 function queryNamespaces() { 00060 $namespaces = implode( ',', $this->namespaces ); 00061 if ( $namespaces == '' ) { 00062 $namespaces = '0'; 00063 } 00064 return 'AND page_namespace IN (' . $namespaces . ')'; 00065 } 00066 00074 function queryLimit( $sql ) { 00075 return $this->db->limitResult( $sql, $this->limit, $this->offset ); 00076 } 00077 00084 function queryRanking( $filteredTerm, $fulltext ) { 00085 return ' ORDER BY ftindex.[RANK] DESC'; // return ' ORDER BY score(1)'; 00086 } 00087 00096 function getQuery( $filteredTerm, $fulltext ) { 00097 return $this->queryLimit( $this->queryMain( $filteredTerm, $fulltext ) . ' ' . 00098 $this->queryNamespaces() . ' ' . 00099 $this->queryRanking( $filteredTerm, $fulltext ) . ' ' ); 00100 } 00101 00108 function getIndexField( $fulltext ) { 00109 return $fulltext ? 'si_text' : 'si_title'; 00110 } 00111 00120 function queryMain( $filteredTerm, $fulltext ) { 00121 $match = $this->parseQuery( $filteredTerm, $fulltext ); 00122 $page = $this->db->tableName( 'page' ); 00123 $searchindex = $this->db->tableName( 'searchindex' ); 00124 00125 return 'SELECT page_id, page_namespace, page_title, ftindex.[RANK]' . 00126 "FROM $page,FREETEXTTABLE($searchindex , $match, LANGUAGE 'English') as ftindex " . 00127 'WHERE page_id=ftindex.[KEY] '; 00128 } 00129 00133 function parseQuery( $filteredText, $fulltext ) { 00134 global $wgContLang; 00135 $lc = SearchEngine::legalSearchChars(); 00136 $this->searchTerms = array(); 00137 00138 # @todo FIXME: This doesn't handle parenthetical expressions. 00139 $m = array(); 00140 $q = array(); 00141 00142 if ( preg_match_all( '/([-+<>~]?)(([' . $lc . ']+)(\*?)|"[^"]*")/', 00143 $filteredText, $m, PREG_SET_ORDER ) ) { 00144 foreach ( $m as $terms ) { 00145 $q[] = $terms[1] . $wgContLang->normalizeForSearch( $terms[2] ); 00146 00147 if ( !empty( $terms[3] ) ) { 00148 $regexp = preg_quote( $terms[3], '/' ); 00149 if ( $terms[4] ) { 00150 $regexp .= "[0-9A-Za-z_]+"; 00151 } 00152 } else { 00153 $regexp = preg_quote( str_replace( '"', '', $terms[2] ), '/' ); 00154 } 00155 $this->searchTerms[] = $regexp; 00156 } 00157 } 00158 00159 $searchon = $this->db->strencode( join( ',', $q ) ); 00160 $field = $this->getIndexField( $fulltext ); 00161 return "$field, '$searchon'"; 00162 } 00163 00173 function update( $id, $title, $text ) { 00174 // We store the column data as UTF-8 byte order marked binary stream 00175 // because we are invoking the plain text IFilter on it so that, and we want it 00176 // to properly decode the stream as UTF-8. SQL doesn't support UTF8 as a data type 00177 // but the indexer will correctly handle it by this method. Since all we are doing 00178 // is passing this data to the indexer and never retrieving it via PHP, this will save space 00179 $table = $this->db->tableName( 'searchindex' ); 00180 $utf8bom = '0xEFBBBF'; 00181 $si_title = $utf8bom . bin2hex( $title ); 00182 $si_text = $utf8bom . bin2hex( $text ); 00183 $sql = "DELETE FROM $table WHERE si_page = $id;"; 00184 $sql .= "INSERT INTO $table (si_page, si_title, si_text) VALUES ($id, $si_title, $si_text)"; 00185 return $this->db->query( $sql, 'SearchMssql::update' ); 00186 } 00187 00196 function updateTitle( $id, $title ) { 00197 $table = $this->db->tableName( 'searchindex' ); 00198 00199 // see update for why we are using the utf8bom 00200 $utf8bom = '0xEFBBBF'; 00201 $si_title = $utf8bom . bin2hex( $title ); 00202 $sql = "DELETE FROM $table WHERE si_page = $id;"; 00203 $sql .= "INSERT INTO $table (si_page, si_title, si_text) VALUES ($id, $si_title, 0x00)"; 00204 return $this->db->query( $sql, 'SearchMssql::updateTitle' ); 00205 } 00206 } 00207 00211 class MssqlSearchResultSet extends SearchResultSet { 00212 function __construct( $resultSet, $terms ) { 00213 $this->mResultSet = $resultSet; 00214 $this->mTerms = $terms; 00215 } 00216 00217 function termMatches() { 00218 return $this->mTerms; 00219 } 00220 00221 function numRows() { 00222 return $this->mResultSet->numRows(); 00223 } 00224 00225 function next() { 00226 $row = $this->mResultSet->fetchObject(); 00227 if ( $row === false ) { 00228 return false; 00229 } 00230 return new SearchResult( $row ); 00231 } 00232 }