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