]> scripts.mit.edu Git - autoinstallsdev/mediawiki.git/blob - vendor/james-heinrich/getid3/getid3/extension.cache.sqlite3.php
MediaWiki 1.30.2
[autoinstallsdev/mediawiki.git] / vendor / james-heinrich / getid3 / getid3 / extension.cache.sqlite3.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.sqlite3.php - part of getID3()                              //
10 // Please see readme.txt for more information                                  //
11 //                                                                            ///
12 /////////////////////////////////////////////////////////////////////////////////
13 ///                                                                            //
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 //                                                                            ///
18 /////////////////////////////////////////////////////////////////////////////////
19 /**
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
22 *
23 *    Normal getID3 usage (example):
24 *
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');
30 *
31 *    getID3_cached usage:
32 *
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');
40 *
41 *
42 * Supported Cache Types    (this extension)
43 *
44 *   SQL Databases:
45 *
46 *   cache_type          cache_options
47 *   -------------------------------------------------------------------
48 *   mysql               host, database, username, password
49 *
50 *   sqlite3             table='getid3_cache', hide=false        (PHP5)
51 *
52 *
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:
57 *
58 * The following lines prevent .htaccess and .htpasswd files from being viewed by Web clients.
59 *
60 * <Files ~ "^\.ht">
61 *     Order allow,deny
62 *     Deny from all
63 *     Satisfy all
64 * </Files>
65 *
66 ********************************************************************************
67 *
68 *   -------------------------------------------------------------------
69 *   DBM-Style Databases:    (use extension.cache.dbm)
70 *
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)
78 *
79 *   PHP must have write access to both dbm_filename and lock_filename.
80 *
81 * Recommended Cache Types
82 *
83 *   Infrequent updates, many reads      any DBM
84 *   Frequent updates                    mysql
85 ********************************************************************************
86 *
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
89 *
90 *
91 */
92 class getID3_cached_sqlite3 extends getID3 {
93
94         /**
95         * __construct()
96         * @param string $table holds name of sqlite table
97         * @return type
98         */
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
120         /**
121         * close the database connection
122         */
123         public function __destruct() {
124                 $db=$this->db;
125                 $db->close();
126         }
127
128         /**
129         * hold the sqlite db
130         * @var SQLite Resource
131         */
132         private $db;
133
134         /**
135         * table to use for caching
136         * @var string $table
137         */
138         private $table;
139
140         /**
141         * clear the cache
142         * @access private
143         * @return type
144         */
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
157         /**
158         * analyze file and cache them, if cached pull from the db
159         * @param type $filename
160         * @return boolean
161         */
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
199         /**
200         * create data base table
201         * this is almost the same as MySQL, with the exception of the dirname being added
202         * @return type
203         */
204         private function create_table() {
205                 $db = $this->db;
206                 $sql = $this->make_table;
207                 return $db->exec($sql);
208         }
209
210         /**
211         * get cached directory
212         *
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.
215         *
216         * @access public
217         * @param string $dir directory to search the cache database for
218         * @return array return an array of matching id3 data
219         */
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
233         /**
234         * use the magical __get() for sql queries
235         *
236         * access as easy as $this->{case name}, returns NULL if query is not found
237         */
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 }