Added wfAbruptExit() function, to replace exit() calls with.
[lhc/web/wiklou.git] / includes / SearchEngine.php
index 6965eb7..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()
@@ -37,14 +38,12 @@ class SearchEngine {
                return "AND cur_is_redirect=0 ";
        }
 
-
-
        /* private */ function initNamespaceCheckbox( $i )
        {
                global $wgUser, $wgNamespacesToBeSearchedDefault;
                
 
-               if ($wgUser->getRights()) {
+               if ($wgUser->getID()) {
                        // User is logged in so we retrieve his default namespaces
                        return $wgUser->getOption( "searchNs".$i );
                }
@@ -54,59 +53,59 @@ class SearchEngine {
                }
        }
 
-
-
+       # Display the "power search" footer. Does not actually perform the search, 
+       # that is done by showResults()
        function powersearch()
        {
                global $wgUser, $wgOut, $wgLang, $wgTitle;
-               $nscb = array();
 
                $search                 = $_REQUEST['search'];
                $searchx                = $_REQUEST['searchx'];
                $listredirs             = $_REQUEST['redirs'];
+               
+               $ret = wfMsg("powersearchtext"); # Text to be returned
+               $tempText = ""; # Temporary text, for substitution into $ret    
 
-
-               if ( ! isset ( $searchx ) ) {   /* First time here */
-                       $listredirs = 1;
-                       for ($i = 0; ($i <= 7); $i++)
-                       {
-                               $nscb[$i] = $this->initNamespaceCheckbox($i);
-                       }
-               } else {
-                       $nscb[0]                = $_REQUEST['ns0'];
-                       $nscb[1]                = $_REQUEST['ns1'];
-                       $nscb[2]                = $_REQUEST['ns2'];
-                       $nscb[3]                = $_REQUEST['ns3'];
-                       $nscb[4]                = $_REQUEST['ns4'];
-                       $nscb[5]                = $_REQUEST['ns5'];
-                       $nscb[6]                = $_REQUEST['ns6'];
-                       $nscb[7]                = $_REQUEST['ns7'];
+               if( isset( $_REQUEST["searchx"] ) ) {
+                       $this->addtoquery["searchx"] = "1";
                }
+               
+               # Do namespace checkboxes
+               $namespaces = $wgLang->getNamespaces();
+               foreach ( $namespaces as $i => $namespace ) {
+                       # Skip virtual namespaces
+                       if ( $i < 0 ) {
+                               continue;
+                       }
 
-               $this->checkboxes["searchx"] = 1;
-               $ret = wfMsg("powersearchtext");
-
-               # Determine namespace checkboxes
+                       $formVar = "ns$i";
 
-               $ns = $wgLang->getNamespaces();
-               array_shift( $ns ); /* Skip "Special" */
+                       # Initialise checkboxValues, either from defaults or from 
+                       # a previous invocation
+                       if ( !isset( $searchx ) ) {
+                               $checkboxValue = $this->initNamespaceCheckbox( $i );
+                       } else {
+                               $checkboxValue = $_REQUEST[$formVar];
+                       }
 
-               $r1 = "";
-               for ( $i = 0; $i < count( $ns ); ++$i ) {
                        $checked = "";
-                       if ( $nscb[$i] == 1 ) {
+                       if ( $checkboxValue == 1 ) {
                                $checked = " checked";
                                $this->addtoquery["ns{$i}"] = 1;
                                array_push( $this->namespacesToSearch, $i );
                        }
-                       $name = str_replace( "_", " ", $ns[$i] );
-                       if ( "" == $name ) { $name = wfMsg( "blanknamespace" ); }
+                       $name = str_replace( "_", " ", $namespaces[$i] );
+                       if ( "" == $name ) { 
+                               $name = wfMsg( "blanknamespace" ); 
+                       }
 
-                       if ( 0 != $i ) { $r1 .= " "; }
-                       $r1 .= "<input type=checkbox value=\"1\" name=\"" .
+                       if ( $tempText !== "" ) { 
+                               $tempText .= " "; 
+                       }
+                       $tempText .= "<input type=checkbox value=\"1\" name=\"" .
                          "ns{$i}\"{$checked}>{$name}\n";
                }
-               $ret = str_replace ( "$1", $r1, $ret );
+               $ret = str_replace ( "$1", $tempText, $ret );
 
                # List redirects checkbox
 
@@ -115,30 +114,33 @@ class SearchEngine {
                        $this->addtoquery["redirs"] = 1;
                        $checked = " checked";
                }
-               $r2 = "<input type=checkbox value=1 name=\"redirs\"{$checked}>\n";
-               $ret = str_replace( "$2", $r2, $ret );
+               $tempText = "<input type=checkbox value=1 name=\"redirs\"{$checked}>\n";
+               $ret = str_replace( "$2", $tempText, $ret );
 
                # Search field
 
-               $r3 = "<input type=text name=\"search\" value=\"" .
+               $tempText = "<input type=text name=\"search\" value=\"" .
                        htmlspecialchars( $search ) ."\" width=80>\n";
-        $ret = str_replace( "$3", $r3, $ret );
+        $ret = str_replace( "$3", $tempText, $ret );
 
                # Searchx button
 
-               $r9 = "<input type=submit name=\"searchx\" value=\"" .
+               $tempText = "<input type=submit name=\"searchx\" value=\"" .
                  wfMsg("powersearch") . "\">\n";
-               $ret = str_replace( "$9", $r9, $ret );
+               $ret = str_replace( "$9", $tempText, $ret );
 
                $ret = "<br><br>\n<form id=\"powersearch\" method=\"get\" " .
                  "action=\"" . wfLocalUrl( "" ) . "\">\n{$ret}\n</form>\n";
 
                if ( isset ( $searchx ) ) {
-                       if ( ! $listredirs ) { $this->doSearchRedirects = false; }
+                       if ( ! $listredirs ) { 
+                               $this->doSearchRedirects = false; 
+                       }
                }
                return $ret;
        }
 
+       # Perform the search and construct the results page
        function showResults()
        {
                global $wgUser, $wgTitle, $wgOut, $wgLang, $wgDisableTextSearch;
@@ -171,17 +173,41 @@ class SearchEngine {
                $searchnamespaces = $this->queryNamespaces();
                $redircond = $this->searchRedirects();
 
-               $sql = "SELECT cur_id,cur_namespace,cur_title," .
-                 "cur_text FROM cur,searchindex " .
-                 "WHERE cur_id=si_page AND {$this->mTitlecond} " .
-                 "{$searchnamespaces} {$redircond}" .
-                 "LIMIT {$offset}, {$limit}";
-               $res1 = wfQuery( $sql, DB_READ, $fname );
-               $num = wfNumRows($res1);
-
                if ( $wgDisableTextSearch ) {
-                       $res2 = 0;
+                       $wgOut->addHTML( wfMsg( "searchdisabled", htmlspecialchars( $search ), $wgInputEncoding ) );
                } else {
+                       $sql = "SELECT cur_id,cur_namespace,cur_title," .
+                         "cur_text FROM cur,searchindex " .
+                         "WHERE cur_id=si_page AND {$this->mTitlecond} " .
+                         "{$searchnamespaces} {$redircond}" .
+                         "LIMIT {$offset}, {$limit}";
+                       $res1 = wfQuery( $sql, DB_READ, $fname );
+                       $num = wfNumRows($res1);
+
+                       $sk = $wgUser->getSkin();
+                       $text = wfMsg( "searchresulttext", $sk->makeKnownLink(
+                         wfMsg( "searchhelppage" ), wfMsg( "searchingwikipedia" ) ) );
+                       $wgOut->addHTML( $text );
+       
+                       $this->parseQuery();
+                       if ( "" == $this->mTitlecond || "" == $this->mTextcond ) {
+                               $wgOut->addHTML( "<h2>" . wfMsg( "badquery" ) . "</h2>\n" .
+                                 "<p>" . wfMsg( "badquerytext" ) );
+                               return;
+                       }
+                       list( $limit, $offset ) = wfCheckLimits( 20, "searchlimit" );
+       
+                       $searchnamespaces = $this->queryNamespaces();
+                       $redircond = $this->searchRedirects();
+       
+                       $sql = "SELECT cur_id,cur_namespace,cur_title," .
+                         "cur_text FROM cur,searchindex " .
+                         "WHERE cur_id=si_page AND {$this->mTitlecond} " .
+                         "{$searchnamespaces} {$redircond}" .
+                         "LIMIT {$offset}, {$limit}";
+                       $res1 = wfQuery( $sql, DB_READ, $fname );
+                       $num = wfNumRows($res1);
+       
                        $sql = "SELECT cur_id,cur_namespace,cur_title," .
                          "cur_text FROM cur,searchindex " .
                          "WHERE cur_id=si_page AND {$this->mTextcond} " .
@@ -189,48 +215,44 @@ class SearchEngine {
                          "LIMIT {$offset}, {$limit}";
                        $res2 = wfQuery( $sql, DB_READ, $fname );
                        $num = $num + wfNumRows($res2);
-               }
-
-                if ( $num == $limit ) {
-                 $top = wfShowingResults( $offset, $limit);
-               } else {
-                 $top = wfShowingResultsNum( $offset, $limit, $num );
-               }
-               $wgOut->addHTML( "<p>{$top}\n" );
-
-               # For powersearch
-
-               $a2l = "" ;
-               $akk = array_keys( $this->addtoquery ) ;
-               foreach ( $akk AS $ak ) {
-                       $a2l .= "&{$ak}={$this->addtoquery[$ak]}" ;
-               }
-
-               $sl = wfViewPrevNext( $offset, $limit, "",
-                 "search=" . wfUrlencode( $this->mUsertext ) . $a2l );
-               $wgOut->addHTML( "<br>{$sl}\n" );
-
-               $foundsome = false;
-
-               if ( 0 == wfNumRows( $res1 ) ) {
-                       $wgOut->addHTML( "<h2>" . wfMsg( "notitlematches" ) .
-                         "</h2>\n" );
-               } else {
-                       $foundsome = true;
-                       $off = $offset + 1;
-                       $wgOut->addHTML( "<h2>" . wfMsg( "titlematches" ) .
-                         "</h2>\n<ol start='{$off}'>" );
 
-                       while ( $row = wfFetchObject( $res1 ) ) {
-                               $this->showHit( $row );
+                       if ( $num == $limit ) {
+                         $top = wfShowingResults( $offset, $limit);
+                       } else {
+                         $top = wfShowingResultsNum( $offset, $limit, $num );
+                       }
+                       $wgOut->addHTML( "<p>{$top}\n" );
+       
+                       # For powersearch
+       
+                       $a2l = "" ;
+                       $akk = array_keys( $this->addtoquery ) ;
+                       foreach ( $akk AS $ak ) {
+                               $a2l .= "&{$ak}={$this->addtoquery[$ak]}" ;
+                       }
+       
+                       $sl = wfViewPrevNext( $offset, $limit, "",
+                         "search=" . wfUrlencode( $this->mUsertext ) . $a2l );
+                       $wgOut->addHTML( "<br>{$sl}\n" );
+       
+                       $foundsome = false;
+       
+                       if ( 0 == wfNumRows( $res1 ) ) {
+                               $wgOut->addHTML( "<h2>" . wfMsg( "notitlematches" ) .
+                                 "</h2>\n" );
+                       } else {
+                               $foundsome = true;
+                               $off = $offset + 1;
+                               $wgOut->addHTML( "<h2>" . wfMsg( "titlematches" ) .
+                                 "</h2>\n<ol start='{$off}'>" );
+       
+                               while ( $row = wfFetchObject( $res1 ) ) {
+                                       $this->showHit( $row );
+                               }
+                               wfFreeResult( $res1 );
+                               $wgOut->addHTML( "</ol>\n" );
                        }
-                       wfFreeResult( $res1 );
-                       $wgOut->addHTML( "</ol>\n" );
-               }
 
-               if ( $wgDisableTextSearch ) {
-                       $wgOut->addHTML( wfMsg( "searchdisabled", $search, $wgInputEncoding ) );
-               } else {
                        if ( 0 == wfNumRows( $res2 ) ) {
                                $wgOut->addHTML( "<h2>" . wfMsg( "notextmatches" ) .
                                  "</h2>\n" );
@@ -245,12 +267,12 @@ class SearchEngine {
                                wfFreeResult( $res2 );
                                $wgOut->addHTML( "</ol>\n" );
                        }
+                       if ( ! $foundsome ) {
+                               $wgOut->addHTML( "<p>" . wfMsg( "nonefound" ) . "\n" );
+                       }
+                       $wgOut->addHTML( "<p>{$sl}\n" );
+                       $wgOut->addHTML( $powersearch );
                }
-               if ( ! $foundsome ) {
-                       $wgOut->addHTML( "<p>" . wfMsg( "nonefound" ) . "\n" );
-               }
-               $wgOut->addHTML( "<p>{$sl}\n" );
-               $wgOut->addHTML( $powersearch );
        }
 
        function legalSearchChars()
@@ -303,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 )
@@ -331,7 +371,7 @@ class SearchEngine {
                if ( "" == $contextchars ) { $contextchars = 50; }
 
                $link = $sk->makeKnownLink( $t, "" );
-               $size = str_replace( "$1", strlen( $row->cur_text ), WfMsg( "nbytes" ) );
+               $size = wfMsg( "nbytes", strlen( $row->cur_text ) );
                $wgOut->addHTML( "<li>{$link} ({$size})" );
 
                $lines = explode( "\n", $row->cur_text );
@@ -377,15 +417,21 @@ class SearchEngine {
 
        function goResult()
        {
-               global $wgOut;
+               global $wgOut, $wgDisableTextSearch;
                $fname = "SearchEngine::goResult";
                
                $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;
@@ -417,19 +463,21 @@ class SearchEngine {
 
                # Try a near match
                #
-               $this->parseQuery();                                                                            
-               $sql = "SELECT cur_id,cur_title,cur_namespace,si_page FROM cur,searchindex " .
-                 "WHERE cur_id=si_page AND {$this->mTitlecond} ORDER BY cur_namespace LIMIT 1";
-
-               if ( "" != $this->mTitlecond ) {
-                       $res = wfQuery( $sql, DB_READ, $fname );
-               }                               
-               if ( isset( $res ) && 0 != wfNumRows( $res ) ) {
-                       $s = wfFetchObject( $res );
-
-                       $t = Title::makeTitle( $s->cur_namespace, $s->cur_title );
-                       $wgOut->redirect( wfLocalUrl( $t->getPrefixedURL() ) );
-                       return;
+               if( !$wgDisableTextSearch ) {
+                       $this->parseQuery();                                                                            
+                       $sql = "SELECT cur_id,cur_title,cur_namespace,si_page FROM cur,searchindex " .
+                         "WHERE cur_id=si_page AND {$this->mTitlecond} ORDER BY cur_namespace LIMIT 1";
+       
+                       if ( "" != $this->mTitlecond ) {
+                               $res = wfQuery( $sql, DB_READ, $fname );
+                       }                               
+                       if ( isset( $res ) && 0 != wfNumRows( $res ) ) {
+                               $s = wfFetchObject( $res );
+       
+                               $t = Title::makeTitle( $s->cur_namespace, $s->cur_title );
+                               $wgOut->redirect( wfLocalUrl( $t->getPrefixedURL() ) );
+                               return;
+                       }
                }
                $wgOut->addHTML( wfMsg("nogomatch", 
                  htmlspecialchars( wfLocalUrl( ucfirst($this->mUsertext), "action=edit") ) )
@@ -438,3 +486,4 @@ class SearchEngine {
        }
 }
 
+?>