ILIAS  release_5-2 Revision v5.2.25-18-g3f80b828510
Sqlite3.php
Go to the documentation of this file.
1<?php
2
4
6
9// available at http://getid3.sourceforge.net //
10// or http://www.getid3.org ///
13// extension.cache.sqlite3.php - part of GetId3() //
14// Please see readme.txt for more information //
15// ///
18// MySQL extension written by Allan Hansen <ahØartemis*dk> //
19// Table name mod by Carlo Capocasa <calroØcarlocapocasa*com> //
20// MySQL extension was reworked for SQLite3 by Karl G. Holz <newaeonØmac*com> //
21// ///
23
106class Sqlite3 extends GetId3
107{
112 private $db;
113
118 private $table;
119
125 public function __construct($table = 'getid3_cache', $hide = false)
126 {
127 $this->table = $table; // Set table
128 $file = dirname(__FILE__) . '/' . basename(__FILE__, 'php') . 'sqlite';
129 if ($hide) {
130 $file = dirname(__FILE__) . '/.ht.' . basename(__FILE__, 'php') . 'sqlite';
131 }
132 $this->db = new SQLite3($file);
133 $db = $this->db;
134 $this->create_table(); // Create cache table if not exists
135 $version = '';
136 $sql = $this->version_check;
137 $stmt = $db->prepare($sql);
138 $stmt->bindValue(':filename', GetId3Core::VERSION, SQLITE3_TEXT);
139 $result = $stmt->execute();
140 list($version) = $result->fetchArray();
141 if ($version != GetId3Core::VERSION) { // Check version number and clear cache if changed
142 $this->clear_cache();
143 }
144
145 return parent::__construct();
146 }
147
151 public function __destruct()
152 {
153 $db = $this->db;
154 $db->close();
155 }
156
162 private function clear_cache()
163 {
164 $db = $this->db;
165 $sql = $this->delete_cache;
166 $db->exec($sql);
167 $sql = $this->set_version;
168 $stmt = $db->prepare($sql);
169 $stmt->bindValue(':filename', GetId3Core::VERSION, SQLITE3_TEXT);
170 $stmt->bindValue(':dirname', GetId3Core::VERSION, SQLITE3_TEXT);
171 $stmt->bindValue(':val', GetId3Core::VERSION, SQLITE3_TEXT);
172
173 return $stmt->execute();
174 }
175
181 public function analyze($filename)
182 {
183 if (!file_exists($filename)) {
184 return false;
185 }
186 // items to track for caching
187 $filetime = filemtime($filename);
188 $filesize = filesize($filename);
189 // this will be saved for a quick directory lookup of analized files
190 // ... why do 50 seperate sql quries when you can do 1 for the same result
191 $dirname = dirname($filename);
192 // Lookup file
193 $db = $this->db;
194 $sql = $this->get_id3_data;
195 $stmt = $db->prepare($sql);
196 $stmt->bindValue(':filename', $filename, SQLITE3_TEXT);
197 $stmt->bindValue(':filesize', $filesize, SQLITE3_INTEGER);
198 $stmt->bindValue(':filetime', $filetime, SQLITE3_INTEGER);
199 $res = $stmt->execute();
200 list($result) = $res->fetchArray();
201 if (count($result) > 0) {
202 return unserialize(base64_decode($result));
203 }
204 // if it hasn't been analyzed before, then do it now
205 $analysis = parent::analyze($filename);
206 // Save result
207 $sql = $this->cache_file;
208 $stmt = $db->prepare($sql);
209 $stmt->bindValue(':filename', $filename, SQLITE3_TEXT);
210 $stmt->bindValue(':dirname', $dirname, SQLITE3_TEXT);
211 $stmt->bindValue(':filesize', $filesize, SQLITE3_INTEGER);
212 $stmt->bindValue(':filetime', $filetime, SQLITE3_INTEGER);
213 $stmt->bindValue(':atime', time(), SQLITE3_INTEGER);
214 $stmt->bindValue(':val', base64_encode(serialize($analysis)),
215 SQLITE3_TEXT);
216 $res = $stmt->execute();
217
218 return $analysis;
219 }
220
226 private function create_table()
227 {
228 $db = $this->db;
229 $sql = $this->make_table;
230
231 return $db->exec($sql);
232 }
233
244 public function get_cached_dir($dir)
245 {
246 $db = $this->db;
247 $rows = array();
248 $sql = $this->get_cached_dir;
249 $stmt = $db->prepare($sql);
250 $stmt->bindValue(':dirname', $dir, SQLITE3_TEXT);
251 $res = $stmt->execute();
252 while ($row = $res->fetchArray()) {
253 $rows[] = unserialize(base64_decode($row));
254 }
255
256 return $rows;
257 }
258
264 public function __get($name)
265 {
266 switch ($name) {
267 case 'version_check':
268 return "SELECT val FROM $this->table WHERE filename = :filename AND filesize = '-1' AND filetime = '-1' AND analyzetime = '-1'";
269 break;
270 case 'delete_cache':
271 return "DELETE FROM $this->table";
272 break;
273 case 'set_version':
274 return "INSERT INTO $this->table (filename, dirname, filesize, filetime, analyzetime, val) VALUES (:filename, :dirname, -1, -1, -1, :val)";
275 break;
276 case 'get_id3_data':
277 return "SELECT val FROM $this->table WHERE filename = :filename AND filesize = :filesize AND filetime = :filetime";
278 break;
279 case 'cache_file':
280 return "INSERT INTO $this->table (filename, dirname, filesize, filetime, analyzetime, val) VALUES (:filename, :dirname, :filesize, :filetime, :atime, :val)";
281 break;
282 case 'make_table':
283 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))";
284 break;
285 case 'get_cached_dir':
286 return "SELECT val FROM $this->table WHERE dirname = :dirname";
287 break;
288 }
289 }
290}
$result
An exception for terminatinating execution or to throw for unit testing.
GetId3() by James Heinrich info@getid3.org //.
Definition: Sqlite3.php:107
__construct($table='getid3_cache', $hide=false)
__construct()
Definition: Sqlite3.php:125
clear_cache()
clear the cache @access private
Definition: Sqlite3.php:162
create_table()
create data base table this is almost the same as MySQL, with the exception of the dirname being adde...
Definition: Sqlite3.php:226
$table
table to use for caching
Definition: Sqlite3.php:118
__destruct()
close the database connection
Definition: Sqlite3.php:151
get_cached_dir($dir)
get cached directory
Definition: Sqlite3.php:244
analyze($filename)
analyze file and cache them, if cached pull from the db
Definition: Sqlite3.php:181
__get($name)
use the magical __get() for sql queries
Definition: Sqlite3.php:264
GetId3() by James Heinrich info@getid3.org //.
Definition: GetId3Core.php:26
if(!file_exists("$old.txt")) if( $old===$new) if(file_exists("$new.txt")) $file