1 | <?php
|
---|
2 | /**
|
---|
3 | * Copyright 2006, 2007, 2008, 2009 Eric D. Hough (http://ehough.com)
|
---|
4 | *
|
---|
5 | * This file is part of TubePress (http://tubepress.org)
|
---|
6 | *
|
---|
7 | * TubePress is free software: you can redistribute it and/or modify
|
---|
8 | * it under the terms of the GNU General Public License as published by
|
---|
9 | * the Free Software Foundation, either version 3 of the License, or
|
---|
10 | * (at your option) any later version.
|
---|
11 | *
|
---|
12 | * TubePress is distributed in the hope that it will be useful,
|
---|
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
15 | * GNU General Public License for more details.
|
---|
16 | *
|
---|
17 | * You should have received a copy of the GNU General Public License
|
---|
18 | * along with TubePress. If not, see <http://www.gnu.org/licenses/>.
|
---|
19 | *
|
---|
20 | */
|
---|
21 |
|
---|
22 | function_exists('tubepress_load_classes')
|
---|
23 | || require(dirname(__FILE__) . '/../../../tubepress_classloader.php');
|
---|
24 | tubepress_load_classes(array('org_tubepress_gdata_retrieval_FeedRetrievalService',
|
---|
25 | 'org_tubepress_cache_CacheService'));
|
---|
26 |
|
---|
27 | /**
|
---|
28 | * Base functionality for TubePressFeedRetrieval services
|
---|
29 | *
|
---|
30 | */
|
---|
31 | abstract class org_tubepress_gdata_retrieval_AbstractFeedRetrievalService implements org_tubepress_gdata_retrieval_FeedRetrievalService
|
---|
32 | {
|
---|
33 | private $_cache;
|
---|
34 |
|
---|
35 | /**
|
---|
36 | * Fetches the RSS from YouTube
|
---|
37 | *
|
---|
38 | * @param org_tubepress_options_manager_OptionsManager $tpom The TubePress options manager
|
---|
39 | *
|
---|
40 | * @return DOMDocument The raw RSS from YouTube
|
---|
41 | */
|
---|
42 | public function fetch($url, $useCache)
|
---|
43 | {
|
---|
44 | $xml = new DOMDocument();
|
---|
45 | if ($useCache) {
|
---|
46 | if ($this->_cache->has($url)) {
|
---|
47 | $cached = $this->_cache->get($url);
|
---|
48 | $xml->loadXML($cached);
|
---|
49 | } else {
|
---|
50 | $xml = $this->_getFromNetwork($url);
|
---|
51 | $this->_cache->save($url, $xml->saveXML());
|
---|
52 | }
|
---|
53 | } else {
|
---|
54 | $xml = $this->_getFromNetwork($url);
|
---|
55 | }
|
---|
56 | return $xml;
|
---|
57 | }
|
---|
58 |
|
---|
59 | private function _getFromNetwork($url)
|
---|
60 | {
|
---|
61 | $data = $this->_fetchFromNetwork($url);
|
---|
62 |
|
---|
63 | $data = trim($data);
|
---|
64 |
|
---|
65 | $doc = new DOMDocument();
|
---|
66 |
|
---|
67 | if (substr($data,0,1) != "<") {
|
---|
68 | throw new Exception("YouTube returned non-xml: " . $data);
|
---|
69 | }
|
---|
70 | if ($doc->loadXML($data) === FALSE) {
|
---|
71 | throw new Exception("YouTube returned invalid XML: " . $data);
|
---|
72 | }
|
---|
73 |
|
---|
74 | return $doc;
|
---|
75 | }
|
---|
76 |
|
---|
77 | protected abstract function _fetchFromNetwork($request);
|
---|
78 |
|
---|
79 | public function setCacheService(org_tubepress_cache_CacheService $cache)
|
---|
80 | {
|
---|
81 | $this->_cache = $cache;
|
---|
82 | }
|
---|
83 | } |
---|