Extension:Google Custom Search Engine

From MediaWiki.org
Jump to: navigation, search
MediaWiki extensions manual - list
Crystal Clear action run.png
Google Custom Search Engine

Release status: stable

Implementation Tag
Description This extension adds a Google Custom Search Engine to your MediaWiki-powered site.
Author(s) User:BiGreat
Latest version 0.1 (2006-11-21)
MediaWiki 1.7.1 and higher
License No license specified
Download see below

Translate the Google Custom Search Engine extension if possible

Check usage and version matrix; code metrics

This extension adds a Google Custom Search Engine to your wiki.

What is the utility of this extension?[edit | edit source]

Adds a Google Custom Search Engine to your wiki, using a specific page that you specify to display the search results. Also enables you to replace the standard MediaWiki search box with a Google search box.

Get Google custom search codes[edit | edit source]

  • Before you start to install this extension, you need to go to www.google.com/cse/, create a Custom Search Engine by clicking the "Create a Custom Search Engine" button, and find the "code" section at "Control panel".
  • At the "code" section, expand "Search box and search results code for your website" and file one wiki page in your site where you want to display the search result (for example : http://yoursite.com/wiki/yourwikiname:GoogleSearch).
Google Custom Search codes
  • After you finish this step, you will get two "codes" as the picture shown right :
    • code 1 is in box 1 : it is the code for the search engine ;
    • code 2 is in box 2: this code is for the search results.

Under some circumstances, Google will only give you one code.

Installation[edit | edit source]

Save the code below into a file called "google-cse.php", and then edit the file: copy the TWO codes (code 1 and code 2) provided by Google and paste them between $output=' and ';//google code end here into google-cse.php.

Extension code[edit | edit source]

<?php
# Google Custom Search Engine Extension
# 
# Tag :
#   <GoogleCSE></GoogleCSE> or <GoogleCSE />
# Ex :
#   Add this tag to the wiki page you configed at your Google Custom Search control panel.
#
# Enjoy !

$wgExtensionFunctions[] = 'GoogleCSE';
$wgExtensionCredits['parserhook'][] = array(
        'name' => 'Google Custom Search Engine',
        'description' => 'Displays a Google Custom Search Engine input',
        'author' => 'Liang Chen The BiGreat',
        'url' => 'http://www.mediawiki.org/wiki/Extension:Google_Custom_Search_Engine'
);
 
function GoogleCSE() {
        global $wgParser;
        $wgParser->setHook('GoogleCSE', 'renderGoogleCSE');
}
 
# The callback function for converting the input text to HTML output
function renderGoogleCSE($input) {
 
        $output='
                        @@@@@@@@@@@PASTE THE TWO CODEs HERE@@@@@@@@@@
When you paste the code from Google, please make sure to remove all the blank spaces.
(They create a <p> or <div> tag, which interferes with the Google code.)
					';//google code end here
 
        return $output;
}


Then, remove all the blank spaces in code 1 and 2. The result should be something like:

$output='<form action="YOURURL" id="cse-search-box">
<input type="hidden" name="cx" value="YOURKEY" /><input type="hidden" name="cof" value="FORID:10" /><input type="hidden" name="ie" value="UTF-8" /><input type="text" name="q" size="31" /><input type="submit" name="sa" value="Search" />
</form><script type="text/javascript" src="http://www.google.com/jsapi"></script><script type="text/javascript">google.load("elements", "1", {packages: "transliteration"});</script><script type="text/javascript" src="http://www.google.com/***/t13n?form=cse-search-box&t13n_langs=fr"></script><script type="text/javascript" src="http://www.google.com/***/brand?form=cse-search-box&lang=fr"></script>
<script type="text/javascript">var googleSearchIframeName = "cse-search-results";var googleSearchFormName = "cse-search-box";var googleSearchFrameWidth = 600;var googleSearchDomain = "www.google.com";var googleSearchPath = "/cse";</script><script type="text/javascript" src="http://www.google.com/afsonline/show_afs_search.js"></script>';//google code end here

Save, upload and configure[edit | edit source]

  • Save your file under the name : google-cse.php ;
  • Upload the google-cse.php to your extensions/ directory, and add include("extensions/google-cse.php"); at the end of your LocalSettings.php file.
  • Go to the wiki page which you want to display the search result like (http://yoursite.com/wiki/yourwikiname:GoogleSearch), add <GoogleCSE></GoogleCSE> to this page and protect it.
  • Then use another code Google provided at the pages you want to be the entrance of the search.

Replacing standard MediaWiki search[edit | edit source]

You can have the Google Custom Search Engine input replace the standard MediaWiki search input. To do that, you need to edit your skin file. If you're using the Vector skin, edit /skins/Vector.php, and replace all the code between "<form ... id="searchform" " and "</form>" by your Google code 1.

If you're using the MonoBook skin, edit /skins/MonoBook.php, and, after function searchBox, replace all between "<form action=" and "</form>" by Google code 1.

Regardless of the skin, you can configure the width of the search box by modifying size="31". 12 seems to work well.

Illustrations[edit | edit source]

Below are screenshots for how the Google custom search input can look in the MonoBook skin.

The entrance at sidebar
Search Result

Site users[edit | edit source]

Please list your site here if you are using this code.

See[edit | edit source]

Google +1 extension