89 $this->options = array(
92 'host' =>
'127.0.0.1',
102 $this->options[
'dbname'] = substr($this->options[
'path'], 1);
106 $this->mysql =
new PDO(
"mysql:dbname={$this->options['dbname']};host={$this->options['host']};port={$this->options['port']}", $this->options[
'user'], $this->options[
'pass'], array(PDO::MYSQL_ATTR_INIT_COMMAND =>
'SET NAMES utf8'));
108 catch (PDOException $e)
114 $this->
id = $name . $type;
116 if (!$query = $this->mysql->query(
'SHOW TABLES'))
123 while ($row = $query->fetchColumn())
128 if (!in_array($this->options[
'extras'][
'prefix'] .
'cache_data', $db))
130 $query = $this->mysql->exec(
'CREATE TABLE `' . $this->options[
'extras'][
'prefix'] .
'cache_data` (`id` TEXT CHARACTER SET utf8 NOT NULL, `items` SMALLINT NOT NULL DEFAULT 0, `data` BLOB NOT NULL, `mtime` INT UNSIGNED NOT NULL, UNIQUE (`id`(125)))');
131 if ($query ===
false)
137 if (!in_array($this->options[
'extras'][
'prefix'] .
'items', $db))
139 $query = $this->mysql->exec(
'CREATE TABLE `' . $this->options[
'extras'][
'prefix'] .
'items` (`feed_id` TEXT CHARACTER SET utf8 NOT NULL, `id` TEXT CHARACTER SET utf8 NOT NULL, `data` TEXT CHARACTER SET utf8 NOT NULL, `posted` INT UNSIGNED NOT NULL, INDEX `feed_id` (`feed_id`(125)))');
140 if ($query ===
false)
155 if ($this->mysql === null)
164 $prepared = self::prepare_simplepie_object_for_cache($data);
166 $query = $this->mysql->prepare(
'SELECT COUNT(*) FROM `' . $this->options[
'extras'][
'prefix'] .
'cache_data` WHERE `id` = :feed');
167 $query->bindValue(
':feed', $this->
id);
168 if ($query->execute())
170 if ($query->fetchColumn() > 0)
172 $items = count($prepared[1]);
175 $sql =
'UPDATE `' . $this->options[
'extras'][
'prefix'] .
'cache_data` SET `items` = :items, `data` = :data, `mtime` = :time WHERE `id` = :feed';
176 $query = $this->mysql->prepare($sql);
177 $query->bindValue(
':items', $items);
181 $sql =
'UPDATE `' . $this->options[
'extras'][
'prefix'] .
'cache_data` SET `data` = :data, `mtime` = :time WHERE `id` = :feed';
182 $query = $this->mysql->prepare($sql);
185 $query->bindValue(
':data', $prepared[0]);
186 $query->bindValue(
':time', time());
187 $query->bindValue(
':feed', $this->
id);
188 if (!$query->execute())
195 $query = $this->mysql->prepare(
'INSERT INTO `' . $this->options[
'extras'][
'prefix'] .
'cache_data` (`id`, `items`, `data`, `mtime`) VALUES(:feed, :count, :data, :time)');
196 $query->bindValue(
':feed', $this->
id);
197 $query->bindValue(
':count', count($prepared[1]));
198 $query->bindValue(
':data', $prepared[0]);
199 $query->bindValue(
':time', time());
200 if (!$query->execute())
206 $ids = array_keys($prepared[1]);
209 foreach ($ids as
$id)
211 $database_ids[] = $this->mysql->quote($id);
214 $query = $this->mysql->prepare(
'SELECT `id` FROM `' . $this->options[
'extras'][
'prefix'] .
'items` WHERE `id` = ' . implode(
' OR `id` = ', $database_ids) .
' AND `feed_id` = :feed');
215 $query->bindValue(
':feed', $this->
id);
217 if ($query->execute())
219 $existing_ids = array();
220 while ($row = $query->fetchColumn())
222 $existing_ids[] = $row;
225 $new_ids = array_diff($ids, $existing_ids);
227 foreach ($new_ids as $new_id)
229 if (!($date = $prepared[1][$new_id]->get_date(
'U')))
234 $query = $this->mysql->prepare(
'INSERT INTO `' . $this->options[
'extras'][
'prefix'] .
'items` (`feed_id`, `id`, `data`, `posted`) VALUES(:feed, :id, :data, :date)');
235 $query->bindValue(
':feed', $this->
id);
236 $query->bindValue(
':id', $new_id);
237 $query->bindValue(
':data', serialize($prepared[1][$new_id]->data));
238 $query->bindValue(
':date', $date);
239 if (!$query->execute())
255 $query = $this->mysql->prepare(
'SELECT `id` FROM `' . $this->options[
'extras'][
'prefix'] .
'cache_data` WHERE `id` = :feed');
256 $query->bindValue(
':feed', $this->
id);
257 if ($query->execute())
259 if ($query->rowCount() > 0)
261 $query = $this->mysql->prepare(
'UPDATE `' . $this->options[
'extras'][
'prefix'] .
'cache_data` SET `items` = 0, `data` = :data, `mtime` = :time WHERE `id` = :feed');
262 $query->bindValue(
':data', serialize($data));
263 $query->bindValue(
':time', time());
264 $query->bindValue(
':feed', $this->
id);
265 if ($this->execute())
272 $query = $this->mysql->prepare(
'INSERT INTO `' . $this->options[
'extras'][
'prefix'] .
'cache_data` (`id`, `items`, `data`, `mtime`) VALUES(:id, 0, :data, :time)');
273 $query->bindValue(
':id', $this->
id);
274 $query->bindValue(
':data', serialize($data));
275 $query->bindValue(
':time', time());
276 if ($query->execute())
293 if ($this->mysql === null)
298 $query = $this->mysql->prepare(
'SELECT `items`, `data` FROM `' . $this->options[
'extras'][
'prefix'] .
'cache_data` WHERE `id` = :id');
299 $query->bindValue(
':id', $this->
id);
300 if ($query->execute() && ($row = $query->fetch()))
302 $data = unserialize($row[1]);
304 if (isset($this->options[
'items'][0]))
306 $items = (int) $this->options[
'items'][0];
310 $items = (int) $row[0];
338 $sql =
'SELECT `data` FROM `' . $this->options[
'extras'][
'prefix'] .
'items` WHERE `feed_id` = :feed ORDER BY `posted` DESC';
341 $sql .=
' LIMIT ' . $items;
344 $query = $this->mysql->prepare($sql);
345 $query->bindValue(
':feed', $this->
id);
346 if ($query->execute())
348 while ($row = $query->fetchColumn())
371 if ($this->mysql === null)
376 $query = $this->mysql->prepare(
'SELECT `mtime` FROM `' . $this->options[
'extras'][
'prefix'] .
'cache_data` WHERE `id` = :id');
377 $query->bindValue(
':id', $this->
id);
378 if ($query->execute() && ($time = $query->fetchColumn()))
395 if ($this->mysql === null)
400 $query = $this->mysql->prepare(
'UPDATE `' . $this->options[
'extras'][
'prefix'] .
'cache_data` SET `mtime` = :time WHERE `id` = :id');
401 $query->bindValue(
':time', time());
402 $query->bindValue(
':id', $this->
id);
403 if ($query->execute() && $query->rowCount() > 0)
420 if ($this->mysql === null)
425 $query = $this->mysql->prepare(
'DELETE FROM `' . $this->options[
'extras'][
'prefix'] .
'cache_data` WHERE `id` = :id');
426 $query->bindValue(
':id', $this->
id);
427 $query2 = $this->mysql->prepare(
'DELETE FROM `' . $this->options[
'extras'][
'prefix'] .
'items` WHERE `feed_id` = :id');
428 $query2->bindValue(
':id', $this->
id);
429 if ($query->execute() && $query2->execute())