58d4dc884df46a830ef312ab246954716be86b0e
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 /////////////////////////////////////////////////////////////////////////////////
9 // extension.cache.sqlite3.php - part of getID3() //
10 // Please see readme.txt for more information //
12 /////////////////////////////////////////////////////////////////////////////////
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> //
18 /////////////////////////////////////////////////////////////////////////////////
20 * This is a caching extension for getID3(). It works the exact same
21 * way as the getID3 class, but return cached information much faster
23 * Normal getID3 usage (example):
25 * require_once 'getid3/getid3.php';
26 * $getID3 = new getID3;
27 * $getID3->encoding = 'UTF-8';
28 * $info1 = $getID3->analyze('file1.flac');
29 * $info2 = $getID3->analyze('file2.wv');
31 * getID3_cached usage:
33 * require_once 'getid3/getid3.php';
34 * require_once 'getid3/extension.cache.sqlite3.php';
35 * // all parameters are optional, defaults are:
36 * $getID3 = new getID3_cached_sqlite3($table='getid3_cache', $hide=FALSE);
37 * $getID3->encoding = 'UTF-8';
38 * $info1 = $getID3->analyze('file1.flac');
39 * $info2 = $getID3->analyze('file2.wv');
42 * Supported Cache Types (this extension)
46 * cache_type cache_options
47 * -------------------------------------------------------------------
48 * mysql host, database, username, password
50 * sqlite3 table='getid3_cache', hide=false (PHP5)
53 * *** database file will be stored in the same directory as this script,
54 * *** webserver must have write access to that directory!
55 * *** set $hide to TRUE to prefix db file with .ht to pervent access from web client
56 * *** this is a default setting in the Apache configuration:
58 * The following lines prevent .htaccess and .htpasswd files from being viewed by Web clients.
66 ********************************************************************************
68 * -------------------------------------------------------------------
69 * DBM-Style Databases: (use extension.cache.dbm)
71 * cache_type cache_options
72 * -------------------------------------------------------------------
73 * gdbm dbm_filename, lock_filename
74 * ndbm dbm_filename, lock_filename
75 * db2 dbm_filename, lock_filename
76 * db3 dbm_filename, lock_filename
77 * db4 dbm_filename, lock_filename (PHP5 required)
79 * PHP must have write access to both dbm_filename and lock_filename.
81 * Recommended Cache Types
83 * Infrequent updates, many reads any DBM
84 * Frequent updates mysql
85 ********************************************************************************
87 * IMHO this is still a bit slow, I'm using this with MP4/MOV/ M4v files
88 * there is a plan to add directory scanning and analyzing to make things work much faster
92 class getID3_cached_sqlite3
extends getID3
{
96 * @param string $table holds name of sqlite table
99 public function __construct($table='getid3_cache', $hide=false) {
100 $this->table
= $table; // Set table
101 $file = dirname(__FILE__
).'/'.basename(__FILE__
, 'php').'sqlite';
103 $file = dirname(__FILE__
).'/.ht.'.basename(__FILE__
, 'php').'sqlite';
105 $this->db
= new SQLite3($file);
107 $this->create_table(); // Create cache table if not exists
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();
117 return parent
::__construct();
121 * close the database connection
123 public function __destruct() {
130 * @var SQLite Resource
135 * table to use for caching
145 private function clear_cache() {
147 $sql = $this->delete_cache
;
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();
158 * analyze file and cache them, if cached pull from the db
159 * @param type $filename
162 public function analyze($filename, $filesize=null, $original_filename='') {
163 if (!file_exists($filename)) {
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);
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));
184 // if it hasn't been analyzed before, then do it now
185 $analysis = parent
::analyze($filename, $filesize, $original_filename);
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();
200 * create data base table
201 * this is almost the same as MySQL, with the exception of the dirname being added
204 private function create_table() {
206 $sql = $this->make_table
;
207 return $db->exec($sql);
211 * get cached directory
213 * This function is not in the MySQL extention, it's ment to speed up requesting multiple files
214 * which is ideal for podcasting, playlists, etc.
217 * @param string $dir directory to search the cache database for
218 * @return array return an array of matching id3 data
220 public function get_cached_dir($dir) {
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));
234 * use the magical __get() for sql queries
236 * access as easy as $this->{case name}, returns NULL if query is not found
238 public function __get($name) {
240 case 'version_check':
241 return "SELECT val FROM $this->table WHERE filename = :filename AND filesize = '-1' AND filetime = '-1' AND analyzetime = '-1'";
244 return "DELETE FROM $this->table";
247 return "INSERT INTO $this->table (filename, dirname, filesize, filetime, analyzetime, val) VALUES (:filename, :dirname, -1, -1, -1, :val)";
250 return "SELECT val FROM $this->table WHERE filename = :filename AND filesize = :filesize AND filetime = :filetime";
253 return "INSERT INTO $this->table (filename, dirname, filesize, filetime, analyzetime, val) VALUES (:filename, :dirname, :filesize, :filetime, :atime, :val)";
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))";
259 case 'get_cached_dir':
260 return "SELECT val FROM $this->table WHERE dirname = :dirname";