r40863 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r40862‎ | r40863 | r40864 >
Date:18:22, 15 September 2008
Author:brion
Status:old
Tags:
Comment:
Hack SearchMySQL's result highlighting regex onto MWSearch so phrase searches match correctly
Modified paths:
  • /trunk/extensions/MWSearch/MWSearch_body.php (modified) (history)

Diff [purge]

Index: trunk/extensions/MWSearch/MWSearch_body.php
@@ -604,10 +604,33 @@
605605 function termMatches() {
606606 $resq = preg_replace( "/\\[.*?\\]:/", " ", $this->mQuery ); # generic prefixes
607607 $resq = preg_replace( "/all:/", " ", $resq );
608 - $resq = trim( preg_replace( "/[ |\\[\\]()\"{}+\\-_@!?%&*=\\|:;><,.\\/]+/", " ", $resq ) );
609 - $terms = array_map( array( &$this, 'regexQuote' ),
610 - explode( ' ', $resq ) );
611 - return $terms;
 608+
 609+ // Fixme: this is ripped from SearchMySQL and probably kind of sucks,
 610+ // but it handles quoted phrase searches more or less correctly.
 611+ // Should encapsulate this stuff better.
 612+
 613+ // FIXME: This doesn't handle parenthetical expressions.
 614+ $regexes = array();
 615+ $m = array();
 616+ $lc = SearchEngine::legalSearchChars();
 617+ if( preg_match_all( '/([-+<>~]?)(([' . $lc . ']+)(\*?)|"[^"]*")/',
 618+ $resq, $m, PREG_SET_ORDER ) ) {
 619+ foreach( $m as $terms ) {
 620+ if( !empty( $terms[3] ) ) {
 621+ // Match individual terms in result highlighting...
 622+ $regexp = preg_quote( $terms[3], '/' );
 623+ if( $terms[4] ) $regexp .= "[0-9A-Za-z_]+";
 624+ } else {
 625+ // Match the quoted term in result highlighting...
 626+ $regexp = preg_quote( str_replace( '"', '', $terms[2] ), '/' );
 627+ }
 628+ $regexes[] = $regexp;
 629+ }
 630+ wfDebug( __METHOD__ . ': Match with /' . implode( '|', $regexes ) . "/\n" );
 631+ } else {
 632+ wfDebug( "Can't understand search query '{$resq}'\n" );
 633+ }
 634+ return $regexes;
612635 }
613636
614637 /**