From 79dd93a9208c84eef9cc9e38f4ee3da1a0b784f1 Mon Sep 17 00:00:00 2001 From: dhawes Date: Fri, 21 Jan 2005 03:27:38 +0000 Subject: [PATCH] refactored a big scary function a bit. hopefully it is more readable this way. this change also paved the way for extra caching in the block's config structure. --- rss/rsslib.php | 169 ++++++++++++++++++++++++------------------------- 1 file changed, 83 insertions(+), 86 deletions(-) diff --git a/rss/rsslib.php b/rss/rsslib.php index 9cb24e3231..7d75ddae34 100644 --- a/rss/rsslib.php +++ b/rss/rsslib.php @@ -335,24 +335,14 @@ if (empty($CFG->block_rss_timeout) ) { } /** - * Determines whether or not to get a news feed remotely or from cache and reads it into a string - * @param int rssid - id of feed in blog_rss table - * @param string url - url of remote feed - * @param string type - either 'A' or 'R' where A is an atom feed and R is either rss or rdf - * @return Atom|MagpieRSS|null This function returns an Atom object in the case of an Atom feed, a MagpieRSS object in the case of an RDF/RSS feed or null if there was an error loading the remote feed. - * NOTE that this function requires allow_url_fopen be On in your php.ini file - * (it may be off for security by your web host) + * Determine by rss id if the raw xml is cached on the file system + * @param int $rssid The id of this feed in the rss table + * @return bool False if cache file is missing or expired */ -function rss_get_feed($rssid, $url, $type) { - +function rss_cache_valid_by_id($rssid) { global $CFG; - $writetofile = false; - $errorstring = ''; - - $urlfailurestring = '

Failed to open remote news feed at: ' . $url .'

'; - $filefailurestring = 'Could not open the file located at: '; $secs = $CFG->block_rss_timeout * 60; - + // If moodle dataroot cache folder is missing create it if (!file_exists($CFG->dataroot .'/cache/')) { mkdir($CFG->dataroot .'/cache'); @@ -364,96 +354,103 @@ function rss_get_feed($rssid, $url, $type) { $file = $CFG->dataroot .'/cache/rsscache/'. $rssid .'.xml'; // echo "file = ". $file; //debug - - //if feed in cache + if (file_exists($file)) { - //check age of cache file + //check age of cached xml file // echo "file exists $file"; //debug - - //get file information capturing any error information - ob_start(); - $data = stat($file); - $errorstring .= ob_get_contents(); - ob_end_clean(); + + //get file information ignoring error information + $data = @stat($file); $now = time(); //Note: there would be a problem here reading data[10] if the above stat() call failed - if (($now - $data[10]) > $secs) { - // The cached file has expired. Attempt to read fresh from source - $xml = load_feed_from_url($url); - if (!empty($xml) && !empty($xml->xml) && empty($xml->ERROR)) { - //success - $writetofile = true; - } else { - // Failed to load remote feed. Since the file exists attempt to read from cache - if ($CFG->debug) { - if (isset($xml) && isset($xml->ERROR)) { - $errorstring = $xml->ERROR . $errorstring .'
'; - } - $errorstring = $urlfailurestring .'

'. $errorstring .'
'; - } - $xml = load_feed_from_file($file); - if (!empty($xml) && empty($xml->xml) && !empty($xml->ERROR)) { - // Failed to load from cache as well! - if ($CFG->debug) { - if (!empty($xml) && !empty($xml->ERROR)) { - $errorstring = $xml->ERROR . $errorstring; - } - $errorstring = $filefailurestring . $file .'

'. $errorstring .'
'; - $err->ERROR = $errorstring .'
'; - return $err; - } - } - } - } else { - // Cached file has not expired. Attempt to read from cached file. - $xml = load_feed_from_file($file); - if (!empty($xml) && empty($xml->xml) && !empty($xml->ERROR)) { - // Failed to load from cache, attempt to read from source - if ($CFG->debug) { - if (!empty($xml) && !empty($xml->ERROR)) { - $errorstring = $xml->ERROR . $errorstring .'
'; - } - $errorstring = $filefailurestring . $file .'

