1<?php
2/**
3 * DokuWiki Plugin Google Trends (Syntax Component)
4 *
5 * @license  GPL3
6 * @author   Vincent Tscherter <tscherter@karmin.ch>
7 *
8 * This plugin is based on code from the googledrawing pluing by Linus Brimstedt & Michael Stewart
9 */
10
11// must be run within Dokuwiki
12if (!defined('DOKU_INC')) die();
13
14if (!defined('DOKU_LF')) define('DOKU_LF', "\n");
15if (!defined('DOKU_TAB')) define('DOKU_TAB', "\t");
16if (!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
17
18require_once DOKU_PLUGIN.'syntax.php';
19
20class syntax_plugin_googletrends extends DokuWiki_Syntax_Plugin {
21
22    public function getType() {
23        return 'substition';
24    }
25
26    public function getPType() {
27        return 'normal';
28    }
29
30    public function getSort() {
31        // Must be before external link (330)
32        return 305;
33    }
34
35
36    public function connectTo($mode) {
37        $this->Lexer->addSpecialPattern('{{googletrends>.*?}}',$mode,'plugin_googletrends');
38    }
39
40
41    public function handle($match, $state, $pos, Doku_Handler $handler){
42		$match = explode("|", preg_replace("/^.*?>(.*)}}$/", "$1", $match));
43		// terms
44		$terms = preg_replace("/[^,a-zA-Z0-9 +]/", "", $match[0]);
45		$data["terms"] = explode(",", $terms );
46		// options
47		$data["hl"] = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2);
48		$data["w"] = 500;
49		$data["h"] = 500;
50		if (isset($match[1])) {
51		  if (preg_match("/hl=([a-z]{2})/", $match[1], $matches)) $data['hl'] = $matches[1];
52		  if (preg_match("/w=([0-9]+)/", $match[1], $matches)) $data['w'] = $matches[1];
53		  if (preg_match("/h=([0-9]+)/", $match[1], $matches)) $data['h'] = $matches[1];
54		}
55        return $data;
56    }
57
58    public function render($mode, Doku_Renderer $renderer, $data) {
59        if($mode != 'xhtml') return false;
60            $renderer->doc .= '<script type="text/javascript" src="//www.google.com/trends/embed.js?'
61				.'hl='.$data["hl"]
62				.'&w='.$data["w"]
63				.'&h='.$data["h"]
64				.'&q='.join($data["terms"], ',+')
65             .'&cmpt=q&content=1&cid=TIMESERIES_GRAPH_0&export=5"></script>';
66        return true;
67    }
68}
69