ILIAS  release_5-3 Revision v5.3.23-19-g915713cf615
extension.cache.mysqli.php
Go to the documentation of this file.
1 <?php
4 // available at http://getid3.sourceforge.net //
5 // or http://www.getid3.org //
6 // also https://github.com/JamesHeinrich/getID3 //
8 // //
9 // extension.cache.mysqli.php - part of getID3() //
10 // Please see readme.txt for more information //
11 // ///
13 // //
14 // This extension written by Allan Hansen <ahØartemis*dk> //
15 // Table name mod by Carlo Capocasa <calroØcarlocapocasa*com> //
16 // ///
18 
19 
74 {
75  // private vars
76  private $mysqli;
77  private $cursor;
78 
79 
80  // public: constructor - see top of this file for cache type and cache_options
81  public function __construct($host, $database, $username, $password, $table='getid3_cache') {
82 
83  // Check for mysqli support
84  if (!function_exists('mysqli_connect')) {
85  throw new Exception('PHP not compiled with mysqli support.');
86  }
87 
88  // Connect to database
89  $this->mysqli = new mysqli($host, $username, $password);
90  if (!$this->mysqli) {
91  throw new Exception('mysqli_connect() failed - check permissions and spelling.');
92  }
93 
94  // Select database
95  if (!$this->mysqli->select_db($database)) {
96  throw new Exception('Cannot use database '.$database);
97  }
98 
99  // Set table
100  $this->table = $table;
101 
102  // Create cache table if not exists
103  $this->create_table();
104 
105  // Check version number and clear cache if changed
106  $version = '';
107  $SQLquery = 'SELECT `value`';
108  $SQLquery .= ' FROM `'.$this->mysqli->real_escape_string($this->table).'`';
109  $SQLquery .= ' WHERE (`filename` = \''.$this->mysqli->real_escape_string(getID3::VERSION).'\')';
110  $SQLquery .= ' AND (`filesize` = -1)';
111  $SQLquery .= ' AND (`filetime` = -1)';
112  $SQLquery .= ' AND (`analyzetime` = -1)';
113  if ($this->cursor = $this->mysqli->query($SQLquery)) {
114  list($version) = $this->cursor->fetch_array();
115  }
116  if ($version != getID3::VERSION) {
117  $this->clear_cache();
118  }
119 
120  parent::__construct();
121  }
122 
123 
124  // public: clear cache
125  public function clear_cache() {
126  $this->mysqli->query('DELETE FROM `'.$this->mysqli->real_escape_string($this->table).'`');
127  $this->mysqli->query('INSERT INTO `'.$this->mysqli->real_escape_string($this->table).'` (`filename`, `filesize`, `filetime`, `analyzetime`, `value`) VALUES (\''.getID3::VERSION.'\', -1, -1, -1, \''.getID3::VERSION.'\')');
128  }
129 
130 
131  // public: analyze file
132  public function analyze($filename, $filesize=null, $original_filename='') {
133 
134  if (file_exists($filename)) {
135 
136  // Short-hands
137  $filetime = filemtime($filename);
138  $filesize = filesize($filename);
139 
140  // Lookup file
141  $SQLquery = 'SELECT `value`';
142  $SQLquery .= ' FROM `'.$this->mysqli->real_escape_string($this->table).'`';
143  $SQLquery .= ' WHERE (`filename` = \''.$this->mysqli->real_escape_string($filename).'\')';
144  $SQLquery .= ' AND (`filesize` = \''.$this->mysqli->real_escape_string($filesize).'\')';
145  $SQLquery .= ' AND (`filetime` = \''.$this->mysqli->real_escape_string($filetime).'\')';
146  $this->cursor = $this->mysqli->query($SQLquery);
147  if ($this->cursor->num_rows > 0) {
148  // Hit
149  list($result) = $this->cursor->fetch_array();
150  return unserialize(base64_decode($result));
151  }
152  }
153 
154  // Miss
155  $analysis = parent::analyze($filename, $filesize, $original_filename);
156 
157  // Save result
158  if (file_exists($filename)) {
159  $SQLquery = 'INSERT INTO `'.$this->mysqli->real_escape_string($this->table).'` (`filename`, `filesize`, `filetime`, `analyzetime`, `value`) VALUES (';
160  $SQLquery .= '\''.$this->mysqli->real_escape_string($filename).'\'';
161  $SQLquery .= ', \''.$this->mysqli->real_escape_string($filesize).'\'';
162  $SQLquery .= ', \''.$this->mysqli->real_escape_string($filetime).'\'';
163  $SQLquery .= ', \''.$this->mysqli->real_escape_string(time() ).'\'';
164  $SQLquery .= ', \''.$this->mysqli->real_escape_string(base64_encode(serialize($analysis))).'\')';
165  $this->cursor = $this->mysqli->query($SQLquery);
166  }
167  return $analysis;
168  }
169 
170 
171  // private: (re)create mysqli table
172  private function create_table($drop=false) {
173  $SQLquery = 'CREATE TABLE IF NOT EXISTS `'.$this->mysqli->real_escape_string($this->table).'` (';
174  $SQLquery .= '`filename` VARCHAR(990) NOT NULL DEFAULT \'\'';
175  $SQLquery .= ', `filesize` INT(11) NOT NULL DEFAULT \'0\'';
176  $SQLquery .= ', `filetime` INT(11) NOT NULL DEFAULT \'0\'';
177  $SQLquery .= ', `analyzetime` INT(11) NOT NULL DEFAULT \'0\'';
178  $SQLquery .= ', `value` LONGTEXT NOT NULL';
179  $SQLquery .= ', PRIMARY KEY (`filename`, `filesize`, `filetime`)) ENGINE=MyISAM CHARACTER SET=latin1 COLLATE=latin1_general_ci';
180  $this->cursor = $this->mysqli->query($SQLquery);
181  echo $this->mysqli->error;
182  }
183 }
$filename
Definition: getid3.php:105
getID3() by James Heinrich info@getid3.org //
const VERSION
Definition: getid3.php:115
__construct($host, $database, $username, $password, $table='getid3_cache')
$password
Definition: pwgen.php:17
Add data(end) time
Method that wraps PHPs time in order to allow simulations with the workflow.
if(empty($password)) $table
Definition: pwgen.php:24
error($message)
Definition: getid3.php:508