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 );
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 $wgOut->addWikiText( wfMsg( 'noexactmatch', wfEscapeWikiText( $term ) ) );
108 return $this->showResults( $term );
112 * @param string $term
115 function showResults( $term ) {
116 $fname = 'SpecialSearch::showResults';
117 wfProfileIn( $fname );
119 $this->setupPage( $term );
122 $wgOut->addWikiText( wfMsg( 'searchresulttext' ) );
124 #if ( !$this->parseQuery() ) {
125 if( '' === trim( $term ) ) {
126 $wgOut->setSubtitle( '' );
127 $wgOut->addHTML( $this->powerSearchBox( $term ) );
128 wfProfileOut( $fname );
132 global $wgDisableTextSearch;
133 if ( $wgDisableTextSearch ) {
134 global $wgForwardSearchUrl;
135 if( $wgForwardSearchUrl ) {
136 $url = str_replace( '$1', urlencode( $term ), $wgForwardSearchUrl );
137 $wgOut->redirect( $url );
140 global $wgInputEncoding;
141 $wgOut->addHTML( wfMsg( 'searchdisabled' ) );
143 wfMsg( 'googlesearch',
144 htmlspecialchars( $term ),
145 htmlspecialchars( $wgInputEncoding ),
146 htmlspecialchars( wfMsg( 'searchbutton' ) )
149 wfProfileOut( $fname );
153 $search = SearchEngine
::create();
154 $search->setLimitOffset( $this->limit
, $this->offset
);
155 $search->setNamespaces( $this->namespaces
);
156 $search->showRedirects
= $this->searchRedirects
;
157 $titleMatches = $search->searchTitle( $term );
158 $textMatches = $search->searchText( $term );
160 $num = ( $titleMatches ?
$titleMatches->numRows() : 0 )
161 +
( $textMatches ?
$textMatches->numRows() : 0);
162 if ( $num >= $this->limit
) {
163 $top = wfShowingResults( $this->offset
, $this->limit
);
165 $top = wfShowingResultsNum( $this->offset
, $this->limit
, $num );
167 $wgOut->addHTML( "<p>{$top}</p>\n" );
169 if( $num ||
$this->offset
) {
170 $prevnext = wfViewPrevNext( $this->offset
, $this->limit
,
171 SpecialPage
::getTitleFor( 'Search' ),
173 $this->powerSearchOptions(),
174 array( 'search' => $term ) ) );
175 $wgOut->addHTML( "<br />{$prevnext}\n" );
178 if( $titleMatches ) {
179 if( $titleMatches->numRows() ) {
180 $wgOut->addWikiText( '==' . wfMsg( 'titlematches' ) . "==\n" );
181 $wgOut->addHTML( $this->showMatches( $titleMatches ) );
183 $wgOut->addWikiText( '==' . wfMsg( 'notitlematches' ) . "==\n" );
188 if( $textMatches->numRows() ) {
189 $wgOut->addWikiText( '==' . wfMsg( 'textmatches' ) . "==\n" );
190 $wgOut->addHTML( $this->showMatches( $textMatches ) );
191 } elseif( $num == 0 ) {
192 # Don't show the 'no text matches' if we received title matches
193 $wgOut->addWikiText( '==' . wfMsg( 'notextmatches' ) . "==\n" );
198 $wgOut->addWikiText( wfMsg( 'nonefound' ) );
200 if( $num ||
$this->offset
) {
201 $wgOut->addHTML( "<p>{$prevnext}</p>\n" );
203 $wgOut->addHTML( $this->powerSearchBox( $term ) );
204 wfProfileOut( $fname );
207 #------------------------------------------------------------------
208 # Private methods below this line
213 function setupPage( $term ) {
215 $wgOut->setPageTitle( wfMsg( 'searchresults' ) );
216 $subtitlemsg = ( Title
::newFromText($term) ?
'searchsubtitle' : 'searchsubtitleinvalid' );
217 $wgOut->setSubtitle( $wgOut->parse( wfMsg( $subtitlemsg, wfEscapeWikiText($term) ) ) );
218 $wgOut->setArticleRelated( false );
219 $wgOut->setRobotpolicy( 'noindex,nofollow' );
223 * Extract default namespaces to search from the given user's
224 * settings, returning a list of index numbers.
230 function userNamespaces( &$user ) {
232 foreach( SearchEngine
::searchableNamespaces() as $ns => $name ) {
233 if( $user->getOption( 'searchNs' . $ns ) ) {
241 * Extract "power search" namespace settings from the request object,
242 * returning a list of index numbers to search.
244 * @param WebRequest $request
248 function powerSearch( &$request ) {
250 foreach( SearchEngine
::searchableNamespaces() as $ns => $name ) {
251 if( $request->getCheck( 'ns' . $ns ) ) {
259 * Reconstruct the 'power search' options for links
263 function powerSearchOptions() {
265 foreach( $this->namespaces
as $n ) {
268 $opt['redirs'] = $this->searchRedirects ?
1 : 0;
274 * @param SearchResultSet $matches
275 * @param string $terms partial regexp for highlighting terms
277 function showMatches( &$matches ) {
278 $fname = 'SpecialSearch::showMatches';
279 wfProfileIn( $fname );
282 $tm = $wgContLang->convertForSearchResult( $matches->termMatches() );
283 $terms = implode( '|', $tm );
285 $off = $this->offset +
1;
286 $out = "<ol start='{$off}'>\n";
288 while( $result = $matches->next() ) {
289 $out .= $this->showHit( $result, $terms );
293 // convert the whole thing to desired language variant
295 $out = $wgContLang->convert( $out );
296 wfProfileOut( $fname );
301 * Format a single hit result
302 * @param SearchResult $result
303 * @param string $terms partial regexp for highlighting terms
305 function showHit( $result, $terms ) {
306 $fname = 'SpecialSearch::showHit';
307 wfProfileIn( $fname );
308 global $wgUser, $wgContLang, $wgLang;
310 $t = $result->getTitle();
311 if( is_null( $t ) ) {
312 wfProfileOut( $fname );
313 return "<!-- Broken link in search result -->\n";
315 $sk =& $wgUser->getSkin();
317 $contextlines = $wgUser->getOption( 'contextlines', 5 );
318 $contextchars = $wgUser->getOption( 'contextchars', 50 );
320 $link = $sk->makeKnownLinkObj( $t );
321 $revision = Revision
::newFromTitle( $t );
322 $text = $revision->getText();
323 $size = wfMsgExt( 'nbytes', array( 'parsemag', 'escape'),
324 $wgLang->formatNum( strlen( $text ) ) );
326 $lines = explode( "\n", $text );
328 $max = intval( $contextchars ) +
1;
329 $pat1 = "/(.*)($terms)(.{0,$max})/i";
334 wfProfileIn( "$fname-extract" );
335 foreach ( $lines as $line ) {
336 if ( 0 == $contextlines ) {
341 if ( ! preg_match( $pat1, $line, $m ) ) {
345 $pre = $wgContLang->truncate( $m[1], -$contextchars, '...' );
347 if ( count( $m ) < 3 ) {
350 $post = $wgContLang->truncate( $m[3], $contextchars, '...' );
355 $line = htmlspecialchars( $pre . $found . $post );
356 $pat2 = '/(' . $terms . ")/i";
357 $line = preg_replace( $pat2,
358 "<span class='searchmatch'>\\1</span>", $line );
360 $extract .= "<br /><small>{$lineno}: {$line}</small>\n";
362 wfProfileOut( "$fname-extract" );
363 wfProfileOut( $fname );
364 return "<li>{$link} ({$size}){$extract}</li>\n";
367 function powerSearchBox( $term ) {
369 foreach( SearchEngine
::searchableNamespaces() as $ns => $name ) {
370 $checked = in_array( $ns, $this->namespaces
)
371 ?
' checked="checked"'
373 $name = str_replace( '_', ' ', $name );
375 $name = wfMsg( 'blanknamespace' );
377 $namespaces .= " <label><input type='checkbox' value=\"1\" name=\"" .
378 "ns{$ns}\"{$checked} />{$name}</label>\n";
381 $checked = $this->searchRedirects
382 ?
' checked="checked"'
384 $redirect = "<input type='checkbox' value='1' name=\"redirs\"{$checked} />\n";
386 $searchField = '<input type="text" name="search" value="' .
387 htmlspecialchars( $term ) ."\" size=\"16\" />\n";
389 $searchButton = '<input type="submit" name="searchx" value="' .
390 htmlspecialchars( wfMsg('powersearch') ) . "\" />\n";
392 $ret = wfMsg( 'powersearchtext',
393 $namespaces, $redirect, $searchField,
394 '', '', '', '', '', # Dummy placeholders
397 $title = SpecialPage
::getTitleFor( 'Search' );
398 $action = $title->escapeLocalURL();
399 return "<br /><br />\n<form id=\"powersearch\" method=\"get\" " .
400 "action=\"$action\">\n{$ret}\n</form>\n";