-
Notifications
You must be signed in to change notification settings - Fork 6
/
action.php
114 lines (106 loc) · 3.22 KB
/
action.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
<?php
/**
* SearchStats Plugin: This plugin records the search words and displays stats in the admin section
*
* @license GPL 2 (http://www.gnu.org/licenses/gpl.html)
* @author Michael Schuh <[email protected]>
*/
if(!defined('DOKU_INC')) die();
if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
require_once DOKU_PLUGIN.'action.php';
class action_plugin_searchstats extends DokuWiki_Action_Plugin {
function getInfo() {
return array(
'author' => 'Michael Schuh',
'email' => '[email protected]',
'date' => @file_get_contents(DOKU_PLUGIN.'searchstats/VERSION'),
'name' => 'Searchstats plugin (action, admin component)',
'desc' => 'This plugin records the search words and displays stats in the admin section',
'url' => 'http://blog.imho.at/20100902/artikel/dokuwiki-plugin-searchstats',
);
}
function register(Doku_Event_Handler $controller) {
$controller->register_hook('SEARCH_QUERY_FULLPAGE', 'BEFORE', $this,
'_getSearchWords');
}
function getSearchWordArray($amount = false) {
$helper = plugin_load('helper', 'searchstats');
if(is_object($helper)) {
$wordArray = $helper->getSearchWordArray($amount);
return $wordArray;
}
return array();
}
/**
* Gets searchwords
*
* @author Michael Schuh <[email protected]>
*/
function _getSearchWords(&$event, $param) {
if(function_exists('idx_get_indexer')) {
$q = ft_queryParser(idx_get_indexer(),$event->data['query']);
}
else {
$q = ft_queryParser($event->data['query']);
}
if(is_array($q['highlight'])) {
$this->_checkSaveFolder();
foreach($q['words'] as $saveWord) {
if(strlen(trim($saveWord)) > 0) {
//remove ;
$saveWord = str_replace(';', '', $saveWord);
$this->_saveSearchWord($saveWord);
}
}
}
}
function _getSaveFolder() {
$helper = plugin_load('helper', 'searchstats');
return $helper->_getSaveFolder();
}
function _checkSaveFolder() {
io_mkdir_p($this->_getSaveFolder());
}
function _getIndexFileName($saveWord) {
return $this->_getSaveFolder().'/'.strlen($saveWord);
}
/**
* Adds searchword in index file
*
* @author Michael Schuh <[email protected]>
*/
function _saveSearchWord($saveWord) {
$fn = $this->_getIndexFileName($saveWord);
$writeF = @fopen($fn.'.tmp', 'w');
if(!$writeF) {
return false;
}
$readF = @fopen($fn.'.idx', 'r');
$wordArray = array();
if($readF) {
while (!feof($readF)) {
$line = fgets($readF, 4096);
$lineArray = explode(';', $line);
if(is_array($lineArray) && strlen($lineArray[0]) > 0 && $lineArray[1])
$wordArray[$lineArray[0]] = $lineArray[1];
}
}
if(isset($wordArray[$saveWord])) {
$wordArray[$saveWord] = $wordArray[$saveWord]+1;
}
else {
$wordArray[$saveWord] = 1;
}
foreach($wordArray as $word => $count) {
if(strlen($word) > 0) {
$line = $word.";".$count;
if(substr($line,-1) != "\n") $line .= "\n";
fwrite($writeF, $line);
}
}
fclose($writeF);
if($conf['fperm']) chmod($fn.'.tmp', $conf['fperm']);
io_rename($fn.'.tmp', $fn.'.idx');
return true;
}
}