xref: /dokuwiki/vendor/simplepie/simplepie/library/SimplePie/Cache/DB.php (revision 59b616ccfd538b2ad784685b41eecdcdcfbf5719)
1*59b616ccSAndreas Gohr<?php
2*59b616ccSAndreas Gohr/**
3*59b616ccSAndreas Gohr * SimplePie
4*59b616ccSAndreas Gohr *
5*59b616ccSAndreas Gohr * A PHP-Based RSS and Atom Feed Framework.
6*59b616ccSAndreas Gohr * Takes the hard work out of managing a complete RSS/Atom solution.
7*59b616ccSAndreas Gohr *
8*59b616ccSAndreas Gohr * Copyright (c) 2004-2016, Ryan Parman, Geoffrey Sneddon, Ryan McCue, and contributors
9*59b616ccSAndreas Gohr * All rights reserved.
10*59b616ccSAndreas Gohr *
11*59b616ccSAndreas Gohr * Redistribution and use in source and binary forms, with or without modification, are
12*59b616ccSAndreas Gohr * permitted provided that the following conditions are met:
13*59b616ccSAndreas Gohr *
14*59b616ccSAndreas Gohr * 	* Redistributions of source code must retain the above copyright notice, this list of
15*59b616ccSAndreas Gohr * 	  conditions and the following disclaimer.
16*59b616ccSAndreas Gohr *
17*59b616ccSAndreas Gohr * 	* Redistributions in binary form must reproduce the above copyright notice, this list
18*59b616ccSAndreas Gohr * 	  of conditions and the following disclaimer in the documentation and/or other materials
19*59b616ccSAndreas Gohr * 	  provided with the distribution.
20*59b616ccSAndreas Gohr *
21*59b616ccSAndreas Gohr * 	* Neither the name of the SimplePie Team nor the names of its contributors may be used
22*59b616ccSAndreas Gohr * 	  to endorse or promote products derived from this software without specific prior
23*59b616ccSAndreas Gohr * 	  written permission.
24*59b616ccSAndreas Gohr *
25*59b616ccSAndreas Gohr * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS
26*59b616ccSAndreas Gohr * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
27*59b616ccSAndreas Gohr * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS
28*59b616ccSAndreas Gohr * AND CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
29*59b616ccSAndreas Gohr * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
30*59b616ccSAndreas Gohr * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
31*59b616ccSAndreas Gohr * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
32*59b616ccSAndreas Gohr * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33*59b616ccSAndreas Gohr * POSSIBILITY OF SUCH DAMAGE.
34*59b616ccSAndreas Gohr *
35*59b616ccSAndreas Gohr * @package SimplePie
36*59b616ccSAndreas Gohr * @copyright 2004-2016 Ryan Parman, Geoffrey Sneddon, Ryan McCue
37*59b616ccSAndreas Gohr * @author Ryan Parman
38*59b616ccSAndreas Gohr * @author Geoffrey Sneddon
39*59b616ccSAndreas Gohr * @author Ryan McCue
40*59b616ccSAndreas Gohr * @link http://simplepie.org/ SimplePie
41*59b616ccSAndreas Gohr * @license http://www.opensource.org/licenses/bsd-license.php BSD License
42*59b616ccSAndreas Gohr */
43*59b616ccSAndreas Gohr
44*59b616ccSAndreas Gohr/**
45*59b616ccSAndreas Gohr * Base class for database-based caches
46*59b616ccSAndreas Gohr *
47*59b616ccSAndreas Gohr * @package SimplePie
48*59b616ccSAndreas Gohr * @subpackage Caching
49*59b616ccSAndreas Gohr */
50*59b616ccSAndreas Gohrabstract class SimplePie_Cache_DB implements SimplePie_Cache_Base
51*59b616ccSAndreas Gohr{
52*59b616ccSAndreas Gohr	/**
53*59b616ccSAndreas Gohr	 * Helper for database conversion
54*59b616ccSAndreas Gohr	 *
55*59b616ccSAndreas Gohr	 * Converts a given {@see SimplePie} object into data to be stored
56*59b616ccSAndreas Gohr	 *
57*59b616ccSAndreas Gohr	 * @param SimplePie $data
58*59b616ccSAndreas Gohr	 * @return array First item is the serialized data for storage, second item is the unique ID for this item
59*59b616ccSAndreas Gohr	 */
60*59b616ccSAndreas Gohr	protected static function prepare_simplepie_object_for_cache($data)
61*59b616ccSAndreas Gohr	{
62*59b616ccSAndreas Gohr		$items = $data->get_items();
63*59b616ccSAndreas Gohr		$items_by_id = array();
64*59b616ccSAndreas Gohr
65*59b616ccSAndreas Gohr		if (!empty($items))
66*59b616ccSAndreas Gohr		{
67*59b616ccSAndreas Gohr			foreach ($items as $item)
68*59b616ccSAndreas Gohr			{
69*59b616ccSAndreas Gohr				$items_by_id[$item->get_id()] = $item;
70*59b616ccSAndreas Gohr			}
71*59b616ccSAndreas Gohr
72*59b616ccSAndreas Gohr			if (count($items_by_id) !== count($items))
73*59b616ccSAndreas Gohr			{
74*59b616ccSAndreas Gohr				$items_by_id = array();
75*59b616ccSAndreas Gohr				foreach ($items as $item)
76*59b616ccSAndreas Gohr				{
77*59b616ccSAndreas Gohr					$items_by_id[$item->get_id(true)] = $item;
78*59b616ccSAndreas Gohr				}
79*59b616ccSAndreas Gohr			}
80*59b616ccSAndreas Gohr
81*59b616ccSAndreas Gohr			if (isset($data->data['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['feed'][0]))
82*59b616ccSAndreas Gohr			{
83*59b616ccSAndreas Gohr				$channel =& $data->data['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['feed'][0];
84*59b616ccSAndreas Gohr			}
85*59b616ccSAndreas Gohr			elseif (isset($data->data['child'][SIMPLEPIE_NAMESPACE_ATOM_03]['feed'][0]))
86*59b616ccSAndreas Gohr			{
87*59b616ccSAndreas Gohr				$channel =& $data->data['child'][SIMPLEPIE_NAMESPACE_ATOM_03]['feed'][0];
88*59b616ccSAndreas Gohr			}
89*59b616ccSAndreas Gohr			elseif (isset($data->data['child'][SIMPLEPIE_NAMESPACE_RDF]['RDF'][0]))
90*59b616ccSAndreas Gohr			{
91*59b616ccSAndreas Gohr				$channel =& $data->data['child'][SIMPLEPIE_NAMESPACE_RDF]['RDF'][0];
92*59b616ccSAndreas Gohr			}
93*59b616ccSAndreas Gohr			elseif (isset($data->data['child'][SIMPLEPIE_NAMESPACE_RSS_20]['rss'][0]['child'][SIMPLEPIE_NAMESPACE_RSS_20]['channel'][0]))
94*59b616ccSAndreas Gohr			{
95*59b616ccSAndreas Gohr				$channel =& $data->data['child'][SIMPLEPIE_NAMESPACE_RSS_20]['rss'][0]['child'][SIMPLEPIE_NAMESPACE_RSS_20]['channel'][0];
96*59b616ccSAndreas Gohr			}
97*59b616ccSAndreas Gohr			else
98*59b616ccSAndreas Gohr			{
99*59b616ccSAndreas Gohr				$channel = null;
100*59b616ccSAndreas Gohr			}
101*59b616ccSAndreas Gohr
102*59b616ccSAndreas Gohr			if ($channel !== null)
103*59b616ccSAndreas Gohr			{
104*59b616ccSAndreas Gohr				if (isset($channel['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['entry']))
105*59b616ccSAndreas Gohr				{
106*59b616ccSAndreas Gohr					unset($channel['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['entry']);
107*59b616ccSAndreas Gohr				}
108*59b616ccSAndreas Gohr				if (isset($channel['child'][SIMPLEPIE_NAMESPACE_ATOM_03]['entry']))
109*59b616ccSAndreas Gohr				{
110*59b616ccSAndreas Gohr					unset($channel['child'][SIMPLEPIE_NAMESPACE_ATOM_03]['entry']);
111*59b616ccSAndreas Gohr				}
112*59b616ccSAndreas Gohr				if (isset($channel['child'][SIMPLEPIE_NAMESPACE_RSS_10]['item']))
113*59b616ccSAndreas Gohr				{
114*59b616ccSAndreas Gohr					unset($channel['child'][SIMPLEPIE_NAMESPACE_RSS_10]['item']);
115*59b616ccSAndreas Gohr				}
116*59b616ccSAndreas Gohr				if (isset($channel['child'][SIMPLEPIE_NAMESPACE_RSS_090]['item']))
117*59b616ccSAndreas Gohr				{
118*59b616ccSAndreas Gohr					unset($channel['child'][SIMPLEPIE_NAMESPACE_RSS_090]['item']);
119*59b616ccSAndreas Gohr				}
120*59b616ccSAndreas Gohr				if (isset($channel['child'][SIMPLEPIE_NAMESPACE_RSS_20]['item']))
121*59b616ccSAndreas Gohr				{
122*59b616ccSAndreas Gohr					unset($channel['child'][SIMPLEPIE_NAMESPACE_RSS_20]['item']);
123*59b616ccSAndreas Gohr				}
124*59b616ccSAndreas Gohr			}
125*59b616ccSAndreas Gohr			if (isset($data->data['items']))
126*59b616ccSAndreas Gohr			{
127*59b616ccSAndreas Gohr				unset($data->data['items']);
128*59b616ccSAndreas Gohr			}
129*59b616ccSAndreas Gohr			if (isset($data->data['ordered_items']))
130*59b616ccSAndreas Gohr			{
131*59b616ccSAndreas Gohr				unset($data->data['ordered_items']);
132*59b616ccSAndreas Gohr			}
133*59b616ccSAndreas Gohr		}
134*59b616ccSAndreas Gohr		return array(serialize($data->data), $items_by_id);
135*59b616ccSAndreas Gohr	}
136*59b616ccSAndreas Gohr}
137