* Fix missing search box contents in MonoBook skin
[lhc/web/wiklou.git] / includes / SpecialSearch.php
1 <?php
2 # Copyright (C) 2004 Brion Vibber <brion@pobox.com>
3 # http://www.mediawiki.org/
4 #
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.
9 #
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.
14 #
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
19
20 /**
21 * Run text & title search and display the output
22 * @package MediaWiki
23 * @subpackage SpecialPage
24 */
25
26 require_once( 'SearchEngine.php' );
27 require_once( 'Revision.php' );
28
29 function wfSpecialSearch( $par='' ) {
30 global $wgRequest, $wgUser;
31
32 $search = $wgRequest->getText( 'search', $par );
33 $searchPage = new SpecialSearch( $wgRequest, $wgUser );
34 if( $wgRequest->getVal( 'fulltext' ) ||
35 !is_null( $wgRequest->getVal( 'offset' ) ) ||
36 !is_null ($wgRequest->getVal( 'searchx' ) ) ) {
37 $searchPage->showResults( $search );
38 } else {
39 $searchPage->goResult( $search );
40 }
41 }
42
43
44 class SpecialSearch {
45 /**
46 * Set up basic search parameters from the request and user settings.
47 * Typically you'll pass $wgRequest and $wgUser.
48 *
49 * @param WebRequest $request
50 * @param User $user
51 * @access public
52 */
53 function SpecialSearch( &$request, &$user ) {
54 list( $this->limit, $this->offset ) = $request->getLimitOffset( 20, 'searchlimit' );
55
56 if( $request->getCheck( 'searchx' ) ) {
57 $this->namespaces = $this->powerSearch( $request );
58 } else {
59 $this->namespaces = $this->userNamespaces( $user );
60 }
61
62 $this->searchRedirects = false;
63 }
64
65 /**
66 * If an exact title match can be found, jump straight ahead to
67 * @param string $term
68 * @access public
69 */
70 function goResult( $term ) {
71 global $wgOut;
72 global $wgGoToEdit;
73
74 $this->setupPage( $term );
75
76 # Try to go to page as entered.
77 #
78 $t = Title::newFromText( $term );
79
80 # If the string cannot be used to create a title
81 if( is_null( $t ) ){
82 return $this->showResults( $term );
83 }
84
85 # If there's an exact or very near match, jump right there.
86 $t = SearchEngine::getNearMatch( $term );
87 if( !is_null( $t ) ) {
88 $wgOut->redirect( $t->getFullURL() );
89 return;
90 }
91
92 # No match, generate an edit URL
93 $t = Title::newFromText( $term );
94 if( is_null( $t ) ) {
95 $editurl = ''; # hrm...
96 } else {
97 # If the feature is enabled, go straight to the edit page
98 if ( $wgGoToEdit ) {
99 $wgOut->redirect( $t->getFullURL( 'action=edit' ) );
100 return;
101 } else {
102 $editurl = $t->escapeLocalURL( 'action=edit' );
103 }
104 }
105 # FIXME: HTML in wiki message
106 $wgOut->addHTML( '<p>' . wfMsg('nogomatch', $editurl, htmlspecialchars( $term ) ) . "</p>\n" );
107
108 return $this->showResults( $term );
109 }
110
111 /**
112 * @param string $term
113 * @access public
114 */
115 function showResults( $term ) {
116 $fname = 'SpecialSearch::showResults';
117 wfProfileIn( $fname );
118
119 $this->setupPage( $term );
120
121 global $wgUser, $wgOut;
122 $sk = $wgUser->getSkin();
123 $wgOut->addWikiText( wfMsg( 'searchresulttext' ) );
124
125 #if ( !$this->parseQuery() ) {
126 if( '' === trim( $term ) ) {
127 $wgOut->addWikiText(
128 '==' . wfMsg( 'badquery' ) . "==\n" .
129 wfMsg( 'badquerytext' ) );
130 wfProfileOut( $fname );
131 return;
132 }
133
134 global $wgDisableTextSearch;
135 if ( $wgDisableTextSearch ) {
136 global $wgForwardSearchUrl;
137 if( $wgForwardSearchUrl ) {
138 $url = str_replace( '$1', urlencode( $term ), $wgForwardSearchUrl );
139 $wgOut->redirect( $url );
140 return;
141 }
142 global $wgInputEncoding;
143 $wgOut->addHTML( wfMsg( 'searchdisabled' ) );
144 $wgOut->addHTML( wfMsg( 'googlesearch',
145 htmlspecialchars( $term ),
146 htmlspecialchars( $wgInputEncoding ) ) );
147 wfProfileOut( $fname );
148 return;
149 }
150
151 $search =& SearchEngine::create();
152 $search->setLimitOffset( $this->limit, $this->offset );
153 $search->setNamespaces( $this->namespaces );
154 $titleMatches = $search->searchTitle( $term );
155 $textMatches = $search->searchText( $term );
156
157 $num = $titleMatches->numRows() + $textMatches->numRows();
158 if ( $num >= $this->limit ) {
159 $top = wfShowingResults( $this->offset, $this->limit );
160 } else {
161 $top = wfShowingResultsNum( $this->offset, $this->limit, $num );
162 }
163 $wgOut->addHTML( "<p>{$top}</p>\n" );
164
165 if( $num || $this->offset ) {
166 $prevnext = wfViewPrevNext( $this->offset, $this->limit,
167 'Special:Search',
168 wfArrayToCGI(
169 $this->powerSearchOptions(),
170 array( 'search' => $term ) ) );
171 $wgOut->addHTML( "<br />{$prevnext}\n" );
172 }
173
174 global $wgContLang;
175 $tm = $wgContLang->convertForSearchResult( $search->termMatches() );
176 $terms = implode( '|', $tm );
177
178 if( $titleMatches->numRows() ) {
179 $wgOut->addWikiText( '==' . wfMsg( 'titlematches' ) . "==\n" );
180 $wgOut->addHTML( $this->showMatches( $titleMatches, $terms ) );
181 } else {
182 $wgOut->addWikiText( '==' . wfMsg( 'notitlematches' ) . "==\n" );
183 }
184
185 if( $textMatches->numRows() ) {
186 $wgOut->addWikiText( '==' . wfMsg( 'textmatches' ) . "==\n" );
187 $wgOut->addHTML( $this->showMatches( $textMatches, $terms ) );
188 } elseif( $num == 0 ) {
189 # Don't show the 'no text matches' if we received title matches
190 $wgOut->addWikiText( '==' . wfMsg( 'notextmatches' ) . "==\n" );
191 }
192
193 if ( $num == 0 ) {
194 $wgOut->addWikiText( wfMsg( 'nonefound' ) );
195 }
196 if( $num || $this->offset ) {
197 $wgOut->addHTML( "<p>{$prevnext}</p>\n" );
198 }
199 $wgOut->addHTML( $this->powerSearchBox( $term ) );
200 wfProfileOut( $fname );
201 }
202
203 #------------------------------------------------------------------
204 # Private methods below this line
205
206 /**
207 *
208 */
209 function setupPage( $term ) {
210 global $wgOut;
211 $wgOut->setPageTitle( wfMsg( 'searchresults' ) );
212 $wgOut->setSubtitle( wfMsg( 'searchquery', htmlspecialchars( $term ) ) );
213 $wgOut->setArticleRelated( false );
214 $wgOut->setRobotpolicy( 'noindex,nofollow' );
215 }
216
217 /**
218 * Extract default namespaces to search from the given user's
219 * settings, returning a list of index numbers.
220 *
221 * @param User $user
222 * @return array
223 * @access private
224 */
225 function userNamespaces( &$user ) {
226 $arr = array();
227 foreach( SearchEngine::searchableNamespaces() as $ns => $name ) {
228 if( $user->getOption( 'searchNs' . $ns ) ) {
229 $arr[] = $ns;
230 }
231 }
232 return $arr;
233 }
234
235 /**
236 * Extract "power search" namespace settings from the request object,
237 * returning a list of index numbers to search.
238 *
239 * @param WebRequest $request
240 * @return array
241 * @access private
242 */
243 function powerSearch( &$request ) {
244 $arr = array();
245 foreach( SearchEngine::searchableNamespaces() as $ns => $name ) {
246 if( $request->getCheck( 'ns' . $ns ) ) {
247 $arr[] = $ns;
248 }
249 }
250 return $arr;
251 }
252
253 /**
254 * Reconstruct the 'power search' options for links
255 * @return array
256 * @access private
257 */
258 function powerSearchOptions() {
259 $opt = array();
260 foreach( $this->namespaces as $n ) {
261 $opt['ns' . $n] = 1;
262 }
263 $opt['redirs'] = $this->searchRedirects ? 1 : 0;
264 $opt['searchx'] = 1;
265 return $opt;
266 }
267
268 /**
269 * @param ResultWrapper $matches
270 * @param string $terms partial regexp for highlighting terms
271 */
272 function showMatches( &$matches, $terms ) {
273 $fname = 'SpecialSearch::showMatches';
274 wfProfileIn( $fname );
275
276 global $wgOut;
277 $off = $this->offset + 1;
278 $out = "<ol start='{$off}'>\n";
279
280 while( $row = $matches->fetchObject() ) {
281 $out .= $this->showHit( $row, $terms );
282 }
283 $out .= "</ol>\n";
284
285 // convert the whole thing to desired language variant
286 global $wgContLang;
287 $out = $wgContLang->convert( $out );
288 wfProfileOut( $fname );
289 return $out;
290 }
291
292 /**
293 * Format a single hit result
294 * @param object $row
295 * @param string $terms partial regexp for highlighting terms
296 */
297 function showHit( $row, $terms ) {
298 $fname = 'SpecialSearch::showHit';
299 wfProfileIn( $fname );
300 global $wgUser, $wgContLang;
301
302 $t = Title::makeTitle( $row->page_namespace, $row->page_title );
303 if( is_null( $t ) ) {
304 wfProfileOut( $fname );
305 return "<!-- Broken link in search result -->\n";
306 }
307 $sk =& $wgUser->getSkin();
308
309 $contextlines = $wgUser->getOption( 'contextlines' );
310 if ( '' == $contextlines ) { $contextlines = 5; }
311 $contextchars = $wgUser->getOption( 'contextchars' );
312 if ( '' == $contextchars ) { $contextchars = 50; }
313
314 $link = $sk->makeKnownLinkObj( $t, '' );
315 $text = Revision::getRevisionText( $row );
316 $size = wfMsg( 'nbytes', strlen( $text ) );
317
318 $lines = explode( "\n", $text );
319
320 $max = IntVal( $contextchars ) + 1;
321 $pat1 = "/(.*)($terms)(.{0,$max})/i";
322
323 $lineno = 0;
324
325 $extract = '';
326 wfProfileIn( "$fname-extract" );
327 foreach ( $lines as $line ) {
328 if ( 0 == $contextlines ) {
329 break;
330 }
331 ++$lineno;
332 if ( ! preg_match( $pat1, $line, $m ) ) {
333 continue;
334 }
335 --$contextlines;
336 $pre = $wgContLang->truncate( $m[1], -$contextchars, '...' );
337
338 if ( count( $m ) < 3 ) {
339 $post = '';
340 } else {
341 $post = $wgContLang->truncate( $m[3], $contextchars, '...' );
342 }
343
344 $found = $m[2];
345
346 $line = htmlspecialchars( $pre . $found . $post );
347 $pat2 = '/(' . $terms . ")/i";
348 $line = preg_replace( $pat2,
349 "<span class='searchmatch'>\\1</span>", $line );
350
351 $extract .= "<br /><small>{$lineno}: {$line}</small>\n";
352 }
353 wfProfileOut( "$fname-extract" );
354 wfProfileOut( $fname );
355 return "<li>{$link} ({$size}){$extract}</li>\n";
356 }
357
358 function powerSearchBox( $term ) {
359 $namespaces = '';
360 foreach( SearchEngine::searchableNamespaces() as $ns => $name ) {
361 $checked = in_array( $ns, $this->namespaces )
362 ? ' checked="checked"'
363 : '';
364 $name = str_replace( '_', ' ', $name );
365 if( '' == $name ) {
366 $name = wfMsg( 'blanknamespace' );
367 }
368 $namespaces .= " <label><input type='checkbox' value=\"1\" name=\"" .
369 "ns{$ns}\"{$checked} />{$name}</label>\n";
370 }
371
372 $checked = $this->searchRedirects
373 ? ' checked="checked"'
374 : '';
375 $redirect = "<input type='checkbox' value='1' name=\"redirs\"{$checked} />\n";
376
377 $searchField = "<input type='text' name=\"search\" value=\"" .
378 htmlspecialchars( $term ) ."\" width=\"80\" />\n";
379
380 $searchButton = '<input type="submit" name="searchx" value="' .
381 htmlspecialchars( wfMsg('powersearch') ) . "\" />\n";
382
383 $ret = wfMsg( 'powersearchtext',
384 $namespaces, $redirect, $searchField,
385 '', '', '', '', '', # Dummy placeholders
386 $searchButton );
387
388 $title = Title::makeTitle( NS_SPECIAL, 'Search' );
389 $action = $title->escapeLocalURL();
390 return "<br /><br />\n<form id=\"powersearch\" method=\"get\" " .
391 "action=\"$action\">\n{$ret}\n</form>\n";
392 }
393 }
394
395 ?>