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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
|
<?php
/**
* Piwik - Open source web analytics
*
* @link http://piwik.org
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
* @version $Id$
*
* @category Piwik
* @package Piwik
*/
/**
*
* @package Piwik
*/
class Piwik_Site
{
/**
* @var int|null
*/
protected $id = null;
/**
* @var array
*/
public static $infoSites = array();
/**
* @param int $idsite
*/
function __construct($idsite)
{
$this->id = (int)$idsite;
if(!isset(self::$infoSites[$this->id]))
{
self::$infoSites[$this->id] = Piwik_SitesManager_API::getInstance()->getSiteFromId($this->id);
}
}
/**
* Sets the cached Site data with an array that associates site IDs with
* individual site data.
*
* @param array $sites The array of sites data. Indexed by site ID.
*/
public static function setSites($sites)
{
self::$infoSites = $sites;
}
/**
* Sets the cached Site data with a non-associated array of site data.
*
* @param array $sites The array of sites data.
*/
public static function setSitesFromArray($sites)
{
$sitesById = array();
foreach($sites as $site)
{
$sitesById[$site['idsite']] = $site;
}
self::setSites($sitesById);
}
/**
* @return string
*/
function __toString()
{
return "site id=".$this->getId().",
name=".$this->getName() .",
url = ". $this->getMainUrl() .",
IPs excluded = ".$this->getExcludedIps().",
timezone = ".$this->getTimezone().",
currency = ".$this->getCurrency().",
creation date = ".$this->getCreationDate();
}
/**
* Returns the name of the site
*
* @return string
*/
function getName()
{
return $this->get('name');
}
/**
* Returns the main url of the site
*
* @return string
*/
function getMainUrl()
{
return $this->get('main_url');
}
/**
* Returns the id of the site
*
* @return int
*/
function getId()
{
return $this->id;
}
/**
* Returns a site property
* @param string $name property to return
* @return mixed
* @throws Exception
*/
protected function get( $name)
{
if(!isset(self::$infoSites[$this->id][$name]))
{
throw new Exception('The requested website id = '.(int)$this->id.' (or its property '.$name.') couldn\'t be found');
}
return self::$infoSites[$this->id][$name];
}
/**
* Returns the creation date of the site
*
* @return Piwik_Date
*/
function getCreationDate()
{
$date = $this->get('ts_created');
return Piwik_Date::factory($date);
}
/**
* Returns the timezone of the size
*
* @return string
*/
function getTimezone()
{
return $this->get('timezone');
}
/**
* Returns the currency of the site
*
* @return string
*/
function getCurrency()
{
return $this->get('currency');
}
/**
* Returns the excluded ips of the site
*
* @return string
*/
function getExcludedIps()
{
return $this->get('excluded_ips');
}
/**
* Returns the excluded query parameters of the site
*
* @return string
*/
function getExcludedQueryParameters()
{
return $this->get('excluded_parameters');
}
/**
* Returns whether ecommerce id enabled for the site
*
* @return bool
*/
function isEcommerceEnabled()
{
return $this->get('ecommerce') == 1;
}
function getSearchKeywordParameters()
{
return $this->get('sitesearch_keyword_parameters');
}
function getSearchCategoryParameters()
{
return $this->get('sitesearch_category_parameters');
}
/**
* Returns whether Site Search Tracking is enabled for the site
*
* @return bool
*/
function isSiteSearchEnabled()
{
return $this->get('sitesearch') == 1;
}
/**
* Checks the given string for valid site ids and returns them as an array
*
* @param string $ids comma separated idSite list
* @return array of valid integer
*/
static public function getIdSitesFromIdSitesString( $ids )
{
if ($ids === 'all')
{
return Piwik_SitesManager_API::getInstance()->getSitesIdWithAtLeastViewAccess();
}
if(!is_array($ids))
{
$ids = explode(',', $ids);
}
$validIds = array();
foreach($ids as $id)
{
$id = trim($id);
if(!empty($id) && is_numeric($id) && $id > 0)
{
$validIds[] = $id;
}
}
$validIds = array_filter($validIds);
$validIds = array_unique($validIds);
return $validIds;
}
/**
* Clears the site cache
*/
static public function clearCache()
{
self::$infoSites = array();
}
/**
* Utility function. Returns the value of the specified field for the
* site with the specified ID.
*
* @param int|string $idsite The ID of the site whose data is being
* accessed.
* @param string $field The name of the field to get.
* @return mixed
*/
static protected function getFor($idsite, $field)
{
$idsite = (int)$idsite;
if (!isset(self::$infoSites[$idsite]))
{
self::$infoSites[$idsite] = Piwik_SitesManager_API::getInstance()->getSiteFromId($idsite);
}
return self::$infoSites[$idsite][$field];
}
/**
* Returns the name of the site with the specified ID.
*
* @param int $idsite The site ID.
* @return string
*/
static public function getNameFor($idsite)
{
return self::getFor($idsite, 'name');
}
/**
* Returns the timezone of the site with the specified ID.
*
* @param int $idsite The site ID.
* @return string
*/
static public function getTimezoneFor($idsite)
{
return self::getFor($idsite, 'timezone');
}
/**
* Returns the creation date of the site with the specified ID.
*
* @param int $idsite The site ID.
* @return string
*/
static public function getCreationDateFor($idsite)
{
return self::getFor($idsite, 'ts_created');
}
/**
* Returns the url for the site with the specified ID.
*
* @param int $idsite The site ID.
* @return string
*/
static public function getMainUrlFor($idsite)
{
return self::getFor($idsite, 'main_url');
}
/**
* Returns whether the site with the specified ID is ecommerce enabled
*
* @param int $idsite The site ID.
* @return string
*/
static public function isEcommerceEnabledFor($idsite)
{
return self::getFor($idsite, 'ecommerce') == 1;
}
/**
* Returns whether the site with the specified ID is Site Search enabled
*
* @param int $idsite The site ID.
* @return string
*/
static public function isSiteSearchEnabledFor($idsite)
{
return self::getFor($idsite, 'sitesearch') == 1;
}
/**
* Returns the currency of the site with the specified ID.
*
* @param int $idsite The site ID.
* @return string
*/
static public function getCurrencyFor($idsite)
{
return self::getFor($idsite, 'currency');
}
/**
* Returns the excluded IP addresses of the site with the specified ID.
*
* @param int $idsite The site ID.
* @return string
*/
static public function getExcludedIpsFor($idsite)
{
return self::getFor($idsite, 'excluded_ips');
}
/**
* Returns the excluded query parameters for the site with the specified ID.
*
* @param int $idsite The site ID.
* @return string
*/
static public function getExcludedQueryParametersFor($idsite)
{
return self::getFor($idsite, 'excluded_parameters');
}
}
|