aboutsummaryrefslogtreecommitdiffhomepage
path: root/inc/3rdparty/libraries/simplepie/library/SimplePie/Cache
diff options
context:
space:
mode:
Diffstat (limited to 'inc/3rdparty/libraries/simplepie/library/SimplePie/Cache')
-rw-r--r--inc/3rdparty/libraries/simplepie/library/SimplePie/Cache/Base.php114
-rw-r--r--inc/3rdparty/libraries/simplepie/library/SimplePie/Cache/DB.php137
-rw-r--r--inc/3rdparty/libraries/simplepie/library/SimplePie/Cache/File.php173
-rw-r--r--inc/3rdparty/libraries/simplepie/library/SimplePie/Cache/Memcache.php183
-rw-r--r--inc/3rdparty/libraries/simplepie/library/SimplePie/Cache/MySQL.php438
5 files changed, 1045 insertions, 0 deletions
diff --git a/inc/3rdparty/libraries/simplepie/library/SimplePie/Cache/Base.php b/inc/3rdparty/libraries/simplepie/library/SimplePie/Cache/Base.php
new file mode 100644
index 00000000..937e3463
--- /dev/null
+++ b/inc/3rdparty/libraries/simplepie/library/SimplePie/Cache/Base.php
@@ -0,0 +1,114 @@
1<?php
2/**
3 * SimplePie
4 *
5 * A PHP-Based RSS and Atom Feed Framework.
6 * Takes the hard work out of managing a complete RSS/Atom solution.
7 *
8 * Copyright (c) 2004-2012, Ryan Parman, Geoffrey Sneddon, Ryan McCue, and contributors
9 * All rights reserved.
10 *
11 * Redistribution and use in source and binary forms, with or without modification, are
12 * permitted provided that the following conditions are met:
13 *
14 * * Redistributions of source code must retain the above copyright notice, this list of
15 * conditions and the following disclaimer.
16 *
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.
20 *
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
23 * written permission.
24 *
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.
34 *
35 * @package SimplePie
36 * @version 1.3.1
37 * @copyright 2004-2012 Ryan Parman, Geoffrey Sneddon, Ryan McCue
38 * @author Ryan Parman
39 * @author Geoffrey Sneddon
40 * @author Ryan McCue
41 * @link http://simplepie.org/ SimplePie
42 * @license http://www.opensource.org/licenses/bsd-license.php BSD License
43 */
44
45/**
46 * Base for cache objects
47 *
48 * Classes to be used with {@see SimplePie_Cache::register()} are expected
49 * to implement this interface.
50 *
51 * @package SimplePie
52 * @subpackage Caching
53 */
54interface SimplePie_Cache_Base
55{
56 /**
57 * Feed cache type
58 *
59 * @var string
60 */
61 const TYPE_FEED = 'spc';
62
63 /**
64 * Image cache type
65 *
66 * @var string
67 */
68 const TYPE_IMAGE = 'spi';
69
70 /**
71 * Create a new cache object
72 *
73 * @param string $location Location string (from SimplePie::$cache_location)
74 * @param string $name Unique ID for the cache
75 * @param string $type Either TYPE_FEED for SimplePie data, or TYPE_IMAGE for image data
76 */
77 public function __construct($location, $name, $type);
78
79 /**
80 * Save data to the cache
81 *
82 * @param array|SimplePie $data Data to store in the cache. If passed a SimplePie object, only cache the $data property
83 * @return bool Successfulness
84 */
85 public function save($data);
86
87 /**
88 * Retrieve the data saved to the cache
89 *
90 * @return array Data for SimplePie::$data
91 */
92 public function load();
93
94 /**
95 * Retrieve the last modified time for the cache
96 *
97 * @return int Timestamp
98 */
99 public function mtime();
100
101 /**
102 * Set the last modified time to the current time
103 *
104 * @return bool Success status
105 */
106 public function touch();
107
108 /**
109 * Remove the cache
110 *
111 * @return bool Success status
112 */
113 public function unlink();
114}
diff --git a/inc/3rdparty/libraries/simplepie/library/SimplePie/Cache/DB.php b/inc/3rdparty/libraries/simplepie/library/SimplePie/Cache/DB.php
new file mode 100644
index 00000000..ac509ae0
--- /dev/null
+++ b/inc/3rdparty/libraries/simplepie/library/SimplePie/Cache/DB.php
@@ -0,0 +1,137 @@
1<?php
2/**
3 * SimplePie
4 *
5 * A PHP-Based RSS and Atom Feed Framework.
6 * Takes the hard work out of managing a complete RSS/Atom solution.
7 *
8 * Copyright (c) 2004-2012, Ryan Parman, Geoffrey Sneddon, Ryan McCue, and contributors
9 * All rights reserved.
10 *
11 * Redistribution and use in source and binary forms, with or without modification, are
12 * permitted provided that the following conditions are met:
13 *
14 * * Redistributions of source code must retain the above copyright notice, this list of
15 * conditions and the following disclaimer.
16 *
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.
20 *
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
23 * written permission.
24 *
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.
34 *
35 * @package SimplePie
36 * @version 1.3.1
37 * @copyright 2004-2012 Ryan Parman, Geoffrey Sneddon, Ryan McCue
38 * @author Ryan Parman
39 * @author Geoffrey Sneddon
40 * @author Ryan McCue
41 * @link http://simplepie.org/ SimplePie
42 * @license http://www.opensource.org/licenses/bsd-license.php BSD License
43 */
44
45/**
46 * Base class for database-based caches
47 *
48 * @package SimplePie
49 * @subpackage Caching
50 */
51abstract class SimplePie_Cache_DB implements SimplePie_Cache_Base
52{
53 /**
54 * Helper for database conversion
55 *
56 * Converts a given {@see SimplePie} object into data to be stored
57 *
58 * @param SimplePie $data
59 * @return array First item is the serialized data for storage, second item is the unique ID for this item
60 */
61 protected static function prepare_simplepie_object_for_cache($data)
62 {
63 $items = $data->get_items();
64 $items_by_id = array();
65
66 if (!empty($items))
67 {
68 foreach ($items as $item)
69 {
70 $items_by_id[$item->get_id()] = $item;
71 }
72
73 if (count($items_by_id) !== count($items))
74 {
75 $items_by_id = array();
76 foreach ($items as $item)
77 {
78 $items_by_id[$item->get_id(true)] = $item;
79 }
80 }
81
82 if (isset($data->data['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['feed'][0]))
83 {
84 $channel =& $data->data['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['feed'][0];
85 }
86 elseif (isset($data->data['child'][SIMPLEPIE_NAMESPACE_ATOM_03]['feed'][0]))
87 {
88 $channel =& $data->data['child'][SIMPLEPIE_NAMESPACE_ATOM_03]['feed'][0];
89 }
90 elseif (isset($data->data['child'][SIMPLEPIE_NAMESPACE_RDF]['RDF'][0]))
91 {
92 $channel =& $data->data['child'][SIMPLEPIE_NAMESPACE_RDF]['RDF'][0];
93 }
94 elseif (isset($data->data['child'][SIMPLEPIE_NAMESPACE_RSS_20]['rss'][0]['child'][SIMPLEPIE_NAMESPACE_RSS_20]['channel'][0]))
95 {
96 $channel =& $data->data['child'][SIMPLEPIE_NAMESPACE_RSS_20]['rss'][0]['child'][SIMPLEPIE_NAMESPACE_RSS_20]['channel'][0];
97 }
98 else
99 {
100 $channel = null;
101 }
102
103 if ($channel !== null)
104 {
105 if (isset($channel['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['entry']))
106 {
107 unset($channel['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['entry']);
108 }
109 if (isset($channel['child'][SIMPLEPIE_NAMESPACE_ATOM_03]['entry']))
110 {
111 unset($channel['child'][SIMPLEPIE_NAMESPACE_ATOM_03]['entry']);
112 }
113 if (isset($channel['child'][SIMPLEPIE_NAMESPACE_RSS_10]['item']))
114 {
115 unset($channel['child'][SIMPLEPIE_NAMESPACE_RSS_10]['item']);
116 }
117 if (isset($channel['child'][SIMPLEPIE_NAMESPACE_RSS_090]['item']))
118 {
119 unset($channel['child'][SIMPLEPIE_NAMESPACE_RSS_090]['item']);
120 }
121 if (isset($channel['child'][SIMPLEPIE_NAMESPACE_RSS_20]['item']))
122 {
123 unset($channel['child'][SIMPLEPIE_NAMESPACE_RSS_20]['item']);
124 }
125 }
126 if (isset($data->data['items']))
127 {
128 unset($data->data['items']);
129 }
130 if (isset($data->data['ordered_items']))
131 {
132 unset($data->data['ordered_items']);
133 }
134 }
135 return array(serialize($data->data), $items_by_id);
136 }
137}
diff --git a/inc/3rdparty/libraries/simplepie/library/SimplePie/Cache/File.php b/inc/3rdparty/libraries/simplepie/library/SimplePie/Cache/File.php
new file mode 100644
index 00000000..5797b3ae
--- /dev/null
+++ b/inc/3rdparty/libraries/simplepie/library/SimplePie/Cache/File.php
@@ -0,0 +1,173 @@
1<?php
2/**
3 * SimplePie
4 *
5 * A PHP-Based RSS and Atom Feed Framework.
6 * Takes the hard work out of managing a complete RSS/Atom solution.
7 *
8 * Copyright (c) 2004-2012, Ryan Parman, Geoffrey Sneddon, Ryan McCue, and contributors
9 * All rights reserved.
10 *
11 * Redistribution and use in source and binary forms, with or without modification, are
12 * permitted provided that the following conditions are met:
13 *
14 * * Redistributions of source code must retain the above copyright notice, this list of
15 * conditions and the following disclaimer.
16 *
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.
20 *
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
23 * written permission.
24 *
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.
34 *
35 * @package SimplePie
36 * @version 1.3.1
37 * @copyright 2004-2012 Ryan Parman, Geoffrey Sneddon, Ryan McCue
38 * @author Ryan Parman
39 * @author Geoffrey Sneddon
40 * @author Ryan McCue
41 * @link http://simplepie.org/ SimplePie
42 * @license http://www.opensource.org/licenses/bsd-license.php BSD License
43 */
44
45/**
46 * Caches data to the filesystem
47 *
48 * @package SimplePie
49 * @subpackage Caching
50 */
51class SimplePie_Cache_File implements SimplePie_Cache_Base
52{
53 /**
54 * Location string
55 *
56 * @see SimplePie::$cache_location
57 * @var string
58 */
59 protected $location;
60
61 /**
62 * Filename
63 *
64 * @var string
65 */
66 protected $filename;
67
68 /**
69 * File extension
70 *
71 * @var string
72 */
73 protected $extension;
74
75 /**
76 * File path
77 *
78 * @var string
79 */
80 protected $name;
81
82 /**
83 * Create a new cache object
84 *
85 * @param string $location Location string (from SimplePie::$cache_location)
86 * @param string $name Unique ID for the cache
87 * @param string $type Either TYPE_FEED for SimplePie data, or TYPE_IMAGE for image data
88 */
89 public function __construct($location, $name, $type)
90 {
91 $this->location = $location;
92 $this->filename = $name;
93 $this->extension = $type;
94 $this->name = "$this->location/$this->filename.$this->extension";
95 }
96
97 /**
98 * Save data to the cache
99 *
100 * @param array|SimplePie $data Data to store in the cache. If passed a SimplePie object, only cache the $data property
101 * @return bool Successfulness
102 */
103 public function save($data)
104 {
105 if (file_exists($this->name) && is_writeable($this->name) || file_exists($this->location) && is_writeable($this->location))
106 {
107 if ($data instanceof SimplePie)
108 {
109 $data = $data->data;
110 }
111
112 $data = serialize($data);
113 return (bool) file_put_contents($this->name, $data);
114 }
115 return false;
116 }
117
118 /**
119 * Retrieve the data saved to the cache
120 *
121 * @return array Data for SimplePie::$data
122 */
123 public function load()
124 {
125 if (file_exists($this->name) && is_readable($this->name))
126 {
127 return unserialize(file_get_contents($this->name));
128 }
129 return false;
130 }
131
132 /**
133 * Retrieve the last modified time for the cache
134 *
135 * @return int Timestamp
136 */
137 public function mtime()
138 {
139 if (file_exists($this->name))
140 {
141 return filemtime($this->name);
142 }
143 return false;
144 }
145
146 /**
147 * Set the last modified time to the current time
148 *
149 * @return bool Success status
150 */
151 public function touch()
152 {
153 if (file_exists($this->name))
154 {
155 return touch($this->name);
156 }
157 return false;
158 }
159
160 /**
161 * Remove the cache
162 *
163 * @return bool Success status
164 */
165 public function unlink()
166 {
167 if (file_exists($this->name))
168 {
169 return unlink($this->name);
170 }
171 return false;
172 }
173}
diff --git a/inc/3rdparty/libraries/simplepie/library/SimplePie/Cache/Memcache.php b/inc/3rdparty/libraries/simplepie/library/SimplePie/Cache/Memcache.php
new file mode 100644
index 00000000..fd447806
--- /dev/null
+++ b/inc/3rdparty/libraries/simplepie/library/SimplePie/Cache/Memcache.php
@@ -0,0 +1,183 @@
1<?php
2/**
3 * SimplePie
4 *
5 * A PHP-Based RSS and Atom Feed Framework.
6 * Takes the hard work out of managing a complete RSS/Atom solution.
7 *
8 * Copyright (c) 2004-2012, Ryan Parman, Geoffrey Sneddon, Ryan McCue, and contributors
9 * All rights reserved.
10 *
11 * Redistribution and use in source and binary forms, with or without modification, are
12 * permitted provided that the following conditions are met:
13 *
14 * * Redistributions of source code must retain the above copyright notice, this list of
15 * conditions and the following disclaimer.
16 *
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.
20 *
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
23 * written permission.
24 *
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.
34 *
35 * @package SimplePie
36 * @version 1.3.1
37 * @copyright 2004-2012 Ryan Parman, Geoffrey Sneddon, Ryan McCue
38 * @author Ryan Parman
39 * @author Geoffrey Sneddon
40 * @author Ryan McCue
41 * @link http://simplepie.org/ SimplePie
42 * @license http://www.opensource.org/licenses/bsd-license.php BSD License
43 */
44
45/**
46 * Caches data to memcache
47 *
48 * Registered for URLs with the "memcache" protocol
49 *
50 * For example, `memcache://localhost:11211/?timeout=3600&prefix=sp_` will
51 * connect to memcache on `localhost` on port 11211. All tables will be
52 * prefixed with `sp_` and data will expire after 3600 seconds
53 *
54 * @package SimplePie
55 * @subpackage Caching
56 * @uses Memcache
57 */
58class SimplePie_Cache_Memcache implements SimplePie_Cache_Base
59{
60 /**
61 * Memcache instance
62 *
63 * @var Memcache
64 */
65 protected $cache;
66
67 /**
68 * Options
69 *
70 * @var array
71 */
72 protected $options;
73
74 /**
75 * Cache name
76 *
77 * @var string
78 */
79 protected $name;
80
81 /**
82 * Create a new cache object
83 *
84 * @param string $location Location string (from SimplePie::$cache_location)
85 * @param string $name Unique ID for the cache
86 * @param string $type Either TYPE_FEED for SimplePie data, or TYPE_IMAGE for image data
87 */
88 public function __construct($location, $name, $type)
89 {
90 $this->options = array(
91 'host' => '127.0.0.1',
92 'port' => 11211,
93 'extras' => array(
94 'timeout' => 3600, // one hour
95 'prefix' => 'simplepie_',
96 ),
97 );
98 $parsed = SimplePie_Cache::parse_URL($location);
99 $this->options['host'] = empty($parsed['host']) ? $this->options['host'] : $parsed['host'];
100 $this->options['port'] = empty($parsed['port']) ? $this->options['port'] : $parsed['port'];
101 $this->options['extras'] = array_merge($this->options['extras'], $parsed['extras']);
102 $this->name = $this->options['extras']['prefix'] . md5("$name:$type");
103
104 $this->cache = new Memcache();
105 $this->cache->addServer($this->options['host'], (int) $this->options['port']);
106 }
107
108 /**
109 * Save data to the cache
110 *
111 * @param array|SimplePie $data Data to store in the cache. If passed a SimplePie object, only cache the $data property
112 * @return bool Successfulness
113 */
114 public function save($data)
115 {
116 if ($data instanceof SimplePie)
117 {
118 $data = $data->data;
119 }
120 return $this->cache->set($this->name, serialize($data), MEMCACHE_COMPRESSED, (int) $this->options['extras']['timeout']);
121 }
122
123 /**
124 * Retrieve the data saved to the cache
125 *
126 * @return array Data for SimplePie::$data
127 */
128 public function load()
129 {
130 $data = $this->cache->get($this->name);
131
132 if ($data !== false)
133 {
134 return unserialize($data);
135 }
136 return false;
137 }
138
139 /**
140 * Retrieve the last modified time for the cache
141 *
142 * @return int Timestamp
143 */
144 public function mtime()
145 {
146 $data = $this->cache->get($this->name);
147
148 if ($data !== false)
149 {
150 // essentially ignore the mtime because Memcache expires on it's own
151 return time();
152 }
153
154 return false;
155 }
156
157 /**
158 * Set the last modified time to the current time
159 *
160 * @return bool Success status
161 */
162 public function touch()
163 {
164 $data = $this->cache->get($this->name);
165
166 if ($data !== false)
167 {
168 return $this->cache->set($this->name, $data, MEMCACHE_COMPRESSED, (int) $this->duration);
169 }
170
171 return false;
172 }
173
174 /**
175 * Remove the cache
176 *
177 * @return bool Success status
178 */
179 public function unlink()
180 {
181 return $this->cache->delete($this->name, 0);
182 }
183}
diff --git a/inc/3rdparty/libraries/simplepie/library/SimplePie/Cache/MySQL.php b/inc/3rdparty/libraries/simplepie/library/SimplePie/Cache/MySQL.php
new file mode 100644
index 00000000..d53ebc11
--- /dev/null
+++ b/inc/3rdparty/libraries/simplepie/library/SimplePie/Cache/MySQL.php
@@ -0,0 +1,438 @@
1<?php
2/**
3 * SimplePie
4 *
5 * A PHP-Based RSS and Atom Feed Framework.
6 * Takes the hard work out of managing a complete RSS/Atom solution.
7 *
8 * Copyright (c) 2004-2012, Ryan Parman, Geoffrey Sneddon, Ryan McCue, and contributors
9 * All rights reserved.
10 *
11 * Redistribution and use in source and binary forms, with or without modification, are
12 * permitted provided that the following conditions are met:
13 *
14 * * Redistributions of source code must retain the above copyright notice, this list of
15 * conditions and the following disclaimer.
16 *
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.
20 *
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
23 * written permission.
24 *
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.
34 *
35 * @package SimplePie
36 * @version 1.3.1
37 * @copyright 2004-2012 Ryan Parman, Geoffrey Sneddon, Ryan McCue
38 * @author Ryan Parman
39 * @author Geoffrey Sneddon
40 * @author Ryan McCue
41 * @link http://simplepie.org/ SimplePie
42 * @license http://www.opensource.org/licenses/bsd-license.php BSD License
43 */
44
45/**
46 * Caches data to a MySQL database
47 *
48 * Registered for URLs with the "mysql" protocol
49 *
50 * For example, `mysql://root:password@localhost:3306/mydb?prefix=sp_` will
51 * connect to the `mydb` database on `localhost` on port 3306, with the user
52 * `root` and the password `password`. All tables will be prefixed with `sp_`
53 *
54 * @package SimplePie
55 * @subpackage Caching
56 */
57class SimplePie_Cache_MySQL extends SimplePie_Cache_DB
58{
59 /**
60 * PDO instance
61 *
62 * @var PDO
63 */
64 protected $mysql;
65
66 /**
67 * Options
68 *
69 * @var array
70 */
71 protected $options;
72
73 /**
74 * Cache ID
75 *
76 * @var string
77 */
78 protected $id;
79
80 /**
81 * Create a new cache object
82 *
83 * @param string $location Location string (from SimplePie::$cache_location)
84 * @param string $name Unique ID for the cache
85 * @param string $type Either TYPE_FEED for SimplePie data, or TYPE_IMAGE for image data
86 */
87 public function __construct($location, $name, $type)
88 {
89 $this->options = array(
90 'user' => null,
91 'pass' => null,
92 'host' => '127.0.0.1',
93 'port' => '3306',
94 'path' => '',
95 'extras' => array(
96 'prefix' => '',
97 ),
98 );
99 $this->options = array_merge_recursive($this->options, SimplePie_Cache::parse_URL($location));
100
101 // Path is prefixed with a "/"
102 $this->options['dbname'] = substr($this->options['path'], 1);
103
104 try
105 {
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'));
107 }
108 catch (PDOException $e)
109 {
110 $this->mysql = null;
111 return;
112 }
113
114 $this->id = $name . $type;
115
116 if (!$query = $this->mysql->query('SHOW TABLES'))
117 {
118 $this->mysql = null;
119 return;
120 }
121
122 $db = array();
123 while ($row = $query->fetchColumn())
124 {
125 $db[] = $row;
126 }
127
128 if (!in_array($this->options['extras']['prefix'] . 'cache_data', $db))
129 {
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)
132 {
133 $this->mysql = null;
134 }
135 }
136
137 if (!in_array($this->options['extras']['prefix'] . 'items', $db))
138 {
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)
141 {
142 $this->mysql = null;
143 }
144 }
145 }
146
147 /**
148 * Save data to the cache
149 *
150 * @param array|SimplePie $data Data to store in the cache. If passed a SimplePie object, only cache the $data property
151 * @return bool Successfulness
152 */
153 public function save($data)
154 {
155 if ($this->mysql === null)
156 {
157 return false;
158 }
159
160 if ($data instanceof SimplePie)
161 {
162 $data = clone $data;
163
164 $prepared = self::prepare_simplepie_object_for_cache($data);
165
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())
169 {
170 if ($query->fetchColumn() > 0)
171 {
172 $items = count($prepared[1]);
173 if ($items)
174 {
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);
178 }
179 else
180 {
181 $sql = 'UPDATE `' . $this->options['extras']['prefix'] . 'cache_data` SET `data` = :data, `mtime` = :time WHERE `id` = :feed';
182 $query = $this->mysql->prepare($sql);
183 }
184
185 $query->bindValue(':data', $prepared[0]);
186 $query->bindValue(':time', time());
187 $query->bindValue(':feed', $this->id);
188 if (!$query->execute())
189 {
190 return false;
191 }
192 }
193 else
194 {
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())
201 {
202 return false;
203 }
204 }
205
206 $ids = array_keys($prepared[1]);
207 if (!empty($ids))
208 {
209 foreach ($ids as $id)
210 {
211 $database_ids[] = $this->mysql->quote($id);
212 }
213
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);
216
217 if ($query->execute())
218 {
219 $existing_ids = array();
220 while ($row = $query->fetchColumn())
221 {
222 $existing_ids[] = $row;
223 }
224
225 $new_ids = array_diff($ids, $existing_ids);
226
227 foreach ($new_ids as $new_id)
228 {
229 if (!($date = $prepared[1][$new_id]->get_date('U')))
230 {
231 $date = time();
232 }
233
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())
240 {
241 return false;
242 }
243 }
244 return true;
245 }
246 }
247 else
248 {
249 return true;
250 }
251 }
252 }
253 else
254 {
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())
258 {
259 if ($query->rowCount() > 0)
260 {
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())
266 {
267 return true;
268 }
269 }
270 else
271 {
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())
277 {
278 return true;
279 }
280 }
281 }
282 }
283 return false;
284 }
285
286 /**
287 * Retrieve the data saved to the cache
288 *
289 * @return array Data for SimplePie::$data
290 */
291 public function load()
292 {
293 if ($this->mysql === null)
294 {
295 return false;
296 }
297
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()))
301 {
302 $data = unserialize($row[1]);
303
304 if (isset($this->options['items'][0]))
305 {
306 $items = (int) $this->options['items'][0];
307 }
308 else
309 {
310 $items = (int) $row[0];
311 }
312
313 if ($items !== 0)
314 {
315 if (isset($data['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['feed'][0]))
316 {
317 $feed =& $data['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['feed'][0];
318 }
319 elseif (isset($data['child'][SIMPLEPIE_NAMESPACE_ATOM_03]['feed'][0]))
320 {
321 $feed =& $data['child'][SIMPLEPIE_NAMESPACE_ATOM_03]['feed'][0];
322 }
323 elseif (isset($data['child'][SIMPLEPIE_NAMESPACE_RDF]['RDF'][0]))
324 {
325 $feed =& $data['child'][SIMPLEPIE_NAMESPACE_RDF]['RDF'][0];
326 }
327 elseif (isset($data['child'][SIMPLEPIE_NAMESPACE_RSS_20]['rss'][0]))
328 {
329 $feed =& $data['child'][SIMPLEPIE_NAMESPACE_RSS_20]['rss'][0];
330 }
331 else
332 {
333 $feed = null;
334 }
335
336 if ($feed !== null)
337 {
338 $sql = 'SELECT `data` FROM `' . $this->options['extras']['prefix'] . 'items` WHERE `feed_id` = :feed ORDER BY `posted` DESC';
339 if ($items > 0)
340 {
341 $sql .= ' LIMIT ' . $items;
342 }
343
344 $query = $this->mysql->prepare($sql);
345 $query->bindValue(':feed', $this->id);
346 if ($query->execute())
347 {
348 while ($row = $query->fetchColumn())
349 {
350 $feed['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['entry'][] = unserialize($row);
351 }
352 }
353 else
354 {
355 return false;
356 }
357 }
358 }
359 return $data;
360 }
361 return false;
362 }
363
364 /**
365 * Retrieve the last modified time for the cache
366 *
367 * @return int Timestamp
368 */
369 public function mtime()
370 {
371 if ($this->mysql === null)
372 {
373 return false;
374 }
375
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()))
379 {
380 return $time;
381 }
382 else
383 {
384 return false;
385 }
386 }
387
388 /**
389 * Set the last modified time to the current time
390 *
391 * @return bool Success status
392 */
393 public function touch()
394 {
395 if ($this->mysql === null)
396 {
397 return false;
398 }
399
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)
404 {
405 return true;
406 }
407 else
408 {
409 return false;
410 }
411 }
412
413 /**
414 * Remove the cache
415 *
416 * @return bool Success status
417 */
418 public function unlink()
419 {
420 if ($this->mysql === null)
421 {
422 return false;
423 }
424
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())
430 {
431 return true;
432 }
433 else
434 {
435 return false;
436 }
437 }
438}