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 # if language supports variants, search in all variants
101 if(sizeof($wgContLang->getVariants())>1){
102 $allTermVariants = $wgContLang->convertLinkToAllVariants($term);
104 foreach($allTermVariants as $termVariant){
105 $t = SearchEngine
::getNearMatch( $termVariant );
106 if( !is_null( $t ) ) {
107 $wgOut->redirect( $t->getFullURL() );
108 wfProfileOut( $fname );
114 # No match, generate an edit URL
115 $t = Title
::newFromText( $term );
116 if( is_null( $t ) ) {
117 $editurl = ''; # hrm...
119 wfRunHooks( 'SpecialSearchNogomatch', array( &$t ) );
120 # If the feature is enabled, go straight to the edit page
122 $wgOut->redirect( $t->getFullURL( 'action=edit' ) );
125 $editurl = $t->escapeLocalURL( 'action=edit' );
128 $wgOut->addWikiText( wfMsg( 'noexactmatch', wfEscapeWikiText( $term ) ) );
130 return $this->showResults( $term );
134 * @param string $term
137 function showResults( $term ) {
138 $fname = 'SpecialSearch::showResults';
139 wfProfileIn( $fname );
141 $this->setupPage( $term );
143 global $wgUser, $wgOut;
144 $sk = $wgUser->getSkin();
145 $wgOut->addWikiText( wfMsg( 'searchresulttext' ) );
147 #if ( !$this->parseQuery() ) {
148 if( '' === trim( $term ) ) {
149 $wgOut->setSubtitle( '' );
150 $wgOut->addHTML( $this->powerSearchBox( $term ) );
151 wfProfileOut( $fname );
155 global $wgDisableTextSearch;
156 if ( $wgDisableTextSearch ) {
157 global $wgForwardSearchUrl;
158 if( $wgForwardSearchUrl ) {
159 $url = str_replace( '$1', urlencode( $term ), $wgForwardSearchUrl );
160 $wgOut->redirect( $url );
163 global $wgInputEncoding;
164 $wgOut->addHTML( wfMsg( 'searchdisabled' ) );
166 wfMsg( 'googlesearch',
167 htmlspecialchars( $term ),
168 htmlspecialchars( $wgInputEncoding ),
169 htmlspecialchars( wfMsg( 'search' ) )
172 wfProfileOut( $fname );
176 $search = SearchEngine
::create();
177 $search->setLimitOffset( $this->limit
, $this->offset
);
178 $search->setNamespaces( $this->namespaces
);
179 $search->showRedirects
= $this->searchRedirects
;
180 $titleMatches = $search->searchTitle( $term );
181 $textMatches = $search->searchText( $term );
183 $num = ( $titleMatches ?
$titleMatches->numRows() : 0 )
184 +
( $textMatches ?
$textMatches->numRows() : 0);
185 if ( $num >= $this->limit
) {
186 $top = wfShowingResults( $this->offset
, $this->limit
);
188 $top = wfShowingResultsNum( $this->offset
, $this->limit
, $num );
190 $wgOut->addHTML( "<p>{$top}</p>\n" );
192 if( $num ||
$this->offset
) {
193 $prevnext = wfViewPrevNext( $this->offset
, $this->limit
,
196 $this->powerSearchOptions(),
197 array( 'search' => $term ) ) );
198 $wgOut->addHTML( "<br />{$prevnext}\n" );
201 if( $titleMatches ) {
202 if( $titleMatches->numRows() ) {
203 $wgOut->addWikiText( '==' . wfMsg( 'titlematches' ) . "==\n" );
204 $wgOut->addHTML( $this->showMatches( $titleMatches ) );
206 $wgOut->addWikiText( '==' . wfMsg( 'notitlematches' ) . "==\n" );
211 if( $textMatches->numRows() ) {
212 $wgOut->addWikiText( '==' . wfMsg( 'textmatches' ) . "==\n" );
213 $wgOut->addHTML( $this->showMatches( $textMatches ) );
214 } elseif( $num == 0 ) {
215 # Don't show the 'no text matches' if we received title matches
216 $wgOut->addWikiText( '==' . wfMsg( 'notextmatches' ) . "==\n" );
221 $wgOut->addWikiText( wfMsg( 'nonefound' ) );
223 if( $num ||
$this->offset
) {
224 $wgOut->addHTML( "<p>{$prevnext}</p>\n" );
226 $wgOut->addHTML( $this->powerSearchBox( $term ) );
227 wfProfileOut( $fname );
230 #------------------------------------------------------------------
231 # Private methods below this line
236 function setupPage( $term ) {
238 $wgOut->setPageTitle( wfMsg( 'searchresults' ) );
239 $subtitlemsg = ( Title
::newFromText($term) ?
'searchsubtitle' : 'searchsubtitleinvalid' );
240 $wgOut->setSubtitle( $wgOut->parse( wfMsg( $subtitlemsg, wfEscapeWikiText($term) ) ) );
241 $wgOut->setArticleRelated( false );
242 $wgOut->setRobotpolicy( 'noindex,nofollow' );
246 * Extract default namespaces to search from the given user's
247 * settings, returning a list of index numbers.
253 function userNamespaces( &$user ) {
255 foreach( SearchEngine
::searchableNamespaces() as $ns => $name ) {
256 if( $user->getOption( 'searchNs' . $ns ) ) {
264 * Extract "power search" namespace settings from the request object,
265 * returning a list of index numbers to search.
267 * @param WebRequest $request
271 function powerSearch( &$request ) {
273 foreach( SearchEngine
::searchableNamespaces() as $ns => $name ) {
274 if( $request->getCheck( 'ns' . $ns ) ) {
282 * Reconstruct the 'power search' options for links
286 function powerSearchOptions() {
288 foreach( $this->namespaces
as $n ) {
291 $opt['redirs'] = $this->searchRedirects ?
1 : 0;
297 * @param SearchResultSet $matches
298 * @param string $terms partial regexp for highlighting terms
300 function showMatches( &$matches ) {
301 $fname = 'SpecialSearch::showMatches';
302 wfProfileIn( $fname );
305 $tm = $wgContLang->convertForSearchResult( $matches->termMatches() );
306 $terms = implode( '|', $tm );
308 $off = $this->offset +
1;
309 $out = "<ol start='{$off}'>\n";
311 while( $result = $matches->next() ) {
312 $out .= $this->showHit( $result, $terms );
316 // convert the whole thing to desired language variant
318 $out = $wgContLang->convert( $out );
319 wfProfileOut( $fname );
324 * Format a single hit result
325 * @param SearchResult $result
326 * @param string $terms partial regexp for highlighting terms
328 function showHit( $result, $terms ) {
329 $fname = 'SpecialSearch::showHit';
330 wfProfileIn( $fname );
331 global $wgUser, $wgContLang, $wgLang;
333 $t = $result->getTitle();
334 if( is_null( $t ) ) {
335 wfProfileOut( $fname );
336 return "<!-- Broken link in search result -->\n";
338 $sk =& $wgUser->getSkin();
340 $contextlines = $wgUser->getOption( 'contextlines' );
341 if ( '' == $contextlines ) { $contextlines = 5; }
342 $contextchars = $wgUser->getOption( 'contextchars' );
343 if ( '' == $contextchars ) { $contextchars = 50; }
345 $link = $sk->makeKnownLinkObj( $t );
346 $revision = Revision
::newFromTitle( $t );
347 $text = $revision->getText();
348 $size = wfMsgExt( 'nbytes', array( 'parsemag', 'escape'),
349 $wgLang->formatNum( strlen( $text ) ) );
351 $lines = explode( "\n", $text );
353 $max = intval( $contextchars ) +
1;
354 $pat1 = "/(.*)($terms)(.{0,$max})/i";
359 wfProfileIn( "$fname-extract" );
360 foreach ( $lines as $line ) {
361 if ( 0 == $contextlines ) {
365 if ( ! preg_match( $pat1, $line, $m ) ) {
369 $pre = $wgContLang->truncate( $m[1], -$contextchars, '...' );
371 if ( count( $m ) < 3 ) {
374 $post = $wgContLang->truncate( $m[3], $contextchars, '...' );
379 $line = htmlspecialchars( $pre . $found . $post );
380 $pat2 = '/(' . $terms . ")/i";
381 $line = preg_replace( $pat2,
382 "<span class='searchmatch'>\\1</span>", $line );
384 $extract .= "<br /><small>{$lineno}: {$line}</small>\n";
386 wfProfileOut( "$fname-extract" );
387 wfProfileOut( $fname );
388 return "<li>{$link} ({$size}){$extract}</li>\n";
391 function powerSearchBox( $term ) {
393 foreach( SearchEngine
::searchableNamespaces() as $ns => $name ) {
394 $checked = in_array( $ns, $this->namespaces
)
395 ?
' checked="checked"'
397 $name = str_replace( '_', ' ', $name );
399 $name = wfMsg( 'blanknamespace' );
401 $namespaces .= " <label><input type='checkbox' value=\"1\" name=\"" .
402 "ns{$ns}\"{$checked} />{$name}</label>\n";
405 $checked = $this->searchRedirects
406 ?
' checked="checked"'
408 $redirect = "<input type='checkbox' value='1' name=\"redirs\"{$checked} />\n";
410 $searchField = '<input type="text" name="search" value="' .
411 htmlspecialchars( $term ) ."\" size=\"16\" />\n";
413 $searchButton = '<input type="submit" name="searchx" value="' .
414 htmlspecialchars( wfMsg('powersearch') ) . "\" />\n";
416 $ret = wfMsg( 'powersearchtext',
417 $namespaces, $redirect, $searchField,
418 '', '', '', '', '', # Dummy placeholders
421 $title = Title
::makeTitle( NS_SPECIAL
, 'Search' );
422 $action = $title->escapeLocalURL();
423 return "<br /><br />\n<form id=\"powersearch\" method=\"get\" " .
424 "action=\"$action\">\n{$ret}\n</form>\n";