MediaWiki  REL1_21
SearchMssql.php
Go to the documentation of this file.
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                                 } else {
00176                                         $regexp = preg_quote( str_replace( '"', '', $terms[2] ), '/' );
00177                                 }
00178                                 $this->searchTerms[] = $regexp;
00179                         }
00180                 }
00181 
00182                 $searchon = $this->db->strencode( join( ',', $q ) );
00183                 $field = $this->getIndexField( $fulltext );
00184                 return "$field, '$searchon'";
00185         }
00186 
00196         function update( $id, $title, $text ) {
00197                 // We store the column data as UTF-8 byte order marked binary stream
00198                 // because we are invoking the plain text IFilter on it so that, and we want it
00199                 // to properly decode the stream as UTF-8.  SQL doesn't support UTF8 as a data type
00200                 // but the indexer will correctly handle it by this method.  Since all we are doing
00201                 // is passing this data to the indexer and never retrieving it via PHP, this will save space
00202                 $table = $this->db->tableName( 'searchindex' );
00203                 $utf8bom = '0xEFBBBF';
00204                 $si_title = $utf8bom . bin2hex( $title );
00205                 $si_text = $utf8bom . bin2hex( $text );
00206                 $sql = "DELETE FROM $table WHERE si_page = $id;";
00207                 $sql .= "INSERT INTO $table (si_page, si_title, si_text) VALUES ($id, $si_title, $si_text)";
00208                 return $this->db->query( $sql, 'SearchMssql::update' );
00209         }
00210 
00219         function updateTitle( $id, $title ) {
00220                 $table = $this->db->tableName( 'searchindex' );
00221 
00222                 // see update for why we are using the utf8bom
00223                 $utf8bom = '0xEFBBBF';
00224                 $si_title = $utf8bom . bin2hex( $title );
00225                 $sql = "DELETE FROM $table WHERE si_page = $id;";
00226                 $sql .= "INSERT INTO $table (si_page, si_title, si_text) VALUES ($id, $si_title, 0x00)";
00227                 return $this->db->query( $sql, 'SearchMssql::updateTitle' );
00228         }
00229 }
00230 
00234 class MssqlSearchResultSet extends SearchResultSet {
00235         function __construct( $resultSet, $terms ) {
00236                 $this->mResultSet = $resultSet;
00237                 $this->mTerms = $terms;
00238         }
00239 
00240         function termMatches() {
00241                 return $this->mTerms;
00242         }
00243 
00244         function numRows() {
00245                 return $this->mResultSet->numRows();
00246         }
00247 
00248         function next() {
00249                 $row = $this->mResultSet->fetchObject();
00250                 if ( $row === false )
00251                         return false;
00252                 return new SearchResult( $row );
00253         }
00254 }