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 # 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 # http://www.gnu.org/copyleft/gpl.html
21 * Run text & title search and display the output
23 * @subpackage SpecialPage
27 require_once( 'SearchEngine.php' );
28 require_once( 'Revision.php' );
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 );
47 * @subpackage SpecialPage
52 * Set up basic search parameters from the request and user settings.
53 * Typically you'll pass $wgRequest and $wgUser.
55 * @param WebRequest $request
59 function SpecialSearch( &$request, &$user ) {
60 list( $this->limit
, $this->offset
) = $request->getLimitOffset( 20, 'searchlimit' );
62 if( $request->getCheck( 'searchx' ) ) {
63 $this->namespaces
= $this->powerSearch( $request );
65 $this->namespaces
= $this->userNamespaces( $user );
68 $this->searchRedirects
= false;
72 * If an exact title match can be found, jump straight ahead to
76 function goResult( $term ) {
80 $this->setupPage( $term );
82 # Try to go to page as entered.
84 $t = Title
::newFromText( $term );
86 # If the string cannot be used to create a title
88 return $this->showResults( $term );
91 # If there's an exact or very near match, jump right there.
92 $t = SearchEngine
::getNearMatch( $term );
93 if( !is_null( $t ) ) {
94 $wgOut->redirect( $t->getFullURL() );
98 # No match, generate an edit URL
99 $t = Title
::newFromText( $term );
100 if( is_null( $t ) ) {
101 $editurl = ''; # hrm...
103 # If the feature is enabled, go straight to the edit page
105 $wgOut->redirect( $t->getFullURL( 'action=edit' ) );
108 $editurl = $t->escapeLocalURL( 'action=edit' );
111 # FIXME: HTML in wiki message
112 $wgOut->addHTML( '<p>' . wfMsg('nogomatch', $editurl, htmlspecialchars( $term ) ) . "</p>\n" );
114 return $this->showResults( $term );
118 * @param string $term
121 function showResults( $term ) {
122 $fname = 'SpecialSearch::showResults';
123 wfProfileIn( $fname );
125 $this->setupPage( $term );
127 global $wgUser, $wgOut;
128 $sk = $wgUser->getSkin();
129 $wgOut->addWikiText( wfMsg( 'searchresulttext' ) );
131 #if ( !$this->parseQuery() ) {
132 if( '' === trim( $term ) ) {
134 '==' . wfMsg( 'badquery' ) . "==\n" .
135 wfMsg( 'badquerytext' ) );
136 wfProfileOut( $fname );
140 global $wgDisableTextSearch;
141 if ( $wgDisableTextSearch ) {
142 global $wgForwardSearchUrl;
143 if( $wgForwardSearchUrl ) {
144 $url = str_replace( '$1', urlencode( $term ), $wgForwardSearchUrl );
145 $wgOut->redirect( $url );
148 global $wgInputEncoding;
149 $wgOut->addHTML( wfMsg( 'searchdisabled' ) );
150 $wgOut->addHTML( wfMsg( 'googlesearch',
151 htmlspecialchars( $term ),
152 htmlspecialchars( $wgInputEncoding ) ) );
153 wfProfileOut( $fname );
157 $search =& SearchEngine
::create();
158 $search->setLimitOffset( $this->limit
, $this->offset
);
159 $search->setNamespaces( $this->namespaces
);
160 $titleMatches = $search->searchTitle( $term );
161 $textMatches = $search->searchText( $term );
163 $num = $titleMatches->numRows() +
$textMatches->numRows();
164 if ( $num >= $this->limit
) {
165 $top = wfShowingResults( $this->offset
, $this->limit
);
167 $top = wfShowingResultsNum( $this->offset
, $this->limit
, $num );
169 $wgOut->addHTML( "<p>{$top}</p>\n" );
171 if( $num ||
$this->offset
) {
172 $prevnext = wfViewPrevNext( $this->offset
, $this->limit
,
175 $this->powerSearchOptions(),
176 array( 'search' => $term ) ) );
177 $wgOut->addHTML( "<br />{$prevnext}\n" );
181 $tm = $wgContLang->convertForSearchResult( $search->termMatches() );
182 $terms = implode( '|', $tm );
184 if( $titleMatches->numRows() ) {
185 $wgOut->addWikiText( '==' . wfMsg( 'titlematches' ) . "==\n" );
186 $wgOut->addHTML( $this->showMatches( $titleMatches, $terms ) );
188 $wgOut->addWikiText( '==' . wfMsg( 'notitlematches' ) . "==\n" );
191 if( $textMatches->numRows() ) {
192 $wgOut->addWikiText( '==' . wfMsg( 'textmatches' ) . "==\n" );
193 $wgOut->addHTML( $this->showMatches( $textMatches, $terms ) );
194 } elseif( $num == 0 ) {
195 # Don't show the 'no text matches' if we received title matches
196 $wgOut->addWikiText( '==' . wfMsg( 'notextmatches' ) . "==\n" );
200 $wgOut->addWikiText( wfMsg( 'nonefound' ) );
202 if( $num ||
$this->offset
) {
203 $wgOut->addHTML( "<p>{$prevnext}</p>\n" );
205 $wgOut->addHTML( $this->powerSearchBox( $term ) );
206 wfProfileOut( $fname );
209 #------------------------------------------------------------------
210 # Private methods below this line
215 function setupPage( $term ) {
217 $wgOut->setPageTitle( wfMsg( 'searchresults' ) );
218 $wgOut->setSubtitle( wfMsg( 'searchquery', htmlspecialchars( $term ) ) );
219 $wgOut->setArticleRelated( false );
220 $wgOut->setRobotpolicy( 'noindex,nofollow' );
224 * Extract default namespaces to search from the given user's
225 * settings, returning a list of index numbers.
231 function userNamespaces( &$user ) {
233 foreach( SearchEngine
::searchableNamespaces() as $ns => $name ) {
234 if( $user->getOption( 'searchNs' . $ns ) ) {
242 * Extract "power search" namespace settings from the request object,
243 * returning a list of index numbers to search.
245 * @param WebRequest $request
249 function powerSearch( &$request ) {
251 foreach( SearchEngine
::searchableNamespaces() as $ns => $name ) {
252 if( $request->getCheck( 'ns' . $ns ) ) {
260 * Reconstruct the 'power search' options for links
264 function powerSearchOptions() {
266 foreach( $this->namespaces
as $n ) {
269 $opt['redirs'] = $this->searchRedirects ?
1 : 0;
275 * @param ResultWrapper $matches
276 * @param string $terms partial regexp for highlighting terms
278 function showMatches( &$matches, $terms ) {
279 $fname = 'SpecialSearch::showMatches';
280 wfProfileIn( $fname );
283 $off = $this->offset +
1;
284 $out = "<ol start='{$off}'>\n";
286 while( $row = $matches->fetchObject() ) {
287 $out .= $this->showHit( $row, $terms );
291 // convert the whole thing to desired language variant
293 $out = $wgContLang->convert( $out );
294 wfProfileOut( $fname );
299 * Format a single hit result
301 * @param string $terms partial regexp for highlighting terms
303 function showHit( $row, $terms ) {
304 $fname = 'SpecialSearch::showHit';
305 wfProfileIn( $fname );
306 global $wgUser, $wgContLang;
308 $t = Title
::makeTitle( $row->page_namespace
, $row->page_title
);
309 if( is_null( $t ) ) {
310 wfProfileOut( $fname );
311 return "<!-- Broken link in search result -->\n";
313 $sk =& $wgUser->getSkin();
315 $contextlines = $wgUser->getOption( 'contextlines' );
316 if ( '' == $contextlines ) { $contextlines = 5; }
317 $contextchars = $wgUser->getOption( 'contextchars' );
318 if ( '' == $contextchars ) { $contextchars = 50; }
320 $link = $sk->makeKnownLinkObj( $t, '' );
321 $text = Revision
::getRevisionText( $row );
322 $size = wfMsg( 'nbytes', strlen( $text ) );
324 $lines = explode( "\n", $text );
326 $max = IntVal( $contextchars ) +
1;
327 $pat1 = "/(.*)($terms)(.{0,$max})/i";
332 wfProfileIn( "$fname-extract" );
333 foreach ( $lines as $line ) {
334 if ( 0 == $contextlines ) {
338 if ( ! preg_match( $pat1, $line, $m ) ) {
342 $pre = $wgContLang->truncate( $m[1], -$contextchars, '...' );
344 if ( count( $m ) < 3 ) {
347 $post = $wgContLang->truncate( $m[3], $contextchars, '...' );
352 $line = htmlspecialchars( $pre . $found . $post );
353 $pat2 = '/(' . $terms . ")/i";
354 $line = preg_replace( $pat2,
355 "<span class='searchmatch'>\\1</span>", $line );
357 $extract .= "<br /><small>{$lineno}: {$line}</small>\n";
359 wfProfileOut( "$fname-extract" );
360 wfProfileOut( $fname );
361 return "<li>{$link} ({$size}){$extract}</li>\n";
364 function powerSearchBox( $term ) {
366 foreach( SearchEngine
::searchableNamespaces() as $ns => $name ) {
367 $checked = in_array( $ns, $this->namespaces
)
368 ?
' checked="checked"'
370 $name = str_replace( '_', ' ', $name );
372 $name = wfMsg( 'blanknamespace' );
374 $namespaces .= " <label><input type='checkbox' value=\"1\" name=\"" .
375 "ns{$ns}\"{$checked} />{$name}</label>\n";
378 $checked = $this->searchRedirects
379 ?
' checked="checked"'
381 $redirect = "<input type='checkbox' value='1' name=\"redirs\"{$checked} />\n";
383 $searchField = "<input type='text' name=\"search\" value=\"" .
384 htmlspecialchars( $term ) ."\" width=\"80\" />\n";
386 $searchButton = '<input type="submit" name="searchx" value="' .
387 htmlspecialchars( wfMsg('powersearch') ) . "\" />\n";
389 $ret = wfMsg( 'powersearchtext',
390 $namespaces, $redirect, $searchField,
391 '', '', '', '', '', # Dummy placeholders
394 $title = Title
::makeTitle( NS_SPECIAL
, 'Search' );
395 $action = $title->escapeLocalURL();
396 return "<br /><br />\n<form id=\"powersearch\" method=\"get\" " .
397 "action=\"$action\">\n{$ret}\n</form>\n";