Added wfAbruptExit() function, to replace exit() calls with.
[lhc/web/wiklou.git] / includes / SearchEngine.php
index cca48df..6ce2156 100644 (file)
@@ -19,6 +19,7 @@ class SearchEngine {
                if( $wgDBmysql4 ) $lc .= "\"~<>*+-";
                $this->mUsertext = trim( preg_replace( "/[^{$lc}]/", " ", $text ) );
                $this->mSearchterms = array();
+               $this->mStrictMatching = true; # Google-style, add '+' on all terms
        }
 
        function queryNamespaces()
@@ -324,19 +325,37 @@ class SearchEngine {
        
        function parseQuery4()
        {
-               # FIXME: not ready yet! Do not use.
-               
                global $wgLang;
                $lc = SearchEngine::legalSearchChars();
-               #$q = preg_replace( "/([+-]?)([$lc]+)/e",
-               #       "\"$1\" . \$wgLang->stripForSearch(\"$2\")",
-               #       $this->mUsertext );
+               $searchon = "";
+               $this->mSearchterms = array();
+
+               # FIXME: This doesn't handle parenthetical expressions.
+               if( preg_match_all( '/([-+<>~]?)(([' . $lc . ']+)(\*?)|"[^"]*")/',
+                         $this->mUsertext, $m, PREG_SET_ORDER ) ) {
+                       foreach( $m as $terms ) {
+                               if( $searchon !== "" ) $searchon .= " ";
+                               if( $this->mStrictMatching && ($terms[1] == "") ) {
+                                       $terms[1] = "+";
+                               }
+                               $searchon .= $terms[1] . $wgLang->stripForSearch( $terms[2] );
+                               if( $terms[3] ) {
+                                       $regexp = preg_quote( $terms[3] );
+                                       if( $terms[4] ) $regexp .= "[0-9A-Za-z_]+";
+                               } else {
+                                       $regexp = preg_quote( str_replace( '"', '', $terms[2] ) );
+                               }
+                               $this->mSearchterms[] = $regexp;
+                       }
+                       wfDebug( "Would search with '$searchon'\n" );
+                       wfDebug( "Match with /\b" . implode( '\b|\b', $this->mSearchterms ) . "\b/\n" );
+               } else {
+                       wfDebug( "Can't understand search query '$this->mUsertext'\n" );
+               }
                
-               $q = $this->mUsertext;
-               $qq = wfStrencode( $wgLang->stripForSearch( $q ) );
-               $this->mSearchterms = preg_split( '/\s+/', $q );
-               $this->mTitlecond = " MATCH(si_title) AGAINST('$qq' IN BOOLEAN MODE)";
-               $this->mTextcond = " (MATCH(si_text) AGAINST('$qq' IN BOOLEAN MODE) AND cur_is_redirect=0)";
+               $searchon = wfStrencode( $searchon );
+               $this->mTitlecond = " MATCH(si_title) AGAINST('$searchon' IN BOOLEAN MODE)";
+               $this->mTextcond = " (MATCH(si_text) AGAINST('$searchon' IN BOOLEAN MODE) AND cur_is_redirect=0)";
        }
 
        function showHit( $row )
@@ -403,10 +422,16 @@ class SearchEngine {
                
                $search         = $_REQUEST['search'];
 
-               # First try to go to page as entered            
+               # First try to go to page as entered.
                #
                $t = Title::newFromText( $search );
 
+               # If the string cannot be used to create a title
+               if( false == $t ){ 
+                       $this->showResults();
+                       return;
+               }
+
                if ( 0 != $t->getArticleID() ) {
                        $wgOut->redirect( wfLocalUrl( $t->getPrefixedURL() ) );
                        return;