Drupal investigation

MongoDbProfilerStorage.php 7.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\HttpKernel\Profiler;
  11. @trigger_error('The '.__NAMESPACE__.'\MongoDbProfilerStorage class is deprecated since Symfony 2.8 and will be removed in 3.0. Use FileProfilerStorage instead.', E_USER_DEPRECATED);
  12. /**
  13. * @deprecated Deprecated since Symfony 2.8, to be removed in Symfony 3.0.
  14. * Use {@link FileProfilerStorage} instead.
  15. */
  16. class MongoDbProfilerStorage implements ProfilerStorageInterface
  17. {
  18. protected $dsn;
  19. protected $lifetime;
  20. private $mongo;
  21. /**
  22. * Constructor.
  23. *
  24. * @param string $dsn A data source name
  25. * @param string $username Not used
  26. * @param string $password Not used
  27. * @param int $lifetime The lifetime to use for the purge
  28. */
  29. public function __construct($dsn, $username = '', $password = '', $lifetime = 86400)
  30. {
  31. $this->dsn = $dsn;
  32. $this->lifetime = (int) $lifetime;
  33. }
  34. /**
  35. * {@inheritdoc}
  36. */
  37. public function find($ip, $url, $limit, $method, $start = null, $end = null)
  38. {
  39. $cursor = $this->getMongo()->find($this->buildQuery($ip, $url, $method, $start, $end), array('_id', 'parent', 'ip', 'method', 'url', 'time', 'status_code'))->sort(array('time' => -1))->limit($limit);
  40. $tokens = array();
  41. foreach ($cursor as $profile) {
  42. $tokens[] = $this->getData($profile);
  43. }
  44. return $tokens;
  45. }
  46. /**
  47. * {@inheritdoc}
  48. */
  49. public function purge()
  50. {
  51. $this->getMongo()->remove(array());
  52. }
  53. /**
  54. * {@inheritdoc}
  55. */
  56. public function read($token)
  57. {
  58. $profile = $this->getMongo()->findOne(array('_id' => $token, 'data' => array('$exists' => true)));
  59. if (null !== $profile) {
  60. $profile = $this->createProfileFromData($this->getData($profile));
  61. }
  62. return $profile;
  63. }
  64. /**
  65. * {@inheritdoc}
  66. */
  67. public function write(Profile $profile)
  68. {
  69. $this->cleanup();
  70. $record = array(
  71. '_id' => $profile->getToken(),
  72. 'parent' => $profile->getParentToken(),
  73. 'data' => base64_encode(serialize($profile->getCollectors())),
  74. 'ip' => $profile->getIp(),
  75. 'method' => $profile->getMethod(),
  76. 'url' => $profile->getUrl(),
  77. 'time' => $profile->getTime(),
  78. 'status_code' => $profile->getStatusCode(),
  79. );
  80. $result = $this->getMongo()->update(array('_id' => $profile->getToken()), array_filter($record, function ($v) { return !empty($v); }), array('upsert' => true));
  81. return (bool) (isset($result['ok']) ? $result['ok'] : $result);
  82. }
  83. /**
  84. * Internal convenience method that returns the instance of the MongoDB Collection.
  85. *
  86. * @return \MongoCollection
  87. *
  88. * @throws \RuntimeException
  89. */
  90. protected function getMongo()
  91. {
  92. if (null !== $this->mongo) {
  93. return $this->mongo;
  94. }
  95. if (!$parsedDsn = $this->parseDsn($this->dsn)) {
  96. throw new \RuntimeException(sprintf('Please check your configuration. You are trying to use MongoDB with an invalid dsn "%s". The expected format is "mongodb://[user:pass@]host/database/collection"', $this->dsn));
  97. }
  98. list($server, $database, $collection) = $parsedDsn;
  99. $mongoClass = version_compare(phpversion('mongo'), '1.3.0', '<') ? '\Mongo' : '\MongoClient';
  100. $mongo = new $mongoClass($server);
  101. return $this->mongo = $mongo->selectCollection($database, $collection);
  102. }
  103. /**
  104. * @param array $data
  105. *
  106. * @return Profile
  107. */
  108. protected function createProfileFromData(array $data)
  109. {
  110. $profile = $this->getProfile($data);
  111. if ($data['parent']) {
  112. $parent = $this->getMongo()->findOne(array('_id' => $data['parent'], 'data' => array('$exists' => true)));
  113. if ($parent) {
  114. $profile->setParent($this->getProfile($this->getData($parent)));
  115. }
  116. }
  117. $profile->setChildren($this->readChildren($data['token']));
  118. return $profile;
  119. }
  120. /**
  121. * @param string $token
  122. *
  123. * @return Profile[] An array of Profile instances
  124. */
  125. protected function readChildren($token)
  126. {
  127. $profiles = array();
  128. $cursor = $this->getMongo()->find(array('parent' => $token, 'data' => array('$exists' => true)));
  129. foreach ($cursor as $d) {
  130. $profiles[] = $this->getProfile($this->getData($d));
  131. }
  132. return $profiles;
  133. }
  134. protected function cleanup()
  135. {
  136. $this->getMongo()->remove(array('time' => array('$lt' => time() - $this->lifetime)));
  137. }
  138. /**
  139. * @param string $ip
  140. * @param string $url
  141. * @param string $method
  142. * @param int $start
  143. * @param int $end
  144. *
  145. * @return array
  146. */
  147. private function buildQuery($ip, $url, $method, $start, $end)
  148. {
  149. $query = array();
  150. if (!empty($ip)) {
  151. $query['ip'] = $ip;
  152. }
  153. if (!empty($url)) {
  154. $query['url'] = $url;
  155. }
  156. if (!empty($method)) {
  157. $query['method'] = $method;
  158. }
  159. if (!empty($start) || !empty($end)) {
  160. $query['time'] = array();
  161. }
  162. if (!empty($start)) {
  163. $query['time']['$gte'] = $start;
  164. }
  165. if (!empty($end)) {
  166. $query['time']['$lte'] = $end;
  167. }
  168. return $query;
  169. }
  170. /**
  171. * @param array $data
  172. *
  173. * @return array
  174. */
  175. private function getData(array $data)
  176. {
  177. return array(
  178. 'token' => $data['_id'],
  179. 'parent' => isset($data['parent']) ? $data['parent'] : null,
  180. 'ip' => isset($data['ip']) ? $data['ip'] : null,
  181. 'method' => isset($data['method']) ? $data['method'] : null,
  182. 'url' => isset($data['url']) ? $data['url'] : null,
  183. 'time' => isset($data['time']) ? $data['time'] : null,
  184. 'data' => isset($data['data']) ? $data['data'] : null,
  185. 'status_code' => isset($data['status_code']) ? $data['status_code'] : null,
  186. );
  187. }
  188. /**
  189. * @param array $data
  190. *
  191. * @return Profile
  192. */
  193. private function getProfile(array $data)
  194. {
  195. $profile = new Profile($data['token']);
  196. $profile->setIp($data['ip']);
  197. $profile->setMethod($data['method']);
  198. $profile->setUrl($data['url']);
  199. $profile->setTime($data['time']);
  200. $profile->setCollectors(unserialize(base64_decode($data['data'])));
  201. return $profile;
  202. }
  203. /**
  204. * @param string $dsn
  205. *
  206. * @return null|array Array($server, $database, $collection)
  207. */
  208. private function parseDsn($dsn)
  209. {
  210. if (!preg_match('#^(mongodb://.*)/(.*)/(.*)$#', $dsn, $matches)) {
  211. return;
  212. }
  213. $server = $matches[1];
  214. $database = $matches[2];
  215. $collection = $matches[3];
  216. preg_match('#^mongodb://(([^:]+):?(.*)(?=@))?@?([^/]*)(.*)$#', $server, $matchesServer);
  217. if ('' == $matchesServer[5] && '' != $matches[2]) {
  218. $server .= '/'.$matches[2];
  219. }
  220. return array($server, $database, $collection);
  221. }
  222. }