ILIAS  release_5-2 Revision v5.2.25-18-g3f80b828510
Mysql.php
Go to the documentation of this file.
1 <?php
2 
3 namespace GetId3\Extension\Cache;
4 
7 
10 // available at http://getid3.sourceforge.net //
11 // or http://www.getid3.org //
13 // //
14 // extension.cache.mysql.php - part of GetId3() //
15 // Please see readme.txt for more information //
16 // ///
18 // //
19 // This extension written by Allan Hansen <ahØartemis*dk> //
20 // Table name mod by Carlo Capocasa <calroØcarlocapocasa*com> //
21 // ///
23 
85 class Mysql extends GetId3
86 {
91  private $cursor;
92 
97  private $connection;
98 
109  public function __construct($host, $database, $username, $password, $table='getid3_cache')
110  {
111  // Check for mysql support
112  if (!function_exists('mysql_pconnect')) {
113  throw new DefaultException('PHP not compiled with mysql support.');
114  }
115 
116  // Connect to database
117  $this->connection = mysql_pconnect($host, $username, $password);
118  if (!$this->connection) {
119  throw new DefaultException('mysql_pconnect() failed - check permissions and spelling.');
120  }
121 
122  // Select database
123  if (!mysql_select_db($database, $this->connection)) {
124  throw new DefaultException('Cannot use database '.$database);
125  }
126 
127  // Set table
128  $this->table = $table;
129 
130  // Create cache table if not exists
131  $this->create_table();
132 
133  // Check version number and clear cache if changed
134  $version = '';
135  if ($this->cursor = mysql_query("SELECT `value` FROM `".mysql_real_escape_string($this->table)."` WHERE (`filename` = '".mysql_real_escape_string(GetId3Core::VERSION)."') AND (`filesize` = '-1') AND (`filetime` = '-1') AND (`analyzetime` = '-1')", $this->connection)) {
136  list($version) = mysql_fetch_array($this->cursor);
137  }
138  if ($version != GetId3Core::VERSION) {
139  $this->clear_cache();
140  }
141 
142  parent::__construct();
143  }
144 
148  public function clear_cache()
149  {
150  $this->cursor = mysql_query("DELETE FROM `".mysql_real_escape_string($this->table)."`", $this->connection);
151  $this->cursor = mysql_query("INSERT INTO `".mysql_real_escape_string($this->table)."` VALUES ('".GetId3Core::VERSION."', -1, -1, -1, '".GetId3Core::VERSION."')", $this->connection);
152  }
153 
160  public function analyze($filename)
161  {
162  if (file_exists($filename)) {
163 
164  // Short-hands
165  $filetime = filemtime($filename);
166  $filesize = filesize($filename);
167 
168  // Lookup file
169  $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);
170  if (mysql_num_rows($this->cursor) > 0) {
171  // Hit
172  list($result) = mysql_fetch_array($this->cursor);
173 
174  return unserialize(base64_decode($result));
175  }
176  }
177 
178  // Miss
179  $analysis = parent::analyze($filename);
180 
181  // Save result
182  if (file_exists($filename)) {
183  $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);
184  }
185 
186  return $analysis;
187  }
188 
194  private function create_table($drop=false)
195  {
196  $this->cursor = mysql_query("CREATE TABLE IF NOT EXISTS `".mysql_real_escape_string($this->table)."` (
197  `filename` VARCHAR(255) NOT NULL DEFAULT '',
198  `filesize` INT(11) NOT NULL DEFAULT '0',
199  `filetime` INT(11) NOT NULL DEFAULT '0',
200  `analyzetime` INT(11) NOT NULL DEFAULT '0',
201  `value` TEXT NOT NULL,
202  PRIMARY KEY (`filename`,`filesize`,`filetime`)) ENGINE=MyISAM", $this->connection);
203  echo mysql_error($this->connection);
204  }
205 }
$result
create_table($drop=false)
private: (re)create sql table
Definition: Mysql.php:194
GetId3() by James Heinrich info@getid3.org //.
Definition: Mysql.php:85
clear_cache()
public: clear cache
Definition: Mysql.php:148
Add data(end) time
Method that wraps PHPs time in order to allow simulations with the workflow.
__construct($host, $database, $username, $password, $table='getid3_cache')
public: constructor - see top of this file for cache type and cache_options
Definition: Mysql.php:109
analyze($filename)
public: analyze file
Definition: Mysql.php:160