1<?php 2/** 3 * search Logger Plugin 4 * 5 * @license GPL 2 (http://www.gnu.org/licenses/gpl.html) 6 * @author i-net software <tools@inetsoftware.de> 7 * @author Gerry Weissbach <gweissbach@inetsoftware.de> 8 */ 9 10// must be run within Dokuwiki 11if(!defined('DOKU_INC')) die(); 12 13if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/'); 14require_once(DOKU_PLUGIN.'action.php'); 15 16class action_plugin_searchcombinedgoogle extends DokuWiki_Action_Plugin { 17 18 private $googleresults = null; 19 private $query = null; 20 21 /** 22 * for backward compatability 23 * @see inc/DokuWiki_Plugin#getInfo() 24 */ 25 function getInfo(){ 26 if ( method_exists(parent, 'getInfo')) { 27 $info = parent::getInfo(); 28 } 29 return is_array($info) ? $info : confToHash(dirname(__FILE__).'/plugin.info.txt'); 30 } 31 32 function register(Doku_Event_Handler $controller) { 33 34 // Log Query 35 $controller->register_hook('SEARCH_QUERY_FULLPAGE', 'AFTER', $this, 'searchlogger__log'); 36 $controller->register_hook('FULLTEXT_SNIPPET_CREATE', 'BEFORE', $this, 'searchlogger__getSnippet'); 37 $controller->register_hook('SEARCH_QUERY_PAGE_TITLE', 'BEFORE', $this, 'searchlogger__getPageTitle'); 38 } 39 40 function searchlogger__getPageTitle(Doku_Event &$event, $args) 41 { 42 if ( !empty($this->googleresults[$event->data['id']]) ) 43 { 44 $event->data['name'] = $this->googleresults[$event->data['id']]->title; 45 $event->data['id'] = $this->googleresults[$event->data['id']]->path; 46 47 return true; 48 } 49 50 return false; 51 } 52 53 function searchlogger__getSnippet(Doku_Event &$event, $args) { 54 if ( !empty($this->googleresults[$event->data['id']]) ) 55 { 56 $event->data['text'] = strip_tags($this->googleresults[$event->data['id']]->content); 57 } else { 58 $event->data['text'] = ''; //p_render('text', p_get_instructions($event->data['snippet']), $INFO); 59 } 60 } 61 62 function searchlogger__log(Doku_Event &$event, $args) { 63 global $ACT, $conf, $cache_wikifn; 64 65 if ( $ACT == 'search' ) { 66 67 $currentResult = $event->result; 68 $site = $_SERVER['SERVER_NAME']; 69 $url = "http://ajax.googleapis.com/ajax/services/search/web?v=1.0&q=" . urlencode($event->data['query'] . " site:" . $site) . "&key=" . $this->getConf('googleapikey') . "&userip=" . $_SERVER['REMOTE_ADDR']; 70 71 if ( $url != $this->query ) 72 { 73 $maxQueries = array(array( 'start' => 0 ),); 74 $results = array(); 75 76 for( $i = 0; $i < count($maxQueries); $i++ ) { 77 78 $http = new DokuHTTPClient(); 79 $http->timeout = 25; //max. 25 sec 80 81 $json = new JSON(); 82 83 $tempResults = $json->decode($http->get($url . '&start=' . intval($maxQueries[$i]->start), $params, 'GET')); 84 85 if ( $tempResults->responseStatus != 200 ) { 86 continue; 87 } 88 89 if ( $i == 0 ) { 90 $maxQueries = $tempResults->responseData->cursor->pages; 91 } 92 93 $results = array_merge($results, $tempResults->responseData->results); 94 } 95 96 $event->result = array(); 97 98 $this->query = $url; 99 100 $oldCache = $cache_wikifn; 101 $cache_wikifn = array(); 102 103 foreach ( $results as $result ) { 104 105 if ( parse_url(urldecode($result->url), PHP_URL_QUERY) != '' ) 106 { 107 continue; // Parameter ignorieren 108 } 109 110 $result->path = parse_url(urldecode($result->url), PHP_URL_PATH); 111 $id = cleanID($result->path); 112 113 resolve_pageid(null,$id,$exists); 114 $this->googleresults[$id] = $result; 115 $event->result[$id] = intval($currentResult[$id]) == 0 ? 'unknown' : $currentResult[$id]; 116 unset($currentResult[$id]); 117 } 118 119 $cache_wikifn = $oldCache; 120 $event->result = array_merge($event->result, $currentResult); 121 } 122 } 123 124 return true; 125 } 126} 127 128//Setup VIM: ex: et ts=2 enc=utf-8 :