'. $errorstring .'
'; - } - $xml = load_feed_from_url($url); - if (!empty($xml) && !empty($xml->xml) && empty($xml->ERROR)) { - // success - $writetofile = true; - } else { - // Failed to read from source as well! - if ($CFG->debug) { - if (!empty($xml) && !empty($xml->ERROR)) { - $errorstring = $xml->ERROR . $errorstring; - } - $errorstring = $urlfailurestring .'

'. $errorstring .'
'; - $err->ERROR = $errorstring .'
'; - return $err; - } - return; + if (empty($data)) { + // error getting stat on file, return + return false; + } + if ( ($now - $data[10]) > $secs) { + // The cached file has expired. + return false; + } + // no error, cache file is valid + return true; + } +} + +/** + * Determines whether or not to get a news feed remotely or from cache and reads it into a string + * @param int rssid - id of feed in blog_rss table + * @param string url - url of remote feed + * @param string type - either 'A' or 'R' where A is an atom feed and R is either rss or rdf + * @return Atom|MagpieRSS|null This function returns an Atom object in the case of an Atom feed, a MagpieRSS object in the case of an RDF/RSS feed or null if there was an error loading the remote feed. + * NOTE that this function requires allow_url_fopen be On in your php.ini file + * (it may be off for security by your web host) + */ +function rss_get_feed($rssid, $url, $type) { + global $CFG; + $errorstring = ''; + $urlfailurestring = '

Failed to open remote news feed at: ' . $url .'

'; + $filefailurestring = 'Could not open the file located at: '; + $file = $CFG->dataroot .'/cache/rsscache/'. $rssid .'.xml'; + + // some flags to be used in managing where the rss data is read from + $writetofile = false; + $readfromfile = false; + $readfromurl = false; + $cachefilevalid = false; + + if (rss_cache_valid_by_id($rssid)) { + //valid xml feed info in cache, read from file + $readfromfile = true; + $cachefilevalid = true; + } else { + // xml feed missing or expired, read from source url + $readfromurl = true; + } + + if ($readfromfile) { + // Cached file has not expired. Attempt to read from cached file. + $xml = load_feed_from_file($file); + if (!empty($xml) && empty($xml->xml) && !empty($xml->ERROR)) { + // Failed to load from cache, attempt to read from source + if ($CFG->debug) { + if (!empty($xml) && !empty($xml->ERROR)) { + $errorstring = $xml->ERROR . $errorstring .'
'; } + $errorstring = $filefailurestring . $file .'

'. $errorstring .'
'; } + $readfromurl = true; // read from file failed, try to get from source next } - } else { - // No cached file at all, read from source + } + + if ($readfromurl) { $xml = load_feed_from_url($url); if (!empty($xml) && !empty($xml->xml) && empty($xml->ERROR)) { //success $writetofile = true; } else { - // Failed to read from source url! + // Failed to load remote feed. Since the file exists attempt to read from cache if ($CFG->debug) { - if (!empty($xml) && !empty($xml->ERROR)) { - $errorstring = $xml->ERROR . $errorstring .'
'; + if (isset($xml) && isset($xml->ERROR)) { + $errorstring = $xml->ERROR . $errorstring .'
'; } $errorstring = $urlfailurestring .'

'. $errorstring .'
'; - $err->ERROR = $errorstring .'
'; - return $err; } - return; + $xml = load_feed_from_file($file); + if (!empty($xml) && empty($xml->xml) && !empty($xml->ERROR)) { + // Failed to load from cache as well! + if ($CFG->debug) { + if (!empty($xml) && !empty($xml->ERROR)) { + $errorstring = $xml->ERROR . $errorstring; + } + $errorstring = $filefailurestring . $file .'

'. $errorstring .'
'; + $err->ERROR = $errorstring .'
'; + return $err; + } + } } } - + // echo 'DEBUG: raw xml was loaded successfully:
';//debug //print_object($xml); //debug -- 2.39.5