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