ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
extension.cache.sqlite3.php
Go to the documentation of this file.
1 <?php
4 // available at http://getid3.sourceforge.net //
5 // or http://www.getid3.org //
6 // also https://github.com/JamesHeinrich/getID3 //
9 // extension.cache.sqlite3.php - part of getID3() //
10 // Please see readme.txt for more information //
11 // ///
14 // MySQL extension written by Allan Hansen <ahØartemis*dk> //
15 // Table name mod by Carlo Capocasa <calroØcarlocapocasa*com> //
16 // MySQL extension was reworked for SQLite3 by Karl G. Holz <newaeonØmac*com> //
17 // ///
19 
93 
99  public function __construct($table='getid3_cache', $hide=false) {
100  $this->table = $table; // Set table
101  $file = dirname(__FILE__).'/'.basename(__FILE__, 'php').'sqlite';
102  if ($hide) {
103  $file = dirname(__FILE__).'/.ht.'.basename(__FILE__, 'php').'sqlite';
104  }
105  $this->db = new SQLite3($file);
106  $db = $this->db;
107  $this->create_table(); // Create cache table if not exists
108  $version = '';
109  $sql = $this->version_check;
110  $stmt = $db->prepare($sql);
111  $stmt->bindValue(':filename', getID3::VERSION, SQLITE3_TEXT);
112  $result = $stmt->execute();
113  list($version) = $result->fetchArray();
114  if ($version != getID3::VERSION) { // Check version number and clear cache if changed
115  $this->clear_cache();
116  }
117  return parent::__construct();
118  }
119 
123  public function __destruct() {
124  $db=$this->db;
125  $db->close();
126  }
127 
132  private $db;
133 
138  private $table;
139 
145  private function clear_cache() {
146  $db = $this->db;
147  $sql = $this->delete_cache;
148  $db->exec($sql);
149  $sql = $this->set_version;
150  $stmt = $db->prepare($sql);
151  $stmt->bindValue(':filename', getID3::VERSION, SQLITE3_TEXT);
152  $stmt->bindValue(':dirname', getID3::VERSION, SQLITE3_TEXT);
153  $stmt->bindValue(':val', getID3::VERSION, SQLITE3_TEXT);
154  return $stmt->execute();
155  }
156 
162  public function analyze($filename, $filesize=null, $original_filename='') {
163  if (!file_exists($filename)) {
164  return false;
165  }
166  // items to track for caching
167  $filetime = filemtime($filename);
168  $filesize_real = filesize($filename);
169  // this will be saved for a quick directory lookup of analized files
170  // ... why do 50 seperate sql quries when you can do 1 for the same result
171  $dirname = dirname($filename);
172  // Lookup file
173  $db = $this->db;
174  $sql = $this->get_id3_data;
175  $stmt = $db->prepare($sql);
176  $stmt->bindValue(':filename', $filename, SQLITE3_TEXT);
177  $stmt->bindValue(':filesize', $filesize_real, SQLITE3_INTEGER);
178  $stmt->bindValue(':filetime', $filetime, SQLITE3_INTEGER);
179  $res = $stmt->execute();
180  list($result) = $res->fetchArray();
181  if (count($result) > 0 ) {
182  return unserialize(base64_decode($result));
183  }
184  // if it hasn't been analyzed before, then do it now
185  $analysis = parent::analyze($filename, $filesize, $original_filename);
186  // Save result
187  $sql = $this->cache_file;
188  $stmt = $db->prepare($sql);
189  $stmt->bindValue(':filename', $filename, SQLITE3_TEXT);
190  $stmt->bindValue(':dirname', $dirname, SQLITE3_TEXT);
191  $stmt->bindValue(':filesize', $filesize_real, SQLITE3_INTEGER);
192  $stmt->bindValue(':filetime', $filetime, SQLITE3_INTEGER);
193  $stmt->bindValue(':atime', time(), SQLITE3_INTEGER);
194  $stmt->bindValue(':val', base64_encode(serialize($analysis)), SQLITE3_TEXT);
195  $res = $stmt->execute();
196  return $analysis;
197  }
198 
204  private function create_table() {
205  $db = $this->db;
206  $sql = $this->make_table;
207  return $db->exec($sql);
208  }
209 
220  public function get_cached_dir($dir) {
221  $db = $this->db;
222  $rows = array();
223  $sql = $this->get_cached_dir;
224  $stmt = $db->prepare($sql);
225  $stmt->bindValue(':dirname', $dir, SQLITE3_TEXT);
226  $res = $stmt->execute();
227  while ($row=$res->fetchArray()) {
228  $rows[] = unserialize(base64_decode($row));
229  }
230  return $rows;
231  }
232 
238  public function __get($name) {
239  switch($name) {
240  case 'version_check':
241  return "SELECT val FROM $this->table WHERE filename = :filename AND filesize = '-1' AND filetime = '-1' AND analyzetime = '-1'";
242  break;
243  case 'delete_cache':
244  return "DELETE FROM $this->table";
245  break;
246  case 'set_version':
247  return "INSERT INTO $this->table (filename, dirname, filesize, filetime, analyzetime, val) VALUES (:filename, :dirname, -1, -1, -1, :val)";
248  break;
249  case 'get_id3_data':
250  return "SELECT val FROM $this->table WHERE filename = :filename AND filesize = :filesize AND filetime = :filetime";
251  break;
252  case 'cache_file':
253  return "INSERT INTO $this->table (filename, dirname, filesize, filetime, analyzetime, val) VALUES (:filename, :dirname, :filesize, :filetime, :atime, :val)";
254  break;
255  case 'make_table':
256  //return "CREATE TABLE IF NOT EXISTS $this->table (filename VARCHAR(255) NOT NULL DEFAULT '', dirname VARCHAR(255) NOT NULL DEFAULT '', filesize INT(11) NOT NULL DEFAULT '0', filetime INT(11) NOT NULL DEFAULT '0', analyzetime INT(11) NOT NULL DEFAULT '0', val text not null, PRIMARY KEY (filename, filesize, filetime))";
257  return "CREATE TABLE IF NOT EXISTS $this->table (filename VARCHAR(255) DEFAULT '', dirname VARCHAR(255) DEFAULT '', filesize INT(11) DEFAULT '0', filetime INT(11) DEFAULT '0', analyzetime INT(11) DEFAULT '0', val text, PRIMARY KEY (filename, filesize, filetime))";
258  break;
259  case 'get_cached_dir':
260  return "SELECT val FROM $this->table WHERE dirname = :dirname";
261  break;
262  }
263  return null;
264  }
265 
266 }
$result
$stmt
$table
table to use for caching
$filename
Definition: getid3.php:105
const VERSION
Definition: getid3.php:115
$version
Definition: build.php:27
__get($name)
use the magical __get() for sql queries
get_cached_dir($dir)
get cached directory
foreach($_POST as $key=> $value) $res
$row
$rows
Definition: xhr_table.php:10
__construct($table='getid3_cache', $hide=false)
__construct()
create_table()
create data base table this is almost the same as MySQL, with the exception of the dirname being adde...
clear_cache()
clear the cache private
getID3() by James Heinrich info@getid3.org //
__destruct()
close the database connection
analyze($filename, $filesize=null, $original_filename='')
analyze file and cache them, if cached pull from the db