Extension:Google Search-2
This page was moved from MetaWiki.
It probably requires cleanup – please feel free to help out. In addition, some links on the page may be red; respective pages might be found at Meta. Remove this template once cleanup is complete.
Contents |
[edit] New MediaWiki 1.12.0 version
I recently updated this to work nicely with MW 1.12.0. Download it here
Note that this is really a mod and not an extension since it requires replacing some of the default files (includes/SpecialSearch.php and skins/MonoBook,Modern,CologneBlue.php). You can Diff the versions to see the changes - very small ones.
This extension tightly integrates Google searching with MediaWiki, completely replacing the existing internal search. We use this on dozens of wikis at Intuit, Inc., via a corporate Google appliance. It retrieves XML results and applies a slightly modified Google standard XSLT to the results. It is part of the Intuit Innovation Lab's wiki baseline for new corporate wikis.
These are all internal wikis, but I uploaded some screenshots so that you can see what it looks like.
These instructions are for MediaWiki 1.7.1. However, the extension was originally written for 1.5.4 and will probably work with any 1.5.x version or above. The main difference is in editing the MediaWiki default scripts to force Google searching to be the default and ONLY search engine. I didn't have to change the extension itself when I updated our baseline from 1.5.4 to 1.7.1.
[edit] extensions/GoogleSearch.php
<?php
# Created by Matt Hart, Intuit, Inc. http://meta.wikimedia.org/wiki/User:MHart
# Note that this is not really an extension in the scrict sense - it isn't added
# to the extensions list and such. Rather it replaces the search.
function wfGoogleSearch( $par='' ) {
global $wgRequest, $wgUser;
global $wgOut, $wgSitename;
global $wgScriptPath;
$searchPage = new SpecialSearch( $wgRequest, $wgUser );
$search = $wgRequest->getText( 'search', $par );
$start = $wgRequest->getText( 'start', $par);
$wgOut->setPageTitle($search . " - Google Search");
$sitesearch = urlencode($_SERVER["HTTP_HOST"] . $wgScriptPath);
# Google URL Parameters
# site=ext_name : this is the Google appliance (GA) "collection" to search
# client=name : this is the GA client that is allowed to make requests
# as_sitesearch= : this restricts the results to this specific wiki, even on shared domains
# so wikis.intuit.com/first gets its own results
# likewise wikis.intuit.com/second gets its own
$url = 'http://googlesearchurl.yoursite.com/search?q=' . urlencode($search) .
'&output=xml&site=ext_name&client=name&as_sitesearch=' .
$sitesearch . '&start=' . $start;
$xmldata = file_get_contents($url);
# This saves the file to a temp location so that XSLTPROC can
# run on it. If you have Sablotron installed with PHP 5, you can
# use it's XSLT functions. The following method, however, works with
# any version of PHP.
$xmlfile = tempnam("","");
$file = fopen($xmlfile,"w");
fwrite($file,$xmldata);
fclose($file);
$results = xsltproc('extensions/Google.xslt',$xmlfile);
$wgOut->addHTML($results);
unlink($xmlfile);
$wgOut->output();
return;
}
function xsltproc($xslfile, $xmlfile)
{
$toexec = "xsltproc " . $xslfile . " " . $xmlfile;
exec($toexec,$results);
return implode("",$results);
}
?>
[edit] extensions/Google.xslt
This is too long to directly post, so it is uploaded here.. Included in the ZIP are the graphics I use.
The modifications I made to this standard transform are to point to the graphics, slightly different messages and to prevent site:whatever.com to re-appear in the search box. It works dynamically - i.e. you don't have to modify this XSLT to match your website, just as you don't have to modify the GoogleSearch.php script except to set your collection and client.
Here are some of the modifications to the standard that you'll see: Graphic locations:
<xsl:variable name="logo_url">http://ilab.intuit.com/wiki/images/Title_Left.gif</xsl:variable> <img src="http://ilab.intuit.com/wiki/images/nav_previous.gif" ... ... and so on
Getting rid of the site:whatever parameter to make the search look better:
<xsl:variable name="html_escaped_query">
<xsl:value-of select="normalize-space(substring-before($qval,'site:'))"
disable-output-escaping="yes"/>
</xsl:variable>
Plenty of misc changes - do a diff against the original. Search for "intuit.com" to change stuff to your URLs.
[edit] MediaWiki script changes
To force this to be the default search engine, you need to change includes/Wiki.php. On MediaWiki 1.5.4, the change had to be made in index.php.
[edit] includes/Wiki.php
if( !$this->getVal('DisableInternalSearch') && !is_null( $search ) && $search !== '' ) {
// Commented OUT the default search
// require_once( 'includes/SpecialSearch.php' );
// $title = Title::makeTitle( NS_SPECIAL, 'Search' );
// wfSpecialSearch();
// Add GoogleSearch here AND see the line below where it's added
wfGoogleSearch();
} else if( !$title or $title->getDBkey() == '' ) {
$title = Title::makeTitle( NS_SPECIAL, 'Badtitle' );
# Die now before we mess up $wgArticle and the skin stops working
throw new ErrorPageError( 'badtitle', 'badtitletext' );
} else if ( $title->getText() == 'Search') {
wfGoogleSearch();
} else if ( $title->getInterwiki() != '' ) {
etc.............
[edit] skins/MonoBook.php
Here I've removed the "Go" button and the "search" text. Now there's a google logo and only one "search" button.
<div id="p-search" class="portlet">
<label for="searchInput"> <img src="<?php global $wgScriptPath; echo $wgScriptPath; ?>/images/google_logo.gif">
<div id="searchBody" class="pBody">
<form action="<?php $this->text('searchaction') ?>" id="searchform"><div>
<input id="searchInput" name="search" type="text" <?php
if($this->haveMsg('accesskey-search')) {
?>accesskey="<?php $this->msg('accesskey-search') ?>"<?php }
if( isset( $this->data['search'] ) ) {
?> value="<?php $this->text('search') ?>"<?php } ?> />
<input type='submit' name="fulltext" class="searchButton" value="<?php $this->msg('search') ?>" />
</div></form>
</div>
</div>
[edit] Summary
Add include("extensions/GoogleSearch.php"); to your LocalSettings.php, place GoogleSearch.php and Google.xslt in extensions/, and put the graphics someplace the scripts can find them - you can just put them all in extensions/ and update the files to point there.
I think that's all the changes I made. The results look great as you can see in the screenshots above - Google is fully integrated, looks like it is "part of" the MediaWiki software. The next/prev results buttons all work as expected.
--MHart 16:24, 4 August 2006 (UTC)
