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 | |
---|
18 | /** |
---|
19 | * This is a caching extension for getID3(). It works the exact same |
---|
20 | * way as the getID3 class, but return cached information very fast |
---|
21 | * |
---|
22 | * Example: |
---|
23 | * |
---|
24 | * Normal getID3 usage (example): |
---|
25 | * |
---|
26 | * require_once 'getid3/getid3.php'; |
---|
27 | * $getID3 = new getID3; |
---|
28 | * $getID3->encoding = 'UTF-8'; |
---|
29 | * $info1 = $getID3->analyze('file1.flac'); |
---|
30 | * $info2 = $getID3->analyze('file2.wv'); |
---|
31 | * |
---|
32 | * getID3_cached usage: |
---|
33 | * |
---|
34 | * require_once 'getid3/getid3.php'; |
---|
35 | * require_once 'getid3/getid3/extension.cache.dbm.php'; |
---|
36 | * $getID3 = new getID3_cached('db3', '/tmp/getid3_cache.dbm', |
---|
37 | * '/tmp/getid3_cache.lock'); |
---|
38 | * $getID3->encoding = 'UTF-8'; |
---|
39 | * $info1 = $getID3->analyze('file1.flac'); |
---|
40 | * $info2 = $getID3->analyze('file2.wv'); |
---|
41 | * |
---|
42 | * |
---|
43 | * Supported Cache Types |
---|
44 | * |
---|
45 | * SQL Databases: (use extension.cache.mysql) |
---|
46 | * |
---|
47 | * cache_type cache_options |
---|
48 | * ------------------------------------------------------------------- |
---|
49 | * mysql host, database, username, password |
---|
50 | * |
---|
51 | * |
---|
52 | * DBM-Style Databases: (this extension) |
---|
53 | * |
---|
54 | * cache_type cache_options |
---|
55 | * ------------------------------------------------------------------- |
---|
56 | * gdbm dbm_filename, lock_filename |
---|
57 | * ndbm dbm_filename, lock_filename |
---|
58 | * db2 dbm_filename, lock_filename |
---|
59 | * db3 dbm_filename, lock_filename |
---|
60 | * db4 dbm_filename, lock_filename (PHP5 required) |
---|
61 | * |
---|
62 | * PHP must have write access to both dbm_filename and lock_filename. |
---|
63 | * |
---|
64 | * |
---|
65 | * Recommended Cache Types |
---|
66 | * |
---|
67 | * Infrequent updates, many reads any DBM |
---|
68 | * Frequent updates mysql |
---|
69 | */ |
---|
70 | |
---|
71 | |
---|
72 | class getID3_cached_dbm extends getID3 |
---|
73 | { |
---|
74 | |
---|
75 | // public: constructor - see top of this file for cache type and cache_options |
---|
76 | function getID3_cached_dbm($cache_type, $dbm_filename, $lock_filename) { |
---|
77 | |
---|
78 | // Check for dba extension |
---|
79 | if (!extension_loaded('dba')) { |
---|
80 | die('PHP is not compiled with dba support, required to use DBM style cache.'); |
---|
81 | } |
---|
82 | |
---|
83 | // Check for specific dba driver |
---|
84 | if (function_exists('dba_handlers')) { // PHP 4.3.0+ |
---|
85 | if (!in_array('db3', dba_handlers())) { |
---|
86 | die('PHP is not compiled --with '.$cache_type.' support, required to use DBM style cache.'); |
---|
87 | } |
---|
88 | } |
---|
89 | else { // PHP <= 4.2.3 |
---|
90 | ob_start(); // nasty, buy the only way to check... |
---|
91 | phpinfo(); |
---|
92 | $contents = ob_get_contents(); |
---|
93 | ob_end_clean(); |
---|
94 | if (!strstr($contents, $cache_type)) { |
---|
95 | die('PHP is not compiled --with '.$cache_type.' support, required to use DBM style cache.'); |
---|
96 | } |
---|
97 | } |
---|
98 | |
---|
99 | // Create lock file if needed |
---|
100 | if (!file_exists($lock_filename)) { |
---|
101 | if (!touch($lock_filename)) { |
---|
102 | die('failed to create lock file: ' . $lock_filename); |
---|
103 | } |
---|
104 | } |
---|
105 | |
---|
106 | // Open lock file for writing |
---|
107 | if (!is_writeable($lock_filename)) { |
---|
108 | die('lock file: ' . $lock_filename . ' is not writable'); |
---|
109 | } |
---|
110 | $this->lock = fopen($lock_filename, 'w'); |
---|
111 | |
---|
112 | // Acquire exclusive write lock to lock file |
---|
113 | flock($this->lock, LOCK_EX); |
---|
114 | |
---|
115 | // Create dbm-file if needed |
---|
116 | if (!file_exists($dbm_filename)) { |
---|
117 | if (!touch($dbm_filename)) { |
---|
118 | die('failed to create dbm file: ' . $dbm_filename); |
---|
119 | } |
---|
120 | } |
---|
121 | |
---|
122 | // Try to open dbm file for writing |
---|
123 | $this->dba = @dba_open($dbm_filename, 'w', $cache_type); |
---|
124 | if (!$this->dba) { |
---|
125 | |
---|
126 | // Failed - create new dbm file |
---|
127 | $this->dba = dba_open($dbm_filename, 'n', $cache_type); |
---|
128 | |
---|
129 | if (!$this->dba) { |
---|
130 | die('failed to create dbm file: ' . $dbm_filename); |
---|
131 | } |
---|
132 | |
---|
133 | // Insert getID3 version number |
---|
134 | dba_insert(GETID3_VERSION, GETID3_VERSION, $this->dba); |
---|
135 | } |
---|
136 | |
---|
137 | // Init misc values |
---|
138 | $this->cache_type = $cache_type; |
---|
139 | $this->dbm_filename = $dbm_filename; |
---|
140 | |
---|
141 | // Register destructor |
---|
142 | register_shutdown_function(array($this, '__destruct')); |
---|
143 | |
---|
144 | // Check version number and clear cache if changed |
---|
145 | if (dba_fetch(GETID3_VERSION, $this->dba) != GETID3_VERSION) { |
---|
146 | $this->clear_cache(); |
---|
147 | } |
---|
148 | |
---|
149 | parent::getID3(); |
---|
150 | } |
---|
151 | |
---|
152 | |
---|
153 | |
---|
154 | // public: destuctor |
---|
155 | function __destruct() { |
---|
156 | |
---|
157 | // Close dbm file |
---|
158 | @dba_close($this->dba); |
---|
159 | |
---|
160 | // Release exclusive lock |
---|
161 | @flock($this->lock, LOCK_UN); |
---|
162 | |
---|
163 | // Close lock file |
---|
164 | @fclose($this->lock); |
---|
165 | } |
---|
166 | |
---|
167 | |
---|
168 | |
---|
169 | // public: clear cache |
---|
170 | function clear_cache() { |
---|
171 | |
---|
172 | // Close dbm file |
---|
173 | dba_close($this->dba); |
---|
174 | |
---|
175 | // Create new dbm file |
---|
176 | $this->dba = dba_open($this->dbm_filename, 'n', $this->cache_type); |
---|
177 | |
---|
178 | if (!$this->dba) { |
---|
179 | die('failed to clear cache/recreate dbm file: ' . $this->dbm_filename); |
---|
180 | } |
---|
181 | |
---|
182 | // Insert getID3 version number |
---|
183 | dba_insert(GETID3_VERSION, GETID3_VERSION, $this->dba); |
---|
184 | |
---|
185 | // Reregister shutdown function |
---|
186 | register_shutdown_function(array($this, '__destruct')); |
---|
187 | } |
---|
188 | |
---|
189 | |
---|
190 | |
---|
191 | // public: analyze file |
---|
192 | function analyze($filename) { |
---|
193 | |
---|
194 | if (file_exists($filename)) { |
---|
195 | |
---|
196 | // Calc key filename::mod_time::size - should be unique |
---|
197 | $key = $filename . '::' . filemtime($filename) . '::' . filesize($filename); |
---|
198 | |
---|
199 | // Loopup key |
---|
200 | $result = dba_fetch($key, $this->dba); |
---|
201 | |
---|
202 | // Hit |
---|
203 | if ($result !== false) { |
---|
204 | return unserialize($result); |
---|
205 | } |
---|
206 | } |
---|
207 | |
---|
208 | // Miss |
---|
209 | $result = parent::analyze($filename); |
---|
210 | |
---|
211 | // Save result |
---|
212 | if (file_exists($filename)) { |
---|
213 | dba_insert($key, serialize($result), $this->dba); |
---|
214 | } |
---|
215 | |
---|
216 | return $result; |
---|
217 | } |
---|
218 | |
---|
219 | } |
---|
220 | |
---|
221 | |
---|
222 | ?> |
---|