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