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.

This commit is contained in:
dhawes
2004-12-30 18:08:38 +00:00
parent 10f2f07908
commit 92ce1eb2ef
5 changed files with 216 additions and 84 deletions
+68 -44
View File
@@ -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,49 +81,13 @@ class block_rss_client extends block_base {
}
}
$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 ($shownumentries > 0 && $shownumentries < count($rss->items) ) {
$count_to = $shownumentries;
} else {
$count_to = count($rss->items);
}
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
$rss->items[$y]['title'] = substr(strip_tags($rss->items[$y]['description']), 0, 20) . '...';
}
// 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);
}
if ($rss->items[$y]['link'] == '') {
$rss->items[$y]['link'] = $rss->items[$y]['guid'];
}
$output .= '<a href="'. $rss->items[$y]['link'] .'" target=_new>'. $rss->items[$y]['title'] . '</a><br />' ."\n";
if ($display_description){
$output .= $rss->items[$y]['description'] . '<br />' ."\n";
}
}
$output .= '<br />';
// print_object($rss); //debug
$feedtitle = get_string('block_rss_remote_news_feed', 'block_rss_client');
if ( isset($rss->channel['link']) && isset($rss->channel['title']) ) {
$feedtitle = '<a href="'. $rss->channel['link'] .'">'. $rss->channel['title'] .'</a>';
}
}
//can we reset the title here?
if (isset($feedtitle) && $feedtitle != '') {
$this->title = $feedtitle;
}
$this->content->text = $output;
return $this->content;
}
@@ -128,5 +103,54 @@ class block_rss_client extends block_base {
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;
} else {
$count_to = count($rss->items);
}
for ($y = 0; $y < $count_to; $y++) {
if ($rss->items[$y]['title'] == '') {
// $rss->items[$y]['description'] = blog_unhtmlentities($rss->items[$y]['description']);
//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) . '...';
}
if ($rss->items[$y]['link'] == '') {
$rss->items[$y]['link'] = $rss->items[$y]['guid'];
}
$returnstring .= '<a href="'. $rss->items[$y]['link'] .'" target=_new>'. $rss->items[$y]['title'] . '</a><br />' ."\n";
if ($display_description && !empty($rss->items[$y]['description'])){
$returnstring .= $rss->items[$y]['description'] . '<br />' ."\n";
}
}
// print_object($rss); //debug
if ( isset($rss->channel['link']) && isset($rss->channel['title']) ) {
$feedtitle = '<a href="'. $rss->channel['link'] .'">'. $rss->channel['title'] .'</a>';
}
}
if (isset($feedtitle) && $feedtitle != '' && $feedtitle != '<a href="'. $rss->channel['link'] .'"></a>') {
$this->title = $feedtitle;
}
$returnstring .= '<br />';
return $returnstring;
}
}
?>
@@ -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'])) {
+3 -2
View File
@@ -11,7 +11,7 @@
} ?>" />
</td>
<td>
<?php print_string('block_rss_num_entries', 'block_rss_client') ?>
<?php print_string('block_rss_client_num_entries', 'block_rss_client') ?>
</td>
</tr>
<tr valign="top">
@@ -39,6 +39,7 @@
<input type="submit" value="<?php print_string('savechanges') ?>"></td>
</tr>
<tr>
<td align=center><a href=" <?php echo $CFG->wwwroot; ?>/blocks/rss_client/block_rss_client_action.php?courseid=<?php echo $courseid; ?>"><?php print_string('block_rss_feeds_add_edit', 'block_rss_client')?></a></center><br /><br />
<td colspan="3" align="center"><a href=" <?php echo $CFG->wwwroot; ?>/blocks/rss_client/block_rss_client_action.php?courseid=<?php echo $courseid; ?>"><?php print_string('block_rss_feeds_add_edit', 'block_rss_client')?></a></center><br /><br />
</td>
</table>
+33 -5
View File
@@ -42,15 +42,37 @@
</td>
<td>
<?php
$selected = '';
$selectedarray = '';
if (isset($this->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 @@
</td>
</tr>
<tr valign="top">
<td align="right"><p><?php print_string('block_rss_word_title', 'block_rss_client'); ?>:</td>
<td><input type="text" name="title" size="30" value="<?php echo $this->config->title; ?>" />
<td align="right"><p><?php print_string('uploadlabel'); ?></td>
<?php
$title = '';
if (!empty($this->config) && !empty($this->config->title)) {
$title = $this->config->title;
}
?>
<td><input type="text" name="title" size="30" value="<?php echo $title; ?>" />
</td>
</tr>
<tr>
+108 -33
View File
@@ -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 .'<br /> allow_url_fopen needs to be On in the php.ini file for this file wrapper call to work. Please refer to <a href="http://us2.php.net/filesystem">http://us2.php.net/filesystem</a>';
$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 <a href="http://us2.php.net/filesystem">http://us2.php.net/filesystem</a>');
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 <a href="http://us2.php.net/filesystem">http://us2.php.net/filesystem</a>');
// 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 <a href="http://us2.php.net/filesystem">http://us2.php.net/filesystem</a>');
} 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 <a href="http://us2.php.net/filesystem">http://us2.php.net/filesystem</a>');
// 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) {