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