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
23 * @subpackage SpecialPage
29 * @param $par String: (default '')
31 function wfSpecialSearch( $par = '' ) {
32 global $wgRequest, $wgUser;
34 $search = $wgRequest->getText( 'search', $par );
35 $searchPage = new SpecialSearch( $wgRequest, $wgUser );
36 if( $wgRequest->getVal( 'fulltext' ) ||
37 !is_null( $wgRequest->getVal( 'offset' ) ) ||
38 !is_null ($wgRequest->getVal( 'searchx' ) ) ) {
39 $searchPage->showResults( $search );
41 $searchPage->goResult( $search );
48 * @subpackage SpecialPage
53 * Set up basic search parameters from the request and user settings.
54 * Typically you'll pass $wgRequest and $wgUser.
56 * @param WebRequest $request
60 function SpecialSearch( &$request, &$user ) {
61 list( $this->limit
, $this->offset
) = $request->getLimitOffset( 20, 'searchlimit' );
63 if( $request->getCheck( 'searchx' ) ) {
64 $this->namespaces
= $this->powerSearch( $request );
66 $this->namespaces
= $this->userNamespaces( $user );
69 $this->searchRedirects
= $request->getcheck( 'redirs' ) ?
true : false;
73 * If an exact title match can be found, jump straight ahead to
77 function goResult( $term ) {
82 $this->setupPage( $term );
84 # Try to go to page as entered.
86 $t = Title
::newFromText( $term );
88 # If the string cannot be used to create a title
90 return $this->showResults( $term );
93 # If there's an exact or very near match, jump right there.
94 $t = SearchEngine
::getNearMatch( $term );
95 if( !is_null( $t ) ) {
96 $wgOut->redirect( $t->getFullURL() );
100 # No match, generate an edit URL
101 $t = Title
::newFromText( $term );
102 if( is_null( $t ) ) {
103 $editurl = ''; # hrm...
105 wfRunHooks( 'SpecialSearchNogomatch', array( &$t ) );
106 # If the feature is enabled, go straight to the edit page
108 $wgOut->redirect( $t->getFullURL( 'action=edit' ) );
111 $editurl = $t->escapeLocalURL( 'action=edit' );
114 $wgOut->addWikiText( wfMsg( 'noexactmatch', wfEscapeWikiText( $term ) ) );
116 return $this->showResults( $term );
120 * @param string $term
123 function showResults( $term ) {
124 $fname = 'SpecialSearch::showResults';
125 wfProfileIn( $fname );
127 $this->setupPage( $term );
129 global $wgUser, $wgOut;
130 $sk = $wgUser->getSkin();
131 $wgOut->addWikiText( wfMsg( 'searchresulttext' ) );
133 #if ( !$this->parseQuery() ) {
134 if( '' === trim( $term ) ) {
135 $wgOut->setSubtitle( '' );
136 $wgOut->addHTML( $this->powerSearchBox( $term ) );
137 wfProfileOut( $fname );
141 global $wgDisableTextSearch;
142 if ( $wgDisableTextSearch ) {
143 global $wgForwardSearchUrl;
144 if( $wgForwardSearchUrl ) {
145 $url = str_replace( '$1', urlencode( $term ), $wgForwardSearchUrl );
146 $wgOut->redirect( $url );
149 global $wgInputEncoding;
150 $wgOut->addHTML( wfMsg( 'searchdisabled' ) );
152 wfMsg( 'googlesearch',
153 htmlspecialchars( $term ),
154 htmlspecialchars( $wgInputEncoding ),
155 htmlspecialchars( wfMsg( 'searchbutton' ) )
158 wfProfileOut( $fname );
162 $search = SearchEngine
::create();
163 $search->setLimitOffset( $this->limit
, $this->offset
);
164 $search->setNamespaces( $this->namespaces
);
165 $search->showRedirects
= $this->searchRedirects
;
166 $titleMatches = $search->searchTitle( $term );
167 $textMatches = $search->searchText( $term );
169 $num = ( $titleMatches ?
$titleMatches->numRows() : 0 )
170 +
( $textMatches ?
$textMatches->numRows() : 0);
171 if ( $num >= $this->limit
) {
172 $top = wfShowingResults( $this->offset
, $this->limit
);
174 $top = wfShowingResultsNum( $this->offset
, $this->limit
, $num );
176 $wgOut->addHTML( "<p>{$top}</p>\n" );
178 if( $num ||
$this->offset
) {
179 $prevnext = wfViewPrevNext( $this->offset
, $this->limit
,
182 $this->powerSearchOptions(),
183 array( 'search' => $term ) ) );
184 $wgOut->addHTML( "<br />{$prevnext}\n" );
187 if( $titleMatches ) {
188 if( $titleMatches->numRows() ) {
189 $wgOut->addWikiText( '==' . wfMsg( 'titlematches' ) . "==\n" );
190 $wgOut->addHTML( $this->showMatches( $titleMatches ) );
192 $wgOut->addWikiText( '==' . wfMsg( 'notitlematches' ) . "==\n" );
197 if( $textMatches->numRows() ) {
198 $wgOut->addWikiText( '==' . wfMsg( 'textmatches' ) . "==\n" );
199 $wgOut->addHTML( $this->showMatches( $textMatches ) );
200 } elseif( $num == 0 ) {
201 # Don't show the 'no text matches' if we received title matches
202 $wgOut->addWikiText( '==' . wfMsg( 'notextmatches' ) . "==\n" );
207 $wgOut->addWikiText( wfMsg( 'nonefound' ) );
209 if( $num ||
$this->offset
) {
210 $wgOut->addHTML( "<p>{$prevnext}</p>\n" );
212 $wgOut->addHTML( $this->powerSearchBox( $term ) );
213 wfProfileOut( $fname );
216 #------------------------------------------------------------------
217 # Private methods below this line
222 function setupPage( $term ) {
224 $wgOut->setPageTitle( wfMsg( 'searchresults' ) );
225 $subtitlemsg = ( Title
::newFromText($term) ?
'searchsubtitle' : 'searchsubtitleinvalid' );
226 $wgOut->setSubtitle( $wgOut->parse( wfMsg( $subtitlemsg, wfEscapeWikiText($term) ) ) );
227 $wgOut->setArticleRelated( false );
228 $wgOut->setRobotpolicy( 'noindex,nofollow' );
232 * Extract default namespaces to search from the given user's
233 * settings, returning a list of index numbers.
239 function userNamespaces( &$user ) {
241 foreach( SearchEngine
::searchableNamespaces() as $ns => $name ) {
242 if( $user->getOption( 'searchNs' . $ns ) ) {
250 * Extract "power search" namespace settings from the request object,
251 * returning a list of index numbers to search.
253 * @param WebRequest $request
257 function powerSearch( &$request ) {
259 foreach( SearchEngine
::searchableNamespaces() as $ns => $name ) {
260 if( $request->getCheck( 'ns' . $ns ) ) {
268 * Reconstruct the 'power search' options for links
272 function powerSearchOptions() {
274 foreach( $this->namespaces
as $n ) {
277 $opt['redirs'] = $this->searchRedirects ?
1 : 0;
283 * @param SearchResultSet $matches
284 * @param string $terms partial regexp for highlighting terms
286 function showMatches( &$matches ) {
287 $fname = 'SpecialSearch::showMatches';
288 wfProfileIn( $fname );
291 $tm = $wgContLang->convertForSearchResult( $matches->termMatches() );
292 $terms = implode( '|', $tm );
294 $off = $this->offset +
1;
295 $out = "<ol start='{$off}'>\n";
297 while( $result = $matches->next() ) {
298 $out .= $this->showHit( $result, $terms );
302 // convert the whole thing to desired language variant
304 $out = $wgContLang->convert( $out );
305 wfProfileOut( $fname );
310 * Format a single hit result
311 * @param SearchResult $result
312 * @param string $terms partial regexp for highlighting terms
314 function showHit( $result, $terms ) {
315 $fname = 'SpecialSearch::showHit';
316 wfProfileIn( $fname );
317 global $wgUser, $wgContLang, $wgLang;
319 $t = $result->getTitle();
320 if( is_null( $t ) ) {
321 wfProfileOut( $fname );
322 return "<!-- Broken link in search result -->\n";
324 $sk =& $wgUser->getSkin();
326 $contextlines = $wgUser->getOption( 'contextlines' );
327 if ( '' == $contextlines ) { $contextlines = 5; }
328 $contextchars = $wgUser->getOption( 'contextchars' );
329 if ( '' == $contextchars ) { $contextchars = 50; }
331 $link = $sk->makeKnownLinkObj( $t );
332 $revision = Revision
::newFromTitle( $t );
333 $text = $revision->getText();
334 $size = wfMsgExt( 'nbytes', array( 'parsemag', 'escape'),
335 $wgLang->formatNum( strlen( $text ) ) );
337 $lines = explode( "\n", $text );
339 $max = intval( $contextchars ) +
1;
340 $pat1 = "/(.*)($terms)(.{0,$max})/i";
345 wfProfileIn( "$fname-extract" );
346 foreach ( $lines as $line ) {
347 if ( 0 == $contextlines ) {
351 if ( ! preg_match( $pat1, $line, $m ) ) {
355 $pre = $wgContLang->truncate( $m[1], -$contextchars, '...' );
357 if ( count( $m ) < 3 ) {
360 $post = $wgContLang->truncate( $m[3], $contextchars, '...' );
365 $line = htmlspecialchars( $pre . $found . $post );
366 $pat2 = '/(' . $terms . ")/i";
367 $line = preg_replace( $pat2,
368 "<span class='searchmatch'>\\1</span>", $line );
370 $extract .= "<br /><small>{$lineno}: {$line}</small>\n";
372 wfProfileOut( "$fname-extract" );
373 wfProfileOut( $fname );
374 return "<li>{$link} ({$size}){$extract}</li>\n";
377 function powerSearchBox( $term ) {
379 foreach( SearchEngine
::searchableNamespaces() as $ns => $name ) {
380 $checked = in_array( $ns, $this->namespaces
)
381 ?
' checked="checked"'
383 $name = str_replace( '_', ' ', $name );
385 $name = wfMsg( 'blanknamespace' );
387 $namespaces .= " <label><input type='checkbox' value=\"1\" name=\"" .
388 "ns{$ns}\"{$checked} />{$name}</label>\n";
391 $checked = $this->searchRedirects
392 ?
' checked="checked"'
394 $redirect = "<input type='checkbox' value='1' name=\"redirs\"{$checked} />\n";
396 $searchField = '<input type="text" name="search" value="' .
397 htmlspecialchars( $term ) ."\" size=\"16\" />\n";
399 $searchButton = '<input type="submit" name="searchx" value="' .
400 htmlspecialchars( wfMsg('powersearch') ) . "\" />\n";
402 $ret = wfMsg( 'powersearchtext',
403 $namespaces, $redirect, $searchField,
404 '', '', '', '', '', # Dummy placeholders
407 $title = Title
::makeTitle( NS_SPECIAL
, 'Search' );
408 $action = $title->escapeLocalURL();
409 return "<br /><br />\n<form id=\"powersearch\" method=\"get\" " .
410 "action=\"$action\">\n{$ret}\n</form>\n";