PluginProbe ʕ •ᴥ•ʔ
Matomo Analytics – Powerful, Privacy-First Insights for WordPress / 1.3.1
Matomo Analytics – Powerful, Privacy-First Insights for WordPress v1.3.1
5.11.1 5.11.0 5.10.2 5.10.1 trunk 1.0.2 1.0.3 1.0.4 1.0.5 1.0.6 1.1.0 1.1.1 1.1.2 1.1.3 1.2.0 1.3.0 1.3.1 1.3.2 4.0.0 4.0.1 4.0.2 4.0.3 4.0.4 4.1.0 4.1.1 4.1.2 4.1.3 4.10.0 4.11.0 4.12.0 4.13.0 4.13.2 4.13.3 4.13.4 4.13.5 4.14.0 4.14.1 4.14.2 4.15.0 4.15.1 4.15.2 4.15.3 4.2.0 4.3.0 4.3.1 4.4.1 4.4.2 4.5.0 4.6.0 5.0.1 5.0.2 5.0.3 5.0.4 5.0.5 5.0.6 5.0.7 5.0.8 5.1.0 5.1.1 5.1.2 5.1.3 5.1.4 5.1.5 5.1.6 5.1.7 5.10.0 5.2.0 5.2.1 5.2.2 5.3.0 5.3.1 5.3.2 5.3.3 5.6.0 5.6.1 5.7.0 5.7.1 5.8.0 5.8.1 5.8.2
matomo / app / core / Site.php
matomo / app / core Last commit date
API 6 years ago Access 6 years ago Application 6 years ago Archive 6 years ago ArchiveProcessor 6 years ago Archiver 6 years ago AssetManager 6 years ago Auth 6 years ago Category 6 years ago CliMulti 6 years ago Columns 6 years ago Composer 6 years ago Concurrency 6 years ago Config 6 years ago Container 6 years ago CronArchive 6 years ago DataAccess 5 years ago DataFiles 6 years ago DataTable 6 years ago Db 6 years ago DeviceDetector 5 years ago Email 6 years ago Exception 6 years ago Http 6 years ago Intl 6 years ago Mail 6 years ago Measurable 6 years ago Menu 6 years ago Metrics 6 years ago Notification 6 years ago Period 6 years ago Plugin 6 years ago ProfessionalServices 6 years ago Report 6 years ago ReportRenderer 6 years ago Scheduler 6 years ago Segment 6 years ago Session 6 years ago Settings 6 years ago Tracker 5 years ago Translation 6 years ago UpdateCheck 6 years ago Updater 6 years ago Updates 6 years ago Validators 6 years ago View 6 years ago ViewDataTable 6 years ago Visualization 6 years ago Widget 6 years ago .htaccess 6 years ago Access.php 6 years ago Archive.php 6 years ago ArchiveProcessor.php 6 years ago AssetManager.php 6 years ago Auth.php 6 years ago BaseFactory.php 6 years ago Cache.php 6 years ago CacheId.php 6 years ago CliMulti.php 6 years ago Common.php 6 years ago Config.php 6 years ago Console.php 6 years ago Context.php 6 years ago Cookie.php 5 years ago CronArchive.php 5 years ago DataArray.php 6 years ago DataTable.php 6 years ago Date.php 6 years ago Db.php 6 years ago DbHelper.php 6 years ago Development.php 6 years ago DeviceDetectorFactory.php 6 years ago ErrorHandler.php 6 years ago EventDispatcher.php 6 years ago ExceptionHandler.php 6 years ago FileIntegrity.php 6 years ago Filechecks.php 6 years ago Filesystem.php 6 years ago FrontController.php 6 years ago Http.php 6 years ago IP.php 6 years ago Log.php 6 years ago LogDeleter.php 6 years ago Mail.php 6 years ago Metrics.php 6 years ago MetricsFormatter.php 6 years ago Nonce.php 5 years ago Notification.php 6 years ago NumberFormatter.php 6 years ago Option.php 5 years ago Period.php 6 years ago Piwik.php 6 years ago Plugin.php 6 years ago Profiler.php 6 years ago ProxyHeaders.php 6 years ago ProxyHttp.php 6 years ago QuickForm2.php 6 years ago RankingQuery.php 6 years ago Registry.php 6 years ago ReportRenderer.php 6 years ago ScheduledTask.php 6 years ago Segment.php 6 years ago Sequence.php 6 years ago Session.php 6 years ago SettingsPiwik.php 6 years ago SettingsServer.php 6 years ago Singleton.php 6 years ago Site.php 6 years ago TCPDF.php 6 years ago TaskScheduler.php 6 years ago Theme.php 6 years ago Timer.php 6 years ago Tracker.php 6 years ago Translate.php 6 years ago Twig.php 6 years ago Unzip.php 6 years ago UpdateCheck.php 6 years ago Updater.php 6 years ago Updates.php 6 years ago Url.php 6 years ago UrlHelper.php 6 years ago Version.php 5 years ago View.php 6 years ago bootstrap.php 6 years ago dispatch.php 6 years ago testMinimumPhpVersion.php 6 years ago
Site.php
684 lines
1 <?php
2 /**
3 * Piwik - free/libre analytics platform
4 *
5 * @link https://matomo.org
6 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
7 *
8 */
9
10 namespace Piwik;
11
12 use Exception;
13 use Piwik\Container\StaticContainer;
14 use Piwik\Exception\UnexpectedWebsiteFoundException;
15 use Piwik\Intl\Data\Provider\CurrencyDataProvider;
16 use Piwik\Plugins\SitesManager\API;
17
18 /**
19 * Provides access to individual [site entity](/guides/persistence-and-the-mysql-backend#websites-aka-sites) data
20 * (including name, URL, etc.).
21 *
22 * **Data Cache**
23 *
24 * Site data can be cached in order to avoid performing too many queries.
25 * If a method needs many site entities, it is more efficient to query all of what
26 * you need beforehand via the **SitesManager** API, then cache it using {@link setSites()} or
27 * {@link setSitesFromArray()}.
28 *
29 * Subsequent calls to `new Site($id)` will use the data in the cache instead of querying the database.
30 *
31 * ### Examples
32 *
33 * **Basic usage**
34 *
35 * $site = new Site($idSite);
36 * $name = $site->getName();
37 *
38 * **Without allocation**
39 *
40 * $name = Site::getNameFor($idSite);
41 *
42 * @api
43 */
44 class Site
45 {
46 const DEFAULT_SITE_TYPE = "website";
47
48 private static $intProperties = [
49 'idsite',
50 'ecommerce',
51 'sitesearch',
52 'exclude_unknown_urls',
53 'keep_url_fragment',
54 ];
55
56 /**
57 * @var int|null
58 */
59 protected $id = null;
60
61 /**
62 * @var array
63 */
64 protected static $infoSites = array();
65
66 private $site = array();
67
68 /**
69 * Constructor.
70 *
71 * @param int $idsite The ID of the site we want data for.
72 * @throws UnexpectedWebsiteFoundException
73 */
74 public function __construct($idsite)
75 {
76 $this->id = (int) $idsite;
77
78 if (!empty(self::$infoSites[$this->id])) {
79 $site = self::$infoSites[$this->id];
80 } else {
81 $site = API::getInstance()->getSiteFromId($this->id);
82
83 if (empty($site)) {
84 throw new UnexpectedWebsiteFoundException('The requested website id = ' . (int)$this->id . ' couldn\'t be found');
85 }
86 }
87
88 $sites = array(&$site);
89 self::triggerSetSitesEvent($sites);
90 self::setSiteFromArray($this->id, $site);
91
92 $this->site = $site;
93
94 // for serialized format to be predictable across php/mysql/pdo/mysqli versions, make sure the int props stay ints
95 foreach (self::$intProperties as $propertyName) {
96 $this->site[$propertyName] = (int)$this->site[$propertyName];
97 }
98 }
99
100 /**
101 * Sets the cached site data with an array that associates site IDs with
102 * individual site data.
103 *
104 * @param array $sites The array of sites data. Indexed by site ID. eg,
105 *
106 * array('1' => array('name' => 'Site 1', ...),
107 * '2' => array('name' => 'Site 2', ...))`
108 */
109 public static function setSites($sites)
110 {
111 self::triggerSetSitesEvent($sites);
112
113 foreach ($sites as $idsite => $site) {
114 self::setSiteFromArray($idsite, $site);
115 }
116 }
117
118 private static function triggerSetSitesEvent(&$sites)
119 {
120 /**
121 * Triggered so plugins can modify website entities without modifying the database.
122 *
123 * This event should **not** be used to add data that is expensive to compute. If you
124 * need to make HTTP requests or query the database for more information, this is not
125 * the place to do it.
126 *
127 * **Example**
128 *
129 * Piwik::addAction('Site.setSites', function (&$sites) {
130 * foreach ($sites as &$site) {
131 * $site['name'] .= " (original)";
132 * }
133 * });
134 *
135 * @param array $sites An array of website entities. [Learn more.](/guides/persistence-and-the-mysql-backend#websites-aka-sites)
136 *
137 * This is not yet public as it doesn't work 100% accurately. Eg if `setSiteFromArray()` is called directly this event will not be triggered.
138 * @ignore
139 */
140 Piwik::postEvent('Site.setSites', array(&$sites));
141 }
142
143 /**
144 * Sets a site information in memory (statically cached).
145 *
146 * Plugins can filter the website attributes before it is cached, eg. to change the website name,
147 * creation date, etc.
148 *
149 * @param $idSite
150 * @param $infoSite
151 * @throws Exception if website or idsite is invalid
152 * @internal
153 */
154 public static function setSiteFromArray($idSite, $infoSite)
155 {
156 if (empty($idSite) || empty($infoSite)) {
157 throw new UnexpectedWebsiteFoundException("An unexpected website was found in the request: website id was set to '$idSite' .");
158 }
159
160 self::$infoSites[$idSite] = $infoSite;
161 }
162
163 /**
164 * Sets the cached Site data with a non-associated array of site data.
165 *
166 * This method will trigger the `Sites.setSites` event modifying `$sites` before setting cached
167 * site data. In other words, this method will change the site data before it is cached and then
168 * return the modified array.
169 *
170 * @param array $sites The array of sites data. eg,
171 *
172 * array(
173 * array('idsite' => '1', 'name' => 'Site 1', ...),
174 * array('idsite' => '2', 'name' => 'Site 2', ...),
175 * )
176 * @return array The modified array.
177 * @deprecated
178 * @internal
179 */
180 public static function setSitesFromArray($sites)
181 {
182 self::triggerSetSitesEvent($sites);
183
184 foreach ($sites as $site) {
185 $idSite = null;
186 if (!empty($site['idsite'])) {
187 $idSite = $site['idsite'];
188 }
189
190 self::setSiteFromArray($idSite, $site);
191 }
192
193 return $sites;
194 }
195
196 /**
197 * The Multisites reports displays the first calendar date as the earliest day available for all websites.
198 * Also, today is the later "today" available across all timezones.
199 * @param array $siteIds Array of IDs for each site being displayed.
200 * @return Date[] of two Date instances. First is the min-date & the second
201 * is the max date.
202 * @ignore
203 */
204 public static function getMinMaxDateAcrossWebsites($siteIds)
205 {
206 $siteIds = self::getIdSitesFromIdSitesString($siteIds);
207 $now = Date::now();
208
209 $minDate = null;
210 $maxDate = $now->subDay(1)->getTimestamp();
211 foreach ($siteIds as $idsite) {
212 // look for 'now' in the website's timezone
213 $timezone = Site::getTimezoneFor($idsite);
214 $date = Date::adjustForTimezone($now->getTimestamp(), $timezone);
215 if ($date > $maxDate) {
216 $maxDate = $date;
217 }
218
219 // look for the absolute minimum date
220 $creationDate = Site::getCreationDateFor($idsite);
221 $date = Date::adjustForTimezone(strtotime($creationDate), $timezone);
222 if (is_null($minDate) || $date < $minDate) {
223 $minDate = $date;
224 }
225 }
226
227 return array(Date::factory($minDate), Date::factory($maxDate));
228 }
229
230 /**
231 * Returns a string representation of the site this instance references.
232 *
233 * Useful for debugging.
234 *
235 * @return string
236 */
237 public function __toString()
238 {
239 return "site id=" . $this->getId() . ",
240 name=" . $this->getName() . ",
241 url = " . $this->getMainUrl() . ",
242 IPs excluded = " . $this->getExcludedIps() . ",
243 timezone = " . $this->getTimezone() . ",
244 currency = " . $this->getCurrency() . ",
245 creation date = " . $this->getCreationDate();
246 }
247
248 /**
249 * Returns the name of the site.
250 *
251 * @return string
252 * @throws Exception if data for the site cannot be found.
253 */
254 public function getName()
255 {
256 return $this->get('name');
257 }
258
259 /**
260 * Returns the main url of the site.
261 *
262 * @return string
263 * @throws Exception if data for the site cannot be found.
264 */
265 public function getMainUrl()
266 {
267 return $this->get('main_url');
268 }
269
270 /**
271 * Returns the id of the site.
272 *
273 * @return int
274 * @throws Exception if data for the site cannot be found.
275 */
276 public function getId()
277 {
278 return $this->id;
279 }
280
281 /**
282 * Returns a site property by name.
283 *
284 * @param string $name Name of the property to return (eg, `'main_url'` or `'name'`).
285 * @return mixed
286 * @throws Exception
287 */
288 protected function get($name)
289 {
290 if (isset($this->site[$name])) {
291 return $this->site[$name];
292 }
293
294 throw new Exception("The property $name could not be found on the website ID " . (int)$this->id);
295 }
296
297 /**
298 * Returns the website type (by default `"website"`, which means it is a single website).
299 *
300 * @return string
301 */
302 public function getType()
303 {
304 $type = $this->get('type');
305 return $type;
306 }
307
308 /**
309 * Returns the creation date of the site.
310 *
311 * @return Date
312 * @throws Exception if data for the site cannot be found.
313 */
314 public function getCreationDate()
315 {
316 $date = $this->get('ts_created');
317 return Date::factory($date);
318 }
319
320 /**
321 * Returns the timezone of the size.
322 *
323 * @return string
324 * @throws Exception if data for the site cannot be found.
325 */
326 public function getTimezone()
327 {
328 return $this->get('timezone');
329 }
330
331 /**
332 * Returns the currency of the site.
333 *
334 * @return string
335 * @throws Exception if data for the site cannot be found.
336 */
337 public function getCurrency()
338 {
339 return $this->get('currency');
340 }
341
342 /**
343 * Returns the excluded ips of the site.
344 *
345 * @return string
346 * @throws Exception if data for the site cannot be found.
347 */
348 public function getExcludedIps()
349 {
350 return $this->get('excluded_ips');
351 }
352
353 /**
354 * Returns the excluded query parameters of the site.
355 *
356 * @return string
357 * @throws Exception if data for the site cannot be found.
358 */
359 public function getExcludedQueryParameters()
360 {
361 return $this->get('excluded_parameters');
362 }
363
364 /**
365 * Returns whether ecommerce is enabled for the site.
366 *
367 * @return bool
368 * @throws Exception if data for the site cannot be found.
369 */
370 public function isEcommerceEnabled()
371 {
372 return $this->get('ecommerce') == 1;
373 }
374
375 /**
376 * Returns the site search keyword query parameters for the site.
377 *
378 * @return string
379 * @throws Exception if data for the site cannot be found.
380 */
381 public function getSearchKeywordParameters()
382 {
383 return $this->get('sitesearch_keyword_parameters');
384 }
385
386 /**
387 * Returns the site search category query parameters for the site.
388 *
389 * @return string
390 * @throws Exception if data for the site cannot be found.
391 */
392 public function getSearchCategoryParameters()
393 {
394 return $this->get('sitesearch_category_parameters');
395 }
396
397 /**
398 * Returns whether Site Search Tracking is enabled for the site.
399 *
400 * @return bool
401 * @throws Exception if data for the site cannot be found.
402 */
403 public function isSiteSearchEnabled()
404 {
405 return $this->get('sitesearch') == 1;
406 }
407
408 /**
409 * Returns the user that created this site.
410 *
411 * @return string|null If null, the site was created before the creation user was tracked.
412 */
413 public function getCreatorLogin()
414 {
415 return $this->get('creator_login');
416 }
417
418 /**
419 * Checks the given string for valid site IDs and returns them as an array.
420 *
421 * @param string|array $ids Comma separated idSite list, eg, `'1,2,3,4'` or an array of IDs, eg,
422 * `array(1, 2, 3, 4)`.
423 * @param bool|string $_restrictSitesToLogin Implementation detail. Used only when running as a scheduled task.
424 * @return array An array of valid, unique integers.
425 */
426 public static function getIdSitesFromIdSitesString($ids, $_restrictSitesToLogin = false)
427 {
428 if ($ids === 'all') {
429 return API::getInstance()->getSitesIdWithAtLeastViewAccess($_restrictSitesToLogin);
430 }
431
432 if (is_bool($ids)) {
433 return array();
434 }
435 if (!is_array($ids)) {
436 $ids = explode(',', $ids);
437 }
438 $validIds = array();
439 foreach ($ids as $id) {
440 $id = trim($id);
441 if (!empty($id) && is_numeric($id) && $id > 0) {
442 $validIds[] = $id;
443 }
444 }
445 $validIds = array_filter($validIds);
446 $validIds = array_unique($validIds);
447
448 return $validIds;
449 }
450
451 /**
452 * Clears the site data cache.
453 *
454 * See also {@link setSites()} and {@link setSitesFromArray()}.
455 */
456 public static function clearCache()
457 {
458 self::$infoSites = array();
459 }
460
461 /**
462 * Clears the site data cache.
463 *
464 * See also {@link setSites()} and {@link setSitesFromArray()}.
465 */
466 public static function clearCacheForSite($idSite)
467 {
468 $idSite = (int)$idSite;
469 unset(self::$infoSites[$idSite]);
470 }
471
472 /**
473 * Utility function. Returns the value of the specified field for the
474 * site with the specified ID.
475 *
476 * @param int $idsite The ID of the site whose data is being accessed.
477 * @param string $field The name of the field to get.
478 * @return string
479 */
480 protected static function getFor($idsite, $field)
481 {
482 if (!isset(self::$infoSites[$idsite])) {
483 $site = API::getInstance()->getSiteFromId($idsite);
484 self::setSiteFromArray($idsite, $site);
485 }
486
487 return self::$infoSites[$idsite][$field];
488 }
489
490 /**
491 * Returns all websites pre-cached
492 *
493 * @ignore
494 */
495 public static function getSites()
496 {
497 return self::$infoSites;
498 }
499
500 /**
501 * @ignore
502 */
503 public static function getSite($idsite)
504 {
505 $idsite = (int)$idsite;
506
507 if (!isset(self::$infoSites[$idsite])) {
508 $site = API::getInstance()->getSiteFromId($idsite);
509 self::setSiteFromArray($idsite, $site);
510 }
511
512 return self::$infoSites[$idsite];
513 }
514
515 /**
516 * Returns the name of the site with the specified ID.
517 *
518 * @param int $idsite The site ID.
519 * @return string
520 */
521 public static function getNameFor($idsite)
522 {
523 return self::getFor($idsite, 'name');
524 }
525
526 /**
527 * Returns the group of the site with the specified ID.
528 *
529 * @param int $idsite The site ID.
530 * @return string
531 */
532 public static function getGroupFor($idsite)
533 {
534 return self::getFor($idsite, 'group');
535 }
536
537 /**
538 * Returns the timezone of the site with the specified ID.
539 *
540 * @param int $idsite The site ID.
541 * @return string
542 */
543 public static function getTimezoneFor($idsite)
544 {
545 return self::getFor($idsite, 'timezone');
546 }
547
548 /**
549 * Returns the type of the site with the specified ID.
550 *
551 * @param $idsite
552 * @return string
553 */
554 public static function getTypeFor($idsite)
555 {
556 return self::getFor($idsite, 'type');
557 }
558
559 /**
560 * Returns the creation date of the site with the specified ID.
561 *
562 * @param int $idsite The site ID.
563 * @return string
564 */
565 public static function getCreationDateFor($idsite)
566 {
567 return self::getFor($idsite, 'ts_created');
568 }
569
570 /**
571 * Returns the url for the site with the specified ID.
572 *
573 * @param int $idsite The site ID.
574 * @return string
575 */
576 public static function getMainUrlFor($idsite)
577 {
578 return self::getFor($idsite, 'main_url');
579 }
580
581 /**
582 * Returns whether the site with the specified ID is ecommerce enabled or not.
583 *
584 * @param int $idsite The site ID.
585 * @return string
586 */
587 public static function isEcommerceEnabledFor($idsite)
588 {
589 return self::getFor($idsite, 'ecommerce') == 1;
590 }
591
592 /**
593 * Returns whether the site with the specified ID is Site Search enabled.
594 *
595 * @param int $idsite The site ID.
596 * @return string
597 */
598 public static function isSiteSearchEnabledFor($idsite)
599 {
600 return self::getFor($idsite, 'sitesearch') == 1;
601 }
602
603 /**
604 * Returns the currency of the site with the specified ID.
605 *
606 * @param int $idsite The site ID.
607 * @return string
608 */
609 public static function getCurrencyFor($idsite)
610 {
611 return self::getFor($idsite, 'currency');
612 }
613
614 /**
615 * Returns the currency of the site with the specified ID.
616 *
617 * @param int $idsite The site ID.
618 * @return string
619 */
620 public static function getCurrencySymbolFor($idsite)
621 {
622 $currencyCode = self::getCurrencyFor($idsite);
623 $key = 'Intl_CurrencySymbol_' . $currencyCode;
624 $symbol = Piwik::translate($key);
625
626 if ($key === $symbol) {
627 return $currencyCode;
628 }
629
630 return $symbol;
631 }
632
633
634 /**
635 * Returns the list of all known currency symbols.
636 *
637 * @return array An array mapping currency codes to their respective currency symbols
638 * and a description, eg, `array('USD' => array('$', 'US dollar'))`.
639 *
640 * @deprecated Use Piwik\Intl\Data\Provider\CurrencyDataProvider instead.
641 * @see \Piwik\Intl\Data\Provider\CurrencyDataProvider::getCurrencyList()
642 * @api
643 */
644 public static function getCurrencyList()
645 {
646 /** @var CurrencyDataProvider $dataProvider */
647 $dataProvider = StaticContainer::get('Piwik\Intl\Data\Provider\CurrencyDataProvider');
648 return $dataProvider->getCurrencyList();
649 }
650
651 /**
652 * Returns the excluded IP addresses of the site with the specified ID.
653 *
654 * @param int $idsite The site ID.
655 * @return string
656 */
657 public static function getExcludedIpsFor($idsite)
658 {
659 return self::getFor($idsite, 'excluded_ips');
660 }
661
662 /**
663 * Returns the excluded query parameters for the site with the specified ID.
664 *
665 * @param int $idsite The site ID.
666 * @return string
667 */
668 public static function getExcludedQueryParametersFor($idsite)
669 {
670 return self::getFor($idsite, 'excluded_parameters');
671 }
672
673 /**
674 * Returns the user that created this site.
675 *
676 * @param int $idsite The site ID.
677 * @return string|null If null, the site was created before the creation user was tracked.
678 */
679 public static function getCreatorLoginFor($idsite)
680 {
681 return self::getFor($idsite, 'creator_login');
682 }
683 }
684