Made powersearch-field message a label for the search field
[lhc/web/wiklou.git] / includes / specials / SpecialSearch.php
index 1930cbb..f71f678 100644 (file)
@@ -41,7 +41,7 @@ function wfSpecialSearch( $par = '' ) {
                || !is_null( $wgRequest->getVal( 'offset' )) 
                || !is_null( $wgRequest->getVal( 'searchx' )) )
        {
-               $searchPage->showResults( $search, 'search' );
+               $searchPage->showResults( $search );
        } else {
                $searchPage->goResult( $search );
        }
@@ -63,6 +63,7 @@ class SpecialSearch {
         */
        function __construct( &$request, &$user ) {
                list( $this->limit, $this->offset ) = $request->getLimitOffset( 20, 'searchlimit' );
+               $this->mPrefix = $request->getVal('prefix', '');
                # Extract requested namespaces
                $this->namespaces = $this->powerSearch( $request );
                if( empty( $this->namespaces ) ) {
@@ -72,6 +73,7 @@ class SpecialSearch {
                $this->searchAdvanced = $request->getVal( 'advanced' );
                $this->active = 'advanced';
                $this->sk = $user->getSkin();
+               $this->didYouMeanHtml = ''; # html of did you mean... link
        }
 
        /**
@@ -111,30 +113,21 @@ class SpecialSearch {
         * @param string $term
         */
        public function showResults( $term ) {
-               global $wgOut, $wgDisableTextSearch;
+               global $wgOut, $wgUser, $wgDisableTextSearch, $wgContLang;
                wfProfileIn( __METHOD__ );
-
-               $this->setupPage( $term );
+               
+               $sk = $wgUser->getSkin();
+               
                $this->searchEngine = SearchEngine::create();
+               $search =& $this->searchEngine;
+               $search->setLimitOffset( $this->limit, $this->offset );
+               $search->setNamespaces( $this->namespaces );
+               $search->showRedirects = $this->searchRedirects;
+               $search->prefix = $this->mPrefix;
+               $term = $search->transformSearchTerm($term);
+               
+               $this->setupPage( $term );
                
-               $t = Title::newFromText( $term );
-
-               $wgOut->addHtml( 
-                       Xml::openElement( 'table', array( 'border'=>0, 'cellpadding'=>0, 'cellspacing'=>0 ) ) .
-                       Xml::openElement( 'tr' ) .
-                       Xml::openElement( 'td' ) . "\n" .
-                       ( $this->searchAdvanced ? $this->powerSearchBox( $term ) : $this->shortDialog( $term ) ) .
-                       Xml::closeElement('td') .
-                       Xml::closeElement('tr') .
-                       Xml::closeElement('table')
-               );
-
-               if( '' === trim( $term ) ) {
-                       // Empty query -- straight view of search form
-                       wfProfileOut( __METHOD__ );
-                       return;
-               }
-
                if( $wgDisableTextSearch ) {
                        global $wgSearchForwardUrl;
                        if( $wgSearchForwardUrl ) {
@@ -158,23 +151,15 @@ class SpecialSearch {
                        wfProfileOut( __METHOD__ );
                        return;
                }
-
-               $search =& $this->searchEngine;
-               $search->setLimitOffset( $this->limit, $this->offset );
-               $search->setNamespaces( $this->namespaces );
-               $search->showRedirects = $this->searchRedirects;
+               
+               $t = Title::newFromText( $term );
+               
+               // fetch search results         
                $rewritten = $search->replacePrefixes($term);
 
                $titleMatches = $search->searchTitle( $rewritten );
-
-               // Sometimes the search engine knows there are too many hits
-               if( $titleMatches instanceof SearchResultTooMany ) {
-                       $wgOut->addWikiText( '==' . wfMsg( 'toomanymatches' ) . "==\n" );
-                       wfProfileOut( __METHOD__ );
-                       return;
-               }
-
-               $textMatches = $search->searchText( $rewritten );
+               if( !($titleMatches instanceof SearchResultTooMany))
+                       $textMatches = $search->searchText( $rewritten );
 
                // did you mean... suggestions
                if( $textMatches && $textMatches->hasSuggestion() ) {
@@ -183,10 +168,37 @@ class SpecialSearch {
                                array( 'search' => $textMatches->getSuggestionQuery(), 'fulltext'       => wfMsg('search') ),
                                $this->powerSearchOptions()
                        );
-                       $suggestLink = '<a href="'.$st->escapeLocalURL($stParams).'">'.
-                               $textMatches->getSuggestionSnippet().'</a>';
+                       $suggestLink = $sk->makeKnownLinkObj( $st,
+                               $textMatches->getSuggestionSnippet(),
+                               $stParams );
 
-                       $wgOut->addHTML('<div class="searchdidyoumean">'.wfMsg('search-suggest',$suggestLink).'</div>');
+                       $this->didYouMeanHtml = '<div class="searchdidyoumean">'.wfMsg('search-suggest',$suggestLink).'</div>';
+               }
+               
+               // start rendering the page             
+               $wgOut->addHtml( 
+                       Xml::openElement( 'table', array( 'border'=>0, 'cellpadding'=>0, 'cellspacing'=>0 ) ) .
+                       Xml::openElement( 'tr' ) .
+                       Xml::openElement( 'td' ) . "\n" .
+                       ( $this->searchAdvanced ? $this->powerSearchBox( $term ) : $this->shortDialog( $term ) ) .
+                       Xml::closeElement('td') .
+                       Xml::closeElement('tr') .
+                       Xml::closeElement('table')
+               );
+               
+               // Sometimes the search engine knows there are too many hits
+               if( $titleMatches instanceof SearchResultTooMany ) {
+                       $wgOut->addWikiText( '==' . wfMsg( 'toomanymatches' ) . "==\n" );
+                       wfProfileOut( __METHOD__ );
+                       return;
+               }
+               
+               $filePrefix = $wgContLang->getFormattedNsText(NS_FILE).':';
+               if( '' === trim( $term ) || $filePrefix === trim( $term ) ) {
+                       $wgOut->addHTML( $this->searchAdvanced ? $this->powerSearchFocus() : $this->searchFocus() );
+                       // Empty query -- straight view of search form
+                       wfProfileOut( __METHOD__ );
+                       return;
                }
 
                // show direct page/create link
@@ -268,6 +280,9 @@ class SpecialSearch {
                        $wgOut->addWikiMsg( 'search-nonefound' );
                }
                $wgOut->addHtml( "</div>" );
+               if( $totalRes === 0 ) {
+                       $wgOut->addHTML( $this->searchAdvanced ? $this->powerSearchFocus() : $this->searchFocus() );
+               }
 
                if( $numSQL || $this->offset ) {
                        $wgOut->addHTML( "<p class='mw-search-pager-bottom'>{$prevnext}</p>\n" );
@@ -372,7 +387,7 @@ class SpecialSearch {
         * @param array $terms terms to highlight
         */
        protected function showHit( $result, $terms ) {
-               global $wgContLang, $wgLang;
+               global $wgContLang, $wgLang, $wgUser;
                wfProfileIn( __METHOD__ );
 
                if( $result->isBrokenTitle() ) {
@@ -380,6 +395,7 @@ class SpecialSearch {
                        return "<!-- Broken link in search result -->\n";
                }
 
+               $sk = $wgUser->getSkin();
                $t = $result->getTitle();
 
                $link = $this->sk->makeKnownLinkObj( $t, $result->getTitleSnippet($terms));
@@ -445,8 +461,8 @@ class SpecialSearch {
                                array('search'    => wfMsgForContent('searchrelated').':'.$t->getPrefixedText(),
                                      'fulltext'  => wfMsg('search') ));
                        
-                       $related = ' -- <a href="'.$st->escapeLocalURL($stParams).'">'. 
-                               wfMsg('search-relatedarticle').'</a>';
+                       $related = ' -- ' . $sk->makeKnownLinkObj( $st,
+                               wfMsg('search-relatedarticle'), $stParams );
                }
 
                // Include a thumbnail for media files...
@@ -498,7 +514,7 @@ class SpecialSearch {
                $out = "<div id='mw-search-interwiki'><div id='mw-search-interwiki-caption'>".
                        wfMsg('search-interwiki-caption')."</div>\n";           
                $off = $this->offset + 1;
-               $out .= "<ul start='{$off}' class='mw-search-iwresults'>\n";
+               $out .= "<ul class='mw-search-iwresults'>\n";
 
                // work out custom project captions
                $customCaptions = array();
@@ -594,35 +610,51 @@ class SpecialSearch {
 
                $redirect = Xml::check( 'redirs', $this->searchRedirects, array( 'value' => '1', 'id' => 'redirs' ) );
                $redirectLabel = Xml::label( wfMsg( 'powersearch-redir' ), 'redirs' );
-               $searchField = Xml::input( 'search', 50, $term, array( 'type' => 'text', 'id' => 'powerSearchText' ) );
-               $searchButton = Xml::submitButton( wfMsg( 'powersearch' ), array( 'name' => 'fulltext' ) ) . "\n";
+               $searchField = Xml::inputLabel( wfMsg('powersearch-field'), 'search', 'powerSearchText', 50, $term,
+                       array( 'type' => 'text') );
+               $searchButton = Xml::submitButton( wfMsg( 'powersearch' ), array( 'name' => 'fulltext' )) . "\n";
                $searchTitle = SpecialPage::getTitleFor( 'Search' );
                
+               $redirectText = '';
+               // show redirects check only if backend supports it 
+               if( $this->searchEngine->acceptListRedirects() ) {
+                       $redirectText = "<p>". $redirect . " " . $redirectLabel ."</p>";
+               }
+               
                $out = Xml::openElement( 'form', array( 'id' => 'powersearch', 'method' => 'get', 'action' => $wgScript ) ) .
                        Xml::hidden( 'title', $searchTitle->getPrefixedText() ) . "\n" .
                        "<p>" .
                        wfMsgExt( 'powersearch-ns', array( 'parseinline' ) ) .
                        "</p>\n" .
+                       '<input type="hidden" name="advanced" value="'.$this->searchAdvanced."\"/>\n".
                        $tables .
-                       "<hr style=\"clear: both\" />\n" .
-                       "<p>" .
-                       $redirect . " " . $redirectLabel .
-                       "</p>\n" .
-                       wfMsgExt( 'powersearch-field', array( 'parseinline' ) ) .
-                       "&nbsp;" .
+                       "<hr style=\"clear: both;\" />\n".                      
+                       $redirectText ."\n".
+                       "<div style=\"padding-top:2px;padding-bottom:2px;\">".
                        $searchField .
                        "&nbsp;" .
+                       Xml::hidden( 'fulltext', 'Advanced search' ) . "\n" .
                        $searchButton .
+                       "</div>".
                        "</form>";
                $t = Title::newFromText( $term );
-               if( $t != null && count($this->namespaces) === 1 ) {
+               /* if( $t != null && count($this->namespaces) === 1 ) {
                        $out .= wfMsgExt( 'searchmenu-prefix', array('parseinline'), $term );
-               }
+               } */
                return Xml::openElement( 'fieldset', array('id' => 'mw-searchoptions','style' => 'margin:0em;') ) .
                        Xml::element( 'legend', null, wfMsg('powersearch-legend') ) .
-                       $this->formHeader($term) . $out . 
+                       $this->formHeader($term) . $out . $this->didYouMeanHtml . 
                        Xml::closeElement( 'fieldset' );
        }
+       
+       protected function searchFocus() {
+               global $wgJsMimeType;
+               return "<script type=\"$wgJsMimeType\">" .
+                       "hookEvent(\"load\", function() {" .
+                               "document.getElementById('searchText').focus();" .
+                       "});" .
+                       "</script>";
+       }
 
        protected function powerSearchFocus() {
                global $wgJsMimeType;
@@ -715,8 +747,8 @@ class SpecialSearch {
                $searchable = SearchEngine::searchableNamespaces();
                $out = Xml::openElement( 'form', array( 'id' => 'search', 'method' => 'get', 'action' => $wgScript ) );
                $out .= Xml::hidden( 'title', $searchTitle->getPrefixedText() ) . "\n";
-               // If searching several, but not all namespaces, show what we are searching.
-               if( count($this->namespaces) > 1 && $this->namespaces !== array_keys($searchable) ) {
+               // show namespaces only for advanced search
+               if( $this->active == 'advanced' ) {
                        $active = array();
                        foreach( $this->namespaces as $ns ) {
                                $active[$ns] = $searchable[$ns];
@@ -733,17 +765,18 @@ class SpecialSearch {
                $out .= Xml::hidden( "redirs", (int)$this->searchRedirects );
                // Term box
                $out .= Xml::input( 'search', 50, $term, array( 'type' => 'text', 'id' => 'searchText' ) ) . "\n";
+               $out .= Xml::hidden( 'fulltext', 'Search' );
                $out .= Xml::submitButton( wfMsg( 'searchbutton' ), array( 'name' => 'fulltext' ) );
                $out .= ' (' . wfMsgExt('searchmenu-help',array('parseinline') ) . ')';
                $out .= Xml::closeElement( 'form' );
                // Add prefix link for single-namespace searches
                $t = Title::newFromText( $term );
-               if( $t != null && count($this->namespaces) === 1 ) {
+               /*if( $t != null && count($this->namespaces) === 1 ) {
                        $out .= wfMsgExt( 'searchmenu-prefix', array('parseinline'), $term );
-               }
+               }*/
                return Xml::openElement( 'fieldset', array('id' => 'mw-searchoptions','style' => 'margin:0em;') ) .
                        Xml::element( 'legend', null, wfMsg('searchmenu-legend') ) .
-                       $this->formHeader($term) . $out .
+                       $this->formHeader($term) . $out . $this->didYouMeanHtml .
                        Xml::closeElement( 'fieldset' );
        }
        
@@ -781,7 +814,7 @@ class SpecialSearch {
                $rows = array();
                $tables = "";
                foreach( $namespaces as $ns => $name ) {
-                       $subj = Namespace::getSubject( $ns );
+                       $subj = MWNamespace::getSubject( $ns );
                        if( !array_key_exists( $subj, $rows ) ) {
                                $rows[$subj] = "";
                        }
@@ -828,7 +861,7 @@ class SpecialSearchOld {
         */
        function __construct( &$request, &$user ) {
                list( $this->limit, $this->offset ) = $request->getLimitOffset( 20, 'searchlimit' );
-
+               $this->mPrefix = $request->getVal('prefix', '');
                $this->namespaces = $this->powerSearch( $request );
                if( empty( $this->namespaces ) ) {
                        $this->namespaces = SearchEngine::userNamespaces( $user );
@@ -874,28 +907,56 @@ class SpecialSearchOld {
                        }
                }
 
-               $wgOut->wrapWikiMsg( "==$1==\n", 'notitlematches' );
+               $extra = $wgOut->parse( '=='.wfMsgNoTrans( 'notitlematches' )."==\n" );
                if( $t->quickUserCan( 'create' ) && $t->quickUserCan( 'edit' ) ) {
-                       $wgOut->addWikiMsg( 'noexactmatch', wfEscapeWikiText( $term ) );
+                       $extra .= wfMsgExt( 'noexactmatch', 'parse', wfEscapeWikiText( $term ) );
                } else {
-                       $wgOut->addWikiMsg( 'noexactmatch-nocreate', wfEscapeWikiText( $term ) );
+                       $extra .= wfMsgExt( 'noexactmatch-nocreate', 'parse', wfEscapeWikiText( $term ) );
                }
 
-               return $this->showResults( $term );
+               $this->showResults( $term, $extra );
        }
 
        /**
         * @param string $term
-        * @public
+        * @param string $extra Extra HTML to add after "did you mean"
         */
-       function showResults( $term ) {
+       public function showResults( $term, $extra = '' ) {
                wfProfileIn( __METHOD__ );
                global $wgOut, $wgUser;
                $sk = $wgUser->getSkin();
 
+               $search = SearchEngine::create();
+               $search->setLimitOffset( $this->limit, $this->offset );
+               $search->setNamespaces( $this->namespaces );
+               $search->showRedirects = $this->searchRedirects;
+               $search->prefix = $this->mPrefix;
+               $term = $search->transformSearchTerm($term);
+
                $this->setupPage( $term );
 
-               $wgOut->addWikiMsg( 'searchresulttext' );
+               $rewritten = $search->replacePrefixes($term);
+               $titleMatches = $search->searchTitle( $rewritten );
+               $textMatches = $search->searchText( $rewritten );
+
+               // did you mean... suggestions
+               if($textMatches && $textMatches->hasSuggestion()){
+                       $st = SpecialPage::getTitleFor( 'Search' );                     
+                       $stParams = wfArrayToCGI( array( 
+                                       'search'        => $textMatches->getSuggestionQuery(), 
+                                       'fulltext'      => wfMsg('search')),
+                                       $this->powerSearchOptions());
+
+                       $suggestLink = $sk->makeKnownLinkObj( $st,
+                               $textMatches->getSuggestionSnippet(),
+                               $stParams );
+
+                       $wgOut->addHTML('<div class="searchdidyoumean">'.wfMsg('search-suggest',$suggestLink).'</div>');
+               }
+
+               $wgOut->addHTML( $extra );
+
+               $wgOut->wrapWikiMsg( "<div class='mw-searchresult'>\n$1</div>", 'searchresulttext' );
 
                if( '' === trim( $term ) ) {
                        // Empty query -- straight view of search form
@@ -931,15 +992,7 @@ class SpecialSearchOld {
                        return;
                }
 
-               $wgOut->addHTML( $this->shortDialog( $term ) );
-
-               $search = SearchEngine::create();
-               $search->setLimitOffset( $this->limit, $this->offset );
-               $search->setNamespaces( $this->namespaces );
-               $search->showRedirects = $this->searchRedirects;
-               $rewritten = $search->replacePrefixes($term);
-
-               $titleMatches = $search->searchTitle( $rewritten );
+               $wgOut->addHTML( $this->shortDialog( $term ) );         
 
                // Sometimes the search engine knows there are too many hits
                if ($titleMatches instanceof SearchResultTooMany) {
@@ -950,22 +1003,6 @@ class SpecialSearchOld {
                        return;
                }
                
-               $textMatches = $search->searchText( $rewritten );
-
-               // did you mean... suggestions
-               if($textMatches && $textMatches->hasSuggestion()){
-                       $st = SpecialPage::getTitleFor( 'Search' );                     
-                       $stParams = wfArrayToCGI( array( 
-                                       'search'        => $textMatches->getSuggestionQuery(), 
-                                       'fulltext'      => wfMsg('search')),
-                                       $this->powerSearchOptions());
-                                       
-                       $suggestLink = '<a href="'.$st->escapeLocalURL($stParams).'">'.
-                                       $textMatches->getSuggestionSnippet().'</a>';
-                                       
-                       $wgOut->addHTML('<div class="searchdidyoumean">'.wfMsg('search-suggest',$suggestLink).'</div>');
-               }
-
                // show number of results
                $num = ( $titleMatches ? $titleMatches->numRows() : 0 )
                        + ( $textMatches ? $textMatches->numRows() : 0);
@@ -1204,8 +1241,8 @@ class SpecialSearchOld {
                                array('search'    => wfMsgForContent('searchrelated').':'.$t->getPrefixedText(),
                                      'fulltext'  => wfMsg('search') ));
                        
-                       $related = ' -- <a href="'.$st->escapeLocalURL($stParams).'">'. 
-                               wfMsg('search-relatedarticle').'</a>';
+                       $related = ' -- ' . $sk->makeKnownLinkObj( $st,
+                               wfMsg('search-relatedarticle'), $stParams );
                }
                                
                // Include a thumbnail for media files...
@@ -1356,7 +1393,7 @@ class SpecialSearchOld {
                // many assumptions about namespace numbering
                $rows = array();
                foreach( $namespaces as $ns => $name ) {
-                       $subj = Namespace::getSubject( $ns );
+                       $subj = MWNamespace::getSubject( $ns );
                        if( !array_key_exists( $subj, $rows ) ) {
                                $rows[$subj] = "";
                        }
@@ -1394,10 +1431,11 @@ class SpecialSearchOld {
                $searchField = Xml::input( 'search', 50, $term, array( 'type' => 'text', 'id' => 'powerSearchText' ) );
                $searchButton = Xml::submitButton( wfMsg( 'powersearch' ), array( 'name' => 'fulltext' ) ) . "\n";
                $searchTitle = SpecialPage::getTitleFor( 'Search' );
+               $searchHiddens = Xml::hidden( 'title', $searchTitle->getPrefixedText() ) . "\n";
+               $searchHiddens .= Xml::hidden( 'fulltext', 'Advanced search' ) . "\n";
                
                $out = Xml::openElement( 'form', array( 'id' => 'powersearch', 'method' => 'get', 'action' => $wgScript ) ) .
-                       Xml::fieldset( wfMsg( 'powersearch-legend' ),
-                               Xml::hidden( 'title', $searchTitle->getPrefixedText() ) . "\n" .
+                       Xml::fieldset( wfMsg( 'powersearch-legend' ),                           
                                "<p>" .
                                wfMsgExt( 'powersearch-ns', array( 'parseinline' ) ) .
                                "</p>\n" .
@@ -1410,6 +1448,7 @@ class SpecialSearchOld {
                                "&nbsp;" .
                                $searchField .
                                "&nbsp;" .
+                               $searchHiddens . 
                                $searchButton ) .
                        "</form>";
 
@@ -1434,13 +1473,14 @@ class SpecialSearchOld {
                        'action' => $wgScript
                ));
                $searchTitle = SpecialPage::getTitleFor( 'Search' );
-               $out .= Xml::hidden( 'title', $searchTitle->getPrefixedText() );
                $out .= Xml::input( 'search', 50, $term, array( 'type' => 'text', 'id' => 'searchText' ) ) . ' ';
                foreach( SearchEngine::searchableNamespaces() as $ns => $name ) {
                        if( in_array( $ns, $this->namespaces ) ) {
                                $out .= Xml::hidden( "ns{$ns}", '1' );
                        }
                }
+               $out .= Xml::hidden( 'title', $searchTitle->getPrefixedText() );
+               $out .= Xml::hidden( 'fulltext', 'Search' );
                $out .= Xml::submitButton( wfMsg( 'searchbutton' ), array( 'name' => 'fulltext' ) );
                $out .= Xml::closeElement( 'form' );