PageRenderTime 26ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/modules/getid3/extension.cache.sqlite3.php

https://gitlab.com/x33n/ampache
PHP | 265 lines | 113 code | 10 blank | 142 comment | 6 complexity | 9943806655b2fb1f6738a0064f1c0240 MD5 | raw file
  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. *** database file will be stored in the same directory as this script,
  53. *** webserver must have write access to that directory!
  54. *** set $hide to TRUE to prefix db file with .ht to pervent access from web client
  55. *** this is a default setting in the Apache configuration:
  56. # The following lines prevent .htaccess and .htpasswd files from being viewed by Web clients.
  57. <Files ~ "^\.ht">
  58. Order allow,deny
  59. Deny from all
  60. Satisfy all
  61. </Files>
  62. ********************************************************************************
  63. *
  64. * -------------------------------------------------------------------
  65. * DBM-Style Databases: (use extension.cache.dbm)
  66. *
  67. * cache_type cache_options
  68. * -------------------------------------------------------------------
  69. * gdbm dbm_filename, lock_filename
  70. * ndbm dbm_filename, lock_filename
  71. * db2 dbm_filename, lock_filename
  72. * db3 dbm_filename, lock_filename
  73. * db4 dbm_filename, lock_filename (PHP5 required)
  74. *
  75. * PHP must have write access to both dbm_filename and lock_filename.
  76. *
  77. * Recommended Cache Types
  78. *
  79. * Infrequent updates, many reads any DBM
  80. * Frequent updates mysql
  81. ********************************************************************************
  82. *
  83. * IMHO this is still a bit slow, I'm using this with MP4/MOV/ M4v files
  84. * there is a plan to add directory scanning and analyzing to make things work much faster
  85. *
  86. *
  87. */
  88. class getID3_cached_sqlite3 extends getID3 {
  89. /**
  90. * __construct()
  91. * @param string $table holds name of sqlite table
  92. * @return type
  93. */
  94. public function __construct($table='getid3_cache', $hide=false) {
  95. $this->table = $table; // Set table
  96. $file = dirname(__FILE__).'/'.basename(__FILE__, 'php').'sqlite';
  97. if ($hide) {
  98. $file = dirname(__FILE__).'/.ht.'.basename(__FILE__, 'php').'sqlite';
  99. }
  100. $this->db = new SQLite3($file);
  101. $db = $this->db;
  102. $this->create_table(); // Create cache table if not exists
  103. $version = '';
  104. $sql = $this->version_check;
  105. $stmt = $db->prepare($sql);
  106. $stmt->bindValue(':filename', getID3::VERSION, SQLITE3_TEXT);
  107. $result = $stmt->execute();
  108. list($version) = $result->fetchArray();
  109. if ($version != getID3::VERSION) { // Check version number and clear cache if changed
  110. $this->clear_cache();
  111. }
  112. return parent::__construct();
  113. }
  114. /**
  115. * close the database connection
  116. */
  117. public function __destruct() {
  118. $db=$this->db;
  119. $db->close();
  120. }
  121. /**
  122. * hold the sqlite db
  123. * @var SQLite Resource
  124. */
  125. private $db;
  126. /**
  127. * table to use for caching
  128. * @var string $table
  129. */
  130. private $table;
  131. /**
  132. * clear the cache
  133. * @access private
  134. * @return type
  135. */
  136. private function clear_cache() {
  137. $db = $this->db;
  138. $sql = $this->delete_cache;
  139. $db->exec($sql);
  140. $sql = $this->set_version;
  141. $stmt = $db->prepare($sql);
  142. $stmt->bindValue(':filename', getID3::VERSION, SQLITE3_TEXT);
  143. $stmt->bindValue(':dirname', getID3::VERSION, SQLITE3_TEXT);
  144. $stmt->bindValue(':val', getID3::VERSION, SQLITE3_TEXT);
  145. return $stmt->execute();
  146. }
  147. /**
  148. * analyze file and cache them, if cached pull from the db
  149. * @param type $filename
  150. * @return boolean
  151. */
  152. public function analyze($filename) {
  153. if (!file_exists($filename)) {
  154. return false;
  155. }
  156. // items to track for caching
  157. $filetime = filemtime($filename);
  158. $filesize = filesize($filename);
  159. // this will be saved for a quick directory lookup of analized files
  160. // ... why do 50 seperate sql quries when you can do 1 for the same result
  161. $dirname = dirname($filename);
  162. // Lookup file
  163. $db = $this->db;
  164. $sql = $this->get_id3_data;
  165. $stmt = $db->prepare($sql);
  166. $stmt->bindValue(':filename', $filename, SQLITE3_TEXT);
  167. $stmt->bindValue(':filesize', $filesize, SQLITE3_INTEGER);
  168. $stmt->bindValue(':filetime', $filetime, SQLITE3_INTEGER);
  169. $res = $stmt->execute();
  170. list($result) = $res->fetchArray();
  171. if (count($result) > 0 ) {
  172. return unserialize(base64_decode($result));
  173. }
  174. // if it hasn't been analyzed before, then do it now
  175. $analysis = parent::analyze($filename);
  176. // Save result
  177. $sql = $this->cache_file;
  178. $stmt = $db->prepare($sql);
  179. $stmt->bindValue(':filename', $filename, SQLITE3_TEXT);
  180. $stmt->bindValue(':dirname', $dirname, SQLITE3_TEXT);
  181. $stmt->bindValue(':filesize', $filesize, SQLITE3_INTEGER);
  182. $stmt->bindValue(':filetime', $filetime, SQLITE3_INTEGER);
  183. $stmt->bindValue(':atime', time(), SQLITE3_INTEGER);
  184. $stmt->bindValue(':val', base64_encode(serialize($analysis)), SQLITE3_TEXT);
  185. $res = $stmt->execute();
  186. return $analysis;
  187. }
  188. /**
  189. * create data base table
  190. * this is almost the same as MySQL, with the exception of the dirname being added
  191. * @return type
  192. */
  193. private function create_table() {
  194. $db = $this->db;
  195. $sql = $this->make_table;
  196. return $db->exec($sql);
  197. }
  198. /**
  199. * get cached directory
  200. *
  201. * This function is not in the MySQL extention, it's ment to speed up requesting multiple files
  202. * which is ideal for podcasting, playlists, etc.
  203. *
  204. * @access public
  205. * @param string $dir directory to search the cache database for
  206. * @return array return an array of matching id3 data
  207. */
  208. public function get_cached_dir($dir) {
  209. $db = $this->db;
  210. $rows = array();
  211. $sql = $this->get_cached_dir;
  212. $stmt = $db->prepare($sql);
  213. $stmt->bindValue(':dirname', $dir, SQLITE3_TEXT);
  214. $res = $stmt->execute();
  215. while ($row=$res->fetchArray()) {
  216. $rows[] = unserialize(base64_decode($row));
  217. }
  218. return $rows;
  219. }
  220. /**
  221. * use the magical __get() for sql queries
  222. *
  223. * access as easy as $this->{case name}, returns NULL if query is not found
  224. */
  225. public function __get($name) {
  226. switch($name) {
  227. case 'version_check':
  228. return "SELECT val FROM $this->table WHERE filename = :filename AND filesize = '-1' AND filetime = '-1' AND analyzetime = '-1'";
  229. break;
  230. case 'delete_cache':
  231. return "DELETE FROM $this->table";
  232. break;
  233. case 'set_version':
  234. return "INSERT INTO $this->table (filename, dirname, filesize, filetime, analyzetime, val) VALUES (:filename, :dirname, -1, -1, -1, :val)";
  235. break;
  236. case 'get_id3_data':
  237. return "SELECT val FROM $this->table WHERE filename = :filename AND filesize = :filesize AND filetime = :filetime";
  238. break;
  239. case 'cache_file':
  240. return "INSERT INTO $this->table (filename, dirname, filesize, filetime, analyzetime, val) VALUES (:filename, :dirname, :filesize, :filetime, :atime, :val)";
  241. break;
  242. case 'make_table':
  243. 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))";
  244. break;
  245. case 'get_cached_dir':
  246. return "SELECT val FROM $this->table WHERE dirname = :dirname";
  247. break;
  248. }
  249. return null;
  250. }
  251. }