* Fixed magic quotes in $_REQUEST, in Setup.php
[lhc/web/wiklou.git] / includes / SearchEngine.php
index 90e0f5b..ee38899 100644 (file)
@@ -1,4 +1,4 @@
-<?
+<?php
 # See search.doc
 
 class SearchEngine {
@@ -9,6 +9,7 @@ class SearchEngine {
        var $addtoquery = array();
        var $namespacesToSearch = array();
        var $alternateTitle;
+       var $all_titles = false;
 
        function SearchEngine( $text )
        {
@@ -129,8 +130,10 @@ class SearchEngine {
                  wfMsg("powersearch") . "\">\n";
                $ret = str_replace( "$9", $tempText, $ret );
 
+               $titleObj = NULL; # this does tricky stuff
+               
                $ret = "<br><br>\n<form id=\"powersearch\" method=\"get\" " .
-                 "action=\"" . wfLocalUrl( "" ) . "\">\n{$ret}\n</form>\n";
+                 "action=\"" . $titleObj->getUrl() . "\">\n{$ret}\n</form>\n";
 
                if ( isset ( $searchx ) ) {
                        if ( ! $listredirs ) { 
@@ -174,7 +177,8 @@ class SearchEngine {
                $redircond = $this->searchRedirects();
 
                if ( $wgDisableTextSearch ) {
-                       $wgOut->addHTML( wfMsg( "searchdisabled", htmlspecialchars( $search ), $wgInputEncoding ) );
+                       $wgOut->addHTML( wfMsg( "searchdisabled" ) );
+                       $wgOut->addHTML( wfMsg( "googlesearch", htmlspecialchars( $search ), $GLOBALS['wgInputEncoding'] ) );
                } else {
                        $sql = "SELECT cur_id,cur_namespace,cur_title," .
                          "cur_text FROM cur,searchindex " .
@@ -431,7 +435,7 @@ class SearchEngine {
                }
 
                if ( 0 != $t->getArticleID() ) {
-                       $wgOut->redirect( wfLocalUrl( $t->getPrefixedURL() ) );
+                       $wgOut->redirect( $t->getURL() );
                        return;
                }
 
@@ -439,7 +443,7 @@ class SearchEngine {
                #
                $t = Title::newFromText( strtolower( $search ) );
                if ( 0 != $t->getArticleID() ) {
-                       $wgOut->redirect( wfLocalUrl( $t->getPrefixedURL() ) );
+                       $wgOut->redirect( $t->getURL() );
                        return;
                }
 
@@ -447,7 +451,7 @@ class SearchEngine {
                #
                $t = Title::newFromText( ucwords( strtolower( $search ) ) );
                if ( 0 != $t->getArticleID() ) {
-                       $wgOut->redirect( wfLocalUrl( $t->getPrefixedURL() ) );
+                       $wgOut->redirect( $t->getURL() );
                        return;
                }
 
@@ -455,12 +459,13 @@ class SearchEngine {
                #
                $t = Title::newFromText( strtoupper( $search ) );
                if ( 0 != $t->getArticleID() ) {
-                       $wgOut->redirect( wfLocalUrl( $t->getPrefixedURL() ) );
+                       $wgOut->redirect( $t->getURL() );
                        return;
                }
-               $wgOut->addHTML( wfMsg("nogomatch", 
-                       htmlspecialchars( wfLocalUrl( ucfirst($this->mUsertext), "action=edit") ) )
-                       . "\n<p>" );
+
+               # No match, generate an edit URL
+               $t = Title::newFromText( $this->mUsertext );
+               $wgOut->addHTML( wfMsg("nogomatch", $t->getURL( "action=edit", true ) ) . "\n<p>" );
 
                # Try a fuzzy title search
                $anyhit = false;
@@ -469,9 +474,10 @@ class SearchEngine {
                        foreach( array(NS_MAIN, NS_WP, NS_USER, NS_IMAGE, NS_MEDIAWIKI) as $namespace){
                                $anyhit |= SearchEngine::doFuzzyTitleSearch( $search, $namespace );
                        }
-               } 
+               }
+               
                if( ! $anyhit ){
-                       $wgOut->addHTML( wfMsg("notitlematches") );             
+                       return $this->showResults();
                }
        }
 
@@ -529,6 +535,15 @@ class SearchEngine {
        /* static */ function getTitlesByLength($aLength, $aNamespace = 0){
                global $wgMemc, $wgDBname;
 
+               // to avoid multiple costly SELECTs in case of no memcached
+               if( $this->all_titles ){ 
+                       if( isset( $this->all_titles[$aLength][$aNamespace] ) ){
+                               return $this->all_titles[$aLength][$aNamespace];
+                       } else {
+                               return array();
+                       }
+               }
+
                $mkey = "$wgDBname:titlesbylength:$aLength:$aNamespace";
                $mkeyts = "$wgDBname:titlesbylength:createtime";
                $ts = $wgMemc->get( $mkeyts );
@@ -541,6 +556,7 @@ class SearchEngine {
                }
 
                $wgMemc->set( $mkeyts, time() );
+
                $res = wfQuery("SELECT cur_title, cur_namespace FROM cur", DB_READ);
                $titles = array(); // length, ns, [titles]
                while( $obj = wfFetchObject( $res ) ){
@@ -555,12 +571,14 @@ class SearchEngine {
                                $wgMemc->set( $mkey, $title_arr, 3600 * 24 );
                        }
                }
-               return $titles[$aLength][$aNamespace];
+               $this->all_titles = $titles;
+               if( isset( $titles[$aLength][$aNamespace] ) )
+                       return $titles[$aLength][$aNamespace];
+               else
+                       return array();
        }
 }
 
 /* private static */ function SearchEngine_pcmp($a, $b){ return $a[0] - $b[0]; }
 
-
-
 ?>