Cleanup next/prev links on special:search. Removed unused mPrefix.
[lhc/web/wiklou.git] / includes / specials / SpecialSearch.php
1 <?php
2 /**
3 * Implements Special:Search
4 *
5 * Copyright © 2004 Brion Vibber <brion@pobox.com>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 * http://www.gnu.org/copyleft/gpl.html
21 *
22 * @file
23 * @ingroup SpecialPage
24 */
25
26 /**
27 * implements Special:Search - Run text & title search and display the output
28 * @ingroup SpecialPage
29 */
30 class SpecialSearch extends SpecialPage {
31 /// Current search profile
32 protected $profile;
33
34 /// Search engine
35 protected $searchEngine;
36
37 const NAMESPACES_CURRENT = 'sense';
38
39 public function __construct() {
40 parent::__construct( 'Search' );
41 }
42
43 /**
44 * Entry point
45 *
46 * @param $par String or null
47 */
48 public function execute( $par ) {
49 global $wgRequest, $wgUser, $wgOut;
50
51 $this->setHeaders();
52 $this->outputHeader();
53 $wgOut->allowClickjacking();
54 $wgOut->addModuleStyles( 'mediawiki.special' );
55
56 // Strip underscores from title parameter; most of the time we'll want
57 // text form here. But don't strip underscores from actual text params!
58 $titleParam = str_replace( '_', ' ', $par );
59
60 // Fetch the search term
61 $search = str_replace( "\n", " ", $wgRequest->getText( 'search', $titleParam ) );
62
63 $this->load( $wgRequest, $wgUser );
64
65 if ( $wgRequest->getVal( 'fulltext' )
66 || !is_null( $wgRequest->getVal( 'offset' ) )
67 || !is_null( $wgRequest->getVal( 'searchx' ) ) )
68 {
69 $this->showResults( $search );
70 } else {
71 $this->goResult( $search );
72 }
73 }
74
75 /**
76 * Set up basic search parameters from the request and user settings.
77 * Typically you'll pass $wgRequest and $wgUser.
78 *
79 * @param $request WebRequest
80 * @param $user User
81 */
82 public function load( &$request, &$user ) {
83 list( $this->limit, $this->offset ) = $request->getLimitOffset( 20, 'searchlimit' );
84
85
86 # Extract manually requested namespaces
87 $nslist = $this->powerSearch( $request );
88 $this->profile = $profile = $request->getVal( 'profile', null );
89 $profiles = $this->getSearchProfiles();
90 if ( $profile === null) {
91 // BC with old request format
92 $this->profile = 'advanced';
93 if ( count( $nslist ) ) {
94 foreach( $profiles as $key => $data ) {
95 if ( $nslist === $data['namespaces'] && $key !== 'advanced') {
96 $this->profile = $key;
97 }
98 }
99 $this->namespaces = $nslist;
100 } else {
101 $this->namespaces = SearchEngine::userNamespaces( $user );
102 }
103 } elseif ( $profile === 'advanced' ) {
104 $this->namespaces = $nslist;
105 } else {
106 if ( isset( $profiles[$profile]['namespaces'] ) ) {
107 $this->namespaces = $profiles[$profile]['namespaces'];
108 } else {
109 // Unknown profile requested
110 $this->profile = 'default';
111 $this->namespaces = $profiles['default']['namespaces'];
112 }
113 }
114
115 // Redirects defaults to true, but we don't know whether it was ticked of or just missing
116 $default = $request->getBool( 'profile' ) ? 0 : 1;
117 $this->searchRedirects = $request->getBool( 'redirs', $default ) ? 1 : 0;
118 $this->sk = $user->getSkin();
119 $this->didYouMeanHtml = ''; # html of did you mean... link
120 $this->fulltext = $request->getVal('fulltext');
121 }
122
123 /**
124 * If an exact title match can be found, jump straight ahead to it.
125 *
126 * @param $term String
127 */
128 public function goResult( $term ) {
129 global $wgOut;
130 $this->setupPage( $term );
131 # Try to go to page as entered.
132 $t = Title::newFromText( $term );
133 # If the string cannot be used to create a title
134 if( is_null( $t ) ) {
135 return $this->showResults( $term );
136 }
137 # If there's an exact or very near match, jump right there.
138 $t = SearchEngine::getNearMatch( $term );
139
140 if ( !wfRunHooks( 'SpecialSearchGo', array( &$t, &$term ) ) ) {
141 # Hook requested termination
142 return;
143 }
144
145 if( !is_null( $t ) ) {
146 $wgOut->redirect( $t->getFullURL() );
147 return;
148 }
149 # No match, generate an edit URL
150 $t = Title::newFromText( $term );
151 if( !is_null( $t ) ) {
152 global $wgGoToEdit;
153 wfRunHooks( 'SpecialSearchNogomatch', array( &$t ) );
154 wfDebugLog( 'nogomatch', $t->getText(), false );
155
156 # If the feature is enabled, go straight to the edit page
157 if( $wgGoToEdit ) {
158 $wgOut->redirect( $t->getFullURL( array( 'action' => 'edit' ) ) );
159 return;
160 }
161 }
162 return $this->showResults( $term );
163 }
164
165 /**
166 * @param $term String
167 */
168 public function showResults( $term ) {
169 global $wgOut, $wgUser, $wgDisableTextSearch, $wgContLang, $wgScript;
170 wfProfileIn( __METHOD__ );
171
172 $sk = $wgUser->getSkin();
173
174 $search = $this->getSearchEngine();
175 $search->setLimitOffset( $this->limit, $this->offset );
176 $search->setNamespaces( $this->namespaces );
177 $search->showRedirects = $this->searchRedirects; // BC
178 $search->setFeatureData( 'list-redirects', $this->searchRedirects );
179 $term = $search->transformSearchTerm($term);
180
181 wfRunHooks( 'SpecialSearchSetupEngine', array( $this, $this->profile, $search ) );
182
183 $this->setupPage( $term );
184
185 if( $wgDisableTextSearch ) {
186 global $wgSearchForwardUrl;
187 if( $wgSearchForwardUrl ) {
188 $url = str_replace( '$1', urlencode( $term ), $wgSearchForwardUrl );
189 $wgOut->redirect( $url );
190 wfProfileOut( __METHOD__ );
191 return;
192 }
193 global $wgInputEncoding;
194 $wgOut->addHTML(
195 Xml::openElement( 'fieldset' ) .
196 Xml::element( 'legend', null, wfMsg( 'search-external' ) ) .
197 Xml::element( 'p', array( 'class' => 'mw-searchdisabled' ), wfMsg( 'searchdisabled' ) ) .
198 wfMsg( 'googlesearch',
199 htmlspecialchars( $term ),
200 htmlspecialchars( $wgInputEncoding ),
201 htmlspecialchars( wfMsg( 'searchbutton' ) )
202 ) .
203 Xml::closeElement( 'fieldset' )
204 );
205 wfProfileOut( __METHOD__ );
206 return;
207 }
208
209 $t = Title::newFromText( $term );
210
211 // fetch search results
212 $rewritten = $search->replacePrefixes($term);
213
214 $titleMatches = $search->searchTitle( $rewritten );
215 if( !($titleMatches instanceof SearchResultTooMany))
216 $textMatches = $search->searchText( $rewritten );
217
218 // did you mean... suggestions
219 if( $textMatches && $textMatches->hasSuggestion() ) {
220 $st = SpecialPage::getTitleFor( 'Search' );
221
222 # mirror Go/Search behaviour of original request ..
223 $didYouMeanParams = array( 'search' => $textMatches->getSuggestionQuery() );
224
225 if($this->fulltext != null)
226 $didYouMeanParams['fulltext'] = $this->fulltext;
227
228 $stParams = array_merge(
229 $didYouMeanParams,
230 $this->powerSearchOptions()
231 );
232
233 $suggestionSnippet = $textMatches->getSuggestionSnippet();
234
235 if( $suggestionSnippet == '' )
236 $suggestionSnippet = null;
237
238 $suggestLink = $sk->linkKnown(
239 $st,
240 $suggestionSnippet,
241 array(),
242 $stParams
243 );
244
245 $this->didYouMeanHtml = '<div class="searchdidyoumean">'.wfMsg('search-suggest',$suggestLink).'</div>';
246 }
247 // start rendering the page
248 $wgOut->addHtml(
249 Xml::openElement(
250 'form',
251 array(
252 'id' => ( $this->profile === 'advanced' ? 'powersearch' : 'search' ),
253 'method' => 'get',
254 'action' => $wgScript
255 )
256 )
257 );
258 $wgOut->addHtml(
259 Xml::openElement( 'table', array( 'id'=>'mw-search-top-table', 'border'=>0, 'cellpadding'=>0, 'cellspacing'=>0 ) ) .
260 Xml::openElement( 'tr' ) .
261 Xml::openElement( 'td' ) . "\n" .
262 $this->shortDialog( $term ) .
263 Xml::closeElement('td') .
264 Xml::closeElement('tr') .
265 Xml::closeElement('table')
266 );
267
268 // Sometimes the search engine knows there are too many hits
269 if( $titleMatches instanceof SearchResultTooMany ) {
270 $wgOut->wrapWikiMsg( "==$1==\n", 'toomanymatches' );
271 wfProfileOut( __METHOD__ );
272 return;
273 }
274
275 $filePrefix = $wgContLang->getFormattedNsText(NS_FILE).':';
276 if( trim( $term ) === '' || $filePrefix === trim( $term ) ) {
277 $wgOut->addHTML( $this->formHeader( $term, 0, 0 ) );
278 $wgOut->addHtml( $this->getProfileForm( $this->profile, $term ) );
279 $wgOut->addHTML( '</form>' );
280 // Empty query -- straight view of search form
281 wfProfileOut( __METHOD__ );
282 return;
283 }
284
285 // Get number of results
286 $titleMatchesNum = $titleMatches ? $titleMatches->numRows() : 0;
287 $textMatchesNum = $textMatches ? $textMatches->numRows() : 0;
288 // Total initial query matches (possible false positives)
289 $num = $titleMatchesNum + $textMatchesNum;
290
291 // Get total actual results (after second filtering, if any)
292 $numTitleMatches = $titleMatches && !is_null( $titleMatches->getTotalHits() ) ?
293 $titleMatches->getTotalHits() : $titleMatchesNum;
294 $numTextMatches = $textMatches && !is_null( $textMatches->getTotalHits() ) ?
295 $textMatches->getTotalHits() : $textMatchesNum;
296
297 // get total number of results if backend can calculate it
298 $totalRes = 0;
299 if($titleMatches && !is_null( $titleMatches->getTotalHits() ) )
300 $totalRes += $titleMatches->getTotalHits();
301 if($textMatches && !is_null( $textMatches->getTotalHits() ))
302 $totalRes += $textMatches->getTotalHits();
303
304 // show number of results and current offset
305 $wgOut->addHTML( $this->formHeader( $term, $num, $totalRes ) );
306 $wgOut->addHtml( $this->getProfileForm( $this->profile, $term ) );
307
308
309 $wgOut->addHtml( Xml::closeElement( 'form' ) );
310 $wgOut->addHtml( "<div class='searchresults'>" );
311
312 // prev/next links
313 if( $num || $this->offset ) {
314 // Show the create link ahead
315 $this->showCreateLink( $t );
316 $prevnext = wfViewPrevNext( $this->offset, $this->limit,
317 SpecialPage::getTitleFor( 'Search' ),
318 wfArrayToCGI( $this->powerSearchOptions(), array( 'search' => $term ) ),
319 max( $titleMatchesNum, $textMatchesNum ) < $this->limit
320 );
321 //$wgOut->addHTML( "<p class='mw-search-pager-top'>{$prevnext}</p>\n" );
322 wfRunHooks( 'SpecialSearchResults', array( $term, &$titleMatches, &$textMatches ) );
323 } else {
324 wfRunHooks( 'SpecialSearchNoResults', array( $term ) );
325 }
326
327 $wgOut->parserOptions()->setEditSection( false );
328 if( $titleMatches ) {
329 if( $numTitleMatches > 0 ) {
330 $wgOut->wrapWikiMsg( "==$1==\n", 'titlematches' );
331 $wgOut->addHTML( $this->showMatches( $titleMatches ) );
332 }
333 $titleMatches->free();
334 }
335 if( $textMatches ) {
336 // output appropriate heading
337 if( $numTextMatches > 0 && $numTitleMatches > 0 ) {
338 // if no title matches the heading is redundant
339 $wgOut->wrapWikiMsg( "==$1==\n", 'textmatches' );
340 } elseif( $totalRes == 0 ) {
341 # Don't show the 'no text matches' if we received title matches
342 # $wgOut->wrapWikiMsg( "==$1==\n", 'notextmatches' );
343 }
344 // show interwiki results if any
345 if( $textMatches->hasInterwikiResults() ) {
346 $wgOut->addHTML( $this->showInterwiki( $textMatches->getInterwikiResults(), $term ) );
347 }
348 // show results
349 if( $numTextMatches > 0 ) {
350 $wgOut->addHTML( $this->showMatches( $textMatches ) );
351 }
352
353 $textMatches->free();
354 }
355 if( $num === 0 ) {
356 $wgOut->wrapWikiMsg( "<p class=\"mw-search-nonefound\">\n$1</p>", array( 'search-nonefound', wfEscapeWikiText( $term ) ) );
357 $this->showCreateLink( $t );
358 }
359 $wgOut->addHtml( "</div>" );
360
361 if( $num || $this->offset ) {
362 $wgOut->addHTML( "<p class='mw-search-pager-bottom'>{$prevnext}</p>\n" );
363 }
364 wfProfileOut( __METHOD__ );
365 }
366
367 protected function showCreateLink( $t ) {
368 global $wgOut;
369
370 // show direct page/create link if applicable
371 $messageName = null;
372 if( !is_null($t) ) {
373 if( $t->isKnown() ) {
374 $messageName = 'searchmenu-exists';
375 } elseif( $t->userCan( 'create' ) ) {
376 $messageName = 'searchmenu-new';
377 } else {
378 $messageName = 'searchmenu-new-nocreate';
379 }
380 }
381 if( $messageName ) {
382 $wgOut->wrapWikiMsg( "<p class=\"mw-search-createlink\">\n$1</p>", array( $messageName, wfEscapeWikiText( $t->getPrefixedText() ) ) );
383 } else {
384 // preserve the paragraph for margins etc...
385 $wgOut->addHtml( '<p></p>' );
386 }
387 }
388
389 /**
390 *
391 */
392 protected function setupPage( $term ) {
393 global $wgOut;
394
395 # Should advanced UI be used?
396 $this->searchAdvanced = ($this->profile === 'advanced');
397 if( strval( $term ) !== '' ) {
398 $wgOut->setPageTitle( wfMsg( 'searchresults') );
399 $wgOut->setHTMLTitle( wfMsg( 'pagetitle', wfMsg( 'searchresults-title', $term ) ) );
400 }
401 // add javascript specific to special:search
402 $wgOut->addModules( 'mediawiki.legacy.search' );
403 $wgOut->addModules( 'mediawiki.special.search' );
404 }
405
406 /**
407 * Extract "power search" namespace settings from the request object,
408 * returning a list of index numbers to search.
409 *
410 * @param $request WebRequest
411 * @return Array
412 */
413 protected function powerSearch( &$request ) {
414 $arr = array();
415 foreach( SearchEngine::searchableNamespaces() as $ns => $name ) {
416 if( $request->getCheck( 'ns' . $ns ) ) {
417 $arr[] = $ns;
418 }
419 }
420
421 return $arr;
422 }
423
424 /**
425 * Reconstruct the 'power search' options for links
426 *
427 * @return Array
428 */
429 protected function powerSearchOptions() {
430 $opt = array();
431 $opt['redirs'] = $this->searchRedirects ? 1 : 0;
432 if( $this->profile !== 'advanced' ) {
433 $opt['profile'] = $this->profile;
434 } else {
435 foreach( $this->namespaces as $n ) {
436 $opt['ns' . $n] = 1;
437 }
438 }
439 return $opt;
440 }
441
442 /**
443 * Show whole set of results
444 *
445 * @param $matches SearchResultSet
446 */
447 protected function showMatches( &$matches ) {
448 global $wgContLang;
449 wfProfileIn( __METHOD__ );
450
451 $terms = $wgContLang->convertForSearchResult( $matches->termMatches() );
452
453 $out = "";
454 $infoLine = $matches->getInfo();
455 if( !is_null($infoLine) ) {
456 $out .= "\n<!-- {$infoLine} -->\n";
457 }
458 $out .= "<ul class='mw-search-results'>\n";
459 while( $result = $matches->next() ) {
460 $out .= $this->showHit( $result, $terms );
461 }
462 $out .= "</ul>\n";
463
464 // convert the whole thing to desired language variant
465 $out = $wgContLang->convert( $out );
466 wfProfileOut( __METHOD__ );
467 return $out;
468 }
469
470 /**
471 * Format a single hit result
472 *
473 * @param $result SearchResult
474 * @param $terms Array: terms to highlight
475 */
476 protected function showHit( $result, $terms ) {
477 global $wgLang, $wgUser;
478 wfProfileIn( __METHOD__ );
479
480 if( $result->isBrokenTitle() ) {
481 wfProfileOut( __METHOD__ );
482 return "<!-- Broken link in search result -->\n";
483 }
484
485 $sk = $wgUser->getSkin();
486 $t = $result->getTitle();
487
488 $titleSnippet = $result->getTitleSnippet($terms);
489
490 if( $titleSnippet == '' )
491 $titleSnippet = null;
492
493 $link_t = clone $t;
494
495 wfRunHooks( 'ShowSearchHitTitle',
496 array( &$link_t, &$titleSnippet, $result, $terms, $this ) );
497
498 $link = $this->sk->linkKnown(
499 $link_t,
500 $titleSnippet
501 );
502
503 //If page content is not readable, just return the title.
504 //This is not quite safe, but better than showing excerpts from non-readable pages
505 //Note that hiding the entry entirely would screw up paging.
506 if( !$t->userCanRead() ) {
507 wfProfileOut( __METHOD__ );
508 return "<li>{$link}</li>\n";
509 }
510
511 // If the page doesn't *exist*... our search index is out of date.
512 // The least confusing at this point is to drop the result.
513 // You may get less results, but... oh well. :P
514 if( $result->isMissingRevision() ) {
515 wfProfileOut( __METHOD__ );
516 return "<!-- missing page " . htmlspecialchars( $t->getPrefixedText() ) . "-->\n";
517 }
518
519 // format redirects / relevant sections
520 $redirectTitle = $result->getRedirectTitle();
521 $redirectText = $result->getRedirectSnippet($terms);
522 $sectionTitle = $result->getSectionTitle();
523 $sectionText = $result->getSectionSnippet($terms);
524 $redirect = '';
525
526 if( !is_null($redirectTitle) ) {
527 if( $redirectText == '' )
528 $redirectText = null;
529
530 $redirect = "<span class='searchalttitle'>" .
531 wfMsg(
532 'search-redirect',
533 $this->sk->linkKnown(
534 $redirectTitle,
535 $redirectText
536 )
537 ) .
538 "</span>";
539 }
540
541 $section = '';
542
543 if( !is_null($sectionTitle) ) {
544 if( $sectionText == '' )
545 $sectionText = null;
546
547 $section = "<span class='searchalttitle'>" .
548 wfMsg(
549 'search-section', $this->sk->linkKnown(
550 $sectionTitle,
551 $sectionText
552 )
553 ) .
554 "</span>";
555 }
556
557 // format text extract
558 $extract = "<div class='searchresult'>".$result->getTextSnippet($terms)."</div>";
559
560 // format score
561 if( is_null( $result->getScore() ) ) {
562 // Search engine doesn't report scoring info
563 $score = '';
564 } else {
565 $percent = sprintf( '%2.1f', $result->getScore() * 100 );
566 $score = wfMsg( 'search-result-score', $wgLang->formatNum( $percent ) )
567 . ' - ';
568 }
569
570 // format description
571 $byteSize = $result->getByteSize();
572 $wordCount = $result->getWordCount();
573 $timestamp = $result->getTimestamp();
574 $size = wfMsgExt(
575 'search-result-size',
576 array( 'parsemag', 'escape' ),
577 $this->sk->formatSize( $byteSize ),
578 $wgLang->formatNum( $wordCount )
579 );
580
581 if( $t->getNamespace() == NS_CATEGORY ) {
582 $cat = Category::newFromTitle( $t );
583 $size = wfMsgExt(
584 'search-result-category-size',
585 array( 'parsemag', 'escape' ),
586 $wgLang->formatNum( $cat->getPageCount() ),
587 $wgLang->formatNum( $cat->getSubcatCount() ),
588 $wgLang->formatNum( $cat->getFileCount() )
589 );
590 }
591
592 $date = $wgLang->timeanddate( $timestamp );
593
594 // link to related articles if supported
595 $related = '';
596 if( $result->hasRelated() ) {
597 $st = SpecialPage::getTitleFor( 'Search' );
598 $stParams = array_merge(
599 $this->powerSearchOptions(),
600 array(
601 'search' => wfMsgForContent( 'searchrelated' ) . ':' . $t->getPrefixedText(),
602 'fulltext' => wfMsg( 'search' )
603 )
604 );
605
606 $related = ' -- ' . $sk->linkKnown(
607 $st,
608 wfMsg('search-relatedarticle'),
609 array(),
610 $stParams
611 );
612 }
613
614 // Include a thumbnail for media files...
615 if( $t->getNamespace() == NS_FILE ) {
616 $img = wfFindFile( $t );
617 if( $img ) {
618 $thumb = $img->transform( array( 'width' => 120, 'height' => 120 ) );
619 if( $thumb ) {
620 $desc = wfMsg( 'parentheses', $img->getShortDesc() );
621 wfProfileOut( __METHOD__ );
622 // Float doesn't seem to interact well with the bullets.
623 // Table messes up vertical alignment of the bullets.
624 // Bullets are therefore disabled (didn't look great anyway).
625 return "<li>" .
626 '<table class="searchResultImage">' .
627 '<tr>' .
628 '<td width="120" align="center" valign="top">' .
629 $thumb->toHtml( array( 'desc-link' => true ) ) .
630 '</td>' .
631 '<td valign="top">' .
632 $link .
633 $extract .
634 "<div class='mw-search-result-data'>{$score}{$desc} - {$date}{$related}</div>" .
635 '</td>' .
636 '</tr>' .
637 '</table>' .
638 "</li>\n";
639 }
640 }
641 }
642
643 wfProfileOut( __METHOD__ );
644 return "<li><div class='mw-search-result-heading'>{$link} {$redirect} {$section}</div> {$extract}\n" .
645 "<div class='mw-search-result-data'>{$score}{$size} - {$date}{$related}</div>" .
646 "</li>\n";
647
648 }
649
650 /**
651 * Show results from other wikis
652 *
653 * @param $matches SearchResultSet
654 * @param $query String
655 */
656 protected function showInterwiki( &$matches, $query ) {
657 global $wgContLang;
658 wfProfileIn( __METHOD__ );
659 $terms = $wgContLang->convertForSearchResult( $matches->termMatches() );
660
661 $out = "<div id='mw-search-interwiki'><div id='mw-search-interwiki-caption'>".
662 wfMsg('search-interwiki-caption')."</div>\n";
663 $out .= "<ul class='mw-search-iwresults'>\n";
664
665 // work out custom project captions
666 $customCaptions = array();
667 $customLines = explode("\n",wfMsg('search-interwiki-custom')); // format per line <iwprefix>:<caption>
668 foreach($customLines as $line) {
669 $parts = explode(":",$line,2);
670 if(count($parts) == 2) // validate line
671 $customCaptions[$parts[0]] = $parts[1];
672 }
673
674 $prev = null;
675 while( $result = $matches->next() ) {
676 $out .= $this->showInterwikiHit( $result, $prev, $terms, $query, $customCaptions );
677 $prev = $result->getInterwikiPrefix();
678 }
679 // TODO: should support paging in a non-confusing way (not sure how though, maybe via ajax)..
680 $out .= "</ul></div>\n";
681
682 // convert the whole thing to desired language variant
683 $out = $wgContLang->convert( $out );
684 wfProfileOut( __METHOD__ );
685 return $out;
686 }
687
688 /**
689 * Show single interwiki link
690 *
691 * @param $result SearchResult
692 * @param $lastInterwiki String
693 * @param $terms Array
694 * @param $query String
695 * @param $customCaptions Array: iw prefix -> caption
696 */
697 protected function showInterwikiHit( $result, $lastInterwiki, $terms, $query, $customCaptions) {
698 wfProfileIn( __METHOD__ );
699
700 if( $result->isBrokenTitle() ) {
701 wfProfileOut( __METHOD__ );
702 return "<!-- Broken link in search result -->\n";
703 }
704
705 $t = $result->getTitle();
706
707 $titleSnippet = $result->getTitleSnippet($terms);
708
709 if( $titleSnippet == '' )
710 $titleSnippet = null;
711
712 $link = $this->sk->linkKnown(
713 $t,
714 $titleSnippet
715 );
716
717 // format redirect if any
718 $redirectTitle = $result->getRedirectTitle();
719 $redirectText = $result->getRedirectSnippet($terms);
720 $redirect = '';
721 if( !is_null($redirectTitle) ) {
722 if( $redirectText == '' )
723 $redirectText = null;
724
725 $redirect = "<span class='searchalttitle'>" .
726 wfMsg(
727 'search-redirect',
728 $this->sk->linkKnown(
729 $redirectTitle,
730 $redirectText
731 )
732 ) .
733 "</span>";
734 }
735
736 $out = "";
737 // display project name
738 if(is_null($lastInterwiki) || $lastInterwiki != $t->getInterwiki()) {
739 if( key_exists($t->getInterwiki(),$customCaptions) )
740 // captions from 'search-interwiki-custom'
741 $caption = $customCaptions[$t->getInterwiki()];
742 else{
743 // default is to show the hostname of the other wiki which might suck
744 // if there are many wikis on one hostname
745 $parsed = parse_url($t->getFullURL());
746 $caption = wfMsg('search-interwiki-default', $parsed['host']);
747 }
748 // "more results" link (special page stuff could be localized, but we might not know target lang)
749 $searchTitle = Title::newFromText($t->getInterwiki().":Special:Search");
750 $searchLink = $this->sk->linkKnown(
751 $searchTitle,
752 wfMsg('search-interwiki-more'),
753 array(),
754 array(
755 'search' => $query,
756 'fulltext' => 'Search'
757 )
758 );
759 $out .= "</ul><div class='mw-search-interwiki-project'><span class='mw-search-interwiki-more'>
760 {$searchLink}</span>{$caption}</div>\n<ul>";
761 }
762
763 $out .= "<li>{$link} {$redirect}</li>\n";
764 wfProfileOut( __METHOD__ );
765 return $out;
766 }
767
768 protected function getProfileForm( $profile, $term ) {
769 // Hidden stuff
770 $opts = array();
771 $opts['redirs'] = $this->searchRedirects;
772 $opts['profile'] = $this->profile;
773
774 if ( $profile === 'advanced' ) {
775 return $this->powerSearchBox( $term, $opts );
776 } else {
777 $form = '';
778 wfRunHooks( 'SpecialSearchProfileForm', array( $this, &$form, $profile, $term, $opts ) );
779 return $form;
780 }
781 }
782
783 /**
784 * Generates the power search box at [[Special:Search]]
785 *
786 * @param $term String: search term
787 * @return String: HTML form
788 */
789 protected function powerSearchBox( $term, $opts ) {
790 // Groups namespaces into rows according to subject
791 $rows = array();
792 foreach( SearchEngine::searchableNamespaces() as $namespace => $name ) {
793 $subject = MWNamespace::getSubject( $namespace );
794 if( !array_key_exists( $subject, $rows ) ) {
795 $rows[$subject] = "";
796 }
797 $name = str_replace( '_', ' ', $name );
798 if( $name == '' ) {
799 $name = wfMsg( 'blanknamespace' );
800 }
801 $rows[$subject] .=
802 Xml::openElement(
803 'td', array( 'style' => 'white-space: nowrap' )
804 ) .
805 Xml::checkLabel(
806 $name,
807 "ns{$namespace}",
808 "mw-search-ns{$namespace}",
809 in_array( $namespace, $this->namespaces )
810 ) .
811 Xml::closeElement( 'td' );
812 }
813 $rows = array_values( $rows );
814 $numRows = count( $rows );
815
816 // Lays out namespaces in multiple floating two-column tables so they'll
817 // be arranged nicely while still accommodating different screen widths
818 $namespaceTables = '';
819 for( $i = 0; $i < $numRows; $i += 4 ) {
820 $namespaceTables .= Xml::openElement(
821 'table',
822 array( 'cellpadding' => 0, 'cellspacing' => 0, 'border' => 0 )
823 );
824 for( $j = $i; $j < $i + 4 && $j < $numRows; $j++ ) {
825 $namespaceTables .= Xml::tags( 'tr', null, $rows[$j] );
826 }
827 $namespaceTables .= Xml::closeElement( 'table' );
828 }
829 // Show redirects check only if backend supports it
830 $redirects = '';
831 if( $this->getSearchEngine()->supports( 'list-redirects' ) ) {
832 $redirects =
833 Xml::checkLabel( wfMsg( 'powersearch-redir' ), 'redirs', 'redirs', $this->searchRedirects );
834 }
835
836 $hidden = '';
837 unset( $opts['redirs'] );
838 foreach( $opts as $key => $value ) {
839 $hidden .= Html::hidden( $key, $value );
840 }
841 // Return final output
842 return
843 Xml::openElement(
844 'fieldset',
845 array( 'id' => 'mw-searchoptions', 'style' => 'margin:0em;' )
846 ) .
847 Xml::element( 'legend', null, wfMsg('powersearch-legend') ) .
848 Xml::tags( 'h4', null, wfMsgExt( 'powersearch-ns', array( 'parseinline' ) ) ) .
849 Xml::tags(
850 'div',
851 array( 'id' => 'mw-search-togglebox' ),
852 Xml::label( wfMsg( 'powersearch-togglelabel' ), 'mw-search-togglelabel' ) .
853 Xml::element(
854 'input',
855 array(
856 'type'=>'button',
857 'id' => 'mw-search-toggleall',
858 'onclick' => 'mwToggleSearchCheckboxes("all");',
859 'value' => wfMsg( 'powersearch-toggleall' )
860 )
861 ) .
862 Xml::element(
863 'input',
864 array(
865 'type'=>'button',
866 'id' => 'mw-search-togglenone',
867 'onclick' => 'mwToggleSearchCheckboxes("none");',
868 'value' => wfMsg( 'powersearch-togglenone' )
869 )
870 )
871 ) .
872 Xml::element( 'div', array( 'class' => 'divider' ), '', false ) .
873 $namespaceTables .
874 Xml::element( 'div', array( 'class' => 'divider' ), '', false ) .
875 $redirects . $hidden .
876 Xml::closeElement( 'fieldset' );
877 }
878
879 protected function getSearchProfiles() {
880 // Builds list of Search Types (profiles)
881 $nsAllSet = array_keys( SearchEngine::searchableNamespaces() );
882
883 $profiles = array(
884 'default' => array(
885 'message' => 'searchprofile-articles',
886 'tooltip' => 'searchprofile-articles-tooltip',
887 'namespaces' => SearchEngine::defaultNamespaces(),
888 'namespace-messages' => SearchEngine::namespacesAsText(
889 SearchEngine::defaultNamespaces()
890 ),
891 ),
892 'images' => array(
893 'message' => 'searchprofile-images',
894 'tooltip' => 'searchprofile-images-tooltip',
895 'namespaces' => array( NS_FILE ),
896 ),
897 'help' => array(
898 'message' => 'searchprofile-project',
899 'tooltip' => 'searchprofile-project-tooltip',
900 'namespaces' => SearchEngine::helpNamespaces(),
901 'namespace-messages' => SearchEngine::namespacesAsText(
902 SearchEngine::helpNamespaces()
903 ),
904 ),
905 'all' => array(
906 'message' => 'searchprofile-everything',
907 'tooltip' => 'searchprofile-everything-tooltip',
908 'namespaces' => $nsAllSet,
909 ),
910 'advanced' => array(
911 'message' => 'searchprofile-advanced',
912 'tooltip' => 'searchprofile-advanced-tooltip',
913 'namespaces' => self::NAMESPACES_CURRENT,
914 )
915 );
916
917 wfRunHooks( 'SpecialSearchProfiles', array( &$profiles ) );
918
919 foreach( $profiles as &$data ) {
920 if ( !is_array( $data['namespaces'] ) ) continue;
921 sort( $data['namespaces'] );
922 }
923
924 return $profiles;
925 }
926
927 protected function formHeader( $term, $resultsShown, $totalNum ) {
928 global $wgLang;
929
930 $out = Xml::openElement('div', array( 'class' => 'mw-search-formheader' ) );
931
932 $bareterm = $term;
933 if( $this->startsWithImage( $term ) ) {
934 // Deletes prefixes
935 $bareterm = substr( $term, strpos( $term, ':' ) + 1 );
936 }
937
938 $profiles = $this->getSearchProfiles();
939
940 // Outputs XML for Search Types
941 $out .= Xml::openElement( 'div', array( 'class' => 'search-types' ) );
942 $out .= Xml::openElement( 'ul' );
943 foreach ( $profiles as $id => $profile ) {
944 if ( !isset( $profile['parameters'] ) ) {
945 $profile['parameters'] = array();
946 }
947 $profile['parameters']['profile'] = $id;
948
949 $tooltipParam = isset( $profile['namespace-messages'] ) ?
950 $wgLang->commaList( $profile['namespace-messages'] ) : null;
951 $out .= Xml::tags(
952 'li',
953 array(
954 'class' => $this->profile === $id ? 'current' : 'normal'
955 ),
956 $this->makeSearchLink(
957 $bareterm,
958 array(),
959 wfMsg( $profile['message'] ),
960 wfMsg( $profile['tooltip'], $tooltipParam ),
961 $profile['parameters']
962 )
963 );
964 }
965 $out .= Xml::closeElement( 'ul' );
966 $out .= Xml::closeElement('div') ;
967
968 // Results-info
969 if ( $resultsShown > 0 ) {
970 if ( $totalNum > 0 ){
971 $top = wfMsgExt( 'showingresultsheader', array( 'parseinline' ),
972 $wgLang->formatNum( $this->offset + 1 ),
973 $wgLang->formatNum( $this->offset + $resultsShown ),
974 $wgLang->formatNum( $totalNum ),
975 wfEscapeWikiText( $term ),
976 $wgLang->formatNum( $resultsShown )
977 );
978 } elseif ( $resultsShown >= $this->limit ) {
979 $top = wfShowingResults( $this->offset, $this->limit );
980 } else {
981 $top = wfShowingResultsNum( $this->offset, $this->limit, $resultsShown );
982 }
983 $out .= Xml::tags( 'div', array( 'class' => 'results-info' ),
984 Xml::tags( 'ul', null, Xml::tags( 'li', null, $top ) )
985 );
986 }
987
988 $out .= Xml::element( 'div', array( 'style' => 'clear:both' ), '', false );
989 $out .= Xml::closeElement('div');
990
991 return $out;
992 }
993
994 protected function shortDialog( $term ) {
995 $out = Html::hidden( 'title', $this->getTitle()->getPrefixedText() ) . "\n";
996 // Term box
997 $out .= Html::input( 'search', $term, 'search', array(
998 'id' => $this->profile === 'advanced' ? 'powerSearchText' : 'searchText',
999 'size' => '50',
1000 'autofocus'
1001 ) ) . "\n";
1002 $out .= Html::hidden( 'fulltext', 'Search' ) . "\n";
1003 $out .= Xml::submitButton( wfMsg( 'searchbutton' ) ) . "\n";
1004 return $out . $this->didYouMeanHtml;
1005 }
1006
1007 /**
1008 * Make a search link with some target namespaces
1009 *
1010 * @param $term String
1011 * @param $namespaces Array ignored
1012 * @param $label String: link's text
1013 * @param $tooltip String: link's tooltip
1014 * @param $params Array: query string parameters
1015 * @return String: HTML fragment
1016 */
1017 protected function makeSearchLink( $term, $namespaces, $label, $tooltip, $params = array() ) {
1018 $opt = $params;
1019 foreach( $namespaces as $n ) {
1020 $opt['ns' . $n] = 1;
1021 }
1022 $opt['redirs'] = $this->searchRedirects;
1023
1024 $stParams = array_merge(
1025 array(
1026 'search' => $term,
1027 'fulltext' => wfMsg( 'search' )
1028 ),
1029 $opt
1030 );
1031
1032 return Xml::element(
1033 'a',
1034 array(
1035 'href' => $this->getTitle()->getLocalURL( $stParams ),
1036 'title' => $tooltip,
1037 'onmousedown' => 'mwSearchHeaderClick(this);',
1038 'onkeydown' => 'mwSearchHeaderClick(this);'),
1039 $label
1040 );
1041 }
1042
1043 /**
1044 * Check if query starts with image: prefix
1045 *
1046 * @param $term String: the string to check
1047 * @return Boolean
1048 */
1049 protected function startsWithImage( $term ) {
1050 global $wgContLang;
1051
1052 $p = explode( ':', $term );
1053 if( count( $p ) > 1 ) {
1054 return $wgContLang->getNsIndex( $p[0] ) == NS_FILE;
1055 }
1056 return false;
1057 }
1058
1059 /**
1060 * Check if query starts with all: prefix
1061 *
1062 * @param $term String: the string to check
1063 * @return Boolean
1064 */
1065 protected function startsWithAll( $term ) {
1066
1067 $allkeyword = wfMsgForContent('searchall');
1068
1069 $p = explode( ':', $term );
1070 if( count( $p ) > 1 ) {
1071 return $p[0] == $allkeyword;
1072 }
1073 return false;
1074 }
1075
1076 /**
1077 * @since 1.18
1078 */
1079 public function getSearchEngine() {
1080 if ( $this->searchEngine === null ) {
1081 $this->searchEngine = SearchEngine::create();
1082 }
1083 return $this->searchEngine;
1084 }
1085 }