extension.cache.dbm.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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. die('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')) { // PHP 4.3.0+
  78. if (!in_array('db3', dba_handlers())) {
  79. die('PHP is not compiled --with '.$cache_type.' support, required to use DBM style cache.');
  80. }
  81. }
  82. else { // PHP <= 4.2.3
  83. ob_start(); // nasty, buy the only way to check...
  84. phpinfo();
  85. $contents = ob_get_contents();
  86. ob_end_clean();
  87. if (!strstr($contents, $cache_type)) {
  88. die('PHP is not compiled --with '.$cache_type.' support, required to use DBM style cache.');
  89. }
  90. }
  91. // Create lock file if needed
  92. if (!file_exists($lock_filename)) {
  93. if (!touch($lock_filename)) {
  94. die('failed to create lock file: ' . $lock_filename);
  95. }
  96. }
  97. // Open lock file for writing
  98. if (!is_writeable($lock_filename)) {
  99. die('lock file: ' . $lock_filename . ' is not writable');
  100. }
  101. $this->lock = fopen($lock_filename, 'w');
  102. // Acquire exclusive write lock to lock file
  103. flock($this->lock, LOCK_EX);
  104. // Create dbm-file if needed
  105. if (!file_exists($dbm_filename)) {
  106. if (!touch($dbm_filename)) {
  107. die('failed to create dbm file: ' . $dbm_filename);
  108. }
  109. }
  110. // Try to open dbm file for writing
  111. $this->dba = @dba_open($dbm_filename, 'w', $cache_type);
  112. if (!$this->dba) {
  113. // Failed - create new dbm file
  114. $this->dba = dba_open($dbm_filename, 'n', $cache_type);
  115. if (!$this->dba) {
  116. die('failed to create dbm file: ' . $dbm_filename);
  117. }
  118. // Insert getID3 version number
  119. dba_insert(GETID3_VERSION, GETID3_VERSION, $this->dba);
  120. }
  121. // Init misc values
  122. $this->cache_type = $cache_type;
  123. $this->dbm_filename = $dbm_filename;
  124. // Register destructor
  125. register_shutdown_function(array($this, '__destruct'));
  126. // Check version number and clear cache if changed
  127. if (dba_fetch(GETID3_VERSION, $this->dba) != GETID3_VERSION) {
  128. $this->clear_cache();
  129. }
  130. parent::getID3();
  131. }
  132. // public: destuctor
  133. function __destruct() {
  134. // Close dbm file
  135. @dba_close($this->dba);
  136. // Release exclusive lock
  137. @flock($this->lock, LOCK_UN);
  138. // Close lock file
  139. @fclose($this->lock);
  140. }
  141. // public: clear cache
  142. function clear_cache() {
  143. // Close dbm file
  144. dba_close($this->dba);
  145. // Create new dbm file
  146. $this->dba = dba_open($this->dbm_filename, 'n', $this->cache_type);
  147. if (!$this->dba) {
  148. die('failed to clear cache/recreate dbm file: ' . $this->dbm_filename);
  149. }
  150. // Insert getID3 version number
  151. dba_insert(GETID3_VERSION, GETID3_VERSION, $this->dba);
  152. // Reregister shutdown function
  153. register_shutdown_function(array($this, '__destruct'));
  154. }
  155. // public: analyze file
  156. function analyze($filename) {
  157. if (file_exists($filename)) {
  158. // Calc key filename::mod_time::size - should be unique
  159. $key = $filename . '::' . filemtime($filename) . '::' . filesize($filename);
  160. // Loopup key
  161. $result = dba_fetch($key, $this->dba);
  162. // Hit
  163. if ($result !== false) {
  164. return unserialize($result);
  165. }
  166. }
  167. // Miss
  168. $result = parent::analyze($filename);
  169. // Save result
  170. if (file_exists($filename)) {
  171. dba_insert($key, serialize($result), $this->dba);
  172. }
  173. return $result;
  174. }
  175. }
  176. ?>