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