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' );
33 * @param string $par (default '')
35 function wfSpecialSearch( $par = '' ) {
36 global $wgRequest, $wgUser;
38 $search = $wgRequest->getText( 'search', $par );
39 $searchPage = new SpecialSearch( $wgRequest, $wgUser );
40 if( $wgRequest->getVal( 'fulltext' ) ||
41 !is_null( $wgRequest->getVal( 'offset' ) ) ||
42 !is_null ($wgRequest->getVal( 'searchx' ) ) ) {
43 $searchPage->showResults( $search );
45 $searchPage->goResult( $search );
52 * @subpackage SpecialPage
57 * Set up basic search parameters from the request and user settings.
58 * Typically you'll pass $wgRequest and $wgUser.
60 * @param WebRequest $request
64 function SpecialSearch( &$request, &$user ) {
65 list( $this->limit
, $this->offset
) = $request->getLimitOffset( 20, 'searchlimit' );
67 if( $request->getCheck( 'searchx' ) ) {
68 $this->namespaces
= $this->powerSearch( $request );
70 $this->namespaces
= $this->userNamespaces( $user );
73 $this->searchRedirects
= false;
77 * If an exact title match can be found, jump straight ahead to
81 function goResult( $term ) {
85 $this->setupPage( $term );
87 # Try to go to page as entered.
89 $t = Title
::newFromText( $term );
91 # If the string cannot be used to create a title
93 return $this->showResults( $term );
96 # If there's an exact or very near match, jump right there.
97 $t = SearchEngine
::getNearMatch( $term );
98 if( !is_null( $t ) ) {
99 $wgOut->redirect( $t->getFullURL() );
103 # No match, generate an edit URL
104 $t = Title
::newFromText( $term );
105 if( is_null( $t ) ) {
106 $editurl = ''; # hrm...
108 # If the feature is enabled, go straight to the edit page
110 $wgOut->redirect( $t->getFullURL( 'action=edit' ) );
113 $editurl = $t->escapeLocalURL( 'action=edit' );
116 $wgOut->addWikiText( wfMsg('nogomatch', $term) );
118 return $this->showResults( $term );
122 * @param string $term
125 function showResults( $term ) {
126 $fname = 'SpecialSearch::showResults';
127 wfProfileIn( $fname );
129 $this->setupPage( $term );
131 global $wgUser, $wgOut;
132 $sk = $wgUser->getSkin();
133 $wgOut->addWikiText( wfMsg( 'searchresulttext' ) );
135 #if ( !$this->parseQuery() ) {
136 if( '' === trim( $term ) ) {
138 '==' . wfMsg( 'badquery' ) . "==\n" .
139 wfMsg( 'badquerytext' ) );
140 wfProfileOut( $fname );
144 global $wgDisableTextSearch;
145 if ( $wgDisableTextSearch ) {
146 global $wgForwardSearchUrl;
147 if( $wgForwardSearchUrl ) {
148 $url = str_replace( '$1', urlencode( $term ), $wgForwardSearchUrl );
149 $wgOut->redirect( $url );
152 global $wgInputEncoding;
153 $wgOut->addHTML( wfMsg( 'searchdisabled' ) );
154 $wgOut->addHTML( wfMsg( 'googlesearch',
155 htmlspecialchars( $term ),
156 htmlspecialchars( $wgInputEncoding ) ) );
157 wfProfileOut( $fname );
161 $search =& SearchEngine
::create();
162 $search->setLimitOffset( $this->limit
, $this->offset
);
163 $search->setNamespaces( $this->namespaces
);
164 $titleMatches = $search->searchTitle( $term );
165 $textMatches = $search->searchText( $term );
167 $num = $titleMatches->numRows() +
$textMatches->numRows();
168 if ( $num >= $this->limit
) {
169 $top = wfShowingResults( $this->offset
, $this->limit
);
171 $top = wfShowingResultsNum( $this->offset
, $this->limit
, $num );
173 $wgOut->addHTML( "<p>{$top}</p>\n" );
175 if( $num ||
$this->offset
) {
176 $prevnext = wfViewPrevNext( $this->offset
, $this->limit
,
179 $this->powerSearchOptions(),
180 array( 'search' => $term ) ) );
181 $wgOut->addHTML( "<br />{$prevnext}\n" );
185 $tm = $wgContLang->convertForSearchResult( $search->termMatches() );
186 $terms = implode( '|', $tm );
188 if( $titleMatches->numRows() ) {
189 $wgOut->addWikiText( '==' . wfMsg( 'titlematches' ) . "==\n" );
190 $wgOut->addHTML( $this->showMatches( $titleMatches, $terms ) );
192 $wgOut->addWikiText( '==' . wfMsg( 'notitlematches' ) . "==\n" );
195 if( $textMatches->numRows() ) {
196 $wgOut->addWikiText( '==' . wfMsg( 'textmatches' ) . "==\n" );
197 $wgOut->addHTML( $this->showMatches( $textMatches, $terms ) );
198 } elseif( $num == 0 ) {
199 # Don't show the 'no text matches' if we received title matches
200 $wgOut->addWikiText( '==' . wfMsg( 'notextmatches' ) . "==\n" );
204 $wgOut->addWikiText( wfMsg( 'nonefound' ) );
206 if( $num ||
$this->offset
) {
207 $wgOut->addHTML( "<p>{$prevnext}</p>\n" );
209 $wgOut->addHTML( $this->powerSearchBox( $term ) );
210 wfProfileOut( $fname );
213 #------------------------------------------------------------------
214 # Private methods below this line
219 function setupPage( $term ) {
221 $wgOut->setPageTitle( wfMsg( 'searchresults' ) );
222 $wgOut->setSubtitle( wfMsg( 'searchquery', htmlspecialchars( $term ) ) );
223 $wgOut->setArticleRelated( false );
224 $wgOut->setRobotpolicy( 'noindex,nofollow' );
228 * Extract default namespaces to search from the given user's
229 * settings, returning a list of index numbers.
235 function userNamespaces( &$user ) {
237 foreach( SearchEngine
::searchableNamespaces() as $ns => $name ) {
238 if( $user->getOption( 'searchNs' . $ns ) ) {
246 * Extract "power search" namespace settings from the request object,
247 * returning a list of index numbers to search.
249 * @param WebRequest $request
253 function powerSearch( &$request ) {
255 foreach( SearchEngine
::searchableNamespaces() as $ns => $name ) {
256 if( $request->getCheck( 'ns' . $ns ) ) {
264 * Reconstruct the 'power search' options for links
268 function powerSearchOptions() {
270 foreach( $this->namespaces
as $n ) {
273 $opt['redirs'] = $this->searchRedirects ?
1 : 0;
279 * @param ResultWrapper $matches
280 * @param string $terms partial regexp for highlighting terms
282 function showMatches( &$matches, $terms ) {
283 $fname = 'SpecialSearch::showMatches';
284 wfProfileIn( $fname );
287 $off = $this->offset +
1;
288 $out = "<ol start='{$off}'>\n";
290 while( $row = $matches->fetchObject() ) {
291 $out .= $this->showHit( $row, $terms );
295 // convert the whole thing to desired language variant
297 $out = $wgContLang->convert( $out );
298 wfProfileOut( $fname );
303 * Format a single hit result
305 * @param string $terms partial regexp for highlighting terms
307 function showHit( $row, $terms ) {
308 $fname = 'SpecialSearch::showHit';
309 wfProfileIn( $fname );
310 global $wgUser, $wgContLang;
312 $t = Title
::makeTitle( $row->page_namespace
, $row->page_title
);
313 if( is_null( $t ) ) {
314 wfProfileOut( $fname );
315 return "<!-- Broken link in search result -->\n";
317 $sk =& $wgUser->getSkin();
319 $contextlines = $wgUser->getOption( 'contextlines' );
320 if ( '' == $contextlines ) { $contextlines = 5; }
321 $contextchars = $wgUser->getOption( 'contextchars' );
322 if ( '' == $contextchars ) { $contextchars = 50; }
324 $link = $sk->makeKnownLinkObj( $t, '' );
325 $text = Revision
::getRevisionText( $row );
326 $size = wfMsg( 'nbytes', strlen( $text ) );
328 $lines = explode( "\n", $text );
330 $max = IntVal( $contextchars ) +
1;
331 $pat1 = "/(.*)($terms)(.{0,$max})/i";
336 wfProfileIn( "$fname-extract" );
337 foreach ( $lines as $line ) {
338 if ( 0 == $contextlines ) {
342 if ( ! preg_match( $pat1, $line, $m ) ) {
346 $pre = $wgContLang->truncate( $m[1], -$contextchars, '...' );
348 if ( count( $m ) < 3 ) {
351 $post = $wgContLang->truncate( $m[3], $contextchars, '...' );
356 $line = htmlspecialchars( $pre . $found . $post );
357 $pat2 = '/(' . $terms . ")/i";
358 $line = preg_replace( $pat2,
359 "<span class='searchmatch'>\\1</span>", $line );
361 $extract .= "<br /><small>{$lineno}: {$line}</small>\n";
363 wfProfileOut( "$fname-extract" );
364 wfProfileOut( $fname );
365 return "<li>{$link} ({$size}){$extract}</li>\n";
368 function powerSearchBox( $term ) {
370 foreach( SearchEngine
::searchableNamespaces() as $ns => $name ) {
371 $checked = in_array( $ns, $this->namespaces
)
372 ?
' checked="checked"'
374 $name = str_replace( '_', ' ', $name );
376 $name = wfMsg( 'blanknamespace' );
378 $namespaces .= " <label><input type='checkbox' value=\"1\" name=\"" .
379 "ns{$ns}\"{$checked} />{$name}</label>\n";
382 $checked = $this->searchRedirects
383 ?
' checked="checked"'
385 $redirect = "<input type='checkbox' value='1' name=\"redirs\"{$checked} />\n";
387 $searchField = "<input type='text' name=\"search\" value=\"" .
388 htmlspecialchars( $term ) ."\" width=\"80\" />\n";
390 $searchButton = '<input type="submit" name="searchx" value="' .
391 htmlspecialchars( wfMsg('powersearch') ) . "\" />\n";
393 $ret = wfMsg( 'powersearchtext',
394 $namespaces, $redirect, $searchField,
395 '', '', '', '', '', # Dummy placeholders
398 $title = Title
::makeTitle( NS_SPECIAL
, 'Search' );
399 $action = $title->escapeLocalURL();
400 return "<br /><br />\n<form id=\"powersearch\" method=\"get\" " .
401 "action=\"$action\">\n{$ret}\n</form>\n";