Profiling points. Avoid reparsing of titles which came out of the database. Snip...
[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 =& $this->getSearchEngine();
145 $titleMatches = $search->searchTitle( $term );
146 $textMatches = $search->searchText( $term );
147
148 $num = $titleMatches->numRows() + $textMatches->numRows();
149 if ( $num >= $this->limit ) {
150 $top = wfShowingResults( $this->offset, $this->limit );
151 } else {
152 $top = wfShowingResultsNum( $this->offset, $this->limit, $num );
153 }
154 $wgOut->addHTML( "<p>{$top}</p>\n" );
155
156 if( $num || $this->offset ) {
157 $prevnext = wfViewPrevNext( $this->offset, $this->limit,
158 'Special:Search',
159 wfArrayToCGI(
160 $this->powerSearchOptions(),
161 array( 'search' => $term ) ) );
162 $wgOut->addHTML( "<br />{$prevnext}\n" );
163 }
164
165 $terms = implode( '|', $search->termMatches() );
166
167 if( $titleMatches->numRows() ) {
168 $wgOut->addWikiText( '==' . wfMsg( 'titlematches' ) . "==\n" );
169 $wgOut->addHTML( $this->showMatches( $titleMatches, $terms ) );
170 } else {
171 $wgOut->addWikiText( '==' . wfMsg( 'notitlematches' ) . "==\n" );
172 }
173
174 if( $textMatches->numRows() ) {
175 $wgOut->addWikiText( '==' . wfMsg( 'textmatches' ) . "==\n" );
176 $wgOut->addHTML( $this->showMatches( $textMatches, $terms ) );
177 } elseif( $num == 0 ) {
178 # Don't show the 'no text matches' if we received title matches
179 $wgOut->addWikiText( '==' . wfMsg( 'notextmatches' ) . "==\n" );
180 }
181
182 if ( $num == 0 ) {
183 $wgOut->addWikiText( wfMsg( 'nonefound' ) );
184 }
185 if( $num || $this->offset ) {
186 $wgOut->addHTML( "<p>{$prevnext}</p>\n" );
187 }
188 $wgOut->addHTML( $this->powerSearchBox( $term ) );
189 wfProfileOut( $fname );
190 }
191
192 #------------------------------------------------------------------
193 # Private methods below this line
194
195 /**
196 *
197 */
198 function setupPage( $term ) {
199 global $wgOut;
200 $wgOut->setPageTitle( wfMsg( 'searchresults' ) );
201 $wgOut->setSubtitle( wfMsg( 'searchquery', htmlspecialchars( $term ) ) );
202 $wgOut->setArticleRelated( false );
203 $wgOut->setRobotpolicy( 'noindex,nofollow' );
204 }
205
206 /**
207 * Load up the appropriate search engine class for the currently
208 * active database backend, and return a configured instance.
209 *
210 * @return SearchEngine
211 * @access private
212 */
213 function &getSearchEngine() {
214 global $wgDBtype, $wgDBmysql4, $wgSearchType;
215 if( $wgDBtype == 'mysql' ) {
216 if( $wgDBmysql4 ) {
217 $class = 'SearchMySQL4';
218 require_once( 'SearchMySQL4.php' );
219 } else {
220 $class = 'SearchMysql3';
221 require_once( 'SearchMySQL3.php' );
222 }
223 } else {
224 $class = 'SearchEngineDummy';
225 }
226 $search = new $class( wfGetDB( DB_SLAVE ) );
227 $search->setLimitOffset( $this->limit, $this->offset );
228 $search->setNamespaces( $this->namespaces );
229 return $search;
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 * @access 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 * @access 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 * @access 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 ResultWrapper $matches
285 * @param string $terms partial regexp for highlighting terms
286 */
287 function showMatches( &$matches, $terms ) {
288 $fname = 'SpecialSearch::showMatches';
289 wfProfileIn( $fname );
290
291 global $wgOut;
292 $off = $this->offset + 1;
293 $out = "<ol start='{$off}'>\n";
294
295 while( $row = $matches->fetchObject() ) {
296 $out .= $this->showHit( $row, $terms );
297 }
298 $out .= "</ol>\n";
299 wfProfileOut( $fname );
300 return $out;
301 }
302
303 /**
304 * Format a single hit result
305 * @param object $row
306 * @param string $terms partial regexp for highlighting terms
307 */
308 function showHit( $row, $terms ) {
309 $fname = 'SpecialSearch::showHit';
310 wfProfileIn( $fname );
311 global $wgUser, $wgContLang;
312
313 $t = Title::makeTitle( $row->cur_namespace, $row->cur_title );
314 if( is_null( $t ) ) {
315 wfProfileOut( $fname );
316 return "<!-- Broken link in search result -->\n";
317 }
318 $sk =& $wgUser->getSkin();
319
320 $contextlines = $wgUser->getOption( 'contextlines' );
321 if ( '' == $contextlines ) { $contextlines = 5; }
322 $contextchars = $wgUser->getOption( 'contextchars' );
323 if ( '' == $contextchars ) { $contextchars = 50; }
324
325 $link = $sk->makeKnownLinkObj( $t, '' );
326 $size = wfMsg( 'nbytes', strlen( $row->cur_text ) );
327
328 $lines = explode( "\n", $row->cur_text );
329 $max = IntVal( $contextchars ) + 1;
330 $pat1 = "/(.*)($terms)(.{0,$max})/i";
331 $lineno = 0;
332
333 $extract = '';
334 wfProfileIn( "$fname-extract" );
335 foreach ( $lines as $line ) {
336 if ( 0 == $contextlines ) {
337 break;
338 }
339 ++$lineno;
340 if ( ! preg_match( $pat1, $line, $m ) ) {
341 continue;
342 }
343 --$contextlines;
344 $pre = $wgContLang->truncate( $m[1], -$contextchars, '...' );
345
346 if ( count( $m ) < 3 ) {
347 $post = '';
348 } else {
349 $post = $wgContLang->truncate( $m[3], $contextchars, '...' );
350 }
351
352 $found = $m[2];
353
354 $line = htmlspecialchars( $pre . $found . $post );
355 $pat2 = '/(' . $terms . ")/i";
356 $line = preg_replace( $pat2,
357 "<span class='searchmatch'>\\1</span>", $line );
358
359 $extract .= "<br /><small>{$lineno}: {$line}</small>\n";
360 }
361 wfProfileOut( "$fname-extract" );
362 wfProfileOut( $fname );
363 return "<li>{$link} ({$size}){$extract}</li>\n";
364 }
365
366 function powerSearchBox( $term ) {
367 $namespaces = '';
368 foreach( SearchEngine::searchableNamespaces() as $ns => $name ) {
369 $checked = in_array( $ns, $this->namespaces )
370 ? ' checked="checked"'
371 : '';
372 $name = str_replace( '_', ' ', $name );
373 if( '' == $name ) {
374 $name = wfMsg( 'blanknamespace' );
375 }
376 $namespaces .= " <label><input type='checkbox' value=\"1\" name=\"" .
377 "ns{$ns}\"{$checked} />{$name}</label>\n";
378 }
379
380 $checked = $this->searchRedirects
381 ? ' checked="checked"'
382 : '';
383 $redirect = "<input type='checkbox' value='1' name=\"redirs\"{$checked} />\n";
384
385 $searchField = "<input type='text' name=\"search\" value=\"" .
386 htmlspecialchars( $term ) ."\" width=\"80\" />\n";
387
388 $searchButton = '<input type="submit" name="searchx" value="' .
389 htmlspecialchars( wfMsg('powersearch') ) . "\" />\n";
390
391 $ret = wfMsg( 'powersearchtext',
392 $namespaces, $redirect, $searchField,
393 '', '', '', '', '', # Dummy placeholders
394 $searchButton );
395
396 $title = Title::makeTitle( NS_SPECIAL, 'Search' );
397 $action = $title->escapeLocalURL();
398 return "<br /><br />\n<form id=\"powersearch\" method=\"get\" " .
399 "action=\"$action\">\n{$ret}\n</form>\n";
400 }
401 }
402
403 ?>