/**
* Run text & title search and display the output
- * @addtogroup SpecialPage
+ * @file
+ * @ingroup SpecialPage
*/
/**
$search = str_replace( "\n", " ", $wgRequest->getText( 'search', $par ) );
$searchPage = new SpecialSearch( $wgRequest, $wgUser );
- if( $wgRequest->getVal( 'fulltext' ) ||
- !is_null( $wgRequest->getVal( 'offset' ) ) ||
- !is_null ($wgRequest->getVal( 'searchx' ) ) ) {
- $searchPage->showResults( $search );
+ if( $wgRequest->getVal( 'fulltext' )
+ || !is_null( $wgRequest->getVal( 'offset' ))
+ || !is_null( $wgRequest->getVal( 'searchx' ))) {
+ $searchPage->showResults( $search, 'search' );
} else {
$searchPage->goResult( $search );
}
/**
* implements Special:Search - Run text & title search and display the output
- * @addtogroup SpecialPage
+ * @ingroup SpecialPage
*/
class SpecialSearch {
$this->namespaces = $this->powerSearch( $request );
if( empty( $this->namespaces ) ) {
- $this->namespaces = $this->userNamespaces( $user );
+ $this->namespaces = SearchEngine::userNamespaces( $user );
}
$this->searchRedirects = $request->getcheck( 'redirs' ) ? true : false;
if ( $wgGoToEdit ) {
$wgOut->redirect( $t->getFullURL( 'action=edit' ) );
return;
- }
+ }
}
+
+ $wgOut->wrapWikiMsg( "==$1==\n", 'notitlematches' );
if( $t->quickUserCan( 'create' ) && $t->quickUserCan( 'edit' ) ) {
$wgOut->addWikiMsg( 'noexactmatch', wfEscapeWikiText( $term ) );
} else {
function showResults( $term ) {
$fname = 'SpecialSearch::showResults';
wfProfileIn( $fname );
+ global $wgOut, $wgUser;
+ $sk = $wgUser->getSkin();
$this->setupPage( $term );
- global $wgOut;
$wgOut->addWikiMsg( 'searchresulttext' );
if( '' === trim( $term ) ) {
return;
}
global $wgInputEncoding;
- $wgOut->addHTML( wfMsg( 'searchdisabled' ) );
$wgOut->addHTML(
+ Xml::openElement( 'fieldset' ) .
+ Xml::element( 'legend', null, wfMsg( 'search-external' ) ) .
+ Xml::element( 'p', array( 'class' => 'mw-searchdisabled' ), wfMsg( 'searchdisabled' ) ) .
wfMsg( 'googlesearch',
htmlspecialchars( $term ),
htmlspecialchars( $wgInputEncoding ),
htmlspecialchars( wfMsg( 'searchbutton' ) )
- )
+ ) .
+ Xml::closeElement( 'fieldset' )
);
wfProfileOut( $fname );
return;
}
-
+
$wgOut->addHTML( $this->shortDialog( $term ) );
$search = SearchEngine::create();
$search->setLimitOffset( $this->limit, $this->offset );
$search->setNamespaces( $this->namespaces );
$search->showRedirects = $this->searchRedirects;
- $titleMatches = $search->searchTitle( $term );
+ $rewritten = $search->replacePrefixes($term);
+
+ $titleMatches = $search->searchTitle( $rewritten );
// Sometimes the search engine knows there are too many hits
if ($titleMatches instanceof SearchResultTooMany) {
wfProfileOut( $fname );
return;
}
- $textMatches = $search->searchText( $term );
+
+ $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);
+ $totalNum = 0;
+ if($titleMatches && !is_null($titleMatches->getTotalHits()))
+ $totalNum += $titleMatches->getTotalHits();
+ if($textMatches && !is_null($textMatches->getTotalHits()))
+ $totalNum += $textMatches->getTotalHits();
if ( $num > 0 ) {
- if ( $num >= $this->limit ) {
+ if ( $totalNum > 0 ){
+ $top = wfMsgExt('showingresultstotal', array( 'parseinline' ),
+ $this->offset+1, $this->offset+$num, $totalNum );
+ } elseif ( $num >= $this->limit ) {
$top = wfShowingResults( $this->offset, $this->limit );
} else {
$top = wfShowingResultsNum( $this->offset, $this->limit, $num );
}
- $wgOut->addHTML( "<p>{$top}</p>\n" );
+ $wgOut->addHTML( "<p class='mw-search-numberresults'>{$top}</p>\n" );
}
+ // prev/next links
if( $num || $this->offset ) {
$prevnext = wfViewPrevNext( $this->offset, $this->limit,
SpecialPage::getTitleFor( 'Search' ),
$this->powerSearchOptions(),
array( 'search' => $term ) ),
($num < $this->limit) );
- $wgOut->addHTML( "<p>{$prevnext}</p>\n" );
+ $wgOut->addHTML( "<p class='mw-search-pager-top'>{$prevnext}</p>\n" );
wfRunHooks( 'SpecialSearchResults', array( $term, $titleMatches, $textMatches ) );
} else {
wfRunHooks( 'SpecialSearchNoResults', array( $term ) );
if( $titleMatches->numRows() ) {
$wgOut->wrapWikiMsg( "==$1==\n", 'titlematches' );
$wgOut->addHTML( $this->showMatches( $titleMatches ) );
- } else {
- $wgOut->wrapWikiMsg( "==$1==\n", 'notitlematches' );
}
$titleMatches->free();
}
if( $textMatches ) {
+ // output appropriate heading
if( $textMatches->numRows() ) {
- $wgOut->wrapWikiMsg( "==$1==\n", 'textmatches' );
- $wgOut->addHTML( $this->showMatches( $textMatches ) );
+ if($titleMatches)
+ $wgOut->wrapWikiMsg( "==$1==\n", 'textmatches' );
+ else // if no title matches the heading is redundant
+ $wgOut->addHTML("<hr/>");
} elseif( $num == 0 ) {
# Don't show the 'no text matches' if we received title matches
$wgOut->wrapWikiMsg( "==$1==\n", 'notextmatches' );
}
+ // show interwiki results if any
+ if( $textMatches->hasInterwikiResults() )
+ $wgOut->addHtml( $this->showInterwiki( $textMatches->getInterwikiResults(), $term ));
+ // show results
+ if( $textMatches->numRows() )
+ $wgOut->addHTML( $this->showMatches( $textMatches ) );
+
$textMatches->free();
}
$wgOut->addWikiMsg( 'nonefound' );
}
if( $num || $this->offset ) {
- $wgOut->addHTML( "<p>{$prevnext}</p>\n" );
+ $wgOut->addHTML( "<p class='mw-search-pager-bottom'>{$prevnext}</p>\n" );
}
$wgOut->addHTML( $this->powerSearchBox( $term ) );
wfProfileOut( $fname );
#------------------------------------------------------------------
# Private methods below this line
-
+
/**
*
*/
function setupPage( $term ) {
global $wgOut;
- $wgOut->setPageTitle( wfMsg( 'searchresults' ) );
- $subtitlemsg = ( Title::newFromText($term) ? 'searchsubtitle' : 'searchsubtitleinvalid' );
+ if( !empty( $term ) )
+ $wgOut->setPageTitle( wfMsg( 'searchresults' ) );
+ $subtitlemsg = ( Title::newFromText( $term ) ? 'searchsubtitle' : 'searchsubtitleinvalid' );
$wgOut->setSubtitle( $wgOut->parse( wfMsg( $subtitlemsg, wfEscapeWikiText($term) ) ) );
$wgOut->setArticleRelated( false );
$wgOut->setRobotpolicy( 'noindex,nofollow' );
}
- /**
- * Extract default namespaces to search from the given user's
- * settings, returning a list of index numbers.
- *
- * @param User $user
- * @return array
- * @private
- */
- function userNamespaces( &$user ) {
- $arr = array();
- foreach( SearchEngine::searchableNamespaces() as $ns => $name ) {
- if( $user->getOption( 'searchNs' . $ns ) ) {
- $arr[] = $ns;
- }
- }
- return $arr;
- }
-
/**
* Extract "power search" namespace settings from the request object,
* returning a list of index numbers to search.
$opt['redirs'] = $this->searchRedirects ? 1 : 0;
return $opt;
}
-
-
/**
+ * Show whole set of results
+ *
* @param SearchResultSet $matches
- * @param string $terms partial regexp for highlighting terms
*/
function showMatches( &$matches ) {
$fname = 'SpecialSearch::showMatches';
wfProfileIn( $fname );
global $wgContLang;
- $tm = $wgContLang->convertForSearchResult( $matches->termMatches() );
- $terms = implode( '|', $tm );
+ $terms = $wgContLang->convertForSearchResult( $matches->termMatches() );
+ $out = "";
+
+ $infoLine = $matches->getInfo();
+ if( !is_null($infoLine) )
+ $out .= "\n<!-- {$infoLine} -->\n";
+
+
$off = $this->offset + 1;
- $out = "<ul start='{$off}' class='mw-search-results'>\n";
+ $out .= "<ul class='mw-search-results'>\n";
while( $result = $matches->next() ) {
$out .= $this->showHit( $result, $terms );
/**
* Format a single hit result
* @param SearchResult $result
- * @param string $terms partial regexp for highlighting terms
+ * @param array $terms terms to highlight
*/
function showHit( $result, $terms ) {
$fname = 'SpecialSearch::showHit';
wfProfileIn( $fname );
global $wgUser, $wgContLang, $wgLang;
-
- $t = $result->getTitle();
- if( is_null( $t ) ) {
+
+ if( $result->isBrokenTitle() ) {
wfProfileOut( $fname );
return "<!-- Broken link in search result -->\n";
}
+
+ $t = $result->getTitle();
$sk = $wgUser->getSkin();
- //$contextlines = $wgUser->getOption( 'contextlines', 5 );
- $contextlines = 2; // Hardcode this. Old defaults sucked. :)
- $contextchars = $wgUser->getOption( 'contextchars', 50 );
-
- $link = $sk->makeKnownLinkObj( $t );
+ $link = $sk->makeKnownLinkObj( $t, $result->getTitleSnippet($terms));
//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()) {
+ wfProfileOut( $fname );
return "<li>{$link}</li>\n";
}
-
- $revision = Revision::newFromTitle( $t );
+
// If the page doesn't *exist*... our search index is out of date.
// The least confusing at this point is to drop the result.
// You may get less results, but... oh well. :P
- if( !$revision ) {
+ if( $result->isMissingRevision() ) {
+ wfProfileOut( $fname );
return "<!-- missing page " .
htmlspecialchars( $t->getPrefixedText() ) . "-->\n";
}
+
+ // format redirects / relevant sections
+ $redirectTitle = $result->getRedirectTitle();
+ $redirectText = $result->getRedirectSnippet($terms);
+ $sectionTitle = $result->getSectionTitle();
+ $sectionText = $result->getSectionSnippet($terms);
+ $redirect = '';
+ if( !is_null($redirectTitle) )
+ $redirect = "<span class='searchalttitle'>"
+ .wfMsg('search-redirect',$sk->makeKnownLinkObj( $redirectTitle, $redirectText))
+ ."</span>";
+ $section = '';
+ if( !is_null($sectionTitle) )
+ $section = "<span class='searchalttitle'>"
+ .wfMsg('search-section', $sk->makeKnownLinkObj( $sectionTitle, $sectionText))
+ ."</span>";
+
+ // format text extract
+ $extract = "<div class='searchresult'>".$result->getTextSnippet($terms)."</div>";
- $text = $revision->getText();
- $size = wfMsgExt( 'search-result-size', array( 'parsemag', 'escape' ),
- $sk->formatSize( strlen( $text ) ),
- str_word_count( $text ) );
- $date = $wgLang->timeanddate( $revision->getTimestamp() );
-
+ // format score
if( is_null( $result->getScore() ) ) {
// Search engine doesn't report scoring info
$score = '';
. ' - ';
}
- $extract = $this->extractText( $text, $terms, $contextlines, $contextchars );
-
+ // format description
+ $byteSize = $result->getByteSize();
+ $wordCount = $result->getWordCount();
+ $timestamp = $result->getTimestamp();
+ $size = wfMsgExt( 'search-result-size', array( 'parsemag', 'escape' ),
+ $sk->formatSize( $byteSize ),
+ $wordCount );
+ $date = $wgLang->timeanddate( $timestamp );
+
+ // link to related articles if supported
+ $related = '';
+ if( $result->hasRelated() ){
+ $st = SpecialPage::getTitleFor( 'Search' );
+ $stParams = wfArrayToCGI( $this->powerSearchOptions(),
+ array('search' => wfMsgForContent('searchrelated').':'.$t->getPrefixedText(),
+ 'fulltext' => wfMsg('search') ));
+
+ $related = ' -- <a href="'.$st->escapeLocalURL($stParams).'">'.
+ wfMsg('search-relatedarticle').'</a>';
+ }
+
// Include a thumbnail for media files...
if( $t->getNamespace() == NS_IMAGE ) {
$img = wfFindFile( $t );
'<td valign="top">' .
$link .
$extract .
- "<div class='mw-search-result-data'>{$score}{$desc} - {$date}</div>" .
+ "<div class='mw-search-result-data'>{$score}{$desc} - {$date}{$related}</div>" .
'</td>' .
'</tr>' .
'</table>' .
}
wfProfileOut( $fname );
- return "<li>{$link} {$extract}\n" .
- "<div class='mw-search-result-data'>{$score}{$size} - {$date}</div>" .
+ return "<li>{$link} {$redirect} {$section} {$extract}\n" .
+ "<div class='mw-search-result-data'>{$score}{$size} - {$date}{$related}</div>" .
"</li>\n";
}
-
- private function extractText( $text, $terms, $contextlines, $contextchars ) {
- global $wgLang, $wgContLang;
- $fname = __METHOD__;
-
- $lines = explode( "\n", $text );
-
- $max = intval( $contextchars ) + 1;
- $pat1 = "/(.*)($terms)(.{0,$max})/i";
- $lineno = 0;
-
- $extract = "";
- wfProfileIn( "$fname-extract" );
- foreach ( $lines as $line ) {
- if ( 0 == $contextlines ) {
- break;
- }
- ++$lineno;
- $m = array();
- if ( ! preg_match( $pat1, $line, $m ) ) {
- continue;
- }
- --$contextlines;
- $pre = $wgContLang->truncate( $m[1], -$contextchars, '...' );
-
- if ( count( $m ) < 3 ) {
- $post = '';
- } else {
- $post = $wgContLang->truncate( $m[3], $contextchars, '...' );
- }
+ /**
+ * Show results from other wikis
+ *
+ * @param SearchResultSet $matches
+ */
+ function showInterwiki( &$matches, $query ) {
+ $fname = 'SpecialSearch::showInterwiki';
+ wfProfileIn( $fname );
- $found = $m[2];
+ global $wgContLang;
+ $terms = $wgContLang->convertForSearchResult( $matches->termMatches() );
- $line = htmlspecialchars( $pre . $found . $post );
- $pat2 = '/(' . $terms . ")/i";
- $line = preg_replace( $pat2,
- "<span class='searchmatch'>\\1</span>", $line );
+ $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";
+
+ // work out custom project captions
+ $customCaptions = array();
+ $customLines = explode("\n",wfMsg('search-interwiki-custom')); // format per line <iwprefix>:<caption>
+ foreach($customLines as $line){
+ $parts = explode(":",$line,2);
+ if(count($parts) == 2) // validate line
+ $customCaptions[$parts[0]] = $parts[1];
+ }
+
+
+ $prev = null;
+ while( $result = $matches->next() ) {
+ $out .= $this->showInterwikiHit( $result, $prev, $terms, $query, $customCaptions );
+ $prev = $result->getInterwikiPrefix();
+ }
+ // FIXME: should support paging in a non-confusing way (not sure how though, maybe via ajax)..
+ $out .= "</ul></div>\n";
- $extract .= "<br /><small>{$line}</small>\n";
+ // convert the whole thing to desired language variant
+ global $wgContLang;
+ $out = $wgContLang->convert( $out );
+ wfProfileOut( $fname );
+ return $out;
+ }
+
+ /**
+ * Show single interwiki link
+ *
+ * @param SearchResult $result
+ * @param string $lastInterwiki
+ * @param array $terms
+ * @param string $query
+ * @param array $customCaptions iw prefix -> caption
+ */
+ function showInterwikiHit( $result, $lastInterwiki, $terms, $query, $customCaptions){
+ $fname = 'SpecialSearch::showInterwikiHit';
+ wfProfileIn( $fname );
+ global $wgUser, $wgContLang, $wgLang;
+
+ if( $result->isBrokenTitle() ) {
+ wfProfileOut( $fname );
+ return "<!-- Broken link in search result -->\n";
}
- wfProfileOut( "$fname-extract" );
- return $extract;
+ $t = $result->getTitle();
+ $sk = $wgUser->getSkin();
+
+ $link = $sk->makeKnownLinkObj( $t, $result->getTitleSnippet($terms));
+
+ // format redirect if any
+ $redirectTitle = $result->getRedirectTitle();
+ $redirectText = $result->getRedirectSnippet($terms);
+ $redirect = '';
+ if( !is_null($redirectTitle) )
+ $redirect = "<span class='searchalttitle'>"
+ .wfMsg('search-redirect',$sk->makeKnownLinkObj( $redirectTitle, $redirectText))
+ ."</span>";
+
+ $out = "";
+ // display project name
+ if(is_null($lastInterwiki) || $lastInterwiki != $t->getInterwiki()){
+ if( key_exists($t->getInterwiki(),$customCaptions) )
+ // captions from 'search-interwiki-custom'
+ $caption = $customCaptions[$t->getInterwiki()];
+ else{
+ // default is to show the hostname of the other wiki which might suck
+ // if there are many wikis on one hostname
+ $parsed = parse_url($t->getFullURL());
+ $caption = wfMsg('search-interwiki-default', $parsed['host']);
+ }
+ // "more results" link (special page stuff could be localized, but we might not know target lang)
+ $searchTitle = Title::newFromText($t->getInterwiki().":Special:Search");
+ $searchLink = $sk->makeKnownLinkObj( $searchTitle, wfMsg('search-interwiki-more'),
+ wfArrayToCGI(array('search' => $query, 'fulltext' => 'Search')));
+ $out .= "</ul><div class='mw-search-interwiki-project'><span class='mw-search-interwiki-more'>{$searchLink}</span>{$caption}</div>\n<ul>";
+ }
+
+ $out .= "<li>{$link} {$redirect}</li>\n";
+ wfProfileOut( $fname );
+ return $out;
}
+
/**
* Generates the power search box at bottom of [[Special:Search]]
$name = wfMsg( 'blanknamespace' );
}
$namespaces .= Xml::openElement( 'span', array( 'style' => 'white-space: nowrap' ) ) .
- Xml::checkLabel( $name, "ns{$ns}", $name, in_array( $ns, $this->namespaces ) ) .
+ Xml::checkLabel( $name, "ns{$ns}", "mw-search-ns{$ns}", in_array( $ns, $this->namespaces ) ) .
Xml::closeElement( 'span' ) . "\n";
}
- $redirect = Xml::check( 'redirs', $this->searchRedirects, array( 'value' => '1' ) );
+ $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";
$out = Xml::openElement( 'form', array( 'id' => 'powersearch', 'method' => 'get', 'action' => $wgScript ) ) .
- Xml::openElement( 'fieldset' ) .
- Xml::element( 'legend', array( ), wfMsg( 'powersearch-legend' ) ) .
- Xml::hidden( 'title', 'Special:Search' ) .
- wfMsgExt( 'powersearchtext', array( 'parse', 'replaceafter' ),
- $namespaces, $redirect, $searchField,
- '', '', '', '', '', # Dummy placeholders
+ Xml::fieldset( wfMsg( 'powersearch-legend' ),
+ Xml::hidden( 'title', 'Special:Search' ) .
+ "<p>" .
+ wfMsgExt( 'powersearch-ns', array( 'parseinline' ) ) .
+ "<br />" .
+ $namespaces .
+ "</p>" .
+ "<p>" .
+ $redirect . " " . $redirectLabel .
+ "</p>" .
+ wfMsgExt( 'powersearch-field', array( 'parseinline' ) ) .
+ " " .
+ $searchField .
+ " " .
$searchButton ) .
- Xml::closeElement( 'fieldset' ) .
- Xml::closeElement( 'form' );
+ "</form>";
return $out;
}
function powerSearchFocus() {
- return "<script type='text/javascript'>" .
- "document.getElementById('powerSearchText').focus();" .
+ global $wgJsMimeType;
+ return "<script type=\"$wgJsMimeType\">" .
+ "hookEvent(\"load\", function(){" .
+ "document.getElementById('powerSearchText').focus();" .
+ "});" .
"</script>";
}
-
+
function shortDialog($term) {
global $wgScript;
-
+
$out = Xml::openElement( 'form', array(
'id' => 'search',
'method' => 'get',
'action' => $wgScript
));
$out .= Xml::hidden( 'title', 'Special:Search' );
- $out .= Xml::input( 'search', 50, $term ) . ' ';
+ $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::submitButton( wfMsg( 'searchbutton' ), array( 'name' => 'fulltext' ) );
$out .= Xml::closeElement( 'form' );
-
+
return $out;
}
}