extension.cache.dbm.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. <?php
  2. /////////////////////////////////////////////////////////////////
  3. /// getID3() by James Heinrich <info@getid3.org> //
  4. // available at http://getid3.sourceforge.net //
  5. // or http://www.getid3.org //
  6. // also https://github.com/JamesHeinrich/getID3 //
  7. /////////////////////////////////////////////////////////////////
  8. // //
  9. // extension.cache.dbm.php - part of getID3() //
  10. // Please see readme.txt for more information //
  11. // ///
  12. /////////////////////////////////////////////////////////////////
  13. // //
  14. // This extension written by Allan Hansen <ahØartemis*dk> //
  15. // ///
  16. /////////////////////////////////////////////////////////////////
  17. /**
  18. * This is a caching extension for getID3(). It works the exact same
  19. * way as the getID3 class, but return cached information very fast
  20. *
  21. * Example:
  22. *
  23. * Normal getID3 usage (example):
  24. *
  25. * require_once 'getid3/getid3.php';
  26. * $getID3 = new getID3;
  27. * $getID3->encoding = 'UTF-8';
  28. * $info1 = $getID3->analyze('file1.flac');
  29. * $info2 = $getID3->analyze('file2.wv');
  30. *
  31. * getID3_cached usage:
  32. *
  33. * require_once 'getid3/getid3.php';
  34. * require_once 'getid3/getid3/extension.cache.dbm.php';
  35. * $getID3 = new getID3_cached('db3', '/tmp/getid3_cache.dbm',
  36. * '/tmp/getid3_cache.lock');
  37. * $getID3->encoding = 'UTF-8';
  38. * $info1 = $getID3->analyze('file1.flac');
  39. * $info2 = $getID3->analyze('file2.wv');
  40. *
  41. *
  42. * Supported Cache Types
  43. *
  44. * SQL Databases: (use extension.cache.mysql)
  45. *
  46. * cache_type cache_options
  47. * -------------------------------------------------------------------
  48. * mysql host, database, username, password
  49. *
  50. *
  51. * DBM-Style Databases: (this extension)
  52. *
  53. * cache_type cache_options
  54. * -------------------------------------------------------------------
  55. * gdbm dbm_filename, lock_filename
  56. * ndbm dbm_filename, lock_filename
  57. * db2 dbm_filename, lock_filename
  58. * db3 dbm_filename, lock_filename
  59. * db4 dbm_filename, lock_filename (PHP5 required)
  60. *
  61. * PHP must have write access to both dbm_filename and lock_filename.
  62. *
  63. *
  64. * Recommended Cache Types
  65. *
  66. * Infrequent updates, many reads any DBM
  67. * Frequent updates mysql
  68. */
  69. class getID3_cached_dbm extends getID3
  70. {
  71. // public: constructor - see top of this file for cache type and cache_options
  72. public function __construct($cache_type, $dbm_filename, $lock_filename) {
  73. // Check for dba extension
  74. if (!extension_loaded('dba')) {
  75. throw new Exception('PHP is not compiled with dba support, required to use DBM style cache.');
  76. }
  77. // Check for specific dba driver
  78. if (!function_exists('dba_handlers') || !in_array($cache_type, dba_handlers())) {
  79. throw new Exception('PHP is not compiled --with '.$cache_type.' support, required to use DBM style cache.');
  80. }
  81. // Create lock file if needed
  82. if (!file_exists($lock_filename)) {
  83. if (!touch($lock_filename)) {
  84. throw new Exception('failed to create lock file: '.$lock_filename);
  85. }
  86. }
  87. // Open lock file for writing
  88. if (!is_writeable($lock_filename)) {
  89. throw new Exception('lock file: '.$lock_filename.' is not writable');
  90. }
  91. $this->lock = fopen($lock_filename, 'w');
  92. // Acquire exclusive write lock to lock file
  93. flock($this->lock, LOCK_EX);
  94. // Create dbm-file if needed
  95. if (!file_exists($dbm_filename)) {
  96. if (!touch($dbm_filename)) {
  97. throw new Exception('failed to create dbm file: '.$dbm_filename);
  98. }
  99. }
  100. // Try to open dbm file for writing
  101. $this->dba = dba_open($dbm_filename, 'w', $cache_type);
  102. if (!$this->dba) {
  103. // Failed - create new dbm file
  104. $this->dba = dba_open($dbm_filename, 'n', $cache_type);
  105. if (!$this->dba) {
  106. throw new Exception('failed to create dbm file: '.$dbm_filename);
  107. }
  108. // Insert getID3 version number
  109. dba_insert(getID3::VERSION, getID3::VERSION, $this->dba);
  110. }
  111. // Init misc values
  112. $this->cache_type = $cache_type;
  113. $this->dbm_filename = $dbm_filename;
  114. // Register destructor
  115. register_shutdown_function(array($this, '__destruct'));
  116. // Check version number and clear cache if changed
  117. if (dba_fetch(getID3::VERSION, $this->dba) != getID3::VERSION) {
  118. $this->clear_cache();
  119. }
  120. parent::__construct();
  121. }
  122. // public: destructor
  123. public function __destruct() {
  124. // Close dbm file
  125. dba_close($this->dba);
  126. // Release exclusive lock
  127. flock($this->lock, LOCK_UN);
  128. // Close lock file
  129. fclose($this->lock);
  130. }
  131. // public: clear cache
  132. public function clear_cache() {
  133. // Close dbm file
  134. dba_close($this->dba);
  135. // Create new dbm file
  136. $this->dba = dba_open($this->dbm_filename, 'n', $this->cache_type);
  137. if (!$this->dba) {
  138. throw new Exception('failed to clear cache/recreate dbm file: '.$this->dbm_filename);
  139. }
  140. // Insert getID3 version number
  141. dba_insert(getID3::VERSION, getID3::VERSION, $this->dba);
  142. // Re-register shutdown function
  143. register_shutdown_function(array($this, '__destruct'));
  144. }
  145. // public: analyze file
  146. public function analyze($filename) {
  147. if (file_exists($filename)) {
  148. // Calc key filename::mod_time::size - should be unique
  149. $key = $filename.'::'.filemtime($filename).'::'.filesize($filename);
  150. // Loopup key
  151. $result = dba_fetch($key, $this->dba);
  152. // Hit
  153. if ($result !== false) {
  154. return unserialize($result);
  155. }
  156. }
  157. // Miss
  158. $result = parent::analyze($filename);
  159. // Save result
  160. if (file_exists($filename)) {
  161. dba_insert($key, serialize($result), $this->dba);
  162. }
  163. return $result;
  164. }
  165. }