extension.cache.mysql.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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. // ///
  15. /////////////////////////////////////////////////////////////////
  16. /**
  17. * This is a caching extension for getID3(). It works the exact same
  18. * way as the getID3 class, but return cached information very fast
  19. *
  20. * Example: (see also demo.cache.mysql.php in /demo/)
  21. *
  22. * Normal getID3 usage (example):
  23. *
  24. * require_once 'getid3/getid3.php';
  25. * $getID3 = new getID3;
  26. * $getID3->encoding = 'UTF-8';
  27. * $info1 = $getID3->analyze('file1.flac');
  28. * $info2 = $getID3->analyze('file2.wv');
  29. *
  30. * getID3_cached usage:
  31. *
  32. * require_once 'getid3/getid3.php';
  33. * require_once 'getid3/getid3/extension.cache.mysql.php';
  34. * $getID3 = new getID3_cached_mysql('localhost', 'database',
  35. * 'username', 'password');
  36. * $getID3->encoding = 'UTF-8';
  37. * $info1 = $getID3->analyze('file1.flac');
  38. * $info2 = $getID3->analyze('file2.wv');
  39. *
  40. *
  41. * Supported Cache Types (this extension)
  42. *
  43. * SQL Databases:
  44. *
  45. * cache_type cache_options
  46. * -------------------------------------------------------------------
  47. * mysql host, database, username, password
  48. *
  49. *
  50. * DBM-Style Databases: (use extension.cache.dbm)
  51. *
  52. * cache_type cache_options
  53. * -------------------------------------------------------------------
  54. * gdbm dbm_filename, lock_filename
  55. * ndbm dbm_filename, lock_filename
  56. * db2 dbm_filename, lock_filename
  57. * db3 dbm_filename, lock_filename
  58. * db4 dbm_filename, lock_filename (PHP5 required)
  59. *
  60. * PHP must have write access to both dbm_filename and lock_filename.
  61. *
  62. *
  63. * Recommended Cache Types
  64. *
  65. * Infrequent updates, many reads any DBM
  66. * Frequent updates mysql
  67. */
  68. class getID3_cached_mysql extends getID3
  69. {
  70. // private vars
  71. var $cursor;
  72. var $connection;
  73. // public: constructor - see top of this file for cache type and cache_options
  74. function getID3_cached_mysql($host, $database, $username, $password) {
  75. // Check for mysql support
  76. if (!function_exists('mysql_pconnect')) {
  77. die('PHP not compiled with mysql support.');
  78. }
  79. // Connect to database
  80. $this->connection = mysql_pconnect($host, $username, $password);
  81. if (!$this->connection) {
  82. die('mysql_pconnect() failed - check permissions and spelling.');
  83. }
  84. // Select database
  85. if (!mysql_select_db($database, $this->connection)) {
  86. die('Cannot use database '.$database);
  87. }
  88. // Create cache table if not exists
  89. $this->create_table();
  90. // Check version number and clear cache if changed
  91. $this->cursor = mysql_query("SELECT `value` FROM `getid3_cache` WHERE (`filename` = '".GETID3_VERSION."') AND (`filesize` = '-1') AND (`filetime` = '-1') AND (`analyzetime` = '-1')", $this->connection);
  92. list($version) = @mysql_fetch_array($this->cursor);
  93. if ($version != GETID3_VERSION) {
  94. $this->clear_cache();
  95. }
  96. parent::getID3();
  97. }
  98. // public: clear cache
  99. function clear_cache() {
  100. $this->cursor = mysql_query("DELETE FROM `getid3_cache`", $this->connection);
  101. $this->cursor = mysql_query("INSERT INTO `getid3_cache` VALUES ('".GETID3_VERSION."', -1, -1, -1, '".GETID3_VERSION."')", $this->connection);
  102. }
  103. // public: analyze file
  104. function analyze($filename) {
  105. if (file_exists($filename)) {
  106. // Short-hands
  107. $filetime = filemtime($filename);
  108. $filesize = filesize($filename);
  109. $filenam2 = mysql_escape_string($filename);
  110. // Loopup file
  111. $this->cursor = mysql_query("SELECT `value` FROM `getid3_cache` WHERE (`filename`='".$filenam2."') AND (`filesize`='".$filesize."') AND (`filetime`='".$filetime."')", $this->connection);
  112. list($result) = @mysql_fetch_array($this->cursor);
  113. // Hit
  114. if ($result) {
  115. return unserialize($result);
  116. }
  117. }
  118. // Miss
  119. $result = parent::analyze($filename);
  120. // Save result
  121. if (file_exists($filename)) {
  122. $res2 = mysql_escape_string(serialize($result));
  123. $this->cursor = mysql_query("INSERT INTO `getid3_cache` (`filename`, `filesize`, `filetime`, `analyzetime`, `value`) VALUES ('".$filenam2."', '".$filesize."', '".$filetime."', '".time()."', '".$res2."')", $this->connection);
  124. }
  125. return $result;
  126. }
  127. // private: (re)create sql table
  128. function create_table($drop = false) {
  129. $this->cursor = mysql_query("CREATE TABLE IF NOT EXISTS `getid3_cache` (
  130. `filename` VARCHAR(255) NOT NULL DEFAULT '',
  131. `filesize` INT(11) NOT NULL DEFAULT '0',
  132. `filetime` INT(11) NOT NULL DEFAULT '0',
  133. `analyzetime` INT(11) NOT NULL DEFAULT '0',
  134. `value` TEXT NOT NULL,
  135. PRIMARY KEY (`filename`,`filesize`,`filetime`)) TYPE=MyISAM", $this->connection);
  136. echo mysql_error($this->connection);
  137. }
  138. }
  139. ?>