From 92ce1eb2efc45043a916fdb897fabf9d718e2e4f Mon Sep 17 00:00:00 2001 From: dhawes Date: Thu, 30 Dec 2004 18:08:38 +0000 Subject: [PATCH] Updated for better reporting when loading from remote url fails. Now displays error when adding URL when it cannot load even when ->debug is off. Added new feature - select multiple feeds to display in each block instance. --- blocks/rss_client/block_rss_client.php | 76 ++++++---- blocks/rss_client/block_rss_client_action.php | 4 + blocks/rss_client/config_global.html | 5 +- blocks/rss_client/config_instance.html | 38 ++++- rss/templib.php | 141 ++++++++++++++---- 5 files changed, 198 insertions(+), 66 deletions(-) diff --git a/blocks/rss_client/block_rss_client.php b/blocks/rss_client/block_rss_client.php index bc7b8fc719..0b79e48655 100644 --- a/blocks/rss_client/block_rss_client.php +++ b/blocks/rss_client/block_rss_client.php @@ -9,8 +9,13 @@ class block_rss_client extends block_base { } function specialization() { + // After the block has been loaded we customize the block's title display if (!empty($this->config) && !empty($this->config->title)) { + // There is a customized block title, display it $this->title = $this->config->title; + } else { + // No customized block title, use localized remote news feed string + $this->title = get_string('block_rss_remote_news_feed', 'block_rss_client'); } } @@ -22,7 +27,7 @@ class block_rss_client extends block_base { } $this->content = new stdClass; - $this->content->footer = ''; + $this->content->footer = ''; if (empty($this->instance) || empty($CFG->blog_version)) { // Either we're being asked for content without @@ -43,7 +48,13 @@ class block_rss_client extends block_base { if (!empty($this->config)) { if (!empty($this->config->rssid)) { - $rssid = intval($this->config->rssid); + if (blog_array_count($this->config->rssid)) { + // rssid is an array of rssids + $rssidarray = $this->config->rssid; + } else { + // rssid is a single rssid + $rssidarray = array($this->config->rssid); + } } if (!empty($this->config->display_description)) { $display_description = intval($this->config->display_description); @@ -70,10 +81,39 @@ class block_rss_client extends block_base { } } + // Daryl Hawes note: if count of rssidarray is greater than 1 + // we should possibly display a drop down menu of selected feed titles + // so user can select a single feed to view (similar to RSSFeed) + foreach ($rssidarray as $rssid) { + $output .= $this->get_rss_by_id($rssid, $display_description, $shownumentries); + } + + $this->content->text = $output; + return $this->content; + } + + function instance_allow_multiple() { + return true; + } + + function has_config() { + return true; + } + + function instance_allow_config() { + return true; + } + + function get_rss_by_id($rssid, $display_description, $shownumentries) { + $returnstring = ''; $rss_record = get_record('block_rss_client', 'id', $rssid); if (isset($rss_record) && isset($rss_record->id)) { $rss = rss_get_feed($rss_record->id, $rss_record->url, $rss_record->type); // print_object($rss); //debug + if (empty($rss)) { + // There was a failure in loading the rss feed + return; + } if ($shownumentries > 0 && $shownumentries < count($rss->items) ) { $count_to = $shownumentries; @@ -84,7 +124,7 @@ class block_rss_client extends block_base { for ($y = 0; $y < $count_to; $y++) { if ($rss->items[$y]['title'] == '') { // $rss->items[$y]['description'] = blog_unhtmlentities($rss->items[$y]['description']); - //can define an additional instance/admin config item for this (20) - max_description_length + //Daryl Hawes note: might want to define an additional instance/admin config item for this (20) - max_description_length $rss->items[$y]['title'] = substr(strip_tags($rss->items[$y]['description']), 0, 20) . '...'; } @@ -92,41 +132,25 @@ class block_rss_client extends block_base { $rss->items[$y]['link'] = $rss->items[$y]['guid']; } - $output .= ''. $rss->items[$y]['title'] . '
' ."\n"; + $returnstring .= ''. $rss->items[$y]['title'] . '
' ."\n"; - if ($display_description){ - $output .= $rss->items[$y]['description'] . '
' ."\n"; + if ($display_description && !empty($rss->items[$y]['description'])){ + $returnstring .= $rss->items[$y]['description'] . '
' ."\n"; } } - $output .= '
'; - // print_object($rss); //debug - $feedtitle = get_string('block_rss_remote_news_feed', 'block_rss_client'); - + // print_object($rss); //debug if ( isset($rss->channel['link']) && isset($rss->channel['title']) ) { $feedtitle = ''. $rss->channel['title'] .''; } } - //can we reset the title here? - if (isset($feedtitle) && $feedtitle != '') { + if (isset($feedtitle) && $feedtitle != '' && $feedtitle != '') { $this->title = $feedtitle; } - - $this->content->text = $output; - return $this->content; - } - - function instance_allow_multiple() { - return true; - } - - function has_config() { - return true; + $returnstring .= '
'; + return $returnstring; } - function instance_allow_config() { - return true; - } } ?> \ No newline at end of file diff --git a/blocks/rss_client/block_rss_client_action.php b/blocks/rss_client/block_rss_client_action.php index 2c178528b5..aa1942f530 100644 --- a/blocks/rss_client/block_rss_client_action.php +++ b/blocks/rss_client/block_rss_client_action.php @@ -80,6 +80,10 @@ } $rss = rss_get_feed($rssid, $url, $rsstype); + + if (empty($rss)) { + print 'There was an error loading this rss feed. You may want to verify the url you have specified before using it.'; + } $dataobject->id = $rssid; if (!empty($rss->channel['description'])) { diff --git a/blocks/rss_client/config_global.html b/blocks/rss_client/config_global.html index c0102f7a74..80ae8b0248 100644 --- a/blocks/rss_client/config_global.html +++ b/blocks/rss_client/config_global.html @@ -11,7 +11,7 @@ } ?>" /> - + @@ -39,6 +39,7 @@ -

+ +

\ No newline at end of file diff --git a/blocks/rss_client/config_instance.html b/blocks/rss_client/config_instance.html index a378d6d0b9..9f2e3eba68 100644 --- a/blocks/rss_client/config_instance.html +++ b/blocks/rss_client/config_instance.html @@ -42,15 +42,37 @@ config) && isset($this->config->rssid)) { - $selected = $this->config->rssid; + if (blog_array_count($this->config->rssid)) { + // rssid is an array of rssids + $selectedarray = $this->config->rssid; + } else { + // rssid is a single rssid + $selectedarray = array($this->config->rssid); + } } if ($rssfeeds = get_records('block_rss_client')) { foreach($rssfeeds as $rssfeed){ $feedoptions[$rssfeed->id] = $rssfeed->title; } - choose_from_menu ($feedoptions, 'rssid', $selected); + $dropdownmenustring = choose_from_menu($feedoptions, 'rssid[]', '', '', '', '0', true); + + //Daryl Hawes note: + // moodle's choose_from_menu() function does not support + // the "multiple" or "size" options, so before printing out the + // calculated drop down menu we insert the keyword "multiple" + $dropdownmenustring = preg_replace("|\"rssid\[\]\" >*|","\"rssid[]\" multiple>", $dropdownmenustring); + + // since there may be multiple rssids to select + // we need to check for each + foreach ($selectedarray as $selected) { + $selected = intval($selected); + $dropdownmenustring = preg_replace("|\"$selected\">*|","\"$selected\" selected>", $dropdownmenustring); + } + + print $dropdownmenustring; + } else { print_string('block_rss_no_feeds', 'block_rss_client'); if ( isadmin() ){ @@ -61,8 +83,14 @@ -

: - +

+ config) && !empty($this->config->title)) { + $title = $this->config->title; + } + ?> + diff --git a/rss/templib.php b/rss/templib.php index c84cf08a44..ce6aa98862 100644 --- a/rss/templib.php +++ b/rss/templib.php @@ -9,29 +9,36 @@ if (empty($CFG->block_rss_client_submitters) ) { if (empty($CFG->block_rss_client_num_entries) ) { $CFG->block_rss_client_num_entries = 5; //default to 5 entries per block } +if (empty($CFG->block_rss_timeout) ) { + $CFG->block_rss_timeout = 30; +} -/* -* rss_get_feed -* Determines whether or not to get a news feed remotely or from cache and reads it into a string -* rssid - id of feed in blog_rss table -* url - remote url of feed -* type - either 'A' or 'R' where A is an atom feed and R is either rss or rdf -* NOTE that this requires allow_url_fopen be On in your php.ini file (it may -* be off for security by your web host) -*/ +/** + * 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;; - $write = 0; - + global $CFG; + $writetofile = false; + $urlfailurestring = 'Failed to open remote feed at: ' . $url .'
allow_url_fopen needs to be On in the php.ini file for this file wrapper call to work. Please refer to http://us2.php.net/filesystem'; + $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'); } + // If moodle dataroot cache/rsscache folder is missing create it if (!file_exists($CFG->dataroot .'/cache/rsscache/')) { mkdir($CFG->dataroot .'/cache/rsscache'); } + $file = $CFG->dataroot .'/cache/rsscache/'. $rssid .'.xml'; // echo "file = ". $file; //debug @@ -45,33 +52,60 @@ function rss_get_feed($rssid, $url, $type) { $data = @stat($file); } $now = time(); - if (($now - $data[10]) > $secs) { //if timedout - // echo "read from original"; //debug - //read from source - if ($CFG->debug){ - $xml = file($url) or die ('Could not open the feed located at the url: ' . $url . 'allow_url_fopen needs to be On in the php.ini file for this file wrapper call to work. Please refer to http://us2.php.net/filesystem'); + if (($now - $data[10]) > $secs) { + // The cached file has expired. Attempt to read fresh from source + $xml = load_feed_from_url($url); + if ($xml) { + //success + $writetofile = true; } else { - $xml = @file($url) or die ('Could not open the feed located at the url: ' . $url .'allow_url_fopen needs to be On in the php.ini file for this file wrapper call to work. Please refer to http://us2.php.net/filesystem'); + // Failed to load remote feed. Since the file exists attempt to read from cache + if ($CFG->debug) { + print $urlfailurestring; + } + $xml = load_feed_from_file($file); + if (!$xml) { + // Failed to load from cache as well! + if ($CFG->debug) { + print $filefailurestring . $file; + return; + } + } } - $write = 1; } else { - // echo "read from cache"; //debug - //read in from cache - if ($CFG->debug){ - $xml = file($file) or die ('Could not open the file located at: ' . $file); - } else { - $xml = @file($file) or die ('Could not open the file located at: ' . $file); + // Cached file has not expired. Attempt to read from cached file. + $xml = load_feed_from_file($file); + if (!$xml) { + // Failed to load from cache, attempt to read from source + if ($CFG->debug) { + print $filefailurestring . $file; + } + $xml = load_feed_from_url($url); + if ($xml) { + // success + $writetofile = true; + } else { + // Failed to read from source as well! + if ($CFG->debug) { + print $urlfailurestring; + } + return; + } } } - } else { //DNE, read from source - // echo "url: ".$url; //debug - - if ($CFG->debug){ - $xml = file($url) or die ('Could not open the feed located at the url: ' . $url . 'allow_url_fopen needs to be Onin the php.ini file for this file wrapper call to work. Please refer to http://us2.php.net/filesystem'); + } else { + // No cached fil at all, read from source + $xml = load_feed_from_url($url); + if ($xml) { + //success + $writetofile = true; } else { - $xml = @file($url) or die ('Could not open the feed located at the url: ' . $url . 'allow_url_fopen needs to be On in the php.ini file for this file wrapper call to work. Please refer to http://us2.php.net/filesystem'); + // Failed to read from source url! + if ($CFG->debug) { + print $urlfailurestring; + } + return; } - $write = 1; } //print_object($xml); //debug @@ -81,7 +115,7 @@ function rss_get_feed($rssid, $url, $type) { $xmlstr = @implode(' ', $xml); } - if ( $write && !empty($xmlstr) ) { //write file to cache + if ( $writetofile && !empty($xmlstr) ) { //write file to cache // jlb: adding file:/ to the start of the file name fixed // some caching problems that I was experiencing. //$file="file:/" + $file; @@ -107,7 +141,39 @@ function rss_get_feed($rssid, $url, $type) { } } +/** + * @param string $file The path to the cached feed to load + */ +function load_feed_from_file($file) { + global $CFG; +// echo "read from cache"; //debug + //read in from cache + if ($CFG->debug){ + $xml = file($file); + } else { + $xml = @file($file); + } + return $xml; +} +/** + * @param string $url The url of the remote news feed to load + */ +function load_feed_from_url($url) { + global $CFG; +// echo "read from original"; //debug + //read from source + if ($CFG->debug){ + $xml = file($url); + } else { + $xml = @file($url); + } + return $xml; +} + +/** + * @param int $rssid . + */ function rss_display_feeds($rssid='none') { global $db, $USER, $CFG, $THEME; global $blogid; //hackish, but if there is a blogid it would be good to preserve it @@ -165,6 +231,13 @@ $deleteString .= '" title="'. get_string('delete') .'" align="absmiddle" border= } } +/** + * @param string $act . + * @param string $url . + * @param int $rssid . + * @param string $rsstype . + * @param bool $printnow . + */ function rss_get_form($act, $url, $rssid, $rsstype, $printnow=true) { global $USER, $CFG, $_SERVER, $blockid, $blockaction; global $blogid; //hackish, but if there is a blogid it would be good to preserve it @@ -229,6 +302,8 @@ function rss_get_form($act, $url, $rssid, $rsstype, $printnow=true) { * added by Daryl Hawes for rss/atom feeds * found at http://us4.php.net/manual/en/function.fwrite.php * added check for moodle debug option. if off then use '@' to suppress error/warning messages + * @param string $filename . + * @param string $content . */ if (! function_exists('file_put_contents')){ function file_put_contents($filename, $content) { -- 2.39.5