]> scripts.mit.edu Git - autoinstallsdev/mediawiki.git/blob - vendor/james-heinrich/getid3/getid3/extension.cache.mysql.php
MediaWiki 1.30.2
[autoinstallsdev/mediawiki.git] / vendor / james-heinrich / getid3 / getid3 / extension.cache.mysql.php
1 <?php
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 /////////////////////////////////////////////////////////////////
8 //                                                             //
9 // extension.cache.mysql.php - part of getID3()                //
10 // Please see readme.txt for more information                  //
11 //                                                            ///
12 /////////////////////////////////////////////////////////////////
13 //                                                             //
14 // This extension written by Allan Hansen <ahØartemis*dk>      //
15 // Table name mod by Carlo Capocasa <calroØcarlocapocasa*com>  //
16 //                                                            ///
17 /////////////////////////////////////////////////////////////////
18
19
20 /**
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
23 *
24 * Example:  (see also demo.cache.mysql.php in /demo/)
25 *
26 *    Normal getID3 usage (example):
27 *
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');
33 *
34 *    getID3_cached usage:
35 *
36 *       require_once 'getid3/getid3.php';
37 *       require_once 'getid3/getid3/extension.cache.mysql.php';
38 *       // 5th parameter (tablename) is optional, default is 'getid3_cache'
39 *       $getID3 = new getID3_cached_mysql('localhost', 'database', 'username', 'password', 'tablename');
40 *       $getID3->encoding = 'UTF-8';
41 *       $info1 = $getID3->analyze('file1.flac');
42 *       $info2 = $getID3->analyze('file2.wv');
43 *
44 *
45 * Supported Cache Types    (this extension)
46 *
47 *   SQL Databases:
48 *
49 *   cache_type          cache_options
50 *   -------------------------------------------------------------------
51 *   mysql               host, database, username, password
52 *
53 *
54 *   DBM-Style Databases:    (use extension.cache.dbm)
55 *
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)
63 *
64 *   PHP must have write access to both dbm_filename and lock_filename.
65 *
66 *
67 * Recommended Cache Types
68 *
69 *   Infrequent updates, many reads      any DBM
70 *   Frequent updates                    mysql
71 */
72
73
74 class getID3_cached_mysql extends getID3
75 {
76
77         // private vars
78         private $cursor;
79         private $connection;
80
81
82         // public: constructor - see top of this file for cache type and cache_options
83         public function __construct($host, $database, $username, $password, $table='getid3_cache') {
84
85                 // Check for mysql support
86                 if (!function_exists('mysql_pconnect')) {
87                         throw new Exception('PHP not compiled with mysql support.');
88                 }
89
90                 // Connect to database
91                 $this->connection = mysql_pconnect($host, $username, $password);
92                 if (!$this->connection) {
93                         throw new Exception('mysql_pconnect() failed - check permissions and spelling.');
94                 }
95
96                 // Select database
97                 if (!mysql_select_db($database, $this->connection)) {
98                         throw new Exception('Cannot use database '.$database);
99                 }
100
101                 // Set table
102                 $this->table = $table;
103
104                 // Create cache table if not exists
105                 $this->create_table();
106
107                 // Check version number and clear cache if changed
108                 $version = '';
109                 $SQLquery  = 'SELECT `value`';
110                 $SQLquery .= ' FROM `'.mysql_real_escape_string($this->table).'`';
111                 $SQLquery .= ' WHERE (`filename` = \''.mysql_real_escape_string(getID3::VERSION).'\')';
112                 $SQLquery .= ' AND (`filesize` = -1)';
113                 $SQLquery .= ' AND (`filetime` = -1)';
114                 $SQLquery .= ' AND (`analyzetime` = -1)';
115                 if ($this->cursor = mysql_query($SQLquery, $this->connection)) {
116                         list($version) = mysql_fetch_array($this->cursor);
117                 }
118                 if ($version != getID3::VERSION) {
119                         $this->clear_cache();
120                 }
121
122                 parent::__construct();
123         }
124
125
126
127         // public: clear cache
128         public function clear_cache() {
129
130                 $this->cursor = mysql_query('DELETE FROM `'.mysql_real_escape_string($this->table).'`', $this->connection);
131                 $this->cursor = mysql_query('INSERT INTO `'.mysql_real_escape_string($this->table).'` VALUES (\''.getID3::VERSION.'\', -1, -1, -1, \''.getID3::VERSION.'\')', $this->connection);
132         }
133
134
135
136         // public: analyze file
137         public function analyze($filename, $filesize=null, $original_filename='') {
138
139                 if (file_exists($filename)) {
140
141                         // Short-hands
142                         $filetime = filemtime($filename);
143                         $filesize =  filesize($filename);
144
145                         // Lookup file
146                         $SQLquery  = 'SELECT `value`';
147                         $SQLquery .= ' FROM `'.mysql_real_escape_string($this->table).'`';
148                         $SQLquery .= ' WHERE (`filename` = \''.mysql_real_escape_string($filename).'\')';
149                         $SQLquery .= '   AND (`filesize` = \''.mysql_real_escape_string($filesize).'\')';
150                         $SQLquery .= '   AND (`filetime` = \''.mysql_real_escape_string($filetime).'\')';
151                         $this->cursor = mysql_query($SQLquery, $this->connection);
152                         if (mysql_num_rows($this->cursor) > 0) {
153                                 // Hit
154                                 list($result) = mysql_fetch_array($this->cursor);
155                                 return unserialize(base64_decode($result));
156                         }
157                 }
158
159                 // Miss
160                 $analysis = parent::analyze($filename, $filesize, $original_filename);
161
162                 // Save result
163                 if (file_exists($filename)) {
164                         $SQLquery  = 'INSERT INTO `'.mysql_real_escape_string($this->table).'` (`filename`, `filesize`, `filetime`, `analyzetime`, `value`) VALUES (';
165                         $SQLquery .=   '\''.mysql_real_escape_string($filename).'\'';
166                         $SQLquery .= ', \''.mysql_real_escape_string($filesize).'\'';
167                         $SQLquery .= ', \''.mysql_real_escape_string($filetime).'\'';
168                         $SQLquery .= ', \''.mysql_real_escape_string(time()   ).'\'';
169                         $SQLquery .= ', \''.mysql_real_escape_string(base64_encode(serialize($analysis))).'\')';
170                         $this->cursor = mysql_query($SQLquery, $this->connection);
171                 }
172                 return $analysis;
173         }
174
175
176
177         // private: (re)create sql table
178         private function create_table($drop=false) {
179
180                 $SQLquery  = 'CREATE TABLE IF NOT EXISTS `'.mysql_real_escape_string($this->table).'` (';
181                 $SQLquery .=   '`filename` VARCHAR(990) NOT NULL DEFAULT \'\'';
182                 $SQLquery .= ', `filesize` INT(11) NOT NULL DEFAULT \'0\'';
183                 $SQLquery .= ', `filetime` INT(11) NOT NULL DEFAULT \'0\'';
184                 $SQLquery .= ', `analyzetime` INT(11) NOT NULL DEFAULT \'0\'';
185                 $SQLquery .= ', `value` LONGTEXT NOT NULL';
186                 $SQLquery .= ', PRIMARY KEY (`filename`, `filesize`, `filetime`)) ENGINE=MyISAM CHARACTER SET=latin1 COLLATE=latin1_general_ci';
187                 $this->cursor = mysql_query($SQLquery, $this->connection);
188                 echo mysql_error($this->connection);
189         }
190 }