Jump to content
MediaWiki

Extension:GoogleAjax

From mediawiki.org
Revision as of 17:05, 11 December 2016 by Ciencia Al Poder (talk | contribs) (+Extensions which host their code in-wiki)

This extension is currently not actively maintained! Although it may still work, any bug reports or feature requests will more than likely be ignored.

MediaWiki extensions manual
GoogleAjax
Release status: unmaintained
Implementation Ajax
Description To add a google search to the search results page using the Google Ajax API[1]
Author(s) Bryden Smith (Bryden talk )
Latest version 1.1
MediaWiki
Licence GNU General Public License (any version)
Download No link

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 Google's Ajax API.[1] It will automatically search for the term that the user has searched for, and return the first result, offering the option to expand to show all results.

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';

Problems with MediaWiki 1.15.1

The extension is currently not correctly working with MediaWiki 1.15.1.[2]

This has now been resolved by changing the name of the Google version of the <div id="content"> to <div id="Googlecontent">

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" );
		}
This specific code line appears twice in MediaWiki 1.15.1. Add the extension code after the second appearance.

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('Googlecontent'));

 // 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='Googlecontent'>Loading...</div>".
			Xml::openElement( 'form', array(	'id' => 'powersearch', 'method' => 'get', 'action' => $wgScript ) ) .

For MediaWiki 1.18:

Locate this section of Code (line 273):

		$wgOut->addHtml( "</div>" );

and change it to:

		$wgOut->addHtml( "</div>" );
		$wgOut->addHtml( "<h2>Google Search Results</h2>");
 $wgOut->addHtml( "<div id='Googlecontent'>Loading...</div>");

references

  1. Google Search API
  2. One of the problems is the use of a <div id="content"> container for the Google search result, the same id being used by MediaWiki's content area. (Fixed, see above)

AltStyle によって変換されたページ (->オリジナル) /