PageRenderTime 53ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/core/Site.php

https://github.com/CodeYellowBV/piwik
PHP | 553 lines | 219 code | 49 blank | 285 comment | 19 complexity | c457e48cdcb37976695ad3cc05dd119f MD5 | raw file
Possible License(s): LGPL-3.0, JSON, MIT, GPL-3.0, LGPL-2.1, GPL-2.0, AGPL-1.0, BSD-2-Clause, BSD-3-Clause
  1. <?php
  2. /**
  3. * Piwik - free/libre analytics platform
  4. *
  5. * @link http://piwik.org
  6. * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
  7. *
  8. */
  9. namespace Piwik;
  10. use Exception;
  11. use Piwik\Plugins\SitesManager\API;
  12. /**
  13. * Provides access to individual [site entity](/guides/persistence-and-the-mysql-backend#websites-aka-sites) data
  14. * (including name, URL, etc.).
  15. *
  16. * **Data Cache**
  17. *
  18. * Site data can be cached in order to avoid performing too many queries.
  19. * If a method needs many site entities, it is more efficient to query all of what
  20. * you need beforehand via the **SitesManager** API, then cache it using {@link setSites()} or
  21. * {@link setSitesFromArray()}.
  22. *
  23. * Subsequent calls to `new Site($id)` will use the data in the cache instead of querying the database.
  24. *
  25. * ### Examples
  26. *
  27. * **Basic usage**
  28. *
  29. * $site = new Site($idSite);
  30. * $name = $site->getName();
  31. *
  32. * **Without allocation**
  33. *
  34. * $name = Site::getNameFor($idSite);
  35. *
  36. * @api
  37. */
  38. class Site
  39. {
  40. const DEFAULT_SITE_TYPE = "website";
  41. /**
  42. * @var int|null
  43. */
  44. protected $id = null;
  45. /**
  46. * @var array
  47. */
  48. protected static $infoSites = array();
  49. /**
  50. * Constructor.
  51. *
  52. * @param int $idsite The ID of the site we want data for.
  53. */
  54. public function __construct($idsite)
  55. {
  56. $this->id = (int)$idsite;
  57. if (!isset(self::$infoSites[$this->id])) {
  58. $site = API::getInstance()->getSiteFromId($this->id);
  59. self::setSite($this->id, $site);
  60. }
  61. }
  62. /**
  63. * Sets the cached site data with an array that associates site IDs with
  64. * individual site data.
  65. *
  66. * @param array $sites The array of sites data. Indexed by site ID. eg,
  67. *
  68. * array('1' => array('name' => 'Site 1', ...),
  69. * '2' => array('name' => 'Site 2', ...))`
  70. */
  71. public static function setSites($sites)
  72. {
  73. foreach($sites as $idsite => $site) {
  74. self::setSite($idsite, $site);
  75. }
  76. }
  77. /**
  78. * Sets a site information in memory (statically cached).
  79. *
  80. * Plugins can filter the website attributes before it is cached, eg. to change the website name,
  81. * creation date, etc.
  82. *
  83. * @param $idSite
  84. * @param $infoSite
  85. * @throws Exception if website or idsite is invalid
  86. */
  87. protected static function setSite($idSite, $infoSite)
  88. {
  89. if(empty($idSite) || empty($infoSite)) {
  90. throw new Exception("An unexpected website was found, check idSite in the request.");
  91. }
  92. /**
  93. * Triggered so plugins can modify website entities without modifying the database.
  94. *
  95. * This event should **not** be used to add data that is expensive to compute. If you
  96. * need to make HTTP requests or query the database for more information, this is not
  97. * the place to do it.
  98. *
  99. * **Example**
  100. *
  101. * Piwik::addAction('Site.setSite', function ($idSite, &$info) {
  102. * $info['name'] .= " (original)";
  103. * });
  104. *
  105. * @param int $idSite The ID of the website entity that will be modified.
  106. * @param array $infoSite The website entity. [Learn more.](/guides/persistence-and-the-mysql-backend#websites-aka-sites)
  107. */
  108. Piwik::postEvent('Site.setSite', array($idSite, &$infoSite));
  109. self::$infoSites[$idSite] = $infoSite;
  110. }
  111. /**
  112. * Sets the cached Site data with a non-associated array of site data.
  113. *
  114. * @param array $sites The array of sites data. eg,
  115. *
  116. * array(
  117. * array('idsite' => '1', 'name' => 'Site 1', ...),
  118. * array('idsite' => '2', 'name' => 'Site 2', ...),
  119. * )
  120. */
  121. public static function setSitesFromArray($sites)
  122. {
  123. foreach ($sites as $site) {
  124. self::setSite($site['idsite'], $site);
  125. }
  126. }
  127. /**
  128. * The Multisites reports displays the first calendar date as the earliest day available for all websites.
  129. * Also, today is the later "today" available across all timezones.
  130. * @param array $siteIds Array of IDs for each site being displayed.
  131. * @return Date[] of two Date instances. First is the min-date & the second
  132. * is the max date.
  133. * @ignore
  134. */
  135. public static function getMinMaxDateAcrossWebsites($siteIds)
  136. {
  137. $siteIds = self::getIdSitesFromIdSitesString($siteIds);
  138. $now = Date::now();
  139. $minDate = null;
  140. $maxDate = $now->subDay(1)->getTimestamp();
  141. foreach ($siteIds as $idsite) {
  142. // look for 'now' in the website's timezone
  143. $timezone = Site::getTimezoneFor($idsite);
  144. $date = Date::adjustForTimezone($now->getTimestamp(), $timezone);
  145. if ($date > $maxDate) {
  146. $maxDate = $date;
  147. }
  148. // look for the absolute minimum date
  149. $creationDate = Site::getCreationDateFor($idsite);
  150. $date = Date::adjustForTimezone(strtotime($creationDate), $timezone);
  151. if (is_null($minDate) || $date < $minDate) {
  152. $minDate = $date;
  153. }
  154. }
  155. return array(Date::factory($minDate), Date::factory($maxDate));
  156. }
  157. /**
  158. * Returns a string representation of the site this instance references.
  159. *
  160. * Useful for debugging.
  161. *
  162. * @return string
  163. */
  164. public function __toString()
  165. {
  166. return "site id=" . $this->getId() . ",
  167. name=" . $this->getName() . ",
  168. url = " . $this->getMainUrl() . ",
  169. IPs excluded = " . $this->getExcludedIps() . ",
  170. timezone = " . $this->getTimezone() . ",
  171. currency = " . $this->getCurrency() . ",
  172. creation date = " . $this->getCreationDate();
  173. }
  174. /**
  175. * Returns the name of the site.
  176. *
  177. * @return string
  178. * @throws Exception if data for the site cannot be found.
  179. */
  180. public function getName()
  181. {
  182. return $this->get('name');
  183. }
  184. /**
  185. * Returns the main url of the site.
  186. *
  187. * @return string
  188. * @throws Exception if data for the site cannot be found.
  189. */
  190. public function getMainUrl()
  191. {
  192. return $this->get('main_url');
  193. }
  194. /**
  195. * Returns the id of the site.
  196. *
  197. * @return int
  198. * @throws Exception if data for the site cannot be found.
  199. */
  200. public function getId()
  201. {
  202. return $this->id;
  203. }
  204. /**
  205. * Returns a site property by name.
  206. *
  207. * @param string $name Name of the property to return (eg, `'main_url'` or `'name'`).
  208. * @return mixed
  209. * @throws Exception
  210. */
  211. protected function get($name)
  212. {
  213. if (!isset(self::$infoSites[$this->id][$name])) {
  214. throw new Exception('The requested website id = ' . (int)$this->id . ' (or its property ' . $name . ') couldn\'t be found');
  215. }
  216. return self::$infoSites[$this->id][$name];
  217. }
  218. /**
  219. * Returns the website type (by default `"website"`, which means it is a single website).
  220. *
  221. * @return string
  222. */
  223. public function getType()
  224. {
  225. $type = $this->get('type');
  226. return $type;
  227. }
  228. /**
  229. * Returns the creation date of the site.
  230. *
  231. * @return Date
  232. * @throws Exception if data for the site cannot be found.
  233. */
  234. public function getCreationDate()
  235. {
  236. $date = $this->get('ts_created');
  237. return Date::factory($date);
  238. }
  239. /**
  240. * Returns the timezone of the size.
  241. *
  242. * @return string
  243. * @throws Exception if data for the site cannot be found.
  244. */
  245. public function getTimezone()
  246. {
  247. return $this->get('timezone');
  248. }
  249. /**
  250. * Returns the currency of the site.
  251. *
  252. * @return string
  253. * @throws Exception if data for the site cannot be found.
  254. */
  255. public function getCurrency()
  256. {
  257. return $this->get('currency');
  258. }
  259. /**
  260. * Returns the excluded ips of the site.
  261. *
  262. * @return string
  263. * @throws Exception if data for the site cannot be found.
  264. */
  265. public function getExcludedIps()
  266. {
  267. return $this->get('excluded_ips');
  268. }
  269. /**
  270. * Returns the excluded query parameters of the site.
  271. *
  272. * @return string
  273. * @throws Exception if data for the site cannot be found.
  274. */
  275. public function getExcludedQueryParameters()
  276. {
  277. return $this->get('excluded_parameters');
  278. }
  279. /**
  280. * Returns whether ecommerce is enabled for the site.
  281. *
  282. * @return bool
  283. * @throws Exception if data for the site cannot be found.
  284. */
  285. public function isEcommerceEnabled()
  286. {
  287. return $this->get('ecommerce') == 1;
  288. }
  289. /**
  290. * Returns the site search keyword query parameters for the site.
  291. *
  292. * @return string
  293. * @throws Exception if data for the site cannot be found.
  294. */
  295. public function getSearchKeywordParameters()
  296. {
  297. return $this->get('sitesearch_keyword_parameters');
  298. }
  299. /**
  300. * Returns the site search category query parameters for the site.
  301. *
  302. * @return string
  303. * @throws Exception if data for the site cannot be found.
  304. */
  305. public function getSearchCategoryParameters()
  306. {
  307. return $this->get('sitesearch_category_parameters');
  308. }
  309. /**
  310. * Returns whether Site Search Tracking is enabled for the site.
  311. *
  312. * @return bool
  313. * @throws Exception if data for the site cannot be found.
  314. */
  315. public function isSiteSearchEnabled()
  316. {
  317. return $this->get('sitesearch') == 1;
  318. }
  319. /**
  320. * Checks the given string for valid site IDs and returns them as an array.
  321. *
  322. * @param string|array $ids Comma separated idSite list, eg, `'1,2,3,4'` or an array of IDs, eg,
  323. * `array(1, 2, 3, 4)`.
  324. * @param bool|string $_restrictSitesToLogin Implementation detail. Used only when running as a scheduled task.
  325. * @return array An array of valid, unique integers.
  326. */
  327. static public function getIdSitesFromIdSitesString($ids, $_restrictSitesToLogin = false)
  328. {
  329. if ($ids === 'all') {
  330. return API::getInstance()->getSitesIdWithAtLeastViewAccess($_restrictSitesToLogin);
  331. }
  332. if(is_bool($ids)) {
  333. return array();
  334. }
  335. if (!is_array($ids)) {
  336. $ids = explode(',', $ids);
  337. }
  338. $validIds = array();
  339. foreach ($ids as $id) {
  340. $id = trim($id);
  341. if (!empty($id) && is_numeric($id) && $id > 0) {
  342. $validIds[] = $id;
  343. }
  344. }
  345. $validIds = array_filter($validIds);
  346. $validIds = array_unique($validIds);
  347. return $validIds;
  348. }
  349. /**
  350. * Clears the site data cache.
  351. *
  352. * See also {@link setSites()} and {@link setSitesFromArray()}.
  353. */
  354. static public function clearCache()
  355. {
  356. self::$infoSites = array();
  357. }
  358. /**
  359. * Utility function. Returns the value of the specified field for the
  360. * site with the specified ID.
  361. *
  362. * @param int $idsite The ID of the site whose data is being accessed.
  363. * @param bool|string $field The name of the field to get.
  364. * @return array|string
  365. */
  366. static protected function getFor($idsite, $field = false)
  367. {
  368. $idsite = (int)$idsite;
  369. if (!isset(self::$infoSites[$idsite])) {
  370. $site = API::getInstance()->getSiteFromId($idsite);
  371. self::setSite($idsite, $site);
  372. }
  373. if($field) {
  374. return self::$infoSites[$idsite][$field];
  375. }
  376. return self::$infoSites[$idsite];
  377. }
  378. /**
  379. * Returns all websites pre-cached
  380. *
  381. * @ignore
  382. */
  383. static public function getSites()
  384. {
  385. return self::$infoSites;
  386. }
  387. /**
  388. * @ignore
  389. */
  390. static public function getSite($id)
  391. {
  392. return self::getFor($id);
  393. }
  394. /**
  395. * Returns the name of the site with the specified ID.
  396. *
  397. * @param int $idsite The site ID.
  398. * @return string
  399. */
  400. static public function getNameFor($idsite)
  401. {
  402. return self::getFor($idsite, 'name');
  403. }
  404. /**
  405. * Returns the group of the site with the specified ID.
  406. *
  407. * @param int $idsite The site ID.
  408. * @return string
  409. */
  410. static public function getGroupFor($idsite)
  411. {
  412. return self::getFor($idsite, 'group');
  413. }
  414. /**
  415. * Returns the timezone of the site with the specified ID.
  416. *
  417. * @param int $idsite The site ID.
  418. * @return string
  419. */
  420. static public function getTimezoneFor($idsite)
  421. {
  422. return self::getFor($idsite, 'timezone');
  423. }
  424. /**
  425. * Returns the type of the site with the specified ID.
  426. *
  427. * @param $idsite
  428. * @return string
  429. */
  430. static public function getTypeFor($idsite)
  431. {
  432. return self::getFor($idsite, 'type');
  433. }
  434. /**
  435. * Returns the creation date of the site with the specified ID.
  436. *
  437. * @param int $idsite The site ID.
  438. * @return string
  439. */
  440. static public function getCreationDateFor($idsite)
  441. {
  442. return self::getFor($idsite, 'ts_created');
  443. }
  444. /**
  445. * Returns the url for the site with the specified ID.
  446. *
  447. * @param int $idsite The site ID.
  448. * @return string
  449. */
  450. static public function getMainUrlFor($idsite)
  451. {
  452. return self::getFor($idsite, 'main_url');
  453. }
  454. /**
  455. * Returns whether the site with the specified ID is ecommerce enabled or not.
  456. *
  457. * @param int $idsite The site ID.
  458. * @return string
  459. */
  460. static public function isEcommerceEnabledFor($idsite)
  461. {
  462. return self::getFor($idsite, 'ecommerce') == 1;
  463. }
  464. /**
  465. * Returns whether the site with the specified ID is Site Search enabled.
  466. *
  467. * @param int $idsite The site ID.
  468. * @return string
  469. */
  470. static public function isSiteSearchEnabledFor($idsite)
  471. {
  472. return self::getFor($idsite, 'sitesearch') == 1;
  473. }
  474. /**
  475. * Returns the currency of the site with the specified ID.
  476. *
  477. * @param int $idsite The site ID.
  478. * @return string
  479. */
  480. static public function getCurrencyFor($idsite)
  481. {
  482. return self::getFor($idsite, 'currency');
  483. }
  484. /**
  485. * Returns the excluded IP addresses of the site with the specified ID.
  486. *
  487. * @param int $idsite The site ID.
  488. * @return string
  489. */
  490. static public function getExcludedIpsFor($idsite)
  491. {
  492. return self::getFor($idsite, 'excluded_ips');
  493. }
  494. /**
  495. * Returns the excluded query parameters for the site with the specified ID.
  496. *
  497. * @param int $idsite The site ID.
  498. * @return string
  499. */
  500. static public function getExcludedQueryParametersFor($idsite)
  501. {
  502. return self::getFor($idsite, 'excluded_parameters');
  503. }
  504. }