Add new message 'showhideselectedlogentries' for revdel of logs.
[lhc/web/wiklou.git] / includes / specials / SpecialSearch.php
index 74ff8ed..079130f 100644 (file)
@@ -35,8 +35,9 @@ class SpecialSearch extends SpecialPage {
         * string when applicable. Extensions can add new profiles with hooks
         * with custom search options just for that profile.
         * null|string
-        */ 
+        */
        protected $profile;
+       function getProfile() { return $this->profile; }
 
        /// Search engine
        protected $searchEngine;
@@ -56,6 +57,7 @@ class SpecialSearch extends SpecialPage {
         * @var array
         */
        protected $namespaces;
+       function getNamespaces() { return $this->namespaces; }
 
        /**
         * @var bool
@@ -108,6 +110,8 @@ class SpecialSearch extends SpecialPage {
 
        /**
         * Set up basic search parameters from the request and user settings.
+        *
+        * @see tests/phpunit/includes/specials/SpecialSearchTest.php
         */
        public function load() {
                $request = $this->getRequest();
@@ -115,23 +119,30 @@ class SpecialSearch extends SpecialPage {
                $this->mPrefix = $request->getVal( 'prefix', '' );
 
                $user = $this->getUser();
+
                # Extract manually requested namespaces
                $nslist = $this->powerSearch( $request );
-               $this->profile = $profile = $request->getVal( 'profile', null );
+               if ( !count( $nslist ) ) {
+                       # Fallback to user preference
+                       $nslist = SearchEngine::userNamespaces( $user );
+               }
+
+               $profile = null;
+               if ( !count( $nslist ) ) {
+                       $profile = 'default';
+               }
+
+               $profile = $request->getVal( 'profile', $profile );
                $profiles = $this->getSearchProfiles();
-               if ( $profile === null) {
+               if ( $profile === null ) {
                        // BC with old request format
-                       $this->profile = 'advanced';
-                       if ( count( $nslist ) ) {
-                               foreach( $profiles as $key => $data ) {
-                                       if ( $nslist === $data['namespaces'] && $key !== 'advanced') {
-                                               $this->profile = $key;
-                                       }
+                       $profile = 'advanced';
+                       foreach( $profiles as $key => $data ) {
+                               if ( $nslist === $data['namespaces'] && $key !== 'advanced') {
+                                       $profile = $key;
                                }
-                               $this->namespaces = $nslist;
-                       } else {
-                               $this->namespaces = SearchEngine::userNamespaces( $user );
                        }
+                       $this->namespaces = $nslist;
                } elseif ( $profile === 'advanced' ) {
                        $this->namespaces = $nslist;
                } else {
@@ -139,7 +150,7 @@ class SpecialSearch extends SpecialPage {
                                $this->namespaces = $profiles[$profile]['namespaces'];
                        } else {
                                // Unknown profile requested
-                               $this->profile = 'default';
+                               $profile = 'default';
                                $this->namespaces = $profiles['default']['namespaces'];
                        }
                }
@@ -149,6 +160,7 @@ class SpecialSearch extends SpecialPage {
                $this->searchRedirects = $request->getBool( 'redirs', $default ) ? 1 : 0;
                $this->didYouMeanHtml = ''; # html of did you mean... link
                $this->fulltext = $request->getVal('fulltext');
+               $this->profile = $profile;
        }
 
        /**
@@ -162,7 +174,8 @@ class SpecialSearch extends SpecialPage {
                $t = Title::newFromText( $term );
                # If the string cannot be used to create a title
                if( is_null( $t ) ) {
-                       return $this->showResults( $term );
+                       $this->showResults( $term );
+                       return;
                }
                # If there's an exact or very near match, jump right there.
                $t = SearchEngine::getNearMatch( $term );
@@ -189,7 +202,7 @@ class SpecialSearch extends SpecialPage {
                                return;
                        }
                }
-               return $this->showResults( $term );
+               $this->showResults( $term );
        }
 
        /**
@@ -220,13 +233,13 @@ class SpecialSearch extends SpecialPage {
                        } else {
                                $out->addHTML(
                                        Xml::openElement( 'fieldset' ) .
-                                       Xml::element( 'legend', null, wfMsg( 'search-external' ) ) .
-                                       Xml::element( 'p', array( 'class' => 'mw-searchdisabled' ), wfMsg( 'searchdisabled' ) ) .
-                                       wfMsg( 'googlesearch',
+                                       Xml::element( 'legend', null, $this->msg( 'search-external' )->text() ) .
+                                       Xml::element( 'p', array( 'class' => 'mw-searchdisabled' ), $this->msg( 'searchdisabled' )->text() ) .
+                                       $this->msg( 'googlesearch' )->rawParams(
                                                htmlspecialchars( $term ),
-                                               htmlspecialchars( 'UTF-8' ),
-                                               htmlspecialchars( wfMsg( 'searchbutton' ) )
-                                       ) .
+                                               'UTF-8',
+                                               $this->msg( 'searchbutton' )->escaped()
+                                       )->text() .
                                        Xml::closeElement( 'fieldset' )
                                );
                        }
@@ -273,7 +286,7 @@ class SpecialSearch extends SpecialPage {
                                $stParams
                        );
 
-                       $this->didYouMeanHtml = '<div class="searchdidyoumean">'.wfMsg('search-suggest',$suggestLink).'</div>';
+                       $this->didYouMeanHtml = '<div class="searchdidyoumean">' . $this->msg( 'search-suggest' )->rawParams( $suggestLink )->text() . '</div>';
                }
                // start rendering the page
                $out->addHtml(
@@ -344,9 +357,8 @@ class SpecialSearch extends SpecialPage {
                if( $num || $this->offset ) {
                        // Show the create link ahead
                        $this->showCreateLink( $t );
-                       $prevnext = wfViewPrevNext( $this->offset, $this->limit,
-                               SpecialPage::getTitleFor( 'Search' ),
-                               wfArrayToCGI( $this->powerSearchOptions(), array( 'search' => $term ) ),
+                       $prevnext = $this->getLanguage()->viewPrevNext( $this->getTitle(), $this->offset, $this->limit,
+                               $this->powerSearchOptions() + array( 'search' => $term ),
                                max( $titleMatchesNum, $textMatchesNum ) < $this->limit
                        );
                        //$out->addHTML( "<p class='mw-search-pager-top'>{$prevnext}</p>\n" );
@@ -408,7 +420,7 @@ class SpecialSearch extends SpecialPage {
                        $this->getOutput()->addHtml( '<p></p>' );
                        return;
                }
-               $messageName = '';
+
                if( $t->isKnown() ) {
                        $messageName = 'searchmenu-exists';
                } elseif( $t->userCan( 'create' ) ) {
@@ -436,8 +448,8 @@ class SpecialSearch extends SpecialPage {
                $this->searchAdvanced = ($this->profile === 'advanced');
                $out = $this->getOutput();
                if( strval( $term ) !== ''  ) {
-                       $out->setPageTitle( wfMsg( 'searchresults') );
-                       $out->setHTMLTitle( wfMsg( 'pagetitle', wfMsg( 'searchresults-title', $term ) ) );
+                       $out->setPageTitle( $this->msg( 'searchresults' ) );
+                       $out->setHTMLTitle( $this->msg( 'pagetitle', $this->msg( 'searchresults-title', $term )->plain() ) );
                }
                // add javascript specific to special:search
                $out->addModules( 'mediawiki.special.search' );
@@ -498,8 +510,10 @@ class SpecialSearch extends SpecialPage {
                        $out .= "\n<!-- {$infoLine} -->\n";
                }
                $out .= "<ul class='mw-search-results'>\n";
-               while( $result = $matches->next() ) {
+               $result = $matches->next();
+               while( $result ) {
                        $out .= $this->showHit( $result, $terms );
+                       $result = $matches->next();
                }
                $out .= "</ul>\n";
 
@@ -545,7 +559,7 @@ class SpecialSearch extends SpecialPage {
                //If page content is not readable, just return the title.
                //This is not quite safe, but better than showing excerpts from non-readable pages
                //Note that hiding the entry entirely would screw up paging.
-               if( !$t->userCanRead() ) {
+               if( !$t->userCan( 'read' ) ) {
                        wfProfileOut( __METHOD__ );
                        return "<li>{$link}</li>\n";
                }
@@ -570,13 +584,8 @@ class SpecialSearch extends SpecialPage {
                                $redirectText = null;
 
                        $redirect = "<span class='searchalttitle'>" .
-                               wfMsg(
-                                       'search-redirect',
-                                       Linker::linkKnown(
-                                               $redirectTitle,
-                                               $redirectText
-                                       )
-                               ) .
+                               $this->msg( 'search-redirect' )->rawParams(
+                                       Linker::linkKnown( $redirectTitle, $redirectText ) )->text() .
                                "</span>";
                }
 
@@ -587,19 +596,15 @@ class SpecialSearch extends SpecialPage {
                                $sectionText = null;
 
                        $section = "<span class='searchalttitle'>" .
-                               wfMsg(
-                                       'search-section', Linker::linkKnown(
-                                               $sectionTitle,
-                                               $sectionText
-                                       )
-                               ) .
+                               $this->msg( 'search-section' )->rawParams(
+                                       Linker::linkKnown( $sectionTitle, $sectionText ) )->text() .
                                "</span>";
                }
 
                // format text extract
                $extract = "<div class='searchresult'>".$result->getTextSnippet($terms)."</div>";
 
-               $lang = $this->getLang();
+               $lang = $this->getLanguage();
 
                // format score
                if( is_null( $result->getScore() ) ) {
@@ -607,7 +612,7 @@ class SpecialSearch extends SpecialPage {
                        $score = '';
                } else {
                        $percent = sprintf( '%2.1f', $result->getScore() * 100 );
-                       $score = wfMsg( 'search-result-score', $lang->formatNum( $percent ) )
+                       $score = $this->msg( 'search-result-score' )->numParams( $percent )->text()
                                . ' - ';
                }
 
@@ -615,25 +620,17 @@ class SpecialSearch extends SpecialPage {
                $byteSize = $result->getByteSize();
                $wordCount = $result->getWordCount();
                $timestamp = $result->getTimestamp();
-               $size = wfMsgExt(
-                       'search-result-size',
-                       array( 'parsemag', 'escape' ),
-                       $lang->formatSize( $byteSize ),
-                       $lang->formatNum( $wordCount )
-               );
+               $size = $this->msg( 'search-result-size', $lang->formatSize( $byteSize ) )
+                       ->numParams( $wordCount )->escaped();
 
                if( $t->getNamespace() == NS_CATEGORY ) {
                        $cat = Category::newFromTitle( $t );
-                       $size = wfMsgExt(
-                               'search-result-category-size',
-                               array( 'parsemag', 'escape' ),
-                               $lang->formatNum( $cat->getPageCount() ),
-                               $lang->formatNum( $cat->getSubcatCount() ),
-                               $lang->formatNum( $cat->getFileCount() )
-                       );
+                       $size = $this->msg( 'search-result-category-size' )
+                               ->numParams( $cat->getPageCount(), $cat->getSubcatCount(), $cat->getFileCount() )
+                               ->escaped();
                }
 
-               $date = $lang->timeanddate( $timestamp );
+               $date = $lang->userTimeAndDate( $timestamp, $this->getUser() );
 
                // link to related articles if supported
                $related = '';
@@ -642,14 +639,15 @@ class SpecialSearch extends SpecialPage {
                        $stParams = array_merge(
                                $this->powerSearchOptions(),
                                array(
-                                       'search' => wfMsgForContent( 'searchrelated' ) . ':' . $t->getPrefixedText(),
-                                       'fulltext' => wfMsg( 'search' )
+                                       'search' => $this->msg( 'searchrelated' )->inContentLanguage()->text() .
+                                               ':' . $t->getPrefixedText(),
+                                       'fulltext' => $this->msg( 'search' )->text()
                                )
                        );
 
                        $related = ' -- ' . Linker::linkKnown(
                                $st,
-                               wfMsg('search-relatedarticle'),
+                               $this->msg( 'search-relatedarticle' )->text(),
                                array(),
                                $stParams
                        );
@@ -661,7 +659,7 @@ class SpecialSearch extends SpecialPage {
                        if( $img ) {
                                $thumb = $img->transform( array( 'width' => 120, 'height' => 120 ) );
                                if( $thumb ) {
-                                       $desc = wfMsg( 'parentheses', $img->getShortDesc() );
+                                       $desc = $this->msg( 'parentheses' )->rawParams( $img->getShortDesc() )->escaped();
                                        wfProfileOut( __METHOD__ );
                                        // Float doesn't seem to interact well with the bullets.
                                        // Table messes up vertical alignment of the bullets.
@@ -705,12 +703,12 @@ class SpecialSearch extends SpecialPage {
                $terms = $wgContLang->convertForSearchResult( $matches->termMatches() );
 
                $out = "<div id='mw-search-interwiki'><div id='mw-search-interwiki-caption'>".
-                       wfMsg('search-interwiki-caption')."</div>\n";
+                       $this->msg( 'search-interwiki-caption' )->text()  . "</div>\n";
                $out .= "<ul class='mw-search-iwresults'>\n";
 
                // work out custom project captions
                $customCaptions = array();
-               $customLines = explode("\n",wfMsg('search-interwiki-custom')); // format per line <iwprefix>:<caption>
+               $customLines = explode( "\n", $this->msg( 'search-interwiki-custom' )->text() ); // format per line <iwprefix>:<caption>
                foreach($customLines as $line) {
                        $parts = explode(":",$line,2);
                        if(count($parts) == 2) // validate line
@@ -718,9 +716,11 @@ class SpecialSearch extends SpecialPage {
                }
 
                $prev = null;
-               while( $result = $matches->next() ) {
+               $result = $matches->next();
+               while( $result ) {
                        $out .= $this->showInterwikiHit( $result, $prev, $terms, $query, $customCaptions );
                        $prev = $result->getInterwikiPrefix();
+                       $result = $matches->next();
                }
                // TODO: should support paging in a non-confusing way (not sure how though, maybe via ajax)..
                $out .= "</ul></div>\n";
@@ -771,13 +771,8 @@ class SpecialSearch extends SpecialPage {
                                $redirectText = null;
 
                        $redirect = "<span class='searchalttitle'>" .
-                               wfMsg(
-                                       'search-redirect',
-                                       Linker::linkKnown(
-                                               $redirectTitle,
-                                               $redirectText
-                                       )
-                               ) .
+                               $this->msg( 'search-redirect' )->rawParams(
+                                       Linker::linkKnown( $redirectTitle, $redirectText ) )->text() .
                                "</span>";
                }
 
@@ -791,13 +786,13 @@ class SpecialSearch extends SpecialPage {
                                // default is to show the hostname of the other wiki which might suck
                                // if there are many wikis on one hostname
                                $parsed = wfParseUrl( $t->getFullURL() );
-                               $caption = wfMsg('search-interwiki-default', $parsed['host']);
+                               $caption = $this->msg( 'search-interwiki-default', $parsed['host'] )->text();
                        }
                        // "more results" link (special page stuff could be localized, but we might not know target lang)
                        $searchTitle = Title::newFromText($t->getInterwiki().":Special:Search");
                        $searchLink = Linker::linkKnown(
                                $searchTitle,
-                               wfMsg('search-interwiki-more'),
+                               $this->msg( 'search-interwiki-more' )->text(),
                                array(),
                                array(
                                        'search' => $query,
@@ -850,7 +845,7 @@ class SpecialSearch extends SpecialPage {
                        }
                        $name = str_replace( '_', ' ', $name );
                        if( $name == '' ) {
-                               $name = wfMsg( 'blanknamespace' );
+                               $name = $this->msg( 'blanknamespace' )->text();
                        }
                        $rows[$subject] .=
                                Xml::openElement(
@@ -886,7 +881,7 @@ class SpecialSearch extends SpecialPage {
                // Show redirects check only if backend supports it
                if( $this->getSearchEngine()->supports( 'list-redirects' ) ) {
                        $showSections['redirects'] =
-                               Xml::checkLabel( wfMsg( 'powersearch-redir' ), 'redirs', 'redirs', $this->searchRedirects );
+                               Xml::checkLabel( $this->msg( 'powersearch-redir' )->text(), 'redirs', 'redirs', $this->searchRedirects );
                }
 
                wfRunHooks( 'SpecialSearchPowerBox', array( &$showSections, $term, $opts ) );
@@ -902,18 +897,18 @@ class SpecialSearch extends SpecialPage {
                                'fieldset',
                                array( 'id' => 'mw-searchoptions', 'style' => 'margin:0em;' )
                        ) .
-                       Xml::element( 'legend', null, wfMsg('powersearch-legend') ) .
-                       Xml::tags( 'h4', null, wfMsgExt( 'powersearch-ns', array( 'parseinline' ) ) ) .
+                       Xml::element( 'legend', null, $this->msg('powersearch-legend' )->text() ) .
+                       Xml::tags( 'h4', null, $this->msg( 'powersearch-ns' )->parse() ) .
                        Xml::tags(
                                'div',
                                array( 'id' => 'mw-search-togglebox' ),
-                               Xml::label( wfMsg( 'powersearch-togglelabel' ), 'mw-search-togglelabel' ) .
+                               Xml::label( $this->msg( 'powersearch-togglelabel' )->text(), 'mw-search-togglelabel' ) .
                                        Xml::element(
                                                'input',
                                                array(
                                                        'type'=>'button',
                                                        'id' => 'mw-search-toggleall',
-                                                       'value' => wfMsg( 'powersearch-toggleall' )
+                                                       'value' => $this->msg( 'powersearch-toggleall' )->text()
                                                )
                                        ) .
                                        Xml::element(
@@ -921,7 +916,7 @@ class SpecialSearch extends SpecialPage {
                                                array(
                                                        'type'=>'button',
                                                        'id' => 'mw-search-togglenone',
-                                                       'value' => wfMsg( 'powersearch-togglenone' )
+                                                       'value' => $this->msg( 'powersearch-togglenone' )->text()
                                                )
                                        )
                        ) .
@@ -998,7 +993,7 @@ class SpecialSearch extends SpecialPage {
                }
 
                $profiles = $this->getSearchProfiles();
-               $lang = $this->getLang();
+               $lang = $this->getLanguage();
 
                // Outputs XML for Search Types
                $out .= Xml::openElement( 'div', array( 'class' => 'search-types' ) );
@@ -1019,8 +1014,8 @@ class SpecialSearch extends SpecialPage {
                                $this->makeSearchLink(
                                        $bareterm,
                                        array(),
-                                       wfMsg( $profile['message'] ),
-                                       wfMsg( $profile['tooltip'], $tooltipParam ),
+                                       $this->msg( $profile['message'] )->text(),
+                                       $this->msg( $profile['tooltip'], $tooltipParam )->text(),
                                        $profile['parameters']
                                )
                        );
@@ -1031,21 +1026,19 @@ class SpecialSearch extends SpecialPage {
                // Results-info
                if ( $resultsShown > 0 ) {
                        if ( $totalNum > 0 ){
-                               $top = wfMsgExt( 'showingresultsheader', array( 'parseinline' ),
-                                       $lang->formatNum( $this->offset + 1 ),
-                                       $lang->formatNum( $this->offset + $resultsShown ),
-                                       $lang->formatNum( $totalNum ),
-                                       wfEscapeWikiText( $term ),
-                                       $lang->formatNum( $resultsShown )
-                               );
+                               $top = $this->msg( 'showingresultsheader' )
+                                       ->numParams( $this->offset + 1, $this->offset + $resultsShown, $totalNum )
+                                       ->params( wfEscapeWikiText( $term ) )
+                                       ->numParams( $resultsShown )
+                                       ->parse();
                        } elseif ( $resultsShown >= $this->limit ) {
-                               $top = wfShowingResults( $this->offset, $this->limit );
+                               $top = $this->msg( 'showingresults' )
+                                       ->numParams( $this->limit, $this->offset + 1 )
+                                       ->parse();
                        } else {
-                               $top =  wfMsgExt( 'showingresultsnum', array( 'parseinline' ),
-                                       $lang->formatNum( $this->limit ),
-                                       $lang->formatNum( $this->offset + 1 ),
-                                       $lang->formatNum( $resultsShown )
-                               );
+                               $top = $this->msg( 'showingresultsnum' )
+                                       ->numParams( $this->limit, $this->offset + 1, $resultsShown )
+                                       ->parse();
                        }
                        $out .= Xml::tags( 'div', array( 'class' => 'results-info' ),
                                Xml::tags( 'ul', null, Xml::tags( 'li', null, $top ) )
@@ -1063,7 +1056,8 @@ class SpecialSearch extends SpecialPage {
         * @return string
         */
        protected function shortDialog( $term ) {
-               $out = Html::hidden( 'title', $this->getTitle()->getPrefixedText() ) . "\n";
+               $out = Html::hidden( 'title', $this->getTitle()->getPrefixedText() );
+               $out .= Html::hidden( 'profile', $this->profile ) . "\n";
                // Term box
                $out .= Html::input( 'search', $term, 'search', array(
                        'id' => $this->profile === 'advanced' ? 'powerSearchText' : 'searchText',
@@ -1071,7 +1065,7 @@ class SpecialSearch extends SpecialPage {
                        'autofocus'
                ) ) . "\n";
                $out .= Html::hidden( 'fulltext', 'Search' ) . "\n";
-               $out .= Xml::submitButton( wfMsg( 'searchbutton' ) ) . "\n";
+               $out .= Xml::submitButton( $this->msg( 'searchbutton' )->text() ) . "\n";
                return $out . $this->didYouMeanHtml;
        }
 
@@ -1095,7 +1089,7 @@ class SpecialSearch extends SpecialPage {
                $stParams = array_merge(
                        array(
                                'search' => $term,
-                               'fulltext' => wfMsg( 'search' )
+                               'fulltext' => $this->msg( 'search' )->text()
                        ),
                        $opt
                );
@@ -1133,7 +1127,7 @@ class SpecialSearch extends SpecialPage {
         */
        protected function startsWithAll( $term ) {
 
-               $allkeyword = wfMsgForContent('searchall');
+               $allkeyword = $this->msg( 'searchall' )->inContentLanguage()->text();
 
                $p = explode( ':', $term );
                if( count( $p ) > 1 ) {