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
26 require_once( 'SearchEngine.php' );
28 function wfSpecialSearch( $par='' ) {
29 global $wgRequest, $wgUser;
31 $search = $wgRequest->getText( 'search', $par );
32 $searchPage = new SpecialSearch( $wgRequest, $wgUser );
33 if( $wgRequest->getVal( 'fulltext' ) ||
34 !is_null( $wgRequest->getVal( 'offset' ) ) ||
35 !is_null ($wgRequest->getVal( 'searchx' ) ) ) {
36 $searchPage->showResults( $search );
38 $searchPage->goResult( $search );
45 * Set up basic search parameters from the request and user settings.
46 * Typically you'll pass $wgRequest and $wgUser.
48 * @param WebRequest $request
52 function SpecialSearch( &$request, &$user ) {
53 list( $this->limit
, $this->offset
) = $request->getLimitOffset( 20, 'searchlimit' );
55 if( $request->getCheck( 'searchx' ) ) {
56 $this->namespaces
= $this->powerSearch( $request );
58 $this->namespaces
= $this->userNamespaces( $user );
61 $this->searchRedirects
= false;
65 * If an exact title match can be found, jump straight ahead to
69 function goResult( $term ) {
73 $this->setupPage( $term );
75 # Try to go to page as entered.
77 $t = Title
::newFromText( $term );
79 # If the string cannot be used to create a title
81 return $this->showResults( $term );
84 # If there's an exact or very near match, jump right there.
85 $t = SearchEngine
::getNearMatch( $term );
86 if( !is_null( $t ) ) {
87 $wgOut->redirect( $t->getFullURL() );
91 # No match, generate an edit URL
92 $t = Title
::newFromText( $term );
94 $editurl = ''; # hrm...
96 # If the feature is enabled, go straight to the edit page
98 $wgOut->redirect( $t->getFullURL( 'action=edit' ) );
101 $editurl = $t->escapeLocalURL( 'action=edit' );
104 # FIXME: HTML in wiki message
105 $wgOut->addHTML( '<p>' . wfMsg('nogomatch', $editurl, htmlspecialchars( $term ) ) . "</p>\n" );
107 return $this->showResults( $term );
111 * @param string $term
114 function showResults( $term ) {
115 $this->setupPage( $term );
117 global $wgUser, $wgOut;
118 $sk = $wgUser->getSkin();
119 $wgOut->addWikiText( wfMsg( 'searchresulttext' ) );
121 #if ( !$this->parseQuery() ) {
122 if( '' === trim( $term ) ) {
124 '==' . wfMsg( 'badquery' ) . "==\n" .
125 wfMsg( 'badquerytext' ) );
129 global $wgDisableTextSearch;
130 if ( $wgDisableTextSearch ) {
131 global $wgInputEncoding;
132 $wgOut->addHTML( wfMsg( 'searchdisabled' ) );
133 $wgOut->addHTML( wfMsg( 'googlesearch',
134 htmlspecialchars( $term ),
135 htmlspecialchars( $wgInputEncoding ) ) );
139 $search =& $this->getSearchEngine();
140 $titleMatches = $search->searchTitle( $term );
141 $textMatches = $search->searchText( $term );
143 $num = $titleMatches->numRows() +
$textMatches->numRows();
144 if ( $num >= $this->limit
) {
145 $top = wfShowingResults( $this->offset
, $this->limit
);
147 $top = wfShowingResultsNum( $this->offset
, $this->limit
, $num );
149 $wgOut->addHTML( "<p>{$top}</p>\n" );
151 if( $num ||
$this->offset
) {
152 $prevnext = wfViewPrevNext( $this->offset
, $this->limit
,
155 $this->powerSearchOptions(),
156 array( 'search' => $term ) ) );
157 $wgOut->addHTML( "<br />{$prevnext}\n" );
160 $terms = implode( '|', $search->termMatches() );
162 if( $titleMatches->numRows() ) {
163 $wgOut->addWikiText( '==' . wfMsg( 'titlematches' ) . "==\n" );
164 $wgOut->addHTML( $this->showMatches( $titleMatches, $terms ) );
166 $wgOut->addWikiText( '==' . wfMsg( 'notitlematches' ) . "==\n" );
169 if( $textMatches->numRows() ) {
170 $wgOut->addWikiText( '==' . wfMsg( 'textmatches' ) . "==\n" );
171 $wgOut->addHTML( $this->showMatches( $textMatches, $terms ) );
172 } elseif( $num == 0 ) {
173 # Don't show the 'no text matches' if we received title matches
174 $wgOut->addWikiText( '==' . wfMsg( 'notextmatches' ) . "==\n" );
178 $wgOut->addWikiText( wfMsg( 'nonefound' ) );
180 if( $num ||
$this->offset
) {
181 $wgOut->addHTML( "<p>{$prevnext}</p>\n" );
183 $wgOut->addHTML( $this->powerSearchBox( $term ) );
186 #------------------------------------------------------------------
187 # Private methods below this line
192 function setupPage( $term ) {
194 $wgOut->setPageTitle( wfMsg( 'searchresults' ) );
195 $wgOut->setSubtitle( wfMsg( 'searchquery', htmlspecialchars( $term ) ) );
196 $wgOut->setArticleRelated( false );
197 $wgOut->setRobotpolicy( 'noindex,nofollow' );
201 * Load up the appropriate search engine class for the currently
202 * active database backend, and return a configured instance.
204 * @return SearchEngine
207 function &getSearchEngine() {
208 global $wgDBtype, $wgDBmysql4, $wgSearchType;
209 if( $wgDBtype == 'mysql' ) {
211 $class = 'SearchMySQL4';
212 require_once( 'SearchMySQL4.php' );
214 $class = 'SearchMysql3';
215 require_once( 'SearchMySQL3.php' );
218 $class = 'SearchEngineDummy';
220 $search = new $class( wfGetDB( DB_SLAVE
) );
221 $search->setLimitOffset( $this->limit
, $this->offset
);
222 $search->setNamespaces( $this->namespaces
);
227 * Extract default namespaces to search from the given user's
228 * settings, returning a list of index numbers.
234 function userNamespaces( &$user ) {
236 foreach( SearchEngine
::searchableNamespaces() as $ns => $name ) {
237 if( $user->getOption( 'searchNs' . $ns ) ) {
245 * Extract "power search" namespace settings from the request object,
246 * returning a list of index numbers to search.
248 * @param WebRequest $request
252 function powerSearch( &$request ) {
254 foreach( SearchEngine
::searchableNamespaces() as $ns => $name ) {
255 if( $request->getCheck( 'ns' . $ns ) ) {
263 * Reconstruct the 'power search' options for links
267 function powerSearchOptions() {
269 foreach( $this->namespaces
as $n ) {
272 $opt['redirs'] = $this->searchRedirects ?
1 : 0;
278 * @param ResultWrapper $matches
279 * @param string $terms partial regexp for highlighting terms
281 function showMatches( &$matches, $terms ) {
283 $off = $this->offset +
1;
284 $out = "<ol start='{$off}'>\n";
286 while( $row = $matches->fetchObject() ) {
287 $out .= $this->showHit( $row, $terms );
294 * Format a single hit result
296 * @param string $terms partial regexp for highlighting terms
298 function showHit( $row, $terms ) {
299 global $wgUser, $wgContLang;
301 $t = Title
::makeName( $row->cur_namespace
, $row->cur_title
);
302 if( is_null( $t ) ) {
303 return "<!-- Broken link in search result -->\n";
305 $sk = $wgUser->getSkin();
307 $contextlines = $wgUser->getOption( 'contextlines' );
308 if ( '' == $contextlines ) { $contextlines = 5; }
309 $contextchars = $wgUser->getOption( 'contextchars' );
310 if ( '' == $contextchars ) { $contextchars = 50; }
312 $link = $sk->makeKnownLink( $t, '' );
313 $size = wfMsg( 'nbytes', strlen( $row->cur_text
) );
315 $lines = explode( "\n", $row->cur_text
);
316 $pat1 = "/(.*)($terms)(.*)/i";
320 foreach ( $lines as $line ) {
321 if ( 0 == $contextlines ) {
326 if ( ! preg_match( $pat1, $line, $m ) ) {
330 $pre = $wgContLang->truncate( $m[1], -$contextchars, '...' );
332 if ( count( $m ) < 3 ) {
335 $post = $wgContLang->truncate( $m[3], $contextchars, '...' );
340 $line = htmlspecialchars( $pre . $found . $post );
341 $pat2 = '/(' . $terms . ")/i";
342 $line = preg_replace( $pat2,
343 "<span class='searchmatch'>\\1</span>", $line );
345 $extract .= "<br /><small>{$lineno}: {$line}</small>\n";
347 return "<li>{$link} ({$size}){$extract}</li>\n";
350 function powerSearchBox( $term ) {
352 foreach( SearchEngine
::searchableNamespaces() as $ns => $name ) {
353 $checked = in_array( $ns, $this->namespaces
)
354 ?
' checked="checked"'
356 $name = str_replace( '_', ' ', $name );
358 $name = wfMsg( 'blanknamespace' );
360 $namespaces .= " <label><input type='checkbox' value=\"1\" name=\"" .
361 "ns{$ns}\"{$checked} />{$name}</label>\n";
364 $checked = $this->searchRedirects
365 ?
' checked="checked"'
367 $redirect = "<input type='checkbox' value='1' name=\"redirs\"{$checked} />\n";
369 $searchField = "<input type='text' name=\"search\" value=\"" .
370 htmlspecialchars( $term ) ."\" width=\"80\" />\n";
372 $searchButton = '<input type="submit" name="searchx" value="' .
373 htmlspecialchars( wfMsg('powersearch') ) . "\" />\n";
375 $ret = wfMsg( 'powersearchtext',
376 $namespaces, $redirect, $searchField,
377 '', '', '', '', '', # Dummy placeholders
380 $title = Title
::makeTitle( NS_SPECIAL
, 'Search' );
381 $action = $title->escapeLocalURL();
382 return "<br /><br />\n<form id=\"powersearch\" method=\"get\" " .
383 "action=\"$action\">\n{$ret}\n</form>\n";