1<?php
2/**
3 * SearchStats Plugin: This plugin records the search words and displays stats in the admin section
4 *
5 * @license		GPL 2 (http://www.gnu.org/licenses/gpl.html)
6 * @author		Michael Schuh <mike.schuh@gmx.at>
7 */
8
9if(!defined('DOKU_INC')) die();
10if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
11require_once DOKU_PLUGIN.'admin.php';
12
13class admin_plugin_searchstats extends DokuWiki_Admin_Plugin {
14
15	function getInfo() {
16		return array(
17              'author' => 'Michael Schuh',
18              'email'  => 'mike.schuh@gmx.at',
19              'date'   => @file_get_contents(DOKU_PLUGIN.'searchstats/VERSION'),
20              'name'   => 'Searchstats plugin (action, admin component)',
21              'desc'   => 'This plugin records the search words and displays stats in the admin section',
22              'url'    => 'http://blog.imho.at/20100902/artikel/dokuwiki-plugin-searchstats',
23              );
24	}
25
26	function getMenuSort() { return 200; }
27	function forAdminOnly() { return false; }
28
29	//Carry out any processing required by the plugin.
30	function handle() {
31		$dataObject = new action_plugin_searchstats();
32		$this->wordArray = $dataObject->getSearchWordArray();
33	}
34
35	//Render html output for the plugin.
36	function html() {
37		if(is_array($this->wordArray) && count($this->wordArray) > 0) {
38			ptln('<h1>'.$this->getLang('menu').'</h1>');
39			//print out bar chart
40			ptln('<br />');
41			$link = $this->_getBarChartTopKeywords(10);
42			ptln('<img src="'.$link.'" />');
43			//print out data table
44			ptln('<br /><br />');
45			ptln('<table class="inline">');
46			ptln('<tr class="row0">');
47			ptln('<th class="col0 leftalign">'.$this->getLang('th_word').'</th>');
48			ptln('<th class="col1">'.$this->getLang('th_count').'</th>');
49			ptln('</tr>');
50			foreach($this->wordArray as $word => $count) {
51				ptln('<tr>');
52				ptln('<td class="col0">'.$word.'</td>');
53				ptln('<td class="col1">'.$count.'</td>');
54				ptln('</tr>');
55			}
56			ptln('</table>');
57		}
58		else {
59		  ptln('<h1>'.$this->getLang('nosearchwords').'</h1>');
60		}
61	}
62
63	function _getBarChartTopKeywords($amount = 10) {
64		$countArray = count($this->wordArray);
65		$amount = ($countArray < $amount ? $countArray : $amount);
66		if(is_array($this->wordArray) && $amount > 0) {
67			$wordArray = array_slice($this->wordArray, 0, $amount);
68
69			$chxl = "&chxl=0:";
70			$chd = "&chd=t:";
71			$top = 0;
72			$i = 0;
73			foreach($wordArray as $word => $count) {
74				if($i == 0) {
75					if(function_exists('bcpow')) {
76						$top = bcpow(10, (int) strlen((string) $count));
77					}
78					else {
79						$strLen = (int) strlen((string) $count);
80						$top = 1;
81						while($strLen-- >= 1) {
82							$top = $top * 10;
83						}
84					}
85				}
86				$i++;
87				$chxl .= "|".$word;
88				$percentage = $count/$top*100;
89				$chd .= ($percentage).($i < $amount ? "," : "");
90			}
91			//$top = $top + (10-($top%10));
92			//set chart type to bar chart
93			$paramString = "?cht=bvg";
94			//set x and y axis visible
95			$paramString .= "&chxt=x,y";
96			//set min and max for y axis
97			$paramString .= "&chxr=1,0,".$top;
98			//set width and height for chart
99			$paramString .= "&chs=750x300";
100			//set color for bars
101			$paramString .= "&chco=A2C180";
102			//set chart title
103			$paramString .= "&chtt=".implode("+", explode(" ", $this->getLang('top10k')));
104			//set automatic bar width
105			$paramString .= "&chbh=a";
106			//chxl for custom axis labels
107			$paramString .= $chxl;
108			//chd for bar heights
109			$paramString .= $chd;
110
111			$link = "http://chart.apis.google.com/chart".$paramString;
112			return $link;
113		}
114		return false;
115	}
116
117	var $wordArray = array();
118
119}
120
121?>