5 * A PHP-Based RSS and Atom Feed Framework.
6 * Takes the hard work out of managing a complete RSS/Atom solution.
8 * Copyright (c) 2004-2009, Ryan Parman, Geoffrey Sneddon, Ryan McCue, and contributors
11 * Redistribution and use in source and binary forms, with or without modification, are
12 * permitted provided that the following conditions are met:
14 * * Redistributions of source code must retain the above copyright notice, this list of
15 * conditions and the following disclaimer.
17 * * Redistributions in binary form must reproduce the above copyright notice, this list
18 * of conditions and the following disclaimer in the documentation and/or other materials
19 * provided with the distribution.
21 * * Neither the name of the SimplePie Team nor the names of its contributors may be used
22 * to endorse or promote products derived from this software without specific prior
25 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS
26 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
27 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS
28 * AND CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
29 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
30 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
31 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
32 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33 * POSSIBILITY OF SUCH DAMAGE.
37 * @copyright 2004-2010 Ryan Parman, Geoffrey Sneddon, Ryan McCue
39 * @author Geoffrey Sneddon
41 * @link http://simplepie.org/ SimplePie
42 * @license http://www.opensource.org/licenses/bsd-license.php BSD License
43 * @todo phpDoc comments
47 class SimplePie_Cache_MySQL
extends SimplePie_Cache_DB
53 public function __construct($url, $name, $extension)
55 $this->options
= array(
58 'host' => '127.0.0.1',
65 $this->options
= array_merge_recursive($this->options
, SimplePie_Cache
::parse_URL($url));
67 // Path is prefixed with a "/"
68 $this->options
['dbname'] = substr($this->options
['path'], 1);
72 $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'));
74 catch (PDOException
$e)
80 $this->id
= $name . $extension;
82 if (!$query = $this->mysql
->query('SHOW TABLES'))
89 while ($row = $query->fetchColumn())
94 if (!in_array($this->options
['extras']['prefix'] . 'cache_data', $db))
96 $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)))');
103 if (!in_array($this->options
['extras']['prefix'] . 'items', $db))
105 $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)))');
106 if ($query === false)
113 public function save($data)
115 if ($this->mysql
=== null)
120 if (is_a($data, 'SimplePie'))
124 $prepared = self
::prepare_simplepie_object_for_cache($data);
126 $query = $this->mysql
->prepare('SELECT COUNT(*) FROM `' . $this->options
['extras']['prefix'] . 'cache_data` WHERE `id` = :feed');
127 $query->bindValue(':feed', $this->id
);
128 if ($query->execute())
130 if ($query->fetchColumn() > 0)
132 $items = count($prepared[1]);
135 $sql = 'UPDATE `' . $this->options
['extras']['prefix'] . 'cache_data` SET `items` = :items, `data` = :data, `mtime` = :time WHERE `id` = :feed';
136 $query = $this->mysql
->prepare($sql);
137 $query->bindValue(':items', $items);
141 $sql = 'UPDATE `' . $this->options
['extras']['prefix'] . 'cache_data` SET `data` = :data, `mtime` = :time WHERE `id` = :feed';
142 $query = $this->mysql
->prepare($sql);
145 $query->bindValue(':data', $prepared[0]);
146 $query->bindValue(':time', time());
147 $query->bindValue(':feed', $this->id
);
148 if (!$query->execute())
155 $query = $this->mysql
->prepare('INSERT INTO `' . $this->options
['extras']['prefix'] . 'cache_data` (`id`, `items`, `data`, `mtime`) VALUES(:feed, :count, :data, :time)');
156 $query->bindValue(':feed', $this->id
);
157 $query->bindValue(':count', count($prepared[1]));
158 $query->bindValue(':data', $prepared[0]);
159 $query->bindValue(':time', time());
160 if (!$query->execute())
166 $ids = array_keys($prepared[1]);
169 foreach ($ids as $id)
171 $database_ids[] = $this->mysql
->quote($id);
174 $query = $this->mysql
->prepare('SELECT `id` FROM `' . $this->options
['extras']['prefix'] . 'items` WHERE `id` = ' . implode(' OR `id` = ', $database_ids) . ' AND `feed_id` = :feed');
175 $query->bindValue(':feed', $this->id
);
177 if ($query->execute())
179 $existing_ids = array();
180 while ($row = $query->fetchColumn())
182 $existing_ids[] = $row;
185 $new_ids = array_diff($ids, $existing_ids);
187 foreach ($new_ids as $new_id)
189 if (!($date = $prepared[1][$new_id]->get_date('U')))
194 $query = $this->mysql
->prepare('INSERT INTO `' . $this->options
['extras']['prefix'] . 'items` (`feed_id`, `id`, `data`, `posted`) VALUES(:feed, :id, :data, :date)');
195 $query->bindValue(':feed', $this->id
);
196 $query->bindValue(':id', $new_id);
197 $query->bindValue(':data', serialize($prepared[1][$new_id]->data
));
198 $query->bindValue(':date', $date);
199 if (!$query->execute())
215 $query = $this->mysql
->prepare('SELECT `id` FROM `' . $this->options
['extras']['prefix'] . 'cache_data` WHERE `id` = :feed');
216 $query->bindValue(':feed', $this->id
);
217 if ($query->execute())
219 if ($query->rowCount() > 0)
221 $query = $this->mysql
->prepare('UPDATE `' . $this->options
['extras']['prefix'] . 'cache_data` SET `items` = 0, `data` = :data, `mtime` = :time WHERE `id` = :feed');
222 $query->bindValue(':data', serialize($data));
223 $query->bindValue(':time', time());
224 $query->bindValue(':feed', $this->id
);
225 if ($this->execute())
232 $query = $this->mysql
->prepare('INSERT INTO `' . $this->options
['extras']['prefix'] . 'cache_data` (`id`, `items`, `data`, `mtime`) VALUES(:id, 0, :data, :time)');
233 $query->bindValue(':id', $this->id
);
234 $query->bindValue(':data', serialize($data));
235 $query->bindValue(':time', time());
236 if ($query->execute())
246 public function load()
248 if ($this->mysql
=== null)
253 $query = $this->mysql
->prepare('SELECT `items`, `data` FROM `' . $this->options
['extras']['prefix'] . 'cache_data` WHERE `id` = :id');
254 $query->bindValue(':id', $this->id
);
255 if ($query->execute() && ($row = $query->fetch()))
257 $data = unserialize($row[1]);
259 if (isset($this->options
['items'][0]))
261 $items = (int) $this->options
['items'][0];
265 $items = (int) $row[0];
270 if (isset($data['child'][SIMPLEPIE_NAMESPACE_ATOM_10
]['feed'][0]))
272 $feed =& $data['child'][SIMPLEPIE_NAMESPACE_ATOM_10
]['feed'][0];
274 elseif (isset($data['child'][SIMPLEPIE_NAMESPACE_ATOM_03
]['feed'][0]))
276 $feed =& $data['child'][SIMPLEPIE_NAMESPACE_ATOM_03
]['feed'][0];
278 elseif (isset($data['child'][SIMPLEPIE_NAMESPACE_RDF
]['RDF'][0]))
280 $feed =& $data['child'][SIMPLEPIE_NAMESPACE_RDF
]['RDF'][0];
282 elseif (isset($data['child'][SIMPLEPIE_NAMESPACE_RSS_20
]['rss'][0]))
284 $feed =& $data['child'][SIMPLEPIE_NAMESPACE_RSS_20
]['rss'][0];
293 $sql = 'SELECT `data` FROM `' . $this->options
['extras']['prefix'] . 'items` WHERE `feed_id` = :feed ORDER BY `posted` DESC';
296 $sql .= ' LIMIT ' . $items;
299 $query = $this->mysql
->prepare($sql);
300 $query->bindValue(':feed', $this->id
);
301 if ($query->execute())
303 while ($row = $query->fetchColumn())
305 $feed['child'][SIMPLEPIE_NAMESPACE_ATOM_10
]['entry'][] = unserialize($row);
319 public function mtime()
321 if ($this->mysql
=== null)
326 $query = $this->mysql
->prepare('SELECT `mtime` FROM `' . $this->options
['extras']['prefix'] . 'cache_data` WHERE `id` = :id');
327 $query->bindValue(':id', $this->id
);
328 if ($query->execute() && ($time = $query->fetchColumn()))
338 public function touch()
340 if ($this->mysql
=== null)
345 $query = $this->mysql
->prepare('UPDATE `' . $this->options
['extras']['prefix'] . 'cache_data` SET `mtime` = :time WHERE `id` = :id');
346 $query->bindValue(':time', time());
347 $query->bindValue(':id', $this->id
);
348 if ($query->execute() && $query->rowCount() > 0)
358 public function unlink()
360 if ($this->mysql
=== null)
365 $query = $this->mysql
->prepare('DELETE FROM `' . $this->options
['extras']['prefix'] . 'cache_data` WHERE `id` = :id');
366 $query->bindValue(':id', $this->id
);
367 $query2 = $this->mysql
->prepare('DELETE FROM `' . $this->options
['extras']['prefix'] . 'items` WHERE `feed_id` = :id');
368 $query2->bindValue(':id', $this->id
);
369 if ($query->execute() && $query2->execute())