From 7622dd339de7c606cf61d4f5309897e44fbb61a1 Mon Sep 17 00:00:00 2001 From: nao-pon Date: Wed, 3 Apr 2013 20:25:31 +0900 Subject: [PATCH] [plugin:gsearch] New plugin "gsearch" added --- .../language/xpwiki/de/plugin/gsearch.lng.php | 11 + .../xpwiki/de_utf8/plugin/gsearch.lng.php | 11 + .../language/xpwiki/en/plugin/gsearch.lng.php | 11 + .../language/xpwiki/ja/plugin/gsearch.lng.php | 11 + .../xpwiki/ja_utf8/plugin/gsearch.lng.php | 11 + .../language/xpwiki/pt/plugin/gsearch.lng.php | 11 + .../xpwiki/pt_utf8/plugin/gsearch.lng.php | 11 + .../modules/xpwiki/plugin/gsearch.inc.php | 328 ++++++++++++++++++ .../modules/xpwiki/skin/css/gsearch.css | 7 + 9 files changed, 412 insertions(+) create mode 100644 xoops_trust_path/modules/xpwiki/language/xpwiki/de/plugin/gsearch.lng.php create mode 100644 xoops_trust_path/modules/xpwiki/language/xpwiki/de_utf8/plugin/gsearch.lng.php create mode 100644 xoops_trust_path/modules/xpwiki/language/xpwiki/en/plugin/gsearch.lng.php create mode 100644 xoops_trust_path/modules/xpwiki/language/xpwiki/ja/plugin/gsearch.lng.php create mode 100644 xoops_trust_path/modules/xpwiki/language/xpwiki/ja_utf8/plugin/gsearch.lng.php create mode 100644 xoops_trust_path/modules/xpwiki/language/xpwiki/pt/plugin/gsearch.lng.php create mode 100644 xoops_trust_path/modules/xpwiki/language/xpwiki/pt_utf8/plugin/gsearch.lng.php create mode 100644 xoops_trust_path/modules/xpwiki/plugin/gsearch.inc.php create mode 100644 xoops_trust_path/modules/xpwiki/skin/css/gsearch.css diff --git a/xoops_trust_path/modules/xpwiki/language/xpwiki/de/plugin/gsearch.lng.php b/xoops_trust_path/modules/xpwiki/language/xpwiki/de/plugin/gsearch.lng.php new file mode 100644 index 00000000..f0995a28 --- /dev/null +++ b/xoops_trust_path/modules/xpwiki/language/xpwiki/de/plugin/gsearch.lng.php @@ -0,0 +1,11 @@ + '# It was not found though %2$s of Google "%1$s" was retrieved.', + 'msg_more' => 'Looks for %2$s of "%1$s" more.', + 'msg_web' => 'Web sites', + 'msg_images' => 'Images', + 'msg_video' => 'Videos', + 'msg_websvc' => 'Web Service', + 'err_badres' => 'Connected failure to Google.', + 'err_option' => '#gsearch(mode,query) Error: The option is not correctly specified.', +); diff --git a/xoops_trust_path/modules/xpwiki/language/xpwiki/de_utf8/plugin/gsearch.lng.php b/xoops_trust_path/modules/xpwiki/language/xpwiki/de_utf8/plugin/gsearch.lng.php new file mode 100644 index 00000000..f0995a28 --- /dev/null +++ b/xoops_trust_path/modules/xpwiki/language/xpwiki/de_utf8/plugin/gsearch.lng.php @@ -0,0 +1,11 @@ + '# It was not found though %2$s of Google "%1$s" was retrieved.', + 'msg_more' => 'Looks for %2$s of "%1$s" more.', + 'msg_web' => 'Web sites', + 'msg_images' => 'Images', + 'msg_video' => 'Videos', + 'msg_websvc' => 'Web Service', + 'err_badres' => 'Connected failure to Google.', + 'err_option' => '#gsearch(mode,query) Error: The option is not correctly specified.', +); diff --git a/xoops_trust_path/modules/xpwiki/language/xpwiki/en/plugin/gsearch.lng.php b/xoops_trust_path/modules/xpwiki/language/xpwiki/en/plugin/gsearch.lng.php new file mode 100644 index 00000000..f0995a28 --- /dev/null +++ b/xoops_trust_path/modules/xpwiki/language/xpwiki/en/plugin/gsearch.lng.php @@ -0,0 +1,11 @@ + '# It was not found though %2$s of Google "%1$s" was retrieved.', + 'msg_more' => 'Looks for %2$s of "%1$s" more.', + 'msg_web' => 'Web sites', + 'msg_images' => 'Images', + 'msg_video' => 'Videos', + 'msg_websvc' => 'Web Service', + 'err_badres' => 'Connected failure to Google.', + 'err_option' => '#gsearch(mode,query) Error: The option is not correctly specified.', +); diff --git a/xoops_trust_path/modules/xpwiki/language/xpwiki/ja/plugin/gsearch.lng.php b/xoops_trust_path/modules/xpwiki/language/xpwiki/ja/plugin/gsearch.lng.php new file mode 100644 index 00000000..28555e3a --- /dev/null +++ b/xoops_trust_path/modules/xpwiki/language/xpwiki/ja/plugin/gsearch.lng.php @@ -0,0 +1,11 @@ + '※ Google で「%1$s」の%2$sを検索しましたが見つかりませんでした。', + 'msg_more' => '「%1$s」の%2$sをもっと探す', + 'msg_web' => 'Webサイト', + 'msg_images' => '画像', + 'msg_video' => '動画', + 'msg_websvc' => 'Webサービス', + 'err_badres' => 'Google のサーバーに接続できませんでした。', + 'err_option' => '#gsearch(mode,query) エラー:オプションが正しく指定されていません。', +); diff --git a/xoops_trust_path/modules/xpwiki/language/xpwiki/ja_utf8/plugin/gsearch.lng.php b/xoops_trust_path/modules/xpwiki/language/xpwiki/ja_utf8/plugin/gsearch.lng.php new file mode 100644 index 00000000..a808c8fe --- /dev/null +++ b/xoops_trust_path/modules/xpwiki/language/xpwiki/ja_utf8/plugin/gsearch.lng.php @@ -0,0 +1,11 @@ + ' Google с%1$s%2$s罎膣≪障荀ゃ障с', + 'msg_more' => '%1$s%2$sc「', + 'msg_web' => 'Web泣ゃ', + 'msg_images' => '糸', + 'msg_video' => '', + 'msg_websvc' => 'Web泣若', + 'err_badres' => 'Google 泣若若・膓с障с', + 'err_option' => '#gsearch(mode,query) :激с潟罩c絎障', +); diff --git a/xoops_trust_path/modules/xpwiki/language/xpwiki/pt/plugin/gsearch.lng.php b/xoops_trust_path/modules/xpwiki/language/xpwiki/pt/plugin/gsearch.lng.php new file mode 100644 index 00000000..f0995a28 --- /dev/null +++ b/xoops_trust_path/modules/xpwiki/language/xpwiki/pt/plugin/gsearch.lng.php @@ -0,0 +1,11 @@ + '# It was not found though %2$s of Google "%1$s" was retrieved.', + 'msg_more' => 'Looks for %2$s of "%1$s" more.', + 'msg_web' => 'Web sites', + 'msg_images' => 'Images', + 'msg_video' => 'Videos', + 'msg_websvc' => 'Web Service', + 'err_badres' => 'Connected failure to Google.', + 'err_option' => '#gsearch(mode,query) Error: The option is not correctly specified.', +); diff --git a/xoops_trust_path/modules/xpwiki/language/xpwiki/pt_utf8/plugin/gsearch.lng.php b/xoops_trust_path/modules/xpwiki/language/xpwiki/pt_utf8/plugin/gsearch.lng.php new file mode 100644 index 00000000..f0995a28 --- /dev/null +++ b/xoops_trust_path/modules/xpwiki/language/xpwiki/pt_utf8/plugin/gsearch.lng.php @@ -0,0 +1,11 @@ + '# It was not found though %2$s of Google "%1$s" was retrieved.', + 'msg_more' => 'Looks for %2$s of "%1$s" more.', + 'msg_web' => 'Web sites', + 'msg_images' => 'Images', + 'msg_video' => 'Videos', + 'msg_websvc' => 'Web Service', + 'err_badres' => 'Connected failure to Google.', + 'err_option' => '#gsearch(mode,query) Error: The option is not correctly specified.', +); diff --git a/xoops_trust_path/modules/xpwiki/plugin/gsearch.inc.php b/xoops_trust_path/modules/xpwiki/plugin/gsearch.inc.php new file mode 100644 index 00000000..79b21e11 --- /dev/null +++ b/xoops_trust_path/modules/xpwiki/plugin/gsearch.inc.php @@ -0,0 +1,328 @@ +][,col:][,target:]) + + var $appid = ''; + var $appid_upg = ''; + var $params; + + function plugin_gsearch_init() + { + $this->config = array( + //////// Config /////// + 'safe' => 'moderate', // [active|moderate|off] セーフサーチ active:強, moderate:中, off:なし + 'filter' => 1, // [0|1] 重複した結果の除外 + 'ng_site' => "", // 除外サイト([SPACE]区切りで最大30サイト) + //'coloration' => "any", // 画像検索対象の色指定[any|color|bw] + //'format_web' => "any", // 検索対象[any|html|msword|pdf|ppt|rss|txt|xls](Web) + //'format_img' => "any", // 検索対象[any|bmp|gif|jpeg|png](Image) + //'format_mov' => "any", // 検索対象[any|avi|flash|mpeg|msmedia|quicktime|realmedia](Movie) + 'max_web' => 8, // 検索件数の規定値(Web) + 'max_img' => 5, // 検索件数の規定値(Image) + 'max_mov' => 4, // 検索件数の規定値(Movie) + 'col_web' => 1, // 表示列数の規定値(Web) + 'col_img' => 5, // 表示列数の規定値(Image) + 'col_mov' => 4, // 表示列数の規定値(Movie) + 'cache_time' => 14400, // Cache time (min) 14400m = 10 days + //////// Config /////// + ); + $this->appkey = ''; + $this->appid_upg = ''; + } + + function plugin_gsearch_convert() + { + error_reporting(E_ALL); + $args = func_get_args(); + if (count($args) < 2) + { + return "

{$this->msg['err_option']}

"; + } + + $this->load_language(); + + $mode = array_shift($args); + $query = array_shift($args); + + // mode 判定 + $mode = trim(strtolower($mode)); + switch($mode) + { + case "images": + case "image": + case "img": + $mode = "img"; + break; + case "video": + case "movie": + case "mov": + $mode = "mov"; + break; + default: + // web + $mode = "web"; + } + + $prms = array( + "target"=>$this->root->link_target, + "max"=>$this->config['max_'.$mode], + "col"=>$this->config['col_'.$mode], + 'safe' => $this->config['safe'], + 'filter' => $this->config['filter'] + ); + $this->fetch_options ($prms, $args); + + $prms['safe'] = strtolower($prms['safe']); + if (!in_array($prms['safe'], array('active', 'moderate', 'off'))) { + $prms['safe'] = $this->config['safe']; + } + $prms['filter'] = $prms['filter']? 1 : 0; + + $this->params = $prms; + + $ret = $this->plugin_gsearch_get($mode,$query); + + $this->func->add_tag_head('gsearch.css'); + return "
{$ret}
"; + + } + + function plugin_gsearch_get($mode,$query) + { + $ttl = $this->config['cache_time'] * 60; + $key = md5($mode.$query.join('',$this->params)); + + // キャッシュ判定 + if (! $html = $this->func->cache_get_db($key, 'gsearch')) { + $html = $this->plugin_gsearch_gethtml($mode,$query); + + // キャッシュ保存 + if ($html) { + if ($html === $this->msg['err_badres']) { + $ttl = 3600; // 1h + } + $this->func->cache_save_db($html, 'gsearch', $ttl, $key); + } + + // Update plainDB + $this->func->need_update_plaindb(); + } + + return $html; + + } + + function plugin_gsearch_gethtml($mode,$query) + { + $search_url = 'http://ajax.googleapis.com/ajax/services/search/$mode?v=1.0&userip='.$_SERVER['REMOTE_ADDR']; + + $qs = htmlspecialchars($query); + // REST リクエストの構築 + $query = rawurlencode(mb_convert_encoding(trim($query),"UTF-8",$this->cont['SOURCE_ENCODING'])); + $max = min((int)$this->params['max'], 8); + + $search_url .= "&q={$query}&rsz={$max}"; + $search_url .= '&hl=' . $this->cont['LANG']; + $search_url .= '&safe=' . $this->params['safe']; + $search_url .= '&filter=' . $this->params['filter']; + $search_url .= '&hl=' . $this->cont['LANG']; + if ($this->appkey) $search_url .= '&key' . $this->appkey; + + $mode = trim(strtolower($mode)); + switch($mode) + { + case "images": + case "image": + case "img": + $mode = "images"; + break; + case "video": + case "movie": + case "mov": + $mode = "video"; + break; + case "web": + default: + $mode = "web"; + } + $url = str_replace('$mode', $mode, $search_url); + //exit($url); + $src = ''; + + // データ取得 + $res = $this->func->http_request($url); + if ($res['rc'] == 200 && $res['data']) + { + $res = json_decode($res['data'], true); + + //$src .= '
'. $url . "\n" . print_r($res, true) . '
'; + + // 該当データなし + if (!$res['responseData']['results']) + { + return sprintf($this->msg['msg_notfound'],$qs,$this->msg['msg_'.$mode]); + } + } + else + { + // データ取得エラー + return $this->msg['err_badres']; + + } + + // 該当データなし + if (!$res['responseData']['results']) + { + return sprintf($this->msg['msg_notfound'],$qs,$this->msg['msg_'.$mode]); + } + + $func = "plugin_gsearch_build_".$mode; + $target = htmlspecialchars($this->params['target']); + $html = $this->$func($res['responseData'],$target,$this->params['col']); + + if (isset($res['responseData']['cursor']) && isset($res['responseData']['cursor']['moreResultsUrl'])) { + $html .= ''; + } + + return $html . $src; + } + + function plugin_gsearch_build_web($res,$target,$col) + { + + $linkurl = 'Url'; // 'URL' or 'ClickUrl' + + $dats = array(); + if (isset($res['results'][0])) + { + $dats = $res['results']; + } + else + { + $dats[0] = (empty($res['results']))? array() : $res['results']; + } + + $html = ""; + if ($dats) + { + $html = $sdiv = $ediv = ""; + if ($col > 1) + { + $sdiv = "
"; + $ediv = "
"; + } + $cnt = 0; + $limit = ceil(count($dats)/$col); + $html .= $sdiv."
    "; + mb_convert_variables($this->cont['SOURCE_ENCODING'],"UTF-8",$dats); + foreach ($dats as $dat) + { + if ($this->plugin_gsearch_check_ngsite($dat['ClickUrl'])) {continue;} + if ($cnt++ % $limit === 0 && $cnt !== 1) $html .= "
".$sdiv."
    "; + $html .= "
  • "; + $html .= "".$dat['titleNoFormatting'].""; + $html .= "
    ".$this->func->make_link(strip_tags($dat['content']))."
    "; + $html .= "
  • "; + } + $html .= "
".$ediv; + } + + return $html; + } + + function plugin_gsearch_build_images($res,$target,$col) + { + $dats = array(); + if (isset($res['results'][0])) + { + $dats = $res['results']; + } + else + { + $dats[0] = (empty($res['results']))? array() : $res['results']; + } + + $html = ""; + if ($dats) + { + $cnt = 0; + $html = ""; + mb_convert_variables($this->cont['SOURCE_ENCODING'],"UTF-8",$dats); + foreach ($dats as $dat) + { + if ($this->plugin_gsearch_check_ngsite($dat['url'])) {continue;} + $title = "[".$dat['titleNoFormatting']."]".htmlspecialchars($dat['contentNoFormatting']); + $size = $dat['width']." x ".$dat['height']; + $site = "[ Site ]"; + + if ($cnt++ % $col === 0 && $cnt !== 1) $html .= ""; + $html .= ""; + } + $html .= "
"; + $html .= "\"{$title}\""; + $html .= "
".$size."
".$site."
"; + $html .= "
"; + } + + return $html; + } + + function plugin_gsearch_build_video($res,$target,$col) + { + $dats = array(); + if (isset($res['results'][0])) + { + $dats = $res['results']; + } + else + { + $dats[0] = (empty($res['results']))? array() : $res['results']; + } + + $html = ""; + if ($dats) + { + $cnt = 0; + $html = ""; + mb_convert_variables($this->cont['SOURCE_ENCODING'],"UTF-8",$dats); + foreach ($dats as $dat) + { + if ($this->plugin_gsearch_check_ngsite($dat['url'])) {continue;} + $title = "[".$dat['titleNoFormatting']."]".htmlspecialchars($dat['contentNoFormatting']); + $site = " [ Site ]"; + $min = (int)($dat['duration'] / 60); + $sec = sprintf("%02d",($dat['duration'] % 60)); + $length = $min.":".$sec; + + if ($cnt++ % $col === 0 && $cnt !== 1) $html .= ""; + $html .= ""; + } + $html .= "
"; + $html .= "\"{$title}\""; + $html .= "
".$length."
".$site.''; + $html .= "
"; + } + + return $html; + } + + function plugin_gsearch_check_ngsite($url) + { + static $ngsites = array(); + if (!isset($ngsites[$this->xpwiki->pid])) {$ngsites[$this->xpwiki->pid] = null;} + if (is_null($ngsites[$this->xpwiki->pid])) + { + $ngsites[$this->xpwiki->pid] = explode(" ",$this->config['ng_site']); + } + foreach($ngsites[$this->xpwiki->pid] as $ngsite) + { + if ($ngsite && preg_match("#".preg_quote($ngsite,"#")."#i",$url)) + { + return true; + } + } + return false; + } + +} +?> \ No newline at end of file diff --git a/xoops_trust_path/modules/xpwiki/skin/css/gsearch.css b/xoops_trust_path/modules/xpwiki/skin/css/gsearch.css new file mode 100644 index 00000000..b5a27147 --- /dev/null +++ b/xoops_trust_path/modules/xpwiki/skin/css/gsearch.css @@ -0,0 +1,7 @@ +$class .gsearch table { + width: 100%; + table-layout: fixed; +} +$class div.gsearch { + margin-bottom: 1em; +} \ No newline at end of file