This repository has been archived by the owner on May 7, 2022. It is now read-only.
forked from timo-bes/piwik-sitesearch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSiteSearch.php
222 lines (188 loc) · 7.16 KB
/
SiteSearch.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
<?php
/**
* Piwik - Open source web analytics
* SiteSearch Plugin
*
* @link http://piwik.org
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*
* @author Timo Besenreuther, EZdesign.de
*
* @category Piwik_Plugins
* @package Piwik_SiteSearch
*/
class Piwik_SiteSearch extends Piwik_Plugin {
/** Information about this plugin */
public function getInformation() {
return array(
'description' => Piwik_Translate('SiteSearch_PluginDescription'),
'author' => 'Timo Besenreuther, EZdesign',
'author_homepage' => 'http://www.ezdesign.de/',
'version' => '0.1.3',
'translationAvailable' => true,
'TrackerPlugin' => true
);
}
/** Install the plugin */
public function install() {
$query1 = 'ALTER IGNORE TABLE `'.Piwik_Common::prefixTable('site').'` '
. 'ADD `sitesearch_url` VARCHAR( 100 ) NULL, '
. 'ADD `sitesearch_parameter` VARCHAR( 100 ) NULL';
$query2 = 'ALTER IGNORE TABLE `'.Piwik_Common::prefixTable('log_action').'` '
. 'ADD `search_term` INTEGER NULL DEFAULT NULL';
$query3 = 'ALTER IGNORE TABLE `'.Piwik_Common::prefixTable('log_action').'` '
. 'ADD INDEX `search_term` (`search_term`)';
$query4 = 'CREATE TABLE `'.Piwik_Common::prefixTable('log_sitesearch').'` ( '
. '`id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY, '
. '`idsite` INT NOT NULL, '
. '`search_term` VARCHAR(255) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL, '
. '`results` INT NOT NULL)';
try {
Zend_Registry::get('db')->query($query1);
} catch (Exception $e) {
// if the column already exist do not throw error
}
try {
Zend_Registry::get('db')->query($query2);
} catch (Exception $e) {}
try {
Zend_Registry::get('db')->query($query3);
} catch (Exception $e) {}
try {
Zend_Registry::get('db')->query($query4);
} catch (Exception $e) {}
}
/** Uninstall the plugin */
public function uninstall() {
$query = 'ALTER TABLE `'.Piwik_Common::prefixTable('site').'` '
. 'DROP `sitesearch_url`, '
. 'DROP `sitesearch_parameter`';
Zend_Registry::get('db')->query($query);
$query = 'ALTER TABLE `'.Piwik_Common::prefixTable('log_action').'` '
. 'DROP `search_term`';
Zend_Registry::get('db')->query($query);
$query = 'DROP TABLE `'.Piwik_Common::prefixTable('log_sitesearch').'`';
Zend_Registry::get('db')->query($query);
}
/** Register Hooks */
public function getListHooksRegistered() {
$hooks = array(
'AssetManager.getJsFiles' => 'getJsFiles',
'AssetManager.getCssFiles' => 'getCssFiles',
'Menu.add' => 'addMenu',
'AdminMenu.add' => 'addAdminMenu',
'WidgetsList.add' => 'addWidgets',
'Tracker.Action.record' => 'logResults',
'ArchiveProcessing_Day.compute' => 'archiveDay',
'ArchiveProcessing_Period.compute' => 'archivePeriod',
'Common.fetchWebsiteAttributes' => 'recordWebsiteDataInCache',
'API.getReportMetadata' => 'getReportMetadata'
);
return $hooks;
}
/** The report metadata allows Piwik Mobile to discover the plugin */
public function getReportMetadata($notification) {
$reports = &$notification->getNotificationObject();
$metrics = array('unique_hits' => Piwik_Translate('SiteSearch_UniqueHits'));
$reports[] = array(
'category' => Piwik_Translate('Actions_Actions'),
'name' => Piwik_Translate('SiteSearch_SiteSearch'),
'module' => 'SiteSearch',
'action' => 'getPiwikMobileReport',
'dimension' => Piwik_Translate('SiteSearch_Keyword'),
'metrics' => $metrics,
'processedMetrics' => false,
'order' => 50
);
}
/** Add SiteSearch config to tracker cache */
public function recordWebsiteDataInCache($notification) {
$idsite = $notification->getNotificationInfo();
$cache =& $notification->getNotificationObject();
$sql = '
SELECT sitesearch_url, sitesearch_parameter
FROM '.Piwik_Common::prefixTable('site').' AS site
WHERE idsite = '.intval($idsite).'
';
$result = Piwik_FetchAll($sql);
$site = $result[0];
$cache['sitesearch_url'] = $site['sitesearch_url'];
$cache['sitesearch_parameter'] = $site['sitesearch_parameter'];
}
/** Add JavaScript */
public function getJsFiles($notification) {
$jsFiles = &$notification->getNotificationObject();
$jsFiles[] = 'plugins/SiteSearch/templates/sitesearch.js';
}
/** Add CSS */
public function getCssFiles($notification) {
$cssFiles = &$notification->getNotificationObject();
$cssFiles[] = 'plugins/SiteSearch/templates/sitesearch.css';
}
/** Normal menu hook */
public function addMenu() {
Piwik_AddMenu('Actions_Actions', 'SiteSearch_SiteSearch',
array('module' => 'SiteSearch', 'action' => 'index'));
}
/** Admin menu hook */
public function addAdminMenu() {
Piwik_AddAdminMenu('SiteSearch_SiteSearch',
array('module' => 'SiteSearch', 'action' => 'admin'),
Piwik::isUserIsSuperUser(), 8);
}
/** Provide Widgets */
public function addWidgets() {
Piwik_AddWidget('Site Search',
Piwik_Translate('SiteSearch_MostPopularInternalSearches'),
'SiteSearch', 'keywordsWidget');
Piwik_AddWidget('Site Search',
Piwik_Translate('SiteSearch_InternalSearchEvolution'),
'SiteSearch', 'evolutionWidget');
Piwik_AddWidget('Site Search',
Piwik_Translate('SiteSearch_PercentageOfSearchUsers'),
'SiteSearch', 'searchPercentageWidget');
}
/** Build archive for a day */
public function archiveDay($notification) {
$archiveProcessing = $notification->getNotificationObject();
Piwik_SiteSearch_Archive::archiveDay($archiveProcessing);
}
/** Build archive for a period */
public function archivePeriod($notification) {
$archiveProcessing = $notification->getNotificationObject();
Piwik_SiteSearch_Archive::archivePeriod($archiveProcessing);
}
/** Logger hook: log number of results, if available */
public function logResults($notification) {
$action = $notification->getNotificationObject();
$idaction = $action->getIdActionUrl();
// load site config from tracker cache
$info = $notification->getNotificationInfo();
$idsite = $info['idSite'];
$site = Piwik_Common::getCacheWebsiteAttributes($idsite);
$site['idsite'] = $idsite;
// search results passed via JS tracker
$data = Piwik_Common::getRequestVar('data', '');
$data = Piwik_Common::unsanitizeInputValue($data);
$data = json_decode($data, true);
$resultCount = false;
if (isset($data['SiteSearch_Results'])) {
$resultCount = intval($data['SiteSearch_Results']);
}
if (!empty($site['sitesearch_url']) && !empty($site['sitesearch_parameter'])) {
// check whether action is a site search
$url = preg_quote($site['sitesearch_url'], '/');
$param = preg_quote($site['sitesearch_parameter'], '/');
$regex = '/'.$url.'(.*)(&|\?)'.$param.'=(.*?)(&|$)/i';
$actionUrl = $action->getActionUrl();
if (preg_match($regex, $actionUrl, $matches)) {
require_once PIWIK_INCLUDE_PATH .'/plugins/SiteSearch/Archive.php';
require_once PIWIK_INCLUDE_PATH .'/plugins/SiteSearch/Db.php';
Piwik_SiteSearch_Archive::logAction(array(
'idaction' => $idaction,
'name' => $actionUrl
), $site['idsite'], $site, $resultCount);
}
}
}
}