Drupal investigation

SqliteProfilerStorage.php 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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__.'\SqliteProfilerStorage class is deprecated since Symfony 2.8 and will be removed in 3.0. Use FileProfilerStorage instead.', E_USER_DEPRECATED);
  12. /**
  13. * SqliteProfilerStorage stores profiling information in a SQLite database.
  14. *
  15. * @author Fabien Potencier <fabien@symfony.com>
  16. *
  17. * @deprecated Deprecated since Symfony 2.8, to be removed in Symfony 3.0.
  18. * Use {@link FileProfilerStorage} instead.
  19. */
  20. class SqliteProfilerStorage extends PdoProfilerStorage
  21. {
  22. /**
  23. * @throws \RuntimeException When neither of SQLite3 or PDO_SQLite extension is enabled
  24. */
  25. protected function initDb()
  26. {
  27. if (null === $this->db || $this->db instanceof \SQLite3) {
  28. if (0 !== strpos($this->dsn, 'sqlite')) {
  29. throw new \RuntimeException(sprintf('Please check your configuration. You are trying to use Sqlite with an invalid dsn "%s". The expected format is "sqlite:/path/to/the/db/file".', $this->dsn));
  30. }
  31. if (class_exists('SQLite3')) {
  32. $db = new \SQLite3(substr($this->dsn, 7, strlen($this->dsn)), \SQLITE3_OPEN_READWRITE | \SQLITE3_OPEN_CREATE);
  33. if (method_exists($db, 'busyTimeout')) {
  34. // busyTimeout only exists for PHP >= 5.3.3
  35. $db->busyTimeout(1000);
  36. }
  37. } elseif (class_exists('PDO') && in_array('sqlite', \PDO::getAvailableDrivers(), true)) {
  38. $db = new \PDO($this->dsn);
  39. } else {
  40. throw new \RuntimeException('You need to enable either the SQLite3 or PDO_SQLite extension for the profiler to run properly.');
  41. }
  42. $db->exec('PRAGMA temp_store=MEMORY; PRAGMA journal_mode=MEMORY;');
  43. $db->exec('CREATE TABLE IF NOT EXISTS sf_profiler_data (token STRING, data STRING, ip STRING, method STRING, url STRING, time INTEGER, parent STRING, created_at INTEGER, status_code INTEGER)');
  44. $db->exec('CREATE INDEX IF NOT EXISTS data_created_at ON sf_profiler_data (created_at)');
  45. $db->exec('CREATE INDEX IF NOT EXISTS data_ip ON sf_profiler_data (ip)');
  46. $db->exec('CREATE INDEX IF NOT EXISTS data_method ON sf_profiler_data (method)');
  47. $db->exec('CREATE INDEX IF NOT EXISTS data_url ON sf_profiler_data (url)');
  48. $db->exec('CREATE INDEX IF NOT EXISTS data_parent ON sf_profiler_data (parent)');
  49. $db->exec('CREATE UNIQUE INDEX IF NOT EXISTS data_token ON sf_profiler_data (token)');
  50. $this->db = $db;
  51. }
  52. return $this->db;
  53. }
  54. protected function exec($db, $query, array $args = array())
  55. {
  56. if ($db instanceof \SQLite3) {
  57. $stmt = $this->prepareStatement($db, $query);
  58. foreach ($args as $arg => $val) {
  59. $stmt->bindValue($arg, $val, is_int($val) ? \SQLITE3_INTEGER : \SQLITE3_TEXT);
  60. }
  61. $res = $stmt->execute();
  62. if (false === $res) {
  63. throw new \RuntimeException(sprintf('Error executing SQLite query "%s"', $query));
  64. }
  65. $res->finalize();
  66. } else {
  67. parent::exec($db, $query, $args);
  68. }
  69. }
  70. protected function fetch($db, $query, array $args = array())
  71. {
  72. $return = array();
  73. if ($db instanceof \SQLite3) {
  74. $stmt = $this->prepareStatement($db, $query);
  75. foreach ($args as $arg => $val) {
  76. $stmt->bindValue($arg, $val, is_int($val) ? \SQLITE3_INTEGER : \SQLITE3_TEXT);
  77. }
  78. $res = $stmt->execute();
  79. while ($row = $res->fetchArray(\SQLITE3_ASSOC)) {
  80. $return[] = $row;
  81. }
  82. $res->finalize();
  83. $stmt->close();
  84. } else {
  85. $return = parent::fetch($db, $query, $args);
  86. }
  87. return $return;
  88. }
  89. /**
  90. * {@inheritdoc}
  91. */
  92. protected function buildCriteria($ip, $url, $start, $end, $limit, $method)
  93. {
  94. $criteria = array();
  95. $args = array();
  96. if ($ip = preg_replace('/[^\d\.]/', '', $ip)) {
  97. $criteria[] = 'ip LIKE :ip';
  98. $args[':ip'] = '%'.$ip.'%';
  99. }
  100. if ($url) {
  101. $criteria[] = 'url LIKE :url ESCAPE "\"';
  102. $args[':url'] = '%'.addcslashes($url, '%_\\').'%';
  103. }
  104. if ($method) {
  105. $criteria[] = 'method = :method';
  106. $args[':method'] = $method;
  107. }
  108. if (!empty($start)) {
  109. $criteria[] = 'time >= :start';
  110. $args[':start'] = $start;
  111. }
  112. if (!empty($end)) {
  113. $criteria[] = 'time <= :end';
  114. $args[':end'] = $end;
  115. }
  116. return array($criteria, $args);
  117. }
  118. protected function close($db)
  119. {
  120. if ($db instanceof \SQLite3) {
  121. $db->close();
  122. }
  123. }
  124. }