MediaWiki  REL1_20
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 
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 
00120         function getQuery( $filteredTerm, $fulltext ) {
00121                 return $this->queryLimit( $this->queryMain( $filteredTerm, $fulltext ) . ' ' .
00122                         $this->queryRedirect() . ' ' .
00123                         $this->queryNamespaces() . ' ' .
00124                         $this->queryRanking( $filteredTerm, $fulltext ) . ' ' );
00125         }
00126 
00133         function getIndexField( $fulltext ) {
00134                 return $fulltext ? 'si_text' : 'si_title';
00135         }
00136 
00145         function queryMain( $filteredTerm, $fulltext ) {
00146                 $match = $this->parseQuery( $filteredTerm, $fulltext );
00147                 $page        = $this->db->tableName( 'page' );
00148                 $searchindex = $this->db->tableName( 'searchindex' );
00149                 
00150                 return 'SELECT page_id, page_namespace, page_title, ftindex.[RANK]' .
00151                         "FROM $page,FREETEXTTABLE($searchindex , $match, LANGUAGE 'English') as ftindex " .
00152                         'WHERE page_id=ftindex.[KEY] ';
00153         }
00154 
00158         function parseQuery( $filteredText, $fulltext ) {
00159                 global $wgContLang;
00160                 $lc = SearchEngine::legalSearchChars();
00161                 $this->searchTerms = array();
00162 
00163                 # @todo FIXME: This doesn't handle parenthetical expressions.
00164                 $m = array();
00165                 $q = array();
00166 
00167                 if ( preg_match_all( '/([-+<>~]?)(([' . $lc . ']+)(\*?)|"[^"]*")/',
00168                         $filteredText, $m, PREG_SET_ORDER ) ) {
00169                         foreach ( $m as $terms ) {
00170                                 $q[] = $terms[1] . $wgContLang->normalizeForSearch( $terms[2] );
00171 
00172                                 if ( !empty( $terms[3] ) ) {
00173                                         $regexp = preg_quote( $terms[3], '/' );
00174                                         if ( $terms[4] )
00175                                                 $regexp .= "[0-9A-Za-z_]+";
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                 return new SearchResult( $row );
00254         }
00255 }
00256 
00257