. * */ function_exists('tubepress_load_classes') || require(dirname(__FILE__) . '/../../../tubepress_classloader.php'); tubepress_load_classes(array('org_tubepress_gdata_retrieval_FeedRetrievalService', 'org_tubepress_cache_CacheService')); /** * Base functionality for TubePressFeedRetrieval services * */ abstract class org_tubepress_gdata_retrieval_AbstractFeedRetrievalService implements org_tubepress_gdata_retrieval_FeedRetrievalService { private $_cache; /** * Fetches the RSS from YouTube * * @param org_tubepress_options_manager_OptionsManager $tpom The TubePress options manager * * @return DOMDocument The raw RSS from YouTube */ public function fetch($url, $useCache) { $xml = new DOMDocument(); if ($useCache) { if ($this->_cache->has($url)) { $cached = $this->_cache->get($url); $xml->loadXML($cached); } else { $xml = $this->_getFromNetwork($url); $this->_cache->save($url, $xml->saveXML()); } } else { $xml = $this->_getFromNetwork($url); } return $xml; } private function _getFromNetwork($url) { $data = $this->_fetchFromNetwork($url); $data = trim($data); $doc = new DOMDocument(); if (substr($data,0,1) != "<") { throw new Exception("YouTube returned non-xml: " . $data); } if ($doc->loadXML($data) === FALSE) { throw new Exception("YouTube returned invalid XML: " . $data); } return $doc; } protected abstract function _fetchFromNetwork($request); public function setCacheService(org_tubepress_cache_CacheService $cache) { $this->_cache = $cache; } }