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