PageRenderTime 47ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/getid3/extension.cache.dbm.php

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