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