File indexing completed on 2024-12-22 05:33:09
0001 <?php 0002 ///////////////////////////////////////////////////////////////// 0003 /// getID3() by James Heinrich <info@getid3.org> // 0004 // available at http://getid3.sourceforge.net // 0005 // or http://www.getid3.org // 0006 // also https://github.com/JamesHeinrich/getID3 // 0007 ///////////////////////////////////////////////////////////////// 0008 // // 0009 // extension.cache.dbm.php - part of getID3() // 0010 // Please see readme.txt for more information // 0011 // /// 0012 ///////////////////////////////////////////////////////////////// 0013 // // 0014 // This extension written by Allan Hansen <ahØartemis*dk> // 0015 // /// 0016 ///////////////////////////////////////////////////////////////// 0017 0018 0019 /** 0020 * This is a caching extension for getID3(). It works the exact same 0021 * way as the getID3 class, but return cached information very fast 0022 * 0023 * Example: 0024 * 0025 * Normal getID3 usage (example): 0026 * 0027 * require_once 'getid3/getid3.php'; 0028 * $getID3 = new getID3; 0029 * $getID3->encoding = 'UTF-8'; 0030 * $info1 = $getID3->analyze('file1.flac'); 0031 * $info2 = $getID3->analyze('file2.wv'); 0032 * 0033 * getID3_cached usage: 0034 * 0035 * require_once 'getid3/getid3.php'; 0036 * require_once 'getid3/getid3/extension.cache.dbm.php'; 0037 * $getID3 = new getID3_cached('db3', '/tmp/getid3_cache.dbm', 0038 * '/tmp/getid3_cache.lock'); 0039 * $getID3->encoding = 'UTF-8'; 0040 * $info1 = $getID3->analyze('file1.flac'); 0041 * $info2 = $getID3->analyze('file2.wv'); 0042 * 0043 * 0044 * Supported Cache Types 0045 * 0046 * SQL Databases: (use extension.cache.mysql) 0047 * 0048 * cache_type cache_options 0049 * ------------------------------------------------------------------- 0050 * mysql host, database, username, password 0051 * 0052 * 0053 * DBM-Style Databases: (this extension) 0054 * 0055 * cache_type cache_options 0056 * ------------------------------------------------------------------- 0057 * gdbm dbm_filename, lock_filename 0058 * ndbm dbm_filename, lock_filename 0059 * db2 dbm_filename, lock_filename 0060 * db3 dbm_filename, lock_filename 0061 * db4 dbm_filename, lock_filename (PHP5 required) 0062 * 0063 * PHP must have write access to both dbm_filename and lock_filename. 0064 * 0065 * 0066 * Recommended Cache Types 0067 * 0068 * Infrequent updates, many reads any DBM 0069 * Frequent updates mysql 0070 */ 0071 0072 0073 class getID3_cached_dbm extends getID3 0074 { 0075 0076 // public: constructor - see top of this file for cache type and cache_options 0077 public function getID3_cached_dbm($cache_type, $dbm_filename, $lock_filename) { 0078 0079 // Check for dba extension 0080 if (!extension_loaded('dba')) { 0081 throw new Exception('PHP is not compiled with dba support, required to use DBM style cache.'); 0082 } 0083 0084 // Check for specific dba driver 0085 if (!function_exists('dba_handlers') || !in_array($cache_type, dba_handlers())) { 0086 throw new Exception('PHP is not compiled --with '.$cache_type.' support, required to use DBM style cache.'); 0087 } 0088 0089 // Create lock file if needed 0090 if (!file_exists($lock_filename)) { 0091 if (!touch($lock_filename)) { 0092 throw new Exception('failed to create lock file: '.$lock_filename); 0093 } 0094 } 0095 0096 // Open lock file for writing 0097 if (!is_writeable($lock_filename)) { 0098 throw new Exception('lock file: '.$lock_filename.' is not writable'); 0099 } 0100 $this->lock = fopen($lock_filename, 'w'); 0101 0102 // Acquire exclusive write lock to lock file 0103 flock($this->lock, LOCK_EX); 0104 0105 // Create dbm-file if needed 0106 if (!file_exists($dbm_filename)) { 0107 if (!touch($dbm_filename)) { 0108 throw new Exception('failed to create dbm file: '.$dbm_filename); 0109 } 0110 } 0111 0112 // Try to open dbm file for writing 0113 $this->dba = dba_open($dbm_filename, 'w', $cache_type); 0114 if (!$this->dba) { 0115 0116 // Failed - create new dbm file 0117 $this->dba = dba_open($dbm_filename, 'n', $cache_type); 0118 0119 if (!$this->dba) { 0120 throw new Exception('failed to create dbm file: '.$dbm_filename); 0121 } 0122 0123 // Insert getID3 version number 0124 dba_insert(getID3::VERSION, getID3::VERSION, $this->dba); 0125 } 0126 0127 // Init misc values 0128 $this->cache_type = $cache_type; 0129 $this->dbm_filename = $dbm_filename; 0130 0131 // Register destructor 0132 register_shutdown_function(array($this, '__destruct')); 0133 0134 // Check version number and clear cache if changed 0135 if (dba_fetch(getID3::VERSION, $this->dba) != getID3::VERSION) { 0136 $this->clear_cache(); 0137 } 0138 0139 parent::__construct(); 0140 } 0141 0142 0143 0144 // public: destructor 0145 public function __destruct() { 0146 0147 // Close dbm file 0148 dba_close($this->dba); 0149 0150 // Release exclusive lock 0151 flock($this->lock, LOCK_UN); 0152 0153 // Close lock file 0154 fclose($this->lock); 0155 } 0156 0157 0158 0159 // public: clear cache 0160 public function clear_cache() { 0161 0162 // Close dbm file 0163 dba_close($this->dba); 0164 0165 // Create new dbm file 0166 $this->dba = dba_open($this->dbm_filename, 'n', $this->cache_type); 0167 0168 if (!$this->dba) { 0169 throw new Exception('failed to clear cache/recreate dbm file: '.$this->dbm_filename); 0170 } 0171 0172 // Insert getID3 version number 0173 dba_insert(getID3::VERSION, getID3::VERSION, $this->dba); 0174 0175 // Re-register shutdown function 0176 register_shutdown_function(array($this, '__destruct')); 0177 } 0178 0179 0180 0181 // public: analyze file 0182 public function analyze($filename) { 0183 0184 if (file_exists($filename)) { 0185 0186 // Calc key filename::mod_time::size - should be unique 0187 $key = $filename.'::'.filemtime($filename).'::'.filesize($filename); 0188 0189 // Loopup key 0190 $result = dba_fetch($key, $this->dba); 0191 0192 // Hit 0193 if ($result !== false) { 0194 return unserialize($result); 0195 } 0196 } 0197 0198 // Miss 0199 $result = parent::analyze($filename); 0200 0201 // Save result 0202 if (file_exists($filename)) { 0203 dba_insert($key, serialize($result), $this->dba); 0204 } 0205 0206 return $result; 0207 } 0208 0209 }