2 # Copyright (C) 2004 Brion Vibber <brion@pobox.com>
3 # http://www.mediawiki.org/
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 2 of the License, or
8 # (at your option) any later version.
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License along
16 # with this program; if not, write to the Free Software Foundation, Inc.,
17 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 # http://www.gnu.org/copyleft/gpl.html
21 * Run text & title search and display the output
22 * @addtogroup SpecialPage
28 * @param $par String: (default '')
30 function wfSpecialSearch( $par = '' ) {
31 global $wgRequest, $wgUser;
33 $search = $wgRequest->getText( 'search', $par );
34 $searchPage = new SpecialSearch( $wgRequest, $wgUser );
35 if( $wgRequest->getVal( 'fulltext' ) ||
36 !is_null( $wgRequest->getVal( 'offset' ) ) ||
37 !is_null ($wgRequest->getVal( 'searchx' ) ) ) {
38 $searchPage->showResults( $search );
40 $searchPage->goResult( $search );
45 * implements Special:Search - Run text & title search and display the output
46 * @addtogroup SpecialPage
51 * Set up basic search parameters from the request and user settings.
52 * Typically you'll pass $wgRequest and $wgUser.
54 * @param WebRequest $request
58 function SpecialSearch( &$request, &$user ) {
59 list( $this->limit
, $this->offset
) = $request->getLimitOffset( 20, 'searchlimit' );
61 if( $request->getCheck( 'searchx' ) ) {
62 $this->namespaces
= $this->powerSearch( $request );
64 $this->namespaces
= $this->userNamespaces( $user );
67 $this->searchRedirects
= $request->getcheck( 'redirs' ) ?
true : false;
71 * If an exact title match can be found, jump straight ahead to it.
75 function goResult( $term ) {
79 $this->setupPage( $term );
81 # Try to go to page as entered.
82 $t = Title
::newFromText( $term );
84 # If the string cannot be used to create a title
86 return $this->showResults( $term );
89 # If there's an exact or very near match, jump right there.
90 $t = SearchEngine
::getNearMatch( $term );
91 if( !is_null( $t ) ) {
92 $wgOut->redirect( $t->getFullURL() );
96 # No match, generate an edit URL
97 $t = Title
::newFromText( $term );
98 if( ! is_null( $t ) ) {
99 wfRunHooks( 'SpecialSearchNogomatch', array( &$t ) );
100 # If the feature is enabled, go straight to the edit page
102 $wgOut->redirect( $t->getFullURL( 'action=edit' ) );
106 if( $t->quickUserCan( 'create' ) && $t->quickUserCan( 'edit' ) ) {
107 $wgOut->addWikiMsg( 'noexactmatch', wfEscapeWikiText( $term ) );
109 $wgOut->addWikiMsg( 'noexactmatch-nocreate', wfEscapeWikiText( $term ) );
112 return $this->showResults( $term );
116 * @param string $term
119 function showResults( $term ) {
120 $fname = 'SpecialSearch::showResults';
121 wfProfileIn( $fname );
123 $this->setupPage( $term );
126 $wgOut->addWikiMsg( 'searchresulttext' );
128 if( '' === trim( $term ) ) {
129 // Empty query -- straight view of search form
130 $wgOut->setSubtitle( '' );
131 $wgOut->addHTML( $this->powerSearchBox( $term ) );
132 $wgOut->addHTML( $this->powerSearchFocus() );
133 wfProfileOut( $fname );
137 global $wgDisableTextSearch;
138 if ( $wgDisableTextSearch ) {
139 global $wgForwardSearchUrl;
140 if( $wgForwardSearchUrl ) {
141 $url = str_replace( '$1', urlencode( $term ), $wgForwardSearchUrl );
142 $wgOut->redirect( $url );
145 global $wgInputEncoding;
146 $wgOut->addHTML( wfMsg( 'searchdisabled' ) );
148 wfMsg( 'googlesearch',
149 htmlspecialchars( $term ),
150 htmlspecialchars( $wgInputEncoding ),
151 htmlspecialchars( wfMsg( 'searchbutton' ) )
154 wfProfileOut( $fname );
158 $search = SearchEngine
::create();
159 $search->setLimitOffset( $this->limit
, $this->offset
);
160 $search->setNamespaces( $this->namespaces
);
161 $search->showRedirects
= $this->searchRedirects
;
162 $titleMatches = $search->searchTitle( $term );
164 // Sometimes the search engine knows there are too many hits
165 if ($titleMatches instanceof SearchResultTooMany
) {
166 $wgOut->addWikiText( '==' . wfMsg( 'toomanymatches' ) . "==\n" );
167 $wgOut->addHTML( $this->powerSearchBox( $term ) );
168 $wgOut->addHTML( $this->powerSearchFocus() );
169 wfProfileOut( $fname );
172 $textMatches = $search->searchText( $term );
174 $num = ( $titleMatches ?
$titleMatches->numRows() : 0 )
175 +
( $textMatches ?
$textMatches->numRows() : 0);
177 if ( $num >= $this->limit
) {
178 $top = wfShowingResults( $this->offset
, $this->limit
);
180 $top = wfShowingResultsNum( $this->offset
, $this->limit
, $num );
182 $wgOut->addHTML( "<p>{$top}</p>\n" );
185 if( $num ||
$this->offset
) {
186 $prevnext = wfViewPrevNext( $this->offset
, $this->limit
,
187 SpecialPage
::getTitleFor( 'Search' ),
189 $this->powerSearchOptions(),
190 array( 'search' => $term ) ),
191 ($num < $this->limit
) );
192 $wgOut->addHTML( "<br />{$prevnext}\n" );
195 if( $titleMatches ) {
196 if( $titleMatches->numRows() ) {
197 $wgOut->wrapWikiMsg( "==$1==\n", 'titlematches' );
198 $wgOut->addHTML( $this->showMatches( $titleMatches ) );
200 $wgOut->wrapWikiMsg( "==$1==\n", 'notitlematches' );
202 $titleMatches->free();
206 if( $textMatches->numRows() ) {
207 $wgOut->wrapWikiMsg( "==$1==\n", 'textmatches' );
208 $wgOut->addHTML( $this->showMatches( $textMatches ) );
209 } elseif( $num == 0 ) {
210 # Don't show the 'no text matches' if we received title matches
211 $wgOut->wrapWikiMsg( "==$1==\n", 'notextmatches' );
213 $textMatches->free();
217 $wgOut->addWikiMsg( 'nonefound' );
219 if( $num ||
$this->offset
) {
220 $wgOut->addHTML( "<p>{$prevnext}</p>\n" );
222 $wgOut->addHTML( $this->powerSearchBox( $term ) );
223 wfProfileOut( $fname );
226 #------------------------------------------------------------------
227 # Private methods below this line
232 function setupPage( $term ) {
234 $wgOut->setPageTitle( wfMsg( 'searchresults' ) );
235 $subtitlemsg = ( Title
::newFromText($term) ?
'searchsubtitle' : 'searchsubtitleinvalid' );
236 $wgOut->setSubtitle( $wgOut->parse( wfMsg( $subtitlemsg, wfEscapeWikiText($term) ) ) );
237 $wgOut->setArticleRelated( false );
238 $wgOut->setRobotpolicy( 'noindex,nofollow' );
242 * Extract default namespaces to search from the given user's
243 * settings, returning a list of index numbers.
249 function userNamespaces( &$user ) {
251 foreach( SearchEngine
::searchableNamespaces() as $ns => $name ) {
252 if( $user->getOption( 'searchNs' . $ns ) ) {
260 * Extract "power search" namespace settings from the request object,
261 * returning a list of index numbers to search.
263 * @param WebRequest $request
267 function powerSearch( &$request ) {
269 foreach( SearchEngine
::searchableNamespaces() as $ns => $name ) {
270 if( $request->getCheck( 'ns' . $ns ) ) {
278 * Reconstruct the 'power search' options for links
282 function powerSearchOptions() {
284 foreach( $this->namespaces
as $n ) {
287 $opt['redirs'] = $this->searchRedirects ?
1 : 0;
293 * @param SearchResultSet $matches
294 * @param string $terms partial regexp for highlighting terms
296 function showMatches( &$matches ) {
297 $fname = 'SpecialSearch::showMatches';
298 wfProfileIn( $fname );
301 $tm = $wgContLang->convertForSearchResult( $matches->termMatches() );
302 $terms = implode( '|', $tm );
304 $off = $this->offset +
1;
305 $out = "<ol start='{$off}'>\n";
307 while( $result = $matches->next() ) {
308 $out .= $this->showHit( $result, $terms );
312 // convert the whole thing to desired language variant
314 $out = $wgContLang->convert( $out );
315 wfProfileOut( $fname );
320 * Format a single hit result
321 * @param SearchResult $result
322 * @param string $terms partial regexp for highlighting terms
324 function showHit( $result, $terms ) {
325 $fname = 'SpecialSearch::showHit';
326 wfProfileIn( $fname );
327 global $wgUser, $wgContLang, $wgLang;
329 $t = $result->getTitle();
330 if( is_null( $t ) ) {
331 wfProfileOut( $fname );
332 return "<!-- Broken link in search result -->\n";
334 $sk = $wgUser->getSkin();
336 $contextlines = $wgUser->getOption( 'contextlines', 5 );
337 $contextchars = $wgUser->getOption( 'contextchars', 50 );
339 $link = $sk->makeKnownLinkObj( $t );
341 //If page content is not readable, just return the title.
342 //This is not quite safe, but better than showing excerpts from non-readable pages
343 //Note that hiding the entry entirely would screw up paging.
344 if (!$t->userCanRead()) {
345 return "<li>{$link}</li>\n";
348 $revision = Revision
::newFromTitle( $t );
349 $text = $revision->getText();
350 $size = wfMsgExt( 'nbytes', array( 'parsemag', 'escape'),
351 $wgLang->formatNum( strlen( $text ) ) );
353 $lines = explode( "\n", $text );
355 $max = intval( $contextchars ) +
1;
356 $pat1 = "/(.*)($terms)(.{0,$max})/i";
361 wfProfileIn( "$fname-extract" );
362 foreach ( $lines as $line ) {
363 if ( 0 == $contextlines ) {
368 if ( ! preg_match( $pat1, $line, $m ) ) {
372 $pre = $wgContLang->truncate( $m[1], -$contextchars, '...' );
374 if ( count( $m ) < 3 ) {
377 $post = $wgContLang->truncate( $m[3], $contextchars, '...' );
382 $line = htmlspecialchars( $pre . $found . $post );
383 $pat2 = '/(' . $terms . ")/i";
384 $line = preg_replace( $pat2,
385 "<span class='searchmatch'>\\1</span>", $line );
387 $extract .= "<br /><small>{$lineno}: {$line}</small>\n";
389 wfProfileOut( "$fname-extract" );
390 wfProfileOut( $fname );
391 return "<li>{$link} ({$size}){$extract}</li>\n";
394 function powerSearchBox( $term ) {
396 foreach( SearchEngine
::searchableNamespaces() as $ns => $name ) {
397 $checked = in_array( $ns, $this->namespaces
)
398 ?
' checked="checked"'
400 $name = str_replace( '_', ' ', $name );
402 $name = wfMsg( 'blanknamespace' );
404 $encName = htmlspecialchars( $name );
405 $namespaces .= " <label><input type='checkbox' value=\"1\" name=\"" .
406 "ns{$ns}\"{$checked} />{$encName}</label>\n";
409 $checked = $this->searchRedirects
410 ?
' checked="checked"'
412 $redirect = "<input type='checkbox' value='1' name=\"redirs\"{$checked} />\n";
414 $searchField = '<input type="text" id="powerSearchText" name="search" value="' .
415 htmlspecialchars( $term ) ."\" size=\"16\" />\n";
417 $searchButton = '<input type="submit" name="searchx" value="' .
418 htmlspecialchars( wfMsg('powersearch') ) . "\" />\n";
420 $ret = wfMsg( 'powersearchtext',
421 $namespaces, $redirect, $searchField,
422 '', '', '', '', '', # Dummy placeholders
425 $title = SpecialPage
::getTitleFor( 'Search' );
426 $action = $title->escapeLocalURL();
427 return "<br /><br />\n<form id=\"powersearch\" method=\"get\" " .
428 "action=\"$action\">\n{$ret}\n</form>\n";
431 function powerSearchFocus() {
432 return "<script type='text/javascript'>" .
433 "document.getElementById('powerSearchText').focus();" .