* Remove <hr/>
[lhc/web/wiklou.git] / includes / specials / 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 * @file
23 * @ingroup SpecialPage
24 */
25
26 /**
27 * Entry point
28 *
29 * @param $par String: (default '')
30 */
31 function wfSpecialSearch( $par = '' ) {
32 global $wgRequest, $wgUser, $wgUseOldSearchUI;
33
34 // Strip underscores from title parameter; most of the time we'll want
35 // text form here. But don't strip underscores from actual text params!
36 $titleParam = str_replace( '_', ' ', $par );
37
38 $search = str_replace( "\n", " ", $wgRequest->getText( 'search', $titleParam ) );
39 $class = $wgUseOldSearchUI ? 'SpecialSearchOld' : 'SpecialSearch';
40 $searchPage = new $class( $wgRequest, $wgUser );
41 if( $wgRequest->getVal( 'fulltext' )
42 || !is_null( $wgRequest->getVal( 'offset' ))
43 || !is_null( $wgRequest->getVal( 'searchx' )) )
44 {
45 $searchPage->showResults( $search, 'search' );
46 } else {
47 $searchPage->goResult( $search );
48 }
49 }
50
51 /**
52 * implements Special:Search - Run text & title search and display the output
53 * @ingroup SpecialPage
54 */
55 class SpecialSearch {
56
57 /**
58 * Set up basic search parameters from the request and user settings.
59 * Typically you'll pass $wgRequest and $wgUser.
60 *
61 * @param WebRequest $request
62 * @param User $user
63 * @public
64 */
65 function __construct( &$request, &$user ) {
66 list( $this->limit, $this->offset ) = $request->getLimitOffset( 20, 'searchlimit' );
67
68 $this->namespaces = $this->powerSearch( $request );
69 if( empty( $this->namespaces ) ) {
70 $this->namespaces = SearchEngine::userNamespaces( $user );
71 }
72
73 $this->searchRedirects = $request->getcheck( 'redirs' ) ? true : false;
74 $this->searchAdvanced = $request->getVal('advanced');
75 }
76
77 /**
78 * If an exact title match can be found, jump straight ahead to it.
79 * @param string $term
80 * @public
81 */
82 function goResult( $term ) {
83 global $wgOut, $wgGoToEdit;
84
85 $this->setupPage( $term );
86
87 # Try to go to page as entered.
88 $t = Title::newFromText( $term );
89
90 # If the string cannot be used to create a title
91 if( is_null( $t ) ) {
92 return $this->showResults( $term );
93 }
94
95 # If there's an exact or very near match, jump right there.
96 $t = SearchEngine::getNearMatch( $term );
97 if( !is_null( $t ) ) {
98 $wgOut->redirect( $t->getFullURL() );
99 return;
100 }
101
102 # No match, generate an edit URL
103 $t = Title::newFromText( $term );
104 if( ! is_null( $t ) ) {
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 }
111 }
112
113 return $this->showResults( $term );
114 }
115
116 /**
117 * @param string $term
118 * @public
119 */
120 function showResults( $term ) {
121 wfProfileIn( __METHOD__ );
122 global $wgOut, $wgUser;
123 $sk = $wgUser->getSkin();
124
125 $this->setupPage( $term );
126 $this->searchEngine = SearchEngine::create();
127
128 $t = Title::newFromText( $term );
129
130 $wgOut->addHtml(
131 Xml::openElement( 'table', array( 'border'=>0, 'cellpadding'=>0, 'cellspacing'=>0 ) ) .
132 Xml::openElement( 'tr' ) .
133 Xml::openElement( 'td' ) . "\n"
134 );
135 if( $this->searchAdvanced ) {
136 $wgOut->addHTML( $this->powerSearchBox( $term ) );
137 $showMenu = false;
138 } else {
139 $wgOut->addHTML( $this->shortDialog( $term ) );
140 $showMenu = true;
141 }
142 $wgOut->addHtml(
143 Xml::closeElement('td') .
144 Xml::closeElement('tr') .
145 Xml::closeElement('table')
146 );
147
148 if( '' === trim( $term ) ) {
149 // Empty query -- straight view of search form
150 wfProfileOut( __METHOD__ );
151 return;
152 }
153
154 global $wgDisableTextSearch;
155 if( $wgDisableTextSearch ) {
156 global $wgSearchForwardUrl;
157 if( $wgSearchForwardUrl ) {
158 $url = str_replace( '$1', urlencode( $term ), $wgSearchForwardUrl );
159 $wgOut->redirect( $url );
160 return;
161 }
162 global $wgInputEncoding;
163 $wgOut->addHTML(
164 Xml::openElement( 'fieldset' ) .
165 Xml::element( 'legend', null, wfMsg( 'search-external' ) ) .
166 Xml::element( 'p', array( 'class' => 'mw-searchdisabled' ), wfMsg( 'searchdisabled' ) ) .
167 wfMsg( 'googlesearch',
168 htmlspecialchars( $term ),
169 htmlspecialchars( $wgInputEncoding ),
170 htmlspecialchars( wfMsg( 'searchbutton' ) )
171 ) .
172 Xml::closeElement( 'fieldset' )
173 );
174 wfProfileOut( __METHOD__ );
175 return;
176 }
177
178 $search =& $this->searchEngine;
179 $search->setLimitOffset( $this->limit, $this->offset );
180 $search->setNamespaces( $this->namespaces );
181 $search->showRedirects = $this->searchRedirects;
182 $rewritten = $search->replacePrefixes($term);
183
184 $titleMatches = $search->searchTitle( $rewritten );
185
186 // Sometimes the search engine knows there are too many hits
187 if( $titleMatches instanceof SearchResultTooMany ) {
188 $wgOut->addWikiText( '==' . wfMsg( 'toomanymatches' ) . "==\n" );
189 wfProfileOut( __METHOD__ );
190 return;
191 }
192
193 $textMatches = $search->searchText( $rewritten );
194
195 // did you mean... suggestions
196 if( $textMatches && $textMatches->hasSuggestion() ) {
197 $st = SpecialPage::getTitleFor( 'Search' );
198 $stParams = wfArrayToCGI(
199 array( 'search' => $textMatches->getSuggestionQuery(), 'fulltext' => wfMsg('search') ),
200 $this->powerSearchOptions()
201 );
202 $suggestLink = '<a href="'.$st->escapeLocalURL($stParams).'">'.
203 $textMatches->getSuggestionSnippet().'</a>';
204
205 $wgOut->addHTML('<div class="searchdidyoumean">'.wfMsg('search-suggest',$suggestLink).'</div>');
206 }
207
208 // show direct page/create link
209 if( !is_null($t) ) {
210 if( !$t->exists() ) {
211 $wgOut->addWikiMsg( 'searchmenu-new', wfEscapeWikiText( $t->getPrefixedText() ) );
212 } else {
213 $wgOut->addWikiMsg( 'searchmenu-exists', wfEscapeWikiText( $t->getPrefixedText() ) );
214 }
215 }
216
217 // show number of results
218 $numTitleMatches = $titleMatches ? $titleMatches->numRows() : 0;
219 $numTextMatches = $textMatches ? $textMatches->numRows() : 0;
220 $highestNum = max( $numTitleMatches, $numTextMatches );
221 // Total query matches (possible false positives)
222 $num = $numTitleMatches + $numTextMatches;
223 // Get total actual results
224 $totalNum = 0;
225 if( $titleMatches && !is_null($titleMatches->getTotalHits()) )
226 $totalNum += $titleMatches->getTotalHits();
227 if( $textMatches && !is_null($textMatches->getTotalHits()) )
228 $totalNum += $textMatches->getTotalHits();
229 if( $num > 0 ) {
230 if( $totalNum > 0 ) {
231 $top = wfMsgExt('showingresultstotal', array( 'parseinline' ),
232 $this->offset+1, $this->offset+$num, $totalNum, $num );
233 } elseif( $num >= $this->limit ) {
234 $top = wfShowingResults( $this->offset, $this->limit );
235 } else {
236 $top = wfShowingResultsNum( $this->offset, $this->limit, $num );
237 }
238 $wgOut->addHTML( "<p class='mw-search-numberresults'>{$top}</p>\n" );
239 }
240
241 // prev/next links
242 if( $num || $this->offset ) {
243 $prevnext = wfViewPrevNext( $this->offset, $this->limit,
244 SpecialPage::getTitleFor( 'Search' ),
245 wfArrayToCGI( $this->powerSearchOptions(), array( 'search' => $term ) ),
246 ($highestNum < $this->limit)
247 );
248 $wgOut->addHTML( "<p class='mw-search-pager-top'>{$prevnext}</p>\n" );
249 wfRunHooks( 'SpecialSearchResults', array( $term, &$titleMatches, &$textMatches ) );
250 } else {
251 wfRunHooks( 'SpecialSearchNoResults', array( $term ) );
252 }
253
254 $wgOut->addHtml( "<div class='searchresults'>" );
255
256 if( $titleMatches ) {
257 if( $numTitleMatches > 0 ) {
258 $wgOut->wrapWikiMsg( "==$1==\n", 'titlematches' );
259 $wgOut->addHTML( $this->showMatches( $titleMatches ) );
260 }
261 $titleMatches->free();
262 }
263
264 if( $textMatches ) {
265 // output appropriate heading
266 if( $numTextMatches > 0 && $numTitleMatches > 0 ) {
267 // if no title matches the heading is redundant
268 $wgOut->wrapWikiMsg( "==$1==\n", 'textmatches' );
269 } elseif( $num == 0 ) {
270 # Don't show the 'no text matches' if we received title matches
271 $wgOut->wrapWikiMsg( "==$1==\n", 'notextmatches' );
272 }
273 // show interwiki results if any
274 if( $textMatches->hasInterwikiResults() )
275 $wgOut->addHTML( $this->showInterwiki( $textMatches->getInterwikiResults(), $term ));
276 // show results
277 if( $numTextMatches > 0 )
278 $wgOut->addHTML( $this->showMatches( $textMatches ) );
279
280 $textMatches->free();
281 }
282
283 if( $num == 0 ) {
284 $wgOut->addWikiMsg( 'search-nonefound' );
285 }
286
287 $wgOut->addHtml( "</div>" );
288
289 if( $num || $this->offset ) {
290 $wgOut->addHTML( "<p class='mw-search-pager-bottom'>{$prevnext}</p>\n" );
291 }
292 wfProfileOut( __METHOD__ );
293 }
294
295 /**
296 *
297 */
298 protected function setupPage( $term ) {
299 global $wgOut;
300 if( !empty( $term ) ) {
301 $wgOut->setPageTitle( wfMsg( 'searchresults') );
302 $wgOut->setHTMLTitle( wfMsg( 'pagetitle', wfMsg( 'searchresults-title', $term) ) );
303 }
304 $wgOut->setArticleRelated( false );
305 $wgOut->setRobotPolicy( 'noindex,nofollow' );
306 }
307
308 /**
309 * Extract "power search" namespace settings from the request object,
310 * returning a list of index numbers to search.
311 *
312 * @param WebRequest $request
313 * @return array
314 */
315 protected function powerSearch( &$request ) {
316 $arr = array();
317 foreach( SearchEngine::searchableNamespaces() as $ns => $name ) {
318 if( $request->getCheck( 'ns' . $ns ) ) {
319 $arr[] = $ns;
320 }
321 }
322 return $arr;
323 }
324
325 /**
326 * Reconstruct the 'power search' options for links
327 * @return array
328 */
329 protected function powerSearchOptions() {
330 $opt = array();
331 foreach( $this->namespaces as $n ) {
332 $opt['ns' . $n] = 1;
333 }
334 $opt['redirs'] = $this->searchRedirects ? 1 : 0;
335 if( $this->searchAdvanced )
336 $opt['advanced'] = $this->searchAdvanced;
337 return $opt;
338 }
339
340 /**
341 * Show whole set of results
342 *
343 * @param SearchResultSet $matches
344 */
345 protected function showMatches( &$matches ) {
346 global $wgContLang;
347 wfProfileIn( __METHOD__ );
348
349 $terms = $wgContLang->convertForSearchResult( $matches->termMatches() );
350
351 $out = "";
352
353 $infoLine = $matches->getInfo();
354 if( !is_null($infoLine) )
355 $out .= "\n<!-- {$infoLine} -->\n";
356
357
358 $off = $this->offset + 1;
359 $out .= "<ul class='mw-search-results'>\n";
360 while( $result = $matches->next() ) {
361 $out .= $this->showHit( $result, $terms );
362 }
363 $out .= "</ul>\n";
364
365 // convert the whole thing to desired language variant
366 $out = $wgContLang->convert( $out );
367 wfProfileOut( __METHOD__ );
368 return $out;
369 }
370
371 /**
372 * Format a single hit result
373 * @param SearchResult $result
374 * @param array $terms terms to highlight
375 */
376 protected function showHit( $result, $terms ) {
377 wfProfileIn( __METHOD__ );
378 global $wgUser, $wgContLang, $wgLang;
379
380 if( $result->isBrokenTitle() ) {
381 wfProfileOut( __METHOD__ );
382 return "<!-- Broken link in search result -->\n";
383 }
384
385 $t = $result->getTitle();
386 $sk = $wgUser->getSkin();
387
388 $link = $sk->makeKnownLinkObj( $t, $result->getTitleSnippet($terms));
389
390 //If page content is not readable, just return the title.
391 //This is not quite safe, but better than showing excerpts from non-readable pages
392 //Note that hiding the entry entirely would screw up paging.
393 if(!$t->userCanRead()) {
394 wfProfileOut( __METHOD__ );
395 return "<li>{$link}</li>\n";
396 }
397
398 // If the page doesn't *exist*... our search index is out of date.
399 // The least confusing at this point is to drop the result.
400 // You may get less results, but... oh well. :P
401 if( $result->isMissingRevision() ) {
402 wfProfileOut( __METHOD__ );
403 return "<!-- missing page " .
404 htmlspecialchars( $t->getPrefixedText() ) . "-->\n";
405 }
406
407 // format redirects / relevant sections
408 $redirectTitle = $result->getRedirectTitle();
409 $redirectText = $result->getRedirectSnippet($terms);
410 $sectionTitle = $result->getSectionTitle();
411 $sectionText = $result->getSectionSnippet($terms);
412 $redirect = '';
413 if( !is_null($redirectTitle) )
414 $redirect = "<span class='searchalttitle'>"
415 .wfMsg('search-redirect',$sk->makeKnownLinkObj( $redirectTitle, $redirectText))
416 ."</span>";
417 $section = '';
418 if( !is_null($sectionTitle) )
419 $section = "<span class='searchalttitle'>"
420 .wfMsg('search-section', $sk->makeKnownLinkObj( $sectionTitle, $sectionText))
421 ."</span>";
422
423 // format text extract
424 $extract = "<div class='searchresult'>".$result->getTextSnippet($terms)."</div>";
425
426 // format score
427 if( is_null( $result->getScore() ) ) {
428 // Search engine doesn't report scoring info
429 $score = '';
430 } else {
431 $percent = sprintf( '%2.1f', $result->getScore() * 100 );
432 $score = wfMsg( 'search-result-score', $wgLang->formatNum( $percent ) )
433 . ' - ';
434 }
435
436 // format description
437 $byteSize = $result->getByteSize();
438 $wordCount = $result->getWordCount();
439 $timestamp = $result->getTimestamp();
440 $size = wfMsgExt( 'search-result-size', array( 'parsemag', 'escape' ),
441 $sk->formatSize( $byteSize ),
442 $wordCount );
443 $date = $wgLang->timeanddate( $timestamp );
444
445 // link to related articles if supported
446 $related = '';
447 if( $result->hasRelated() ) {
448 $st = SpecialPage::getTitleFor( 'Search' );
449 $stParams = wfArrayToCGI( $this->powerSearchOptions(),
450 array('search' => wfMsgForContent('searchrelated').':'.$t->getPrefixedText(),
451 'fulltext' => wfMsg('search') ));
452
453 $related = ' -- <a href="'.$st->escapeLocalURL($stParams).'">'.
454 wfMsg('search-relatedarticle').'</a>';
455 }
456
457 // Include a thumbnail for media files...
458 if( $t->getNamespace() == NS_IMAGE ) {
459 $img = wfFindFile( $t );
460 if( $img ) {
461 $thumb = $img->transform( array( 'width' => 120, 'height' => 120 ) );
462 if( $thumb ) {
463 $desc = $img->getShortDesc();
464 wfProfileOut( __METHOD__ );
465 // Float doesn't seem to interact well with the bullets.
466 // Table messes up vertical alignment of the bullets.
467 // Bullets are therefore disabled (didn't look great anyway).
468 return "<li>" .
469 '<table class="searchResultImage">' .
470 '<tr>' .
471 '<td width="120" align="center" valign="top">' .
472 $thumb->toHtml( array( 'desc-link' => true ) ) .
473 '</td>' .
474 '<td valign="top">' .
475 $link .
476 $extract .
477 "<div class='mw-search-result-data'>{$score}{$desc} - {$date}{$related}</div>" .
478 '</td>' .
479 '</tr>' .
480 '</table>' .
481 "</li>\n";
482 }
483 }
484 }
485
486 wfProfileOut( __METHOD__ );
487 return "<li>{$link} {$redirect} {$section} {$extract}\n" .
488 "<div class='mw-search-result-data'>{$score}{$size} - {$date}{$related}</div>" .
489 "</li>\n";
490
491 }
492
493 /**
494 * Show results from other wikis
495 *
496 * @param SearchResultSet $matches
497 */
498 protected function showInterwiki( &$matches, $query ) {
499 wfProfileIn( __METHOD__ );
500
501 global $wgContLang;
502 $terms = $wgContLang->convertForSearchResult( $matches->termMatches() );
503
504 $out = "<div id='mw-search-interwiki'><div id='mw-search-interwiki-caption'>".
505 wfMsg('search-interwiki-caption')."</div>\n";
506 $off = $this->offset + 1;
507 $out .= "<ul start='{$off}' class='mw-search-iwresults'>\n";
508
509 // work out custom project captions
510 $customCaptions = array();
511 $customLines = explode("\n",wfMsg('search-interwiki-custom')); // format per line <iwprefix>:<caption>
512 foreach($customLines as $line) {
513 $parts = explode(":",$line,2);
514 if(count($parts) == 2) // validate line
515 $customCaptions[$parts[0]] = $parts[1];
516 }
517
518
519 $prev = null;
520 while( $result = $matches->next() ) {
521 $out .= $this->showInterwikiHit( $result, $prev, $terms, $query, $customCaptions );
522 $prev = $result->getInterwikiPrefix();
523 }
524 // FIXME: should support paging in a non-confusing way (not sure how though, maybe via ajax)..
525 $out .= "</ul></div>\n";
526
527 // convert the whole thing to desired language variant
528 global $wgContLang;
529 $out = $wgContLang->convert( $out );
530 wfProfileOut( __METHOD__ );
531 return $out;
532 }
533
534 /**
535 * Show single interwiki link
536 *
537 * @param SearchResult $result
538 * @param string $lastInterwiki
539 * @param array $terms
540 * @param string $query
541 * @param array $customCaptions iw prefix -> caption
542 */
543 protected function showInterwikiHit( $result, $lastInterwiki, $terms, $query, $customCaptions) {
544 wfProfileIn( __METHOD__ );
545 global $wgUser, $wgContLang, $wgLang;
546
547 if( $result->isBrokenTitle() ) {
548 wfProfileOut( __METHOD__ );
549 return "<!-- Broken link in search result -->\n";
550 }
551
552 $t = $result->getTitle();
553 $sk = $wgUser->getSkin();
554
555 $link = $sk->makeKnownLinkObj( $t, $result->getTitleSnippet($terms));
556
557 // format redirect if any
558 $redirectTitle = $result->getRedirectTitle();
559 $redirectText = $result->getRedirectSnippet($terms);
560 $redirect = '';
561 if( !is_null($redirectTitle) )
562 $redirect = "<span class='searchalttitle'>"
563 .wfMsg('search-redirect',$sk->makeKnownLinkObj( $redirectTitle, $redirectText))
564 ."</span>";
565
566 $out = "";
567 // display project name
568 if(is_null($lastInterwiki) || $lastInterwiki != $t->getInterwiki()) {
569 if( key_exists($t->getInterwiki(),$customCaptions) )
570 // captions from 'search-interwiki-custom'
571 $caption = $customCaptions[$t->getInterwiki()];
572 else{
573 // default is to show the hostname of the other wiki which might suck
574 // if there are many wikis on one hostname
575 $parsed = parse_url($t->getFullURL());
576 $caption = wfMsg('search-interwiki-default', $parsed['host']);
577 }
578 // "more results" link (special page stuff could be localized, but we might not know target lang)
579 $searchTitle = Title::newFromText($t->getInterwiki().":Special:Search");
580 $searchLink = $sk->makeKnownLinkObj( $searchTitle, wfMsg('search-interwiki-more'),
581 wfArrayToCGI(array('search' => $query, 'fulltext' => 'Search')));
582 $out .= "</ul><div class='mw-search-interwiki-project'><span class='mw-search-interwiki-more'>
583 {$searchLink}</span>{$caption}</div>\n<ul>";
584 }
585
586 $out .= "<li>{$link} {$redirect}</li>\n";
587 wfProfileOut( __METHOD__ );
588 return $out;
589 }
590
591
592 /**
593 * Generates the power search box at bottom of [[Special:Search]]
594 * @param $term string: search term
595 * @return $out string: HTML form
596 */
597 protected function powerSearchBox( $term ) {
598 global $wgScript, $wgContLang;
599
600 $namespaces = SearchEngine::searchableNamespaces();
601
602 // group namespaces into rows according to subject; try not to make too
603 // many assumptions about namespace numbering
604 $rows = array();
605 foreach( $namespaces as $ns => $name ) {
606 $subj = Namespace::getSubject( $ns );
607 if( !array_key_exists( $subj, $rows ) ) {
608 $rows[$subj] = "";
609 }
610 $name = str_replace( '_', ' ', $name );
611 if( '' == $name ) {
612 $name = wfMsg( 'blanknamespace' );
613 }
614 $rows[$subj] .= Xml::openElement( 'td', array( 'style' => 'white-space: nowrap' ) ) .
615 Xml::checkLabel( $name, "ns{$ns}", "mw-search-ns{$ns}", in_array( $ns, $this->namespaces ) ) .
616 Xml::closeElement( 'td' ) . "\n";
617 }
618 $rows = array_values( $rows );
619 $numRows = count( $rows );
620
621 // lay out namespaces in multiple floating two-column tables so they'll
622 // be arranged nicely while still accommodating different screen widths
623 $rowsPerTable = 3; // seems to look nice
624
625 // float to the right on RTL wikis
626 $tableStyle = ( $wgContLang->isRTL() ?
627 'float: right; margin: 0 0 1em 1em' :
628 'float: left; margin: 0 1em 1em 0' );
629
630 $tables = "";
631 for( $i = 0; $i < $numRows; $i += $rowsPerTable ) {
632 $tables .= Xml::openElement( 'table', array( 'style' => $tableStyle ) );
633 for( $j = $i; $j < $i + $rowsPerTable && $j < $numRows; $j++ ) {
634 $tables .= "<tr>\n" . $rows[$j] . "</tr>";
635 }
636 $tables .= Xml::closeElement( 'table' ) . "\n";
637 }
638
639 $redirect = Xml::check( 'redirs', $this->searchRedirects, array( 'value' => '1', 'id' => 'redirs' ) );
640 $redirectLabel = Xml::label( wfMsg( 'powersearch-redir' ), 'redirs' );
641 $searchField = Xml::input( 'search', 50, $term, array( 'type' => 'text', 'id' => 'powerSearchText' ) );
642 $searchButton = Xml::submitButton( wfMsg( 'powersearch' ), array( 'name' => 'fulltext' ) ) . "\n";
643 $searchTitle = SpecialPage::getTitleFor( 'Search' );
644
645 $out = Xml::openElement( 'form', array( 'id' => 'powersearch', 'method' => 'get', 'action' => $wgScript ) ) .
646 Xml::hidden( 'title', $searchTitle->getPrefixedText() ) . "\n" .
647 "<p>" .
648 wfMsgExt( 'powersearch-ns', array( 'parseinline' ) ) .
649 "</p>\n" .
650 $tables .
651 "<hr style=\"clear: both\" />\n" .
652 "<p>" .
653 $redirect . " " . $redirectLabel .
654 "</p>\n" .
655 wfMsgExt( 'powersearch-field', array( 'parseinline' ) ) .
656 "&nbsp;" .
657 $searchField .
658 "&nbsp;" .
659 $searchButton .
660 "</form>";
661 $t = Title::newFromText( $term );
662 if( $t != null )
663 $out .= wfMsgExt( 'searchmenu-prefix', array('parseinline'), $term );
664
665 return Xml::openElement( 'fieldset', array('id' => 'mw-searchoptions','style' => 'margin:0em;') ) .
666 Xml::element( 'legend', null, wfMsg('powersearch-legend') ) .
667 $this->formHeader($term) . $out .
668 Xml::closeElement( 'fieldset' );
669 }
670
671 protected function powerSearchFocus() {
672 global $wgJsMimeType;
673 return "<script type=\"$wgJsMimeType\">" .
674 "hookEvent(\"load\", function() {" .
675 "document.getElementById('powerSearchText').focus();" .
676 "});" .
677 "</script>";
678 }
679
680 /** Make a search link with some target namespaces */
681 protected function makeSearchLink( $term, $namespaces, $label, $tooltip, $params=array() ) {
682 $opt = $params;
683 foreach( $namespaces as $n ) {
684 $opt['ns' . $n] = 1;
685 }
686 $opt['redirs'] = $this->searchRedirects ? 1 : 0;
687
688 $st = SpecialPage::getTitleFor( 'Search' );
689 $stParams = wfArrayToCGI( array( 'search' => $term, 'fulltext' => wfMsg( 'search' ) ), $opt );
690
691 return Xml::element( 'a',
692 array( 'href'=> $st->getLocalURL( $stParams ), 'title' => $tooltip ),
693 $label );
694 }
695
696 /** Check if query starts with image: prefix */
697 protected function startsWithImage( $term ) {
698 global $wgContLang;
699
700 $p = explode( ':', $term );
701 if( count( $p ) > 1 ) {
702 return $wgContLang->getNsIndex( $p[0] ) == NS_IMAGE;
703 }
704 return false;
705 }
706
707 protected function formHeader( $term ) {
708 global $wgContLang, $wgCanonicalNamespaceNames;
709
710 $sep = '&nbsp;&nbsp;&nbsp;';
711 $out = Xml::openElement('div', array( 'style' => 'padding-bottom:0.5em;' ) );
712
713 $bareterm = $term;
714 if( $this->startsWithImage( $term ) )
715 $bareterm = substr( $term, strpos( $term, ':' ) + 1 ); // delete all/image prefix
716
717 $nsAllSet = array_keys( SearchEngine::searchableNamespaces() );
718 // figure out the active search profile header
719 if( $this->searchAdvanced )
720 $active = 'advanced';
721 else if( $this->namespaces === NS_IMAGE || $this->startsWithImage( $term ) )
722 $active = 'images';
723 elseif( $this->namespaces === $nsAllSet )
724 $active = 'all';
725 elseif( $this->namespaces === SearchEngine::defaultNamespaces() )
726 $active = 'default';
727 elseif( $this->namespaces === SearchEngine::defaultAndProjectNamespaces() )
728 $active = 'withproject';
729 elseif( $this->namespaces === SearchEngine::projectNamespaces() )
730 $active = 'project';
731 else
732 $active = 'advanced';
733
734
735 // search profiles headers
736 $m = wfMsg( 'searchprofile-articles' );
737 $tt = wfMsg( 'searchprofile-articles-tooltip',
738 implode( ', ', SearchEngine::namespacesAsText( SearchEngine::defaultNamespaces() ) ) );
739 if( $active == 'default' ) {
740 $out .= Xml::element( 'strong', array( 'title'=>$tt ), $m );
741 } else {
742 $out .= $this->makeSearchLink( $bareterm, SearchEngine::defaultNamespaces(), $m, $tt );
743 }
744 $out .= $sep;
745
746 $m = wfMsg( 'searchprofile-images' );
747 $tt = wfMsg( 'searchprofile-images-tooltip' );
748 if( $active == 'images' ) {
749 $out .= Xml::element( 'strong', array( 'title'=>$tt ), $m );
750 } else {
751 $imageTextForm = $wgContLang->getFormattedNsText(NS_IMAGE).':'.$bareterm;
752 $out .= $this->makeSearchLink( $imageTextForm, array( NS_IMAGE ) , $m, $tt );
753 }
754 $out .= $sep;
755
756 $m = wfMsg( 'searchprofile-articles-and-proj' );
757 $tt = wfMsg( 'searchprofile-project-tooltip',
758 implode( ', ', SearchEngine::namespacesAsText( SearchEngine::defaultAndProjectNamespaces() ) ) );
759 if( $active == 'withproject' ) {
760 $out .= Xml::element( 'strong', array( 'title'=>$tt ), $m );
761 } else {
762 $out .= $this->makeSearchLink( $bareterm, SearchEngine::defaultAndProjectNamespaces(), $m, $tt );
763 }
764 $out .= $sep;
765
766 $m = wfMsg( 'searchprofile-project' );
767 $tt = wfMsg( 'searchprofile-project-tooltip',
768 implode( ', ', SearchEngine::namespacesAsText( SearchEngine::projectNamespaces() ) ) );
769 if( $active == 'project' ) {
770 $out .= Xml::element( 'strong', array( 'title'=>$tt ), $m );
771 } else {
772 $out .= $this->makeSearchLink( $bareterm, SearchEngine::projectNamespaces(), $m, $tt );
773 }
774 $out .= $sep;
775
776 $m = wfMsg( 'searchprofile-everything' );
777 $tt = wfMsg( 'searchprofile-everything-tooltip' );
778 if( $active == 'all' ) {
779 $out .= Xml::element( 'strong', array( 'title'=>$tt ), $m );
780 } else {
781 $out .= $this->makeSearchLink( $bareterm, $nsAllSet, $m, $tt );
782 }
783 $out .= $sep;
784
785 $m = wfMsg( 'searchprofile-advanced' );
786 $tt = wfMsg( 'searchprofile-advanced-tooltip' );
787 if( $active == 'advanced' ) {
788 $out .= Xml::element( 'strong', array( 'title'=>$tt ), $m );
789 } else {
790 $out .= $this->makeSearchLink( $bareterm, $this->namespaces, $m, $tt, array( 'advanced' => '1' ) );
791 }
792 $out .= Xml::closeElement('div') ;
793
794 return $out;
795 }
796
797 protected function shortDialog( $term ) {
798 global $wgScript;
799 $out = Xml::openElement( 'form', array( 'id' => 'search', 'method' => 'get', 'action' => $wgScript ) );
800 $searchTitle = SpecialPage::getTitleFor( 'Search' );
801 $out .= Xml::hidden( 'title', $searchTitle->getPrefixedText() ) . "\n";
802 $out .= Xml::input( 'search', 50, $term, array( 'type' => 'text', 'id' => 'searchText' ) ) . "\n";
803 foreach( SearchEngine::searchableNamespaces() as $ns => $name ) {
804 if( in_array( $ns, $this->namespaces ) ) {
805 $out .= Xml::hidden( "ns{$ns}", '1' );
806 }
807 }
808 $out .= Xml::submitButton( wfMsg( 'searchbutton' ), array( 'name' => 'fulltext' ) );
809 $out .= ' (' . wfMsgExt('searchmenu-help',array('parseinline') ) . ')';
810 $out .= Xml::closeElement( 'form' );
811 $t = Title::newFromText( $term );
812 if( $t != null )
813 $out .= wfMsgExt( 'searchmenu-prefix', array('parseinline'), $term );
814 return Xml::openElement( 'fieldset', array('id' => 'mw-searchoptions','style' => 'margin:0em;') ) .
815 Xml::element( 'legend', null, wfMsg('searchmenu-legend') ) .
816 $this->formHeader($term) . $out .
817 Xml::closeElement( 'fieldset' );
818 }
819 }
820
821 /**
822 * implements Special:Search - Run text & title search and display the output
823 * @ingroup SpecialPage
824 */
825 class SpecialSearchOld {
826
827 /**
828 * Set up basic search parameters from the request and user settings.
829 * Typically you'll pass $wgRequest and $wgUser.
830 *
831 * @param WebRequest $request
832 * @param User $user
833 * @public
834 */
835 function __construct( &$request, &$user ) {
836 list( $this->limit, $this->offset ) = $request->getLimitOffset( 20, 'searchlimit' );
837
838 $this->namespaces = $this->powerSearch( $request );
839 if( empty( $this->namespaces ) ) {
840 $this->namespaces = SearchEngine::userNamespaces( $user );
841 }
842
843 $this->searchRedirects = $request->getcheck( 'redirs' ) ? true : false;
844 }
845
846 /**
847 * If an exact title match can be found, jump straight ahead to it.
848 * @param string $term
849 * @public
850 */
851 function goResult( $term ) {
852 global $wgOut;
853 global $wgGoToEdit;
854
855 $this->setupPage( $term );
856
857 # Try to go to page as entered.
858 $t = Title::newFromText( $term );
859
860 # If the string cannot be used to create a title
861 if( is_null( $t ) ){
862 return $this->showResults( $term );
863 }
864
865 # If there's an exact or very near match, jump right there.
866 $t = SearchEngine::getNearMatch( $term );
867 if( !is_null( $t ) ) {
868 $wgOut->redirect( $t->getFullURL() );
869 return;
870 }
871
872 # No match, generate an edit URL
873 $t = Title::newFromText( $term );
874 if( ! is_null( $t ) ) {
875 wfRunHooks( 'SpecialSearchNogomatch', array( &$t ) );
876 # If the feature is enabled, go straight to the edit page
877 if ( $wgGoToEdit ) {
878 $wgOut->redirect( $t->getFullURL( 'action=edit' ) );
879 return;
880 }
881 }
882
883 $wgOut->wrapWikiMsg( "==$1==\n", 'notitlematches' );
884 if( $t->quickUserCan( 'create' ) && $t->quickUserCan( 'edit' ) ) {
885 $wgOut->addWikiMsg( 'noexactmatch', wfEscapeWikiText( $term ) );
886 } else {
887 $wgOut->addWikiMsg( 'noexactmatch-nocreate', wfEscapeWikiText( $term ) );
888 }
889
890 return $this->showResults( $term );
891 }
892
893 /**
894 * @param string $term
895 * @public
896 */
897 function showResults( $term ) {
898 wfProfileIn( __METHOD__ );
899 global $wgOut, $wgUser;
900 $sk = $wgUser->getSkin();
901
902 $this->setupPage( $term );
903
904 $wgOut->addWikiMsg( 'searchresulttext' );
905
906 if( '' === trim( $term ) ) {
907 // Empty query -- straight view of search form
908 $wgOut->setSubtitle( '' );
909 $wgOut->addHTML( $this->powerSearchBox( $term ) );
910 $wgOut->addHTML( $this->powerSearchFocus() );
911 wfProfileOut( __METHOD__ );
912 return;
913 }
914
915 global $wgDisableTextSearch;
916 if ( $wgDisableTextSearch ) {
917 global $wgSearchForwardUrl;
918 if( $wgSearchForwardUrl ) {
919 $url = str_replace( '$1', urlencode( $term ), $wgSearchForwardUrl );
920 $wgOut->redirect( $url );
921 return;
922 }
923 global $wgInputEncoding;
924 $wgOut->addHTML(
925 Xml::openElement( 'fieldset' ) .
926 Xml::element( 'legend', null, wfMsg( 'search-external' ) ) .
927 Xml::element( 'p', array( 'class' => 'mw-searchdisabled' ), wfMsg( 'searchdisabled' ) ) .
928 wfMsg( 'googlesearch',
929 htmlspecialchars( $term ),
930 htmlspecialchars( $wgInputEncoding ),
931 htmlspecialchars( wfMsg( 'searchbutton' ) )
932 ) .
933 Xml::closeElement( 'fieldset' )
934 );
935 wfProfileOut( __METHOD__ );
936 return;
937 }
938
939 $wgOut->addHTML( $this->shortDialog( $term ) );
940
941 $search = SearchEngine::create();
942 $search->setLimitOffset( $this->limit, $this->offset );
943 $search->setNamespaces( $this->namespaces );
944 $search->showRedirects = $this->searchRedirects;
945 $rewritten = $search->replacePrefixes($term);
946
947 $titleMatches = $search->searchTitle( $rewritten );
948
949 // Sometimes the search engine knows there are too many hits
950 if ($titleMatches instanceof SearchResultTooMany) {
951 $wgOut->addWikiText( '==' . wfMsg( 'toomanymatches' ) . "==\n" );
952 $wgOut->addHTML( $this->powerSearchBox( $term ) );
953 $wgOut->addHTML( $this->powerSearchFocus() );
954 wfProfileOut( __METHOD__ );
955 return;
956 }
957
958 $textMatches = $search->searchText( $rewritten );
959
960 // did you mean... suggestions
961 if($textMatches && $textMatches->hasSuggestion()){
962 $st = SpecialPage::getTitleFor( 'Search' );
963 $stParams = wfArrayToCGI( array(
964 'search' => $textMatches->getSuggestionQuery(),
965 'fulltext' => wfMsg('search')),
966 $this->powerSearchOptions());
967
968 $suggestLink = '<a href="'.$st->escapeLocalURL($stParams).'">'.
969 $textMatches->getSuggestionSnippet().'</a>';
970
971 $wgOut->addHTML('<div class="searchdidyoumean">'.wfMsg('search-suggest',$suggestLink).'</div>');
972 }
973
974 // show number of results
975 $num = ( $titleMatches ? $titleMatches->numRows() : 0 )
976 + ( $textMatches ? $textMatches->numRows() : 0);
977 $totalNum = 0;
978 if($titleMatches && !is_null($titleMatches->getTotalHits()))
979 $totalNum += $titleMatches->getTotalHits();
980 if($textMatches && !is_null($textMatches->getTotalHits()))
981 $totalNum += $textMatches->getTotalHits();
982 if ( $num > 0 ) {
983 if ( $totalNum > 0 ){
984 $top = wfMsgExt('showingresultstotal', array( 'parseinline' ),
985 $this->offset+1, $this->offset+$num, $totalNum, $num );
986 } elseif ( $num >= $this->limit ) {
987 $top = wfShowingResults( $this->offset, $this->limit );
988 } else {
989 $top = wfShowingResultsNum( $this->offset, $this->limit, $num );
990 }
991 $wgOut->addHTML( "<p class='mw-search-numberresults'>{$top}</p>\n" );
992 }
993
994 // prev/next links
995 if( $num || $this->offset ) {
996 $prevnext = wfViewPrevNext( $this->offset, $this->limit,
997 SpecialPage::getTitleFor( 'Search' ),
998 wfArrayToCGI(
999 $this->powerSearchOptions(),
1000 array( 'search' => $term ) ),
1001 ($num < $this->limit) );
1002 $wgOut->addHTML( "<p class='mw-search-pager-top'>{$prevnext}</p>\n" );
1003 wfRunHooks( 'SpecialSearchResults', array( $term, &$titleMatches, &$textMatches ) );
1004 } else {
1005 wfRunHooks( 'SpecialSearchNoResults', array( $term ) );
1006 }
1007
1008 if( $titleMatches ) {
1009 if( $titleMatches->numRows() ) {
1010 $wgOut->wrapWikiMsg( "==$1==\n", 'titlematches' );
1011 $wgOut->addHTML( $this->showMatches( $titleMatches ) );
1012 }
1013 $titleMatches->free();
1014 }
1015
1016 if( $textMatches ) {
1017 // output appropriate heading
1018 if( $textMatches->numRows() ) {
1019 if($titleMatches)
1020 $wgOut->wrapWikiMsg( "==$1==\n", 'textmatches' );
1021 else // if no title matches the heading is redundant
1022 $wgOut->addHTML("<hr/>");
1023 } elseif( $num == 0 ) {
1024 # Don't show the 'no text matches' if we received title matches
1025 $wgOut->wrapWikiMsg( "==$1==\n", 'notextmatches' );
1026 }
1027 // show interwiki results if any
1028 if( $textMatches->hasInterwikiResults() )
1029 $wgOut->addHTML( $this->showInterwiki( $textMatches->getInterwikiResults(), $term ));
1030 // show results
1031 if( $textMatches->numRows() )
1032 $wgOut->addHTML( $this->showMatches( $textMatches ) );
1033
1034 $textMatches->free();
1035 }
1036
1037 if ( $num == 0 ) {
1038 $wgOut->addWikiMsg( 'nonefound' );
1039 }
1040 if( $num || $this->offset ) {
1041 $wgOut->addHTML( "<p class='mw-search-pager-bottom'>{$prevnext}</p>\n" );
1042 }
1043 $wgOut->addHTML( $this->powerSearchBox( $term ) );
1044 wfProfileOut( __METHOD__ );
1045 }
1046
1047 #------------------------------------------------------------------
1048 # Private methods below this line
1049
1050 /**
1051 *
1052 */
1053 function setupPage( $term ) {
1054 global $wgOut;
1055 if( !empty( $term ) ){
1056 $wgOut->setPageTitle( wfMsg( 'searchresults') );
1057 $wgOut->setHTMLTitle( wfMsg( 'pagetitle', wfMsg( 'searchresults-title', $term) ) );
1058 }
1059 $subtitlemsg = ( Title::newFromText( $term ) ? 'searchsubtitle' : 'searchsubtitleinvalid' );
1060 $wgOut->setSubtitle( $wgOut->parse( wfMsg( $subtitlemsg, wfEscapeWikiText($term) ) ) );
1061 $wgOut->setArticleRelated( false );
1062 $wgOut->setRobotPolicy( 'noindex,nofollow' );
1063 }
1064
1065 /**
1066 * Extract "power search" namespace settings from the request object,
1067 * returning a list of index numbers to search.
1068 *
1069 * @param WebRequest $request
1070 * @return array
1071 * @private
1072 */
1073 function powerSearch( &$request ) {
1074 $arr = array();
1075 foreach( SearchEngine::searchableNamespaces() as $ns => $name ) {
1076 if( $request->getCheck( 'ns' . $ns ) ) {
1077 $arr[] = $ns;
1078 }
1079 }
1080 return $arr;
1081 }
1082
1083 /**
1084 * Reconstruct the 'power search' options for links
1085 * @return array
1086 * @private
1087 */
1088 function powerSearchOptions() {
1089 $opt = array();
1090 foreach( $this->namespaces as $n ) {
1091 $opt['ns' . $n] = 1;
1092 }
1093 $opt['redirs'] = $this->searchRedirects ? 1 : 0;
1094 return $opt;
1095 }
1096
1097 /**
1098 * Show whole set of results
1099 *
1100 * @param SearchResultSet $matches
1101 */
1102 function showMatches( &$matches ) {
1103 wfProfileIn( __METHOD__ );
1104
1105 global $wgContLang;
1106 $terms = $wgContLang->convertForSearchResult( $matches->termMatches() );
1107
1108 $out = "";
1109
1110 $infoLine = $matches->getInfo();
1111 if( !is_null($infoLine) )
1112 $out .= "\n<!-- {$infoLine} -->\n";
1113
1114
1115 $off = $this->offset + 1;
1116 $out .= "<ul class='mw-search-results'>\n";
1117
1118 while( $result = $matches->next() ) {
1119 $out .= $this->showHit( $result, $terms );
1120 }
1121 $out .= "</ul>\n";
1122
1123 // convert the whole thing to desired language variant
1124 global $wgContLang;
1125 $out = $wgContLang->convert( $out );
1126 wfProfileOut( __METHOD__ );
1127 return $out;
1128 }
1129
1130 /**
1131 * Format a single hit result
1132 * @param SearchResult $result
1133 * @param array $terms terms to highlight
1134 */
1135 function showHit( $result, $terms ) {
1136 wfProfileIn( __METHOD__ );
1137 global $wgUser, $wgContLang, $wgLang;
1138
1139 if( $result->isBrokenTitle() ) {
1140 wfProfileOut( __METHOD__ );
1141 return "<!-- Broken link in search result -->\n";
1142 }
1143
1144 $t = $result->getTitle();
1145 $sk = $wgUser->getSkin();
1146
1147 $link = $sk->makeKnownLinkObj( $t, $result->getTitleSnippet($terms));
1148
1149 //If page content is not readable, just return the title.
1150 //This is not quite safe, but better than showing excerpts from non-readable pages
1151 //Note that hiding the entry entirely would screw up paging.
1152 if (!$t->userCanRead()) {
1153 wfProfileOut( __METHOD__ );
1154 return "<li>{$link}</li>\n";
1155 }
1156
1157 // If the page doesn't *exist*... our search index is out of date.
1158 // The least confusing at this point is to drop the result.
1159 // You may get less results, but... oh well. :P
1160 if( $result->isMissingRevision() ) {
1161 wfProfileOut( __METHOD__ );
1162 return "<!-- missing page " .
1163 htmlspecialchars( $t->getPrefixedText() ) . "-->\n";
1164 }
1165
1166 // format redirects / relevant sections
1167 $redirectTitle = $result->getRedirectTitle();
1168 $redirectText = $result->getRedirectSnippet($terms);
1169 $sectionTitle = $result->getSectionTitle();
1170 $sectionText = $result->getSectionSnippet($terms);
1171 $redirect = '';
1172 if( !is_null($redirectTitle) )
1173 $redirect = "<span class='searchalttitle'>"
1174 .wfMsg('search-redirect',$sk->makeKnownLinkObj( $redirectTitle, $redirectText))
1175 ."</span>";
1176 $section = '';
1177 if( !is_null($sectionTitle) )
1178 $section = "<span class='searchalttitle'>"
1179 .wfMsg('search-section', $sk->makeKnownLinkObj( $sectionTitle, $sectionText))
1180 ."</span>";
1181
1182 // format text extract
1183 $extract = "<div class='searchresult'>".$result->getTextSnippet($terms)."</div>";
1184
1185 // format score
1186 if( is_null( $result->getScore() ) ) {
1187 // Search engine doesn't report scoring info
1188 $score = '';
1189 } else {
1190 $percent = sprintf( '%2.1f', $result->getScore() * 100 );
1191 $score = wfMsg( 'search-result-score', $wgLang->formatNum( $percent ) )
1192 . ' - ';
1193 }
1194
1195 // format description
1196 $byteSize = $result->getByteSize();
1197 $wordCount = $result->getWordCount();
1198 $timestamp = $result->getTimestamp();
1199 $size = wfMsgExt( 'search-result-size', array( 'parsemag', 'escape' ),
1200 $sk->formatSize( $byteSize ),
1201 $wordCount );
1202 $date = $wgLang->timeanddate( $timestamp );
1203
1204 // link to related articles if supported
1205 $related = '';
1206 if( $result->hasRelated() ){
1207 $st = SpecialPage::getTitleFor( 'Search' );
1208 $stParams = wfArrayToCGI( $this->powerSearchOptions(),
1209 array('search' => wfMsgForContent('searchrelated').':'.$t->getPrefixedText(),
1210 'fulltext' => wfMsg('search') ));
1211
1212 $related = ' -- <a href="'.$st->escapeLocalURL($stParams).'">'.
1213 wfMsg('search-relatedarticle').'</a>';
1214 }
1215
1216 // Include a thumbnail for media files...
1217 if( $t->getNamespace() == NS_IMAGE ) {
1218 $img = wfFindFile( $t );
1219 if( $img ) {
1220 $thumb = $img->transform( array( 'width' => 120, 'height' => 120 ) );
1221 if( $thumb ) {
1222 $desc = $img->getShortDesc();
1223 wfProfileOut( __METHOD__ );
1224 // Ugly table. :D
1225 // Float doesn't seem to interact well with the bullets.
1226 // Table messes up vertical alignment of the bullet, but I'm
1227 // not sure what more I can do about that. :(
1228 return "<li>" .
1229 '<table class="searchResultImage">' .
1230 '<tr>' .
1231 '<td width="120" align="center">' .
1232 $thumb->toHtml( array( 'desc-link' => true ) ) .
1233 '</td>' .
1234 '<td valign="top">' .
1235 $link .
1236 $extract .
1237 "<div class='mw-search-result-data'>{$score}{$desc} - {$date}{$related}</div>" .
1238 '</td>' .
1239 '</tr>' .
1240 '</table>' .
1241 "</li>\n";
1242 }
1243 }
1244 }
1245
1246 wfProfileOut( __METHOD__ );
1247 return "<li>{$link} {$redirect} {$section} {$extract}\n" .
1248 "<div class='mw-search-result-data'>{$score}{$size} - {$date}{$related}</div>" .
1249 "</li>\n";
1250
1251 }
1252
1253 /**
1254 * Show results from other wikis
1255 *
1256 * @param SearchResultSet $matches
1257 */
1258 function showInterwiki( &$matches, $query ) {
1259 wfProfileIn( __METHOD__ );
1260
1261 global $wgContLang;
1262 $terms = $wgContLang->convertForSearchResult( $matches->termMatches() );
1263
1264 $out = "<div id='mw-search-interwiki'><div id='mw-search-interwiki-caption'>".wfMsg('search-interwiki-caption')."</div>\n";
1265 $off = $this->offset + 1;
1266 $out .= "<ul start='{$off}' class='mw-search-iwresults'>\n";
1267
1268 // work out custom project captions
1269 $customCaptions = array();
1270 $customLines = explode("\n",wfMsg('search-interwiki-custom')); // format per line <iwprefix>:<caption>
1271 foreach($customLines as $line){
1272 $parts = explode(":",$line,2);
1273 if(count($parts) == 2) // validate line
1274 $customCaptions[$parts[0]] = $parts[1];
1275 }
1276
1277
1278 $prev = null;
1279 while( $result = $matches->next() ) {
1280 $out .= $this->showInterwikiHit( $result, $prev, $terms, $query, $customCaptions );
1281 $prev = $result->getInterwikiPrefix();
1282 }
1283 // FIXME: should support paging in a non-confusing way (not sure how though, maybe via ajax)..
1284 $out .= "</ul></div>\n";
1285
1286 // convert the whole thing to desired language variant
1287 global $wgContLang;
1288 $out = $wgContLang->convert( $out );
1289 wfProfileOut( __METHOD__ );
1290 return $out;
1291 }
1292
1293 /**
1294 * Show single interwiki link
1295 *
1296 * @param SearchResult $result
1297 * @param string $lastInterwiki
1298 * @param array $terms
1299 * @param string $query
1300 * @param array $customCaptions iw prefix -> caption
1301 */
1302 function showInterwikiHit( $result, $lastInterwiki, $terms, $query, $customCaptions) {
1303 wfProfileIn( __METHOD__ );
1304 global $wgUser, $wgContLang, $wgLang;
1305
1306 if( $result->isBrokenTitle() ) {
1307 wfProfileOut( __METHOD__ );
1308 return "<!-- Broken link in search result -->\n";
1309 }
1310
1311 $t = $result->getTitle();
1312 $sk = $wgUser->getSkin();
1313
1314 $link = $sk->makeKnownLinkObj( $t, $result->getTitleSnippet($terms));
1315
1316 // format redirect if any
1317 $redirectTitle = $result->getRedirectTitle();
1318 $redirectText = $result->getRedirectSnippet($terms);
1319 $redirect = '';
1320 if( !is_null($redirectTitle) )
1321 $redirect = "<span class='searchalttitle'>"
1322 .wfMsg('search-redirect',$sk->makeKnownLinkObj( $redirectTitle, $redirectText))
1323 ."</span>";
1324
1325 $out = "";
1326 // display project name
1327 if(is_null($lastInterwiki) || $lastInterwiki != $t->getInterwiki()){
1328 if( key_exists($t->getInterwiki(),$customCaptions) )
1329 // captions from 'search-interwiki-custom'
1330 $caption = $customCaptions[$t->getInterwiki()];
1331 else{
1332 // default is to show the hostname of the other wiki which might suck
1333 // if there are many wikis on one hostname
1334 $parsed = parse_url($t->getFullURL());
1335 $caption = wfMsg('search-interwiki-default', $parsed['host']);
1336 }
1337 // "more results" link (special page stuff could be localized, but we might not know target lang)
1338 $searchTitle = Title::newFromText($t->getInterwiki().":Special:Search");
1339 $searchLink = $sk->makeKnownLinkObj( $searchTitle, wfMsg('search-interwiki-more'),
1340 wfArrayToCGI(array('search' => $query, 'fulltext' => 'Search')));
1341 $out .= "</ul><div class='mw-search-interwiki-project'><span class='mw-search-interwiki-more'>{$searchLink}</span>{$caption}</div>\n<ul>";
1342 }
1343
1344 $out .= "<li>{$link} {$redirect}</li>\n";
1345 wfProfileOut( __METHOD__ );
1346 return $out;
1347 }
1348
1349
1350 /**
1351 * Generates the power search box at bottom of [[Special:Search]]
1352 * @param $term string: search term
1353 * @return $out string: HTML form
1354 */
1355 function powerSearchBox( $term ) {
1356 global $wgScript, $wgContLang;
1357
1358 $namespaces = SearchEngine::searchableNamespaces();
1359
1360 // group namespaces into rows according to subject; try not to make too
1361 // many assumptions about namespace numbering
1362 $rows = array();
1363 foreach( $namespaces as $ns => $name ) {
1364 $subj = Namespace::getSubject( $ns );
1365 if( !array_key_exists( $subj, $rows ) ) {
1366 $rows[$subj] = "";
1367 }
1368 $name = str_replace( '_', ' ', $name );
1369 if( '' == $name ) {
1370 $name = wfMsg( 'blanknamespace' );
1371 }
1372 $rows[$subj] .= Xml::openElement( 'td', array( 'style' => 'white-space: nowrap' ) ) .
1373 Xml::checkLabel( $name, "ns{$ns}", "mw-search-ns{$ns}", in_array( $ns, $this->namespaces ) ) .
1374 Xml::closeElement( 'td' ) . "\n";
1375 }
1376 $rows = array_values( $rows );
1377 $numRows = count( $rows );
1378
1379 // lay out namespaces in multiple floating two-column tables so they'll
1380 // be arranged nicely while still accommodating different screen widths
1381 $rowsPerTable = 3; // seems to look nice
1382
1383 // float to the right on RTL wikis
1384 $tableStyle = ( $wgContLang->isRTL() ?
1385 'float: right; margin: 0 0 1em 1em' :
1386 'float: left; margin: 0 1em 1em 0' );
1387
1388 $tables = "";
1389 for( $i = 0; $i < $numRows; $i += $rowsPerTable ) {
1390 $tables .= Xml::openElement( 'table', array( 'style' => $tableStyle ) );
1391 for( $j = $i; $j < $i + $rowsPerTable && $j < $numRows; $j++ ) {
1392 $tables .= "<tr>\n" . $rows[$j] . "</tr>";
1393 }
1394 $tables .= Xml::closeElement( 'table' ) . "\n";
1395 }
1396
1397 $redirect = Xml::check( 'redirs', $this->searchRedirects, array( 'value' => '1', 'id' => 'redirs' ) );
1398 $redirectLabel = Xml::label( wfMsg( 'powersearch-redir' ), 'redirs' );
1399 $searchField = Xml::input( 'search', 50, $term, array( 'type' => 'text', 'id' => 'powerSearchText' ) );
1400 $searchButton = Xml::submitButton( wfMsg( 'powersearch' ), array( 'name' => 'fulltext' ) ) . "\n";
1401 $searchTitle = SpecialPage::getTitleFor( 'Search' );
1402
1403 $out = Xml::openElement( 'form', array( 'id' => 'powersearch', 'method' => 'get', 'action' => $wgScript ) ) .
1404 Xml::fieldset( wfMsg( 'powersearch-legend' ),
1405 Xml::hidden( 'title', $searchTitle->getPrefixedText() ) . "\n" .
1406 "<p>" .
1407 wfMsgExt( 'powersearch-ns', array( 'parseinline' ) ) .
1408 "</p>\n" .
1409 $tables .
1410 "<hr style=\"clear: both\" />\n" .
1411 "<p>" .
1412 $redirect . " " . $redirectLabel .
1413 "</p>\n" .
1414 wfMsgExt( 'powersearch-field', array( 'parseinline' ) ) .
1415 "&nbsp;" .
1416 $searchField .
1417 "&nbsp;" .
1418 $searchButton ) .
1419 "</form>";
1420
1421 return $out;
1422 }
1423
1424 function powerSearchFocus() {
1425 global $wgJsMimeType;
1426 return "<script type=\"$wgJsMimeType\">" .
1427 "hookEvent(\"load\", function(){" .
1428 "document.getElementById('powerSearchText').focus();" .
1429 "});" .
1430 "</script>";
1431 }
1432
1433 function shortDialog($term) {
1434 global $wgScript;
1435
1436 $out = Xml::openElement( 'form', array(
1437 'id' => 'search',
1438 'method' => 'get',
1439 'action' => $wgScript
1440 ));
1441 $searchTitle = SpecialPage::getTitleFor( 'Search' );
1442 $out .= Xml::hidden( 'title', $searchTitle->getPrefixedText() );
1443 $out .= Xml::input( 'search', 50, $term, array( 'type' => 'text', 'id' => 'searchText' ) ) . ' ';
1444 foreach( SearchEngine::searchableNamespaces() as $ns => $name ) {
1445 if( in_array( $ns, $this->namespaces ) ) {
1446 $out .= Xml::hidden( "ns{$ns}", '1' );
1447 }
1448 }
1449 $out .= Xml::submitButton( wfMsg( 'searchbutton' ), array( 'name' => 'fulltext' ) );
1450 $out .= Xml::closeElement( 'form' );
1451
1452 return $out;
1453 }
1454 }