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 pathAPI.php
192 lines (157 loc) · 5.77 KB
/
API.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
<?php
/**
* Piwik - Open source web analytics
* SiteSearch Plugin
* API
*
* @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_API {
// remember idSearch for filtering associated pages
private $idSearch;
// singleton instance
static private $instance = null;
/** Get singleton instance
* @return Piwik_SiteSearch_API */
static public function getInstance() {
if (self::$instance == null) {
self::$instance = new self;
}
return self::$instance;
}
/** Get a site configuration */
private function getSite($idSite) {
return Piwik_SitesManager_API::getInstance()->getSiteFromId($idSite);
}
/** Convert date to sql ready string */
private function convertDate($date) {
return Piwik_Date::factory($date)->toString();
}
/** Filter DataTable for search id */
private function filterDataTable($dataTable, $idSearch) {
$this->idSearch = $idSearch;
// other filter can be queued before calling this method
$dataTable->queueFilter('ColumnCallbackDeleteRow',
array(Piwik_SiteSearch_Archive::SEARCH_TERM_ID,
array($this, 'doFilterDataTable')));
$dataTable->applyQueuedFilters();
}
public function doFilterDataTable($idSearch) {
return $idSearch == $this->idSearch;
}
/** Returns period object
* @return Piwik_Period */
private function getPeriod($date, $period) {
return Piwik_Period::factory($period, Piwik_Date::factory($date));
}
/** Get evolution of search
* @return Piwik_DataTable */
public function getSearchEvolution($idSite, $period, $date) {
Piwik::checkUserHasViewAccess($idSite);
$idSearch = Piwik_Common::getRequestVar('idSearch', false);
if (!$idSearch) {
// render a overview of all keywords
// data is taken from numeric archive
$dataTable = Piwik_SiteSearch_Archive::getDataTable(
array('totalSearches', 'visitsWithSearches'), $idSite, $period,
$date, true);
} else {
// render overview for only one keyword
// data is taken from general keyword blob archive
$dataTable = Piwik_SiteSearch_Archive::getDataTable(
'keywords', $idSite, $period, $date);
$dataTable->queueFilter('ReplaceColumnNames', array(array(
'label' => 'label_hidden')));
$this->filterDataTable($dataTable, $idSearch);
}
return $dataTable;
}
/** Get evolution of search percentage
* @return Piwik_DataTable */
public function getSearchPercentageEvolution($idSite, $period, $date) {
Piwik::checkUserHasViewAccess($idSite);
$archive = Piwik_Archive::build($idSite, $period, $date);
$dataTable = $archive->getDataTableFromNumeric(
array('SiteSearch_visitsWithSearches', 'nb_visits'));
$dataTable->filter('ColumnCallbackAddColumnQuotient',
array('search_percentage', 'SiteSearch_visitsWithSearches',
'nb_visits', 4));
$dataTable->filter('ColumnCallbackReplace',
array('search_percentage', array($this, 'quotientToPercentageSafe')));
return $dataTable;
}
public function quotientToPercentageSafe($quotient) {
return 100 * $quotient;
}
/** Get search refinements
* @return Piwik_DataTable */
public function getSearchRefinements($idSite, $period, $date) {
Piwik::checkUserHasViewAccess($idSite);
$dataTable = Piwik_SiteSearch_Archive::getDataTable(
'refinements', $idSite, $period, $date);
$idSearch = Piwik_Common::getRequestVar('idSearch', false);
$this->filterDataTable($dataTable, $idSearch);
return $dataTable;
}
/** Get the most popular search keywords
* @return Piwik_DataTable */
public function getSearchKeywords($idSite, $period, $date) {
Piwik::checkUserHasViewAccess($idSite);
$period = $this->getPeriod($date, $period);
return Piwik_SiteSearch_Archive::getDataTable(
'keywords', $idSite, $period, $date);
}
/** Get keywords without search results
* @return Piwik_DataTable */
public function getNoResults($idSite, $period, $date) {
Piwik::checkUserHasViewAccess($idSite);
$period = $this->getPeriod($date, $period);
return Piwik_SiteSearch_Archive::getDataTable(
'noResults', $idSite, $period, $date);
}
/** Get the next sites after keyword was searched
* @return Piwik_DataTable */
public function getFollowingPages($idSite, $period, $date) {
Piwik::checkUserHasViewAccess($idSite);
return $this->getAssociatedPages($idSite, true, $period, $date);
}
/** Get the next sites before keyword was searched
* @return Piwik_DataTable */
public function getPreviousPages($idSite, $period, $date) {
Piwik::checkUserHasViewAccess($idSite);
return $this->getAssociatedPages($idSite, false, $period, $date);
}
/** Get table containing informatino about associated pages
* @return Piwik_DataTable */
private function getAssociatedPages($idSite, $following, $period, $date) {
Piwik::checkUserHasViewAccess($idSite);
$idSearch = intval(Piwik_Common::getRequestVar('idSearch', 0));
$name = ($following ? 'following' : 'previous').'Pages';
$dataTable = Piwik_SiteSearch_Archive
::getDataTable($name, $idSite, $period, $date);
if (!$idSearch) {
return $dataTable;
}
$this->filterDataTable($dataTable, $idSearch);
return $dataTable;
}
/** This method is used for accessing the Piwik Mobile report */
public function getPiwikMobileReport($idSite, $period, $date, $segment=false, $columns=false) {
Piwik::checkUserHasViewAccess($idSite);
$dataTable = $this->getSearchKeywords($idSite, $period, $date);
$dataTable->queueFilter('ReplaceColumnNames', array(array(
Piwik_SiteSearch_Archive::UNIQUE_HITS => 'unique_hits',
'label' => 'label_old',
Piwik_SiteSearch_Archive::SEARCH_TERM => 'label')));
$dataTable->applyQueuedFilters();
$dataTable->filter('Sort', array('unique_hits', 'desc'));
return $dataTable;
}
}
?>