How to add Google search to your MediaWiki Search Results Page

From Meta, a Wikimedia project coordination wiki

If nothing was found on Wiki with standard search, you may want to offer Google search to your visitors.

Instruction[edit]

Adding a Google Search bar to MediaWiki is fairly simple. However, there seems to be little documentation for how to do it. So I thought I'd write up a quick, simple guide to implement a basic Google Search bar in the Search Results page.

Common part[edit]

The code for the Search Results page is generated by a PHP file called SpecialSearch.php. In SpecialSearch.php there is a function showResults( $term ) which formats and outputs all the search results from a query. Currently this function starts on Line 114 or so.

You can insert the following code in different locations in this function to cause the Googlesearch bar to show up where you would like it to appear. I will describe how to make it display below all of the search results, or lack thereof, but before the Power Search options.

Although MediaWiki includes a default Google Search bar implementation, the Googlesearchheader we are using here is probably still undefined. We need to define it before we run any searches. Define this header by visiting your page MediaWiki:Googlesearchheader:

http://YourWikiAddress/index.php/MediaWiki:Googlesearchheader

Simply edit the page like you would any new wiki article. I just made the header wikitext:

== Try Google Search: ==

If you wish to customize the way the actual Search bar displays you can do so by going to your page MediaWiki:Googlesearch:

http://YourWikiAddress/index.php/MediaWiki:Googlesearch

Changing the code there as you wish. For example paste the Google AdSense Search Code here.

Note: This page will probably look quite garbled as it is meant to contain straight HTML and the Wiki will attempt to display it as Wikitext. Do not worry if the page output looks like garbage.

Once you have created the Googlesearchheader page and updated the Googlesearch page (if necessary), you can now go ahead and modify include/SpecialSearch.php file. Then upon searching for a term that doesn't match any specific article title in the wiki, you should then receive the Search Results page with a Google Search bar included.

Version 1.13.0[edit]

The file is placed as www/includes/specials/SpecialSearch.php.

Version 1.12.0[edit]

Solution of $wgShowGoogleSearch shows the Google search page, but hides the default text search. I have put new variable into LocalSetting.php

$wgShowGoogleSearch = true;

and modified include/SpecialSearch.php to load this variable and inserted few new lines enabling showing the Google search just by LocalSetting.php. All the modifications are made in the showResults( $term ) function. You may want to add this code at the very end of this function to get better formated output.

  $wgOut->addHTML( $this->powerSearchBox( $term ) );
+ global $wgShowGoogleSearch;
+ if ( $wgShowGoogleSearch ) {
+     $wgOut->addWikiText( wfMsg( 'Googlesearchheader' ) );
+     $wgOut->addHTML( wfMsg( 'Googlesearch', $term ) );
+ }       
  wfProfileOut( $fname );

Version 1.5[edit]

Simply set $wgDisableTextSearch = true; in your LocalSettings.php file.

Q. How to do it without disabling normal MediaWiki search? (or am I doing it wrong?)

A. ???

Q. How to do it in 1.5rc4? $wgDisableTextSearch is not there anymore...

A. Just add the $wgDisableTextSearch = true; to your LocalSettings.php file the same as in 1.4.8. This overrides $wgDisableTextSearch = false; located in the /includes/DefaultSettings.php file.

Q. But adding $wgDisableTextSearch = true; will disable the built-in search function. In previous versions it was possible to have both, the Mediawiki search and Googlesearch.

Version 1.4.8[edit]

I am not aware of which version the below referrs to, but as of version 1.4.8 to enable Google search, simply set $wgDisableTextSearch = true; in your LocalSettings.php file.

Prior versions without simple implementation[edit]

  • Find the end of the showResults( $term ) function in SpecialSearch.php. This should be on Line 200 unless you have modified this code already somehow.
  • Find the line above the end of this function which reads:
                           $wgOut->addHTML( $this->powerSearchBox( $term ) );
This is the display code for the Power Search options.
  • Insert the following lines of code directly above the line you just found.
                           $wgOut->addWikiText( wfMsg( 'Googlesearchheader' ) );
                           $wgOut->addHTML( wfMsg( 'Googlesearch', $term ) );
These lines pull a header line to be placed above your search menu and the HTML code to implement the Google Search bar, respectively.
  • Save the changes to SpecialSearch.php and upload it to your site if it is located remotely. Replace the original SpecialSearch.php in the /includes directory of your MediaWiki with this one.

Editorial Note[edit]

If you see any way in which to improve this article,
please either contact me with suggestions or just go
ahead and update it yourself.
  -- Phil MacCabe