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.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
80 // public: constructor - see top of this file for cache type and cache_options
81 public function __construct($host, $database, $username, $password, $table='getid3_cache') {
83 // Check for mysqli support
84 if (!function_exists('mysqli_connect')) {
85 throw new Exception('PHP not compiled with mysqli support.');
88 // Connect to database
89 $this->mysqli
= new mysqli($host, $username, $password);
91 throw new Exception('mysqli_connect() failed - check permissions and spelling.');
95 if (!$this->mysqli
->select_db($database)) {
96 throw new Exception('Cannot use database '.$database);
100 $this->table
= $table;
102 // Create cache table if not exists
103 $this->create_table();
105 // Check version number and clear cache if changed
107 $SQLquery = 'SELECT `value`';
108 $SQLquery .= ' FROM `'.$this->mysqli
->real_escape_string($this->table
).'`';
109 $SQLquery .= ' WHERE (`filename` = \''.$this->mysqli
->real_escape_string(getID3
::VERSION
).'\')';
110 $SQLquery .= ' AND (`filesize` = -1)';
111 $SQLquery .= ' AND (`filetime` = -1)';
112 $SQLquery .= ' AND (`analyzetime` = -1)';
113 if ($this->cursor
= $this->mysqli
->query($SQLquery)) {
114 list($version) = $this->cursor
->fetch_array();
116 if ($version != getID3
::VERSION
) {
117 $this->clear_cache();
120 parent
::__construct();
124 // public: clear cache
125 public function clear_cache() {
126 $this->mysqli
->query('DELETE FROM `'.$this->mysqli
->real_escape_string($this->table
).'`');
127 $this->mysqli
->query('INSERT INTO `'.$this->mysqli
->real_escape_string($this->table
).'` (`filename`, `filesize`, `filetime`, `analyzetime`, `value`) VALUES (\''.getID3
::VERSION
.'\', -1, -1, -1, \''.getID3
::VERSION
.'\')');
131 // public: analyze file
132 public function analyze($filename, $filesize=null, $original_filename='') {
134 if (file_exists($filename)) {
137 $filetime = filemtime($filename);
138 $filesize = filesize($filename);
141 $SQLquery = 'SELECT `value`';
142 $SQLquery .= ' FROM `'.$this->mysqli
->real_escape_string($this->table
).'`';
143 $SQLquery .= ' WHERE (`filename` = \''.$this->mysqli
->real_escape_string($filename).'\')';
144 $SQLquery .= ' AND (`filesize` = \''.$this->mysqli
->real_escape_string($filesize).'\')';
145 $SQLquery .= ' AND (`filetime` = \''.$this->mysqli
->real_escape_string($filetime).'\')';
146 $this->cursor
= $this->mysqli
->query($SQLquery);
147 if ($this->cursor
->num_rows
> 0) {
149 list($result) = $this->cursor
->fetch_array();
150 return unserialize(base64_decode($result));
155 $analysis = parent
::analyze($filename, $filesize, $original_filename);
158 if (file_exists($filename)) {
159 $SQLquery = 'INSERT INTO `'.$this->mysqli
->real_escape_string($this->table
).'` (`filename`, `filesize`, `filetime`, `analyzetime`, `value`) VALUES (';
160 $SQLquery .= '\''.$this->mysqli
->real_escape_string($filename).'\'';
161 $SQLquery .= ', \''.$this->mysqli
->real_escape_string($filesize).'\'';
162 $SQLquery .= ', \''.$this->mysqli
->real_escape_string($filetime).'\'';
163 $SQLquery .= ', \''.$this->mysqli
->real_escape_string(time() ).'\'';
164 $SQLquery .= ', \''.$this->mysqli
->real_escape_string(base64_encode(serialize($analysis))).'\')';
165 $this->cursor
= $this->mysqli
->query($SQLquery);
171 // private: (re)create mysqli table
172 private function create_table($drop=false) {
173 $SQLquery = 'CREATE TABLE IF NOT EXISTS `'.$this->mysqli
->real_escape_string($this->table
).'` (';
174 $SQLquery .= '`filename` VARCHAR(990) NOT NULL DEFAULT \'\'';
175 $SQLquery .= ', `filesize` INT(11) NOT NULL DEFAULT \'0\'';
176 $SQLquery .= ', `filetime` INT(11) NOT NULL DEFAULT \'0\'';
177 $SQLquery .= ', `analyzetime` INT(11) NOT NULL DEFAULT \'0\'';
178 $SQLquery .= ', `value` LONGTEXT NOT NULL';
179 $SQLquery .= ', PRIMARY KEY (`filename`, `filesize`, `filetime`)) ENGINE=MyISAM CHARACTER SET=latin1 COLLATE=latin1_general_ci';
180 $this->cursor
= $this->mysqli
->query($SQLquery);
181 echo $this->mysqli
->error
;