Extension:GoogleAjax

From MediaWiki.org

Jump to: navigation, search

       

Manual on MediaWiki Extensions
List of MediaWiki Extensions
Crystal Clear action run.png
GoogleAjax

Release status: Stable

Implementation  Ajax
Description To add a google search to the search results page using the Google Ajax API
Author(s)  Bryden Smith (BrydenTalk)
Last Version  1
License GPL
Download no link

check usage (experimental)

[edit] What can this extension do?

This will add a section to the bottom of the search results page for a google search, just above the Advanced Search box, using googles Ajax API it will automatically search for the term that the user has searched for, and return the #1 result, with the option to expand to show all results.

[edit] Usage

This is a bit of a Hack, but once it is put in place it is stable and works very smoothly, no further configuration is needed.

To use this, you will need to have Ajax enabled by adding the following to LocalSettings.php

$wgUseAjax = 'true';

[edit] Installation

To install this extension, add the following to SpecialSearch.php:

please make sure you save a backup of specialsearch.php before you apply this extension

In the SpecialSearch.php file Locate the following code:

		if( $num || $this->offset ) {
			$wgOut->addHTML( "<p class='mw-search-pager-bottom'>{$prevnext}</p>\n" );
		}

Paste in the following code after that:

		$wgOut->addScript( "<script src='http://www.google.com/jsapi'></script>
    <script type='text/javascript'>
 
    google.load('search', '1');
 
    // Whenever a search starts, alert the query.
    function searchStart(searchControl, searcher, query) {
      var content = document.getElementById('search_control');
      var queryDiv = document.getElementById('query');
      if (! queryDiv) {
        var queryDiv = document.createElement('div');
        queryDiv.id = 'query';
        document.body.appendChild(queryDiv);
      }
      queryDiv.innerHTML = 'User searched for: ' + query;
    }
 
    function OnLoad() {
      // Create a search control
      var searchControl = new google.search.SearchControl();
 
      // Add a WebSearcher
      searchControl.addSearcher(new google.search.WebSearch());
 
      // Set a callback so that whenever a search is started we will call searchStart
      searchControl.setSearchStartingCallback(this, searchStart);
 
      // tell the searcher to draw itself and tell it where to attach
      searchControl.draw(document.getElementById('content'));
 
      // execute an inital search
      searchControl.execute('{$term}');
    }
 
    google.setOnLoadCallback(OnLoad);
    </script>/n");

Then, locate the next section of Code:

		$out = 	Xml::openElement( 'form', array(	'id' => 'powersearch', 'method' => 'get', 'action' => $wgScript ) ) .

and change it to:

		$out = 	"<h2>Google Search Results</h2>".
			"<div id='content'>Loading...</div>".
			Xml::openElement( 'form', array(	'id' => 'powersearch', 'method' => 'get', 'action' => $wgScript ) ) .