3 /////////////////////////////////////////////////////////////////
4 /// getID3() by James Heinrich <info@getid3.org> //
5 // available at https://github.com/JamesHeinrich/getID3 //
6 // or https://www.getid3.org //
7 // or http://getid3.sourceforge.net //
9 // extension.cache.mysqli.php - part of getID3() //
10 // Please see readme.txt for more information //
12 /////////////////////////////////////////////////////////////////
14 // This extension written by Allan Hansen <ahØartemis*dk> //
15 // Table name mod by Carlo Capocasa <calroØcarlocapocasa*com> //
17 /////////////////////////////////////////////////////////////////
21 * This is a caching extension for getID3(). It works the exact same
22 * way as the getID3 class, but return cached information very fast
24 * Example: (see also demo.cache.mysql.php in /demo/)
26 * Normal getID3 usage (example):
28 * require_once 'getid3/getid3.php';
29 * $getID3 = new getID3;
30 * $getID3->encoding = 'UTF-8';
31 * $info1 = $getID3->analyze('file1.flac');
32 * $info2 = $getID3->analyze('file2.wv');
34 * getID3_cached usage:
36 * require_once 'getid3/getid3.php';
37 * require_once 'getid3/getid3/extension.cache.mysqli.php';
38 * // 5th parameter (tablename) is optional, default is 'getid3_cache'
39 * $getID3 = new getID3_cached_mysqli('localhost', 'database', 'username', 'password', 'tablename');
40 * $getID3->encoding = 'UTF-8';
41 * $info1 = $getID3->analyze('file1.flac');
42 * $info2 = $getID3->analyze('file2.wv');
45 * Supported Cache Types (this extension)
49 * cache_type cache_options
50 * -------------------------------------------------------------------
51 * mysqli host, database, username, password
54 * DBM-Style Databases: (use extension.cache.dbm)
56 * cache_type cache_options
57 * -------------------------------------------------------------------
58 * gdbm dbm_filename, lock_filename
59 * ndbm dbm_filename, lock_filename
60 * db2 dbm_filename, lock_filename
61 * db3 dbm_filename, lock_filename
62 * db4 dbm_filename, lock_filename (PHP5 required)
64 * PHP must have write access to both dbm_filename and lock_filename.
67 * Recommended Cache Types
69 * Infrequent updates, many reads any DBM
70 * Frequent updates mysqli
73 class getID3_cached_mysqli
extends getID3
93 private $db_structure_check;
97 * constructor - see top of this file for cache type and cache_options
100 * @param string $database
101 * @param string $username
102 * @param string $password
103 * @param string $table
106 * @throws getid3_exception
108 public function __construct($host, $database, $username, $password, $table='getid3_cache') {
110 // Check for mysqli support
111 if (!function_exists('mysqli_connect')) {
112 throw new Exception('PHP not compiled with mysqli support.');
115 // Connect to database
116 $this->mysqli
= new mysqli($host, $username, $password);
117 if ($this->mysqli
->connect_error
) {
118 throw new Exception('Connect Error (' . $this->mysqli
->connect_errno
. ') ' . $this->mysqli
->connect_error
);
122 if (!$this->mysqli
->select_db($database)) {
123 throw new Exception('Cannot use database '.$database);
127 $this->table
= $table;
129 // Create cache table if not exists
130 $this->create_table();
132 $this->db_structure_check
= true; // set to false if you know your table structure has already been migrated to use `hash` as the primary key to avoid
133 $this->migrate_db_structure();
135 // Check version number and clear cache if changed
137 $SQLquery = 'SELECT `value`';
138 $SQLquery .= ' FROM `'.$this->mysqli
->real_escape_string($this->table
).'`';
139 $SQLquery .= ' WHERE (`filename` = \''.$this->mysqli
->real_escape_string(getID3
::VERSION
).'\')';
140 $SQLquery .= ' AND (`hash` = \'getID3::VERSION\')';
141 if ($this->cursor
= $this->mysqli
->query($SQLquery)) {
142 list($version) = $this->cursor
->fetch_array();
144 if ($version != getID3
::VERSION
) {
145 $this->clear_cache();
148 parent
::__construct();
155 public function clear_cache() {
156 $this->mysqli
->query('TRUNCATE TABLE `'.$this->mysqli
->real_escape_string($this->table
).'`');
157 $this->mysqli
->query('INSERT INTO `'.$this->mysqli
->real_escape_string($this->table
).'` (`hash`, `filename`, `filesize`, `filetime`, `analyzetime`, `value`) VALUES (\'getID3::VERSION\', \''.getID3
::VERSION
.'\', -1, -1, -1, \''.getID3
::VERSION
.'\')');
162 * migrate database structure if needed
164 public function migrate_db_structure() {
165 // Check for table structure
166 if ($this->db_structure_check
) {
167 $SQLquery = 'SHOW COLUMNS';
168 $SQLquery .= ' FROM `'.$this->mysqli
->real_escape_string($this->table
).'`';
169 $SQLquery .= ' LIKE \'hash\'';
170 $this->cursor
= $this->mysqli
->query($SQLquery);
171 if ($this->cursor
->num_rows
== 0) {
172 // table has not been migrated, add column, add hashes, change index
173 $SQLquery = 'ALTER TABLE `getid3_cache` DROP PRIMARY KEY, ADD `hash` CHAR(32) NOT NULL DEFAULT \'\' FIRST, ADD PRIMARY KEY(`hash`)';
174 $this->mysqli
->query($SQLquery);
176 $SQLquery = 'UPDATE `getid3_cache` SET';
177 $SQLquery .= ' `hash` = MD5(`filename`, `filesize`, `filetime`)';
178 $SQLquery .= ' WHERE (`filesize` > -1)';
179 $this->mysqli
->query($SQLquery);
181 $SQLquery = 'UPDATE `getid3_cache` SET';
182 $SQLquery .= ' `hash` = \'getID3::VERSION\'';
183 $SQLquery .= ' WHERE (`filesize` = -1)';
184 $SQLquery .= ' AND (`filetime` = -1)';
185 $SQLquery .= ' AND (`filetime` = -1)';
186 $this->mysqli
->query($SQLquery);
195 * @param string $filename
196 * @param int $filesize
197 * @param string $original_filename
198 * @param resource $fp
202 public function analyze($filename, $filesize=null, $original_filename='', $fp=null) {
205 if (file_exists($filename)) {
208 $filetime = filemtime($filename);
209 $filesize = filesize($filename);
212 $SQLquery = 'SELECT `value`';
213 $SQLquery .= ' FROM `'.$this->mysqli
->real_escape_string($this->table
).'`';
214 $SQLquery .= ' WHERE (`hash` = \''.$this->mysqli
->real_escape_string(md5($filename.$filesize.$filetime)).'\')';
215 $this->cursor
= $this->mysqli
->query($SQLquery);
216 if ($this->cursor
->num_rows
> 0) {
218 list($result) = $this->cursor
->fetch_array();
219 return unserialize(base64_decode($result));
224 $analysis = parent
::analyze($filename, $filesize, $original_filename, $fp);
227 if (file_exists($filename)) {
228 $SQLquery = 'INSERT INTO `'.$this->mysqli
->real_escape_string($this->table
).'` (`hash`, `filename`, `filesize`, `filetime`, `analyzetime`, `value`) VALUES (';
229 $SQLquery .= '\''.$this->mysqli
->real_escape_string(md5($filename.$filesize.$filetime)).'\'';
230 $SQLquery .= ', \''.$this->mysqli
->real_escape_string($filename).'\'';
231 $SQLquery .= ', \''.$this->mysqli
->real_escape_string($filesize).'\'';
232 $SQLquery .= ', \''.$this->mysqli
->real_escape_string($filetime).'\'';
233 $SQLquery .= ', UNIX_TIMESTAMP()';
234 $SQLquery .= ', \''.$this->mysqli
->real_escape_string(base64_encode(serialize($analysis))).'\'';
236 $this->cursor
= $this->mysqli
->query($SQLquery);
243 * (re)create mysqli table
247 private function create_table($drop=false) {
249 $SQLquery = 'DROP TABLE IF EXISTS `'.$this->mysqli
->real_escape_string($this->table
).'`';
250 $this->mysqli
->query($SQLquery);
252 $SQLquery = 'CREATE TABLE IF NOT EXISTS `'.$this->mysqli
->real_escape_string($this->table
).'` (';
253 $SQLquery .= '`hash` CHAR(32) NOT NULL DEFAULT \'\'';
254 $SQLquery .= ', `filename` VARCHAR(1000) NOT NULL DEFAULT \'\'';
255 $SQLquery .= ', `filesize` INT(11) NOT NULL DEFAULT \'0\'';
256 $SQLquery .= ', `filetime` INT(11) NOT NULL DEFAULT \'0\'';
257 $SQLquery .= ', `analyzetime` INT(11) NOT NULL DEFAULT \'0\'';
258 $SQLquery .= ', `value` LONGTEXT NOT NULL';
259 $SQLquery .= ', PRIMARY KEY (`hash`))';
260 $this->cursor
= $this->mysqli
->query($SQLquery);
261 echo $this->mysqli
->error
;