ILIAS  release_5-2 Revision v5.2.25-18-g3f80b828510
Dbm.php
Go to the documentation of this file.
1 <?php
2 
4 
7 
10 // available at http://getid3.sourceforge.net //
11 // or http://www.getid3.org //
13 // //
14 // extension.cache.dbm.php - part of GetId3() //
15 // Please see readme.txt for more information //
16 // ///
18 // //
19 // This extension written by Allan Hansen <ahØartemis*dk> //
20 // ///
22 
83 class Dbm extends GetId3
84 {
85 
94  public function __construct($cache_type, $dbm_filename, $lock_filename)
95  {
96  // Check for dba extension
97  if (!extension_loaded('dba')) {
98  throw new DefaultException('PHP is not compiled with dba support, required to use DBM style cache.');
99  }
100 
101  // Check for specific dba driver
102  if (!function_exists('dba_handlers') || !in_array($cache_type, dba_handlers())) {
103  throw new DefaultException('PHP is not compiled --with '.$cache_type.' support, required to use DBM style cache.');
104  }
105 
106  // Create lock file if needed
107  if (!file_exists($lock_filename)) {
108  if (!touch($lock_filename)) {
109  throw new DefaultException('failed to create lock file: '.$lock_filename);
110  }
111  }
112 
113  // Open lock file for writing
114  if (!is_writeable($lock_filename)) {
115  throw new DefaultException('lock file: '.$lock_filename.' is not writable');
116  }
117  $this->lock = fopen($lock_filename, 'w');
118 
119  // Acquire exclusive write lock to lock file
120  flock($this->lock, LOCK_EX);
121 
122  // Create dbm-file if needed
123  if (!file_exists($dbm_filename)) {
124  if (!touch($dbm_filename)) {
125  throw new DefaultException('failed to create dbm file: '.$dbm_filename);
126  }
127  }
128 
129  // Try to open dbm file for writing
130  $this->dba = dba_open($dbm_filename, 'w', $cache_type);
131  if (!$this->dba) {
132 
133  // Failed - create new dbm file
134  $this->dba = dba_open($dbm_filename, 'n', $cache_type);
135 
136  if (!$this->dba) {
137  throw new DefaultException('failed to create dbm file: '.$dbm_filename);
138  }
139 
140  // Insert GetId3 version number
141  dba_insert(GetId3Core::VERSION, GetId3Core::VERSION, $this->dba);
142  }
143 
144  // Init misc values
145  $this->cache_type = $cache_type;
146  $this->dbm_filename = $dbm_filename;
147 
148  // Register destructor
149  register_shutdown_function(array($this, '__destruct'));
150 
151  // Check version number and clear cache if changed
152  if (dba_fetch(GetId3Core::VERSION, $this->dba) != GetId3Core::VERSION) {
153  $this->clear_cache();
154  }
155 
156  parent::__construct();
157  }
158 
162  public function __destruct()
163  {
164  // Close dbm file
165  dba_close($this->dba);
166 
167  // Release exclusive lock
168  flock($this->lock, LOCK_UN);
169 
170  // Close lock file
171  fclose($this->lock);
172  }
173 
179  public function clear_cache()
180  {
181  // Close dbm file
182  dba_close($this->dba);
183 
184  // Create new dbm file
185  $this->dba = dba_open($this->dbm_filename, 'n', $this->cache_type);
186 
187  if (!$this->dba) {
188  throw new DefaultException('failed to clear cache/recreate dbm file: '.$this->dbm_filename);
189  }
190 
191  // Insert GetId3 version number
192  dba_insert(GetId3Core::VERSION, GetId3Core::VERSION, $this->dba);
193 
194  // Re-register shutdown function
195  register_shutdown_function(array($this, '__destruct'));
196  }
197 
204  public function analyze($filename)
205  {
206  if (file_exists($filename)) {
207 
208  // Calc key filename::mod_time::size - should be unique
209  $key = $filename.'::'.filemtime($filename).'::'.filesize($filename);
210 
211  // Loopup key
212  $result = dba_fetch($key, $this->dba);
213 
214  // Hit
215  if ($result !== false) {
216  return unserialize($result);
217  }
218  }
219 
220  // Miss
221  $result = parent::analyze($filename);
222 
223  // Save result
224  if (file_exists($filename)) {
225  dba_insert($key, serialize($result), $this->dba);
226  }
227 
228  return $result;
229  }
230 }
$result
__construct($cache_type, $dbm_filename, $lock_filename)
public: constructor - see top of this file for cache type and cache_options
Definition: Dbm.php:94
analyze($filename)
analyze file
Definition: Dbm.php:204
GetId3() by James Heinrich info@getid3.org //.
Definition: Dbm.php:83
clear_cache()
clear cache
Definition: Dbm.php:179
Create styles array
The data for the language used.