extension.cache.dbm.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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. /////////////////////////////////////////////////////////////////
  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. // public: constructor - see top of this file for cache type and cache_options
  71. function getID3_cached_dbm($cache_type, $dbm_filename, $lock_filename) {
  72. // Check for dba extension
  73. if (!extension_loaded('dba')) {
  74. throw new Exception('PHP is not compiled with dba support, required to use DBM style cache.');
  75. }
  76. // Check for specific dba driver
  77. if (!function_exists('dba_handlers') || !in_array($cache_type, dba_handlers())) {
  78. throw new Exception('PHP is not compiled --with '.$cache_type.' support, required to use DBM style cache.');
  79. }
  80. // Create lock file if needed
  81. if (!file_exists($lock_filename)) {
  82. if (!touch($lock_filename)) {
  83. throw new Exception('failed to create lock file: '.$lock_filename);
  84. }
  85. }
  86. // Open lock file for writing
  87. if (!is_writeable($lock_filename)) {
  88. throw new Exception('lock file: '.$lock_filename.' is not writable');
  89. }
  90. $this->lock = fopen($lock_filename, 'w');
  91. // Acquire exclusive write lock to lock file
  92. flock($this->lock, LOCK_EX);
  93. // Create dbm-file if needed
  94. if (!file_exists($dbm_filename)) {
  95. if (!touch($dbm_filename)) {
  96. throw new Exception('failed to create dbm file: '.$dbm_filename);
  97. }
  98. }
  99. // Try to open dbm file for writing
  100. $this->dba = dba_open($dbm_filename, 'w', $cache_type);
  101. if (!$this->dba) {
  102. // Failed - create new dbm file
  103. $this->dba = dba_open($dbm_filename, 'n', $cache_type);
  104. if (!$this->dba) {
  105. throw new Exception('failed to create dbm file: '.$dbm_filename);
  106. }
  107. // Insert getID3 version number
  108. dba_insert(getID3::VERSION, getID3::VERSION, $this->dba);
  109. }
  110. // Init misc values
  111. $this->cache_type = $cache_type;
  112. $this->dbm_filename = $dbm_filename;
  113. // Register destructor
  114. register_shutdown_function(array($this, '__destruct'));
  115. // Check version number and clear cache if changed
  116. if (dba_fetch(getID3::VERSION, $this->dba) != getID3::VERSION) {
  117. $this->clear_cache();
  118. }
  119. parent::getID3();
  120. }
  121. // public: destuctor
  122. function __destruct() {
  123. // Close dbm file
  124. dba_close($this->dba);
  125. // Release exclusive lock
  126. flock($this->lock, LOCK_UN);
  127. // Close lock file
  128. fclose($this->lock);
  129. }
  130. // public: clear cache
  131. function clear_cache() {
  132. // Close dbm file
  133. dba_close($this->dba);
  134. // Create new dbm file
  135. $this->dba = dba_open($this->dbm_filename, 'n', $this->cache_type);
  136. if (!$this->dba) {
  137. throw new Exception('failed to clear cache/recreate dbm file: '.$this->dbm_filename);
  138. }
  139. // Insert getID3 version number
  140. dba_insert(getID3::VERSION, getID3::VERSION, $this->dba);
  141. // Re-register shutdown function
  142. register_shutdown_function(array($this, '__destruct'));
  143. }
  144. // public: analyze file
  145. function analyze($filename) {
  146. if (file_exists($filename)) {
  147. // Calc key filename::mod_time::size - should be unique
  148. $key = $filename.'::'.filemtime($filename).'::'.filesize($filename);
  149. // Loopup key
  150. $result = dba_fetch($key, $this->dba);
  151. // Hit
  152. if ($result !== false) {
  153. return unserialize($result);
  154. }
  155. }
  156. // Miss
  157. $result = parent::analyze($filename);
  158. // Save result
  159. if (file_exists($filename)) {
  160. dba_insert($key, serialize($result), $this->dba);
  161. }
  162. return $result;
  163. }
  164. }
  165. ?>