extension.cache.dbm.php 6.4 KB

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