-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Model.php
285 lines (236 loc) · 7.86 KB
/
Model.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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
<?php
/**
* Matomo - free/libre analytics platform
*
* @link https://matomo.org
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*/
namespace Piwik\Plugins\ReferrersManager;
use Piwik\Cache;
use Piwik\Container\StaticContainer;
use Piwik\Option;
use Piwik\Piwik;
use Piwik\Plugins\Referrers\SearchEngine;
use Piwik\Plugins\Referrers\Social;
/**
*
*/
class Model
{
const OPTION_KEY_DISABLE_DEFAULT_SOCIALS = 'disable_default_socials';
const OPTION_KEY_USERDEFINED_SOCIALS = 'userdefined_socials';
const OPTION_KEY_USERDEFINED_SEARCHENGINES = 'userdefined_searchengines';
public static function getInstance()
{
return StaticContainer::get('Piwik\Plugins\ReferrersManager\Model');
}
/**
* Returns if Matomo's built-in social list is used or not
*
* @return bool
*/
public function areDefaultSocialsDisabled()
{
return !!Option::get(self::OPTION_KEY_DISABLE_DEFAULT_SOCIALS);
}
/**
* Sets if Matomo's built-in social list should be used or not
*
* @param bool $disabled
*/
public function setDefaultSocialsDisabled($disabled = true)
{
Option::set(self::OPTION_KEY_DISABLE_DEFAULT_SOCIALS, $disabled);
$this->clearSocialCache();
}
/**
* Clears cache for social data
*/
public function clearSocialCache()
{
Option::clearCachedOption(self::OPTION_KEY_DISABLE_DEFAULT_SOCIALS);
Option::delete(Social::OPTION_STORAGE_NAME);
$cacheId = 'Social-' . Social::OPTION_STORAGE_NAME;
$cache = Cache::getEagerCache();
$cache->delete($cacheId);
\Piwik\Tracker\Cache::deleteTrackerCache();
}
/**
* Returns the list of userdefined socials
*
* @return array
*/
public function getUserDefinedSocials()
{
$socials = json_decode(Option::get(self::OPTION_KEY_USERDEFINED_SOCIALS), true);
if (!empty($socials)) {
return (array)$socials;
}
return [];
}
/**
* Sets user defined socials
*
* @param array $socialList
*/
public function setUserDefinedSocials($socialList = [])
{
Option::set(self::OPTION_KEY_USERDEFINED_SOCIALS, json_encode($socialList));
$this->clearSocialCache();
}
/**
* Returns the list of userdefined search engines
*
* @return array
*/
public function getUserDefinedSearchEngines()
{
$engines = json_decode(Option::get(self::OPTION_KEY_USERDEFINED_SEARCHENGINES), true);
if (!empty($engines)) {
// convert engines saved in legacy format
foreach ($engines as $url => $definition) {
if (!array_key_exists('name', $definition) && isset($definition[0]) && isset($definition[1])) {
$engines[$url] = array(
'name' => $definition[0],
'params' => $definition[1],
'backlink' => @$definition[2],
'charsets' => @$definition[3]
);
}
}
return $engines;
}
return [];
}
/**
* Sets user defined search engines
*
* @param array $engineList
*/
public function setUserDefinedSearchEngines($engineList = [])
{
Option::set(self::OPTION_KEY_USERDEFINED_SEARCHENGINES, json_encode($engineList));
$this->clearSearchEngineCache();
}
/**
* Clears cache for social data
*/
public function clearSearchEngineCache()
{
Option::clearCachedOption(self::OPTION_KEY_USERDEFINED_SEARCHENGINES);
Option::delete(SearchEngine::OPTION_STORAGE_NAME);
$cache = Cache::getEagerCache();
$cacheId = 'SearchEngine-' . SearchEngine::OPTION_STORAGE_NAME;
$cache->delete($cacheId);
\Piwik\Tracker\Cache::deleteTrackerCache();
}
/**
* Wrapper method to Matomo's internal method to return search engine data
* @return array
*/
public function getSearchEngines()
{
return \Piwik\Plugins\Referrers\SearchEngine::getInstance()->getDefinitions();
}
/**
* Returns all search engine information known to Matomo
*
* @return array
*/
public function getSearchEngineDefinitions()
{
$mergedSearchInfos = [];
$searchEngineInfos = $this->getSearchEngines();
foreach ($searchEngineInfos AS $url => $infos) {
$parameters = !is_array($infos['params']) ? $infos['params'] : implode(', ', $infos['params']);
if (empty($mergedSearchInfos[$infos['name']])) {
$mergedSearchInfos[$infos['name']] = [];
}
$mergedSearchInfos[$infos['name']][] = [
'url' => $url,
'parameters' => $parameters,
'backlink' => !empty($infos['backlink']) ? $infos['backlink'] : '',
'charset' => !empty($infos['charsets']) ? implode(', ', $infos['charsets']) : '',
];
}
ksort($mergedSearchInfos, SORT_LOCALE_STRING);
return $mergedSearchInfos;
}
/**
* Returns an array containing all logos for search engines
*
* @return array (name => logo-src)
*/
public function getSearchEngineLogos()
{
$searchEngineLogos = [];
$searchEngineNames = \Piwik\Plugins\Referrers\SearchEngine::getInstance()->getNames();
foreach ($searchEngineNames AS $name => $url) {
$searchEngineLogos[$name] = \Piwik\Plugins\Referrers\SearchEngine::getInstance()->getLogoFromUrl($url);
}
return $searchEngineLogos;
}
/**
* Wrapper method to Matomo' internal method to return search engine data
* @return array
*/
public function getSocials()
{
return \Piwik\Plugins\Referrers\Social::getInstance()->getDefinitions();
}
/**
* Returns all social informations known to Matomo
*
* @return array
*/
public function getSocialsDefinitions()
{
$mergedSocials = [];
$urls = $this->getSocials();
foreach ($urls AS $url => $name) {
$mergedSocials[urldecode($name)][] = $url;
}
ksort($mergedSocials, SORT_LOCALE_STRING);
return $mergedSocials;
}
/**
* Returns an array containing all logos for socials
*
* @return array (name => logo-src)
*/
public function getSocialsLogos()
{
$socialsLogos = [];
$urls = \Piwik\Plugins\Referrers\Social::getInstance()->getDefinitions();
foreach ($urls AS $url => $name) {
$socialsLogos[urldecode($name)] = \Piwik\Plugins\Referrers\Social::getInstance()->getLogoFromUrl($url);
}
return $socialsLogos;
}
public function detectSearchEngine($url)
{
$detectedEngine = SearchEngine::getInstance()->extractInformationFromUrl($url);
if (!empty($detectedEngine['name'])) {
$detectedEngine['image'] = SearchEngine::getInstance()->getLogoFromUrl(SearchEngine::getInstance()->getUrlFromName($detectedEngine['name']));
if ($detectedEngine['keywords'] === false) {
$detectedEngine['keywords'] = '<i>' . Piwik::translate('General_NotDefined',
Piwik::translate('General_ColumnKeyword')) . '</i>';
}
}
return $detectedEngine;
}
public function detectSocial($url)
{
$detectedSocial = Social::getInstance()->getSocialNetworkFromDomain($url);
if (!empty($detectedSocial) && $detectedSocial != Piwik::translate('General_Unknown')) {
$image = Social::getInstance()->getLogoFromUrl($url);
$detectedSocial = [
'name' => $detectedSocial,
'image' => $image
];
} else {
$detectedSocial = false;
}
return $detectedSocial;
}
}