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