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.'action.php';
12
13class action_plugin_searchstats extends DokuWiki_Action_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 register(Doku_Event_Handler $controller) {
27				$controller->register_hook('SEARCH_QUERY_FULLPAGE', 'BEFORE', $this,
28																	 '_getSearchWords');
29		}
30
31		function getSearchWordArray($amount = false) {
32			$helper = plugin_load('helper', 'searchstats');
33			if(is_object($helper)) {
34			 $wordArray = $helper->getSearchWordArray($amount);
35			 return $wordArray;
36			}
37			return array();
38		}
39
40		/**
41		 * Gets searchwords
42		 *
43		 * @author		 Michael Schuh <mike.schuh@gmx.at>
44		 */
45		function _getSearchWords(&$event, $param) {
46			if(function_exists('idx_get_indexer')) {
47				$q = ft_queryParser(idx_get_indexer(),$event->data['query']);
48			}
49			else {
50				$q = ft_queryParser($event->data['query']);
51			}
52			if(is_array($q['highlight'])) {
53				$this->_checkSaveFolder();
54				foreach($q['words'] as $saveWord) {
55					if(strlen(trim($saveWord)) > 0) {
56						//remove ;
57						$saveWord = str_replace(';', '', $saveWord);
58						$this->_saveSearchWord($saveWord);
59					}
60				}
61			}
62		}
63
64		function _getSaveFolder() {
65			$helper = plugin_load('helper', 'searchstats');
66			return $helper->_getSaveFolder();
67		}
68
69		function _checkSaveFolder() {
70			io_mkdir_p($this->_getSaveFolder());
71		}
72		function _getIndexFileName($saveWord) {
73			return $this->_getSaveFolder().'/'.strlen($saveWord);
74		}
75		/**
76		 * Adds searchword in index file
77		 *
78		 * @author		 Michael Schuh <mike.schuh@gmx.at>
79		 */
80		function _saveSearchWord($saveWord) {
81			$fn = $this->_getIndexFileName($saveWord);
82			$writeF = @fopen($fn.'.tmp', 'w');
83			if(!$writeF) {
84				return false;
85			}
86			$readF = @fopen($fn.'.idx', 'r');
87			$wordArray = array();
88			if($readF) {
89				while (!feof($readF)) {
90					$line = fgets($readF, 4096);
91					$lineArray = explode(';', $line);
92					if(is_array($lineArray) && strlen($lineArray[0]) > 0 && $lineArray[1])
93						$wordArray[$lineArray[0]] = $lineArray[1];
94				}
95			}
96			if(isset($wordArray[$saveWord])) {
97				$wordArray[$saveWord] = $wordArray[$saveWord]+1;
98			}
99			else {
100				$wordArray[$saveWord] = 1;
101			}
102			foreach($wordArray as $word => $count) {
103				if(strlen($word) > 0) {
104					$line = $word.";".$count;
105					if(substr($line,-1) != "\n") $line .= "\n";
106					fwrite($writeF, $line);
107				}
108			}
109			fclose($writeF);
110			if($conf['fperm']) chmod($fn.'.tmp', $conf['fperm']);
111			io_rename($fn.'.tmp', $fn.'.idx');
112			return true;
113		}
114}