我发现了几个mediawiki扩展,允许您在mediawiki站点上放置一个用于搜索web的google搜索框。但是,它们似乎都没有启用suggest的选项,suggest根据用户输入的内容填充可能的搜索词的下拉菜单。我该怎么做?
我将此发布到stackoverflow,因为解决方案很可能需要编程。
仅供参考,我发现的现有扩展是:
-http://www.mediawiki.org/wiki/Extension:GoogleSiteSearch
-http://www.mediawiki.org/wiki/Extension:Google

最佳答案

首先,您需要向mediawiki安装添加一个新文件。就称之为googlesuggest.php。您需要此文件,因为浏览器Web安全存在跨域问题(您可以感谢浏览器开发人员)
添加以下代码:

<?php
$q = strtolower($_GET["q"]);
if (!$q) return;

$url="http://suggestqueries.google.com/complete/search?qu=".$q;
$text = file_get_contents($url); //Get content from Google suggest
$text=str_replace("window.google.ac.h([\"$q\",[[","",$text); //Remove unwanted portion
$arr_items=explode("],[",$text); //Split and put it in arrary
foreach($arr_items as $items)
{            $arr_item=explode(",",$items);
            $key=$arr_item[0]; //Get the keyword, the arrary will have other details such as no.of resutls also.
            $key=trim($key,"\""); //Use to remove quotes
        if (strpos(strtolower($key), $q) !== false) {
            echo "$key\n";
        }

}
?>

然后您需要从jQuery.com下载jquery
然后你需要得到这个插件:http://docs.jquery.com/UI/Autocomplete
然后你需要编辑头部部分。添加以下行。
<script type="text/javascript" src="PATHTOJQUERY.JS"></script>
<script type='text/javascript' src='PATHTOjquery.autocomplete.js'></script>
<link rel="stylesheet" type="text/css" href="PATHTOjquery.autocomplete.css" />

<script type="text/javascript">
var keywords=['qualitypoint','qpt','quality','one','two'];
$().ready(function() {


    $("#q").autocomplete("googleSuggest.php", {
        width: 260,
        selectFirst: false
    });

    $("#q").result(function(event, data, formatted) {
        if (data)
            $(this).parent().next().find("input").val(data[1]);
    });


});</script>

然后,在您希望进行Web搜索的位置:
    <form method="get" action="http://google.com/search" autocomplete="off" >
        <p>

            <input type="text" id="q" />
<input type="submit" value="Google Search" />

        </p>
    </form>

09-16 06:15