/library/GetID3/extension.cache.mysql.php

https://bitbucket.org/kdeberna/akalepse · PHP · 173 lines · 59 code · 31 blank · 83 comment · 9 complexity · 9c534d083bb007698b1a304c90554676 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. /////////////////////////////////////////////////////////////////
  7. // //
  8. // extension.cache.mysql.php - part of getID3() //
  9. // Please see readme.txt for more information //
  10. // ///
  11. /////////////////////////////////////////////////////////////////
  12. // //
  13. // This extension written by Allan Hansen <ahØartemis*dk> //
  14. // Table name mod by Carlo Capocasa <calroØcarlocapocasa*com> //
  15. // ///
  16. /////////////////////////////////////////////////////////////////
  17. /**
  18. * This is a caching extension for getID3(). It works the exact same
  19. * way as the getID3 class, but return cached information very fast
  20. *
  21. * Example: (see also demo.cache.mysql.php in /demo/)
  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/getid3/extension.cache.mysql.php';
  35. * // 5th parameter (tablename) is optional, default is 'getid3_cache'
  36. * $getID3 = new getID3_cached_mysql('localhost', 'database', 'username', 'password', 'tablename');
  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. *
  51. * DBM-Style Databases: (use extension.cache.dbm)
  52. *
  53. * cache_type cache_options
  54. * -------------------------------------------------------------------
  55. * gdbm dbm_filename, lock_filename
  56. * ndbm dbm_filename, lock_filename
  57. * db2 dbm_filename, lock_filename
  58. * db3 dbm_filename, lock_filename
  59. * db4 dbm_filename, lock_filename (PHP5 required)
  60. *
  61. * PHP must have write access to both dbm_filename and lock_filename.
  62. *
  63. *
  64. * Recommended Cache Types
  65. *
  66. * Infrequent updates, many reads any DBM
  67. * Frequent updates mysql
  68. */
  69. class getID3_cached_mysql extends getID3
  70. {
  71. // private vars
  72. var $cursor;
  73. var $connection;
  74. // public: constructor - see top of this file for cache type and cache_options
  75. function getID3_cached_mysql($host, $database, $username, $password, $table='getid3_cache') {
  76. // Check for mysql support
  77. if (!function_exists('mysql_pconnect')) {
  78. throw new Exception('PHP not compiled with mysql support.');
  79. }
  80. // Connect to database
  81. $this->connection = mysql_pconnect($host, $username, $password);
  82. if (!$this->connection) {
  83. throw new Exception('mysql_pconnect() failed - check permissions and spelling.');
  84. }
  85. // Select database
  86. if (!mysql_select_db($database, $this->connection)) {
  87. throw new Exception('Cannot use database '.$database);
  88. }
  89. // Set table
  90. $this->table = $table;
  91. // Create cache table if not exists
  92. $this->create_table();
  93. // Check version number and clear cache if changed
  94. $version = '';
  95. if ($this->cursor = mysql_query("SELECT `value` FROM `".mysql_real_escape_string($this->table)."` WHERE (`filename` = '".mysql_real_escape_string(getID3::VERSION)."') AND (`filesize` = '-1') AND (`filetime` = '-1') AND (`analyzetime` = '-1')", $this->connection)) {
  96. list($version) = mysql_fetch_array($this->cursor);
  97. }
  98. if ($version != getID3::VERSION) {
  99. $this->clear_cache();
  100. }
  101. parent::getID3();
  102. }
  103. // public: clear cache
  104. function clear_cache() {
  105. $this->cursor = mysql_query("DELETE FROM `".mysql_real_escape_string($this->table)."`", $this->connection);
  106. $this->cursor = mysql_query("INSERT INTO `".mysql_real_escape_string($this->table)."` VALUES ('".getID3::VERSION."', -1, -1, -1, '".getID3::VERSION."')", $this->connection);
  107. }
  108. // public: analyze file
  109. function analyze($filename) {
  110. if (file_exists($filename)) {
  111. // Short-hands
  112. $filetime = filemtime($filename);
  113. $filesize = filesize($filename);
  114. // Lookup file
  115. $this->cursor = mysql_query("SELECT `value` FROM `".mysql_real_escape_string($this->table)."` WHERE (`filename` = '".mysql_real_escape_string($filename)."') AND (`filesize` = '".mysql_real_escape_string($filesize)."') AND (`filetime` = '".mysql_real_escape_string($filetime)."')", $this->connection);
  116. if (mysql_num_rows($this->cursor) > 0) {
  117. // Hit
  118. list($result) = mysql_fetch_array($this->cursor);
  119. return unserialize(base64_decode($result));
  120. }
  121. }
  122. // Miss
  123. $analysis = parent::analyze($filename);
  124. // Save result
  125. if (file_exists($filename)) {
  126. $this->cursor = mysql_query("INSERT INTO `".mysql_real_escape_string($this->table)."` (`filename`, `filesize`, `filetime`, `analyzetime`, `value`) VALUES ('".mysql_real_escape_string($filename)."', '".mysql_real_escape_string($filesize)."', '".mysql_real_escape_string($filetime)."', '".mysql_real_escape_string(time())."', '".mysql_real_escape_string(base64_encode(serialize($analysis)))."')", $this->connection);
  127. }
  128. return $analysis;
  129. }
  130. // private: (re)create sql table
  131. function create_table($drop=false) {
  132. $this->cursor = mysql_query("CREATE TABLE IF NOT EXISTS `".mysql_real_escape_string($this->table)."` (
  133. `filename` VARCHAR(255) NOT NULL DEFAULT '',
  134. `filesize` INT(11) NOT NULL DEFAULT '0',
  135. `filetime` INT(11) NOT NULL DEFAULT '0',
  136. `analyzetime` INT(11) NOT NULL DEFAULT '0',
  137. `value` TEXT NOT NULL,
  138. PRIMARY KEY (`filename`,`filesize`,`filetime`)) TYPE=MyISAM", $this->connection);
  139. echo mysql_error($this->connection);
  140. }
  141. }
  142. ?>