extension.cache.sqlite3.php 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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. * __construct()
  95. * @param string $table holds name of sqlite table
  96. * @return type
  97. */
  98. public function __construct($table='getid3_cache', $hide=false) {
  99. $this->table = $table; // Set table
  100. $file = dirname(__FILE__).'/'.basename(__FILE__, 'php').'sqlite';
  101. if ($hide) {
  102. $file = dirname(__FILE__).'/.ht.'.basename(__FILE__, 'php').'sqlite';
  103. }
  104. $this->db = new SQLite3($file);
  105. $db = $this->db;
  106. $this->create_table(); // Create cache table if not exists
  107. $version = '';
  108. $sql = $this->version_check;
  109. $stmt = $db->prepare($sql);
  110. $stmt->bindValue(':filename', getID3::VERSION, SQLITE3_TEXT);
  111. $result = $stmt->execute();
  112. list($version) = $result->fetchArray();
  113. if ($version != getID3::VERSION) { // Check version number and clear cache if changed
  114. $this->clear_cache();
  115. }
  116. return parent::__construct();
  117. }
  118. /**
  119. * close the database connection
  120. */
  121. public function __destruct() {
  122. $db=$this->db;
  123. $db->close();
  124. }
  125. /**
  126. * hold the sqlite db
  127. * @var SQLite Resource
  128. */
  129. private $db;
  130. /**
  131. * table to use for caching
  132. * @var string $table
  133. */
  134. private $table;
  135. /**
  136. * clear the cache
  137. * @access private
  138. * @return type
  139. */
  140. private function clear_cache() {
  141. $db = $this->db;
  142. $sql = $this->delete_cache;
  143. $db->exec($sql);
  144. $sql = $this->set_version;
  145. $stmt = $db->prepare($sql);
  146. $stmt->bindValue(':filename', getID3::VERSION, SQLITE3_TEXT);
  147. $stmt->bindValue(':dirname', getID3::VERSION, SQLITE3_TEXT);
  148. $stmt->bindValue(':val', getID3::VERSION, SQLITE3_TEXT);
  149. return $stmt->execute();
  150. }
  151. /**
  152. * analyze file and cache them, if cached pull from the db
  153. * @param type $filename
  154. * @return boolean
  155. */
  156. public function analyze($filename, $filesize=null, $original_filename='') {
  157. if (!file_exists($filename)) {
  158. return false;
  159. }
  160. // items to track for caching
  161. $filetime = filemtime($filename);
  162. $filesize_real = filesize($filename);
  163. // this will be saved for a quick directory lookup of analized files
  164. // ... why do 50 seperate sql quries when you can do 1 for the same result
  165. $dirname = dirname($filename);
  166. // Lookup file
  167. $db = $this->db;
  168. $sql = $this->get_id3_data;
  169. $stmt = $db->prepare($sql);
  170. $stmt->bindValue(':filename', $filename, SQLITE3_TEXT);
  171. $stmt->bindValue(':filesize', $filesize_real, SQLITE3_INTEGER);
  172. $stmt->bindValue(':filetime', $filetime, SQLITE3_INTEGER);
  173. $res = $stmt->execute();
  174. list($result) = $res->fetchArray();
  175. if (count($result) > 0 ) {
  176. return unserialize(base64_decode($result));
  177. }
  178. // if it hasn't been analyzed before, then do it now
  179. $analysis = parent::analyze($filename, $filesize, $original_filename);
  180. // Save result
  181. $sql = $this->cache_file;
  182. $stmt = $db->prepare($sql);
  183. $stmt->bindValue(':filename', $filename, SQLITE3_TEXT);
  184. $stmt->bindValue(':dirname', $dirname, SQLITE3_TEXT);
  185. $stmt->bindValue(':filesize', $filesize_real, SQLITE3_INTEGER);
  186. $stmt->bindValue(':filetime', $filetime, SQLITE3_INTEGER);
  187. $stmt->bindValue(':atime', time(), SQLITE3_INTEGER);
  188. $stmt->bindValue(':val', base64_encode(serialize($analysis)), SQLITE3_TEXT);
  189. $res = $stmt->execute();
  190. return $analysis;
  191. }
  192. /**
  193. * create data base table
  194. * this is almost the same as MySQL, with the exception of the dirname being added
  195. * @return type
  196. */
  197. private function create_table() {
  198. $db = $this->db;
  199. $sql = $this->make_table;
  200. return $db->exec($sql);
  201. }
  202. /**
  203. * get cached directory
  204. *
  205. * This function is not in the MySQL extention, it's ment to speed up requesting multiple files
  206. * which is ideal for podcasting, playlists, etc.
  207. *
  208. * @access public
  209. * @param string $dir directory to search the cache database for
  210. * @return array return an array of matching id3 data
  211. */
  212. public function get_cached_dir($dir) {
  213. $db = $this->db;
  214. $rows = array();
  215. $sql = $this->get_cached_dir;
  216. $stmt = $db->prepare($sql);
  217. $stmt->bindValue(':dirname', $dir, SQLITE3_TEXT);
  218. $res = $stmt->execute();
  219. while ($row=$res->fetchArray()) {
  220. $rows[] = unserialize(base64_decode($row));
  221. }
  222. return $rows;
  223. }
  224. /**
  225. * use the magical __get() for sql queries
  226. *
  227. * access as easy as $this->{case name}, returns NULL if query is not found
  228. */
  229. public function __get($name) {
  230. switch($name) {
  231. case 'version_check':
  232. return "SELECT val FROM $this->table WHERE filename = :filename AND filesize = '-1' AND filetime = '-1' AND analyzetime = '-1'";
  233. break;
  234. case 'delete_cache':
  235. return "DELETE FROM $this->table";
  236. break;
  237. case 'set_version':
  238. return "INSERT INTO $this->table (filename, dirname, filesize, filetime, analyzetime, val) VALUES (:filename, :dirname, -1, -1, -1, :val)";
  239. break;
  240. case 'get_id3_data':
  241. return "SELECT val FROM $this->table WHERE filename = :filename AND filesize = :filesize AND filetime = :filetime";
  242. break;
  243. case 'cache_file':
  244. return "INSERT INTO $this->table (filename, dirname, filesize, filetime, analyzetime, val) VALUES (:filename, :dirname, :filesize, :filetime, :atime, :val)";
  245. break;
  246. case 'make_table':
  247. //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))";
  248. 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))";
  249. break;
  250. case 'get_cached_dir':
  251. return "SELECT val FROM $this->table WHERE dirname = :dirname";
  252. break;
  253. }
  254. return null;
  255. }
  256. }