New RSS Action, powered with Magpie RSS


See also
Demos: --Demo off--

For this action to work, you'll have to first download Magpie RSS and put it in the ./3rdparty/plugins/magpierss/.
(Note : ./ is the wikka base directory.)

After this is done, create a ./cache/ directory and make it writable.

 

And now, make a backup of the current RSS action (./actions/rss.php) and replace the old by this one :
<?php
// Action usage:
// {{rss http://domain.com/feed.xml}} or {{rss url="http://domain.com/feed.xml" maxitems="5"}}

// As said in the magpie RSS cookbook :
    # Magpie throws USER_WARNINGS only
    # so you can cloak these, by only showing ERRORs
// Those warnings are triggered when Magpie can't download the remote RSS file, and there is no cached version.
// Another possible warning is when the feed is improperly parsed. The cookbook talks about illegal HTML, I handled that pretty well for atom feeds.
error_reporting(E_ERROR);
// This can create an error or warning on some shared hosting, so comment it out if it does.(user // or # in front of the line)

// This is the default cache dir.
$rss_cache_path = "./cache/"; // set this to a writable directory to store the cache files in
$caching = 1; // change this to false to disable caching || Set to 0 for false and 1 for true.

if (!$vars['maxitems']) {
$maxitems = 15; // set this to the maximum items the RSS action should display if there is no maxitems specified in the variables.

}
else {
$maxitems = $vars['maxitems'];
}


// Sticking with default Magpie RSS value : 1 hour.
#$lowest_cache_time_allowed = "5"; // set this to the lowest caching time allowed
#$rss_cache_time = (int)trim($vars['cachetime']);
#if (!$rss_cache_time) {
#    $rss_cache_time = 30; // set this for default cache time
#} elseif ($rss_cache_time < $lowest_cache_time_allowed) {
#    $rss_cache_time = $lowest_cache_time_allowed;
#}


//Action configuration
$rss_path = $vars['url'];
if ((!$rss_path) && $wikka_vars) $rss_path = $wikka_vars;
$rss_path = $this->cleanUrl(trim($rss_path));

if (preg_match("/^(http|https):\/\/([^\\s\"<>]+)$/i", $rss_path))
{

// Set Your Proxy Settings Here
define('MAGPIE_PROXY_HOST', '');
define('MAGPIE_PROXY_PORT', '');

// Path To MagpieRSS
define('MAGPIE_DIR', '3rdparty/plugins/magpierss/');

// Cache Settings
define('MAGPIE_CACHE_DIR', $rss_cache_path);
define('MAGPIE_CACHE_ON', $caching);

// Not sure how to use this one, so i'll stick with commenting it, default aging with Magpie is set to 1 hour.
#define('MAGPIE_CACHE_AGE', 10);

require_once(MAGPIE_DIR.'rss_fetch.inc');

    $rss = fetch_rss( $rss_path );
    if ($rss) {
        $i = 1;
        $cached_output = "<h3>".$rss->channel['title']."</h3>";
        if ($rss->channel['link']) {
            $cached_output .= '<div align="right"><a href="'.$rss->channel['link'].'">FEED URL : '.$rss->channel['link'].'</a></div>';
        }
        $cached_output .= "<ul>\n";
        foreach ($rss->items as $item) {
            $href = $item['link'];
            $title = $item['title'];
            if ($item['content']) {
                $description = implode($item['content']);
        }
            else {
                $description = $item['description'];
            }
            $cached_output .= "<li><a href=$href>$title</a><br />$description</li>";
            if ($i >= $maxitems) {
                break;
            }
            $i++;
        }
        $cached_output .= "</ul>\n";
   
        echo $this->ReturnSafeHTML($cached_output);
    } else {
        echo "An error occured when fetching or parsing the feed.<br />Check that the feed is compliant.";
    }
} else {
    echo '<em class="error">Error: Invalid RSS syntax. <br /> Proper usage: {{rss http://domain.com/feed.xml}} or {{rss url="http://domain.com/feed.xml" maxitems="5"}}</em>';
}

?>


You also need to patch the rss_fetch.inc file included with magpierss. This patch adds proxy support. Save the file below as magpierss_proxy_support.patch in the same directory as rss_fetch.inc (usually 3rdparty/plugins/magpierss).

diff -u --recursive magpierss-0.71.1/rss_fetch.inc magpierss-0.71.1-proxy/rss_fetch.inc
--- magpierss-0.71.1/rss_fetch.inc      2005-02-09 13:59:01.000000000 -0600
+++ magpierss-0.71.1-proxy/rss_fetch.inc        2005-08-06 16:52:02.000000000 -0500
@@ -270,6 +270,8 @@
	 $client->agent = MAGPIE_USER_AGENT;
	 $client->read_timeout = MAGPIE_FETCH_TIME_OUT;
	 $client->use_gzip = MAGPIE_USE_GZIP;
+    $client->proxy_host = MAGPIE_PROXY_HOST;
+    $client->proxy_port = MAGPIE_PROXY_PORT;
	 if (is_array($headers) ) {
		 $client->rawheaders = $headers;
	 }
@@ -391,6 +393,14 @@
	 if ( !defined('MAGPIE_USE_GZIP') ) {
		 define('MAGPIE_USE_GZIP', true);
	 }
+
+    if ( !defined('MAGPIE_PROXY_HOST') ) {
+       define ('MAGPIE_PROXY_HOST', '');
+    }
+
+    if ( !defined('MAGPIE_PROXY_PORT', '') ) {
+       define ('MAGPIE_PROXY_PORT', '');
+    }
 }

 // NOTE: the following code should really be in Snoopy, or at least


Change the the magpeirss directory. Then apply the patch by using the following command (in Linux):

 patch -Np1 <magpierss_proxy_support.patch 


To set a proxy, modify the MAGPIE_PROXY_HOST and MAGPIE_PROXY_PORT defines in your new rss.php file.

I think that it is NOT idiot proof and there may be place for optimisations.
Feel free to edit the code and tell me what you did, i'm a learner !

And, at last, please comment, so I'll know that people are seeing it. Thank you !



CategoryDevelopmentSyndication
Comments
Comment by JavaWoman
2005-06-14 07:37:59
Thanks, SamuelDr!

For anyone who wants an RSS reader that supports ATOM this looks like a nice drop-in replacement.

Yes, I'm sure it can be refined, but it's certainly a good start. :)

(I'll play with it as soon as I have the time - I'd like to see how it behaves in error conditions, for example.)
Comment by SamuelDr
2005-06-14 17:44:52
Thanx.
Yeah, good idea, error conditions.
Magpie RSS automatically uses the version in cache when it can't find the given feed url. it's written in the documentation.
Comment by JavaWoman
2005-06-14 22:40:31
A few things to consider (and test):

What if there's no version in the cache yet? There can't be one if you give it a new URL that doesn't exist. What if it does exist but a connection can't be made or it times out on the request? (again first time - no cache yet.)

And what happens when the cache expires but the URL no longer exists at that time? Will it endlessly use the cache or will it inform you?

What format does Magpie return when there's an error condition and how does the action handle that?
Comment by SamuelDr
2005-06-15 01:11:59
That's the milestone where I am, errors.
Now, just see the code to understand.
I added a PHP thingie to report only errors (as magpie RSS generates php WARNINGS) and a condition that verifies if the RSS feed has been loaded
Comment by JavaWoman
2005-06-15 10:24:23
Good!
Especially the condition, otherwise with an empty (or no) array returned you'd end up with an empty ul element which would be invalid! But it would be good to print a heading with the feed URL as well. If you have several feeds on page it woudl be hard to see which one is having probnlems otherwise; just the URL would do.
Comment by NilsLindenberg
2005-06-15 11:15:58
>But it would be good to print a heading with the feed URL as well.

$cached_output = "<h3>".$rss->channel['title']."</h3>";

oik, it misses the link, so what about this (untested):

$cached_output = '<h3><a href=" '.$url.' ">'.$rss->channel['title'].'</a></h3>';

(i made a space betwenn things like "' to make them disthinguable)

$rss_cache_file = ""; // initial value, no need to ever change

that seems to be from the old action, isn't it? Since it does not seem to be in use anymore, try without it :)

// set this to the maximum items the RSS action should ever display

that comment does not fit to the code below
Comment by SamuelDr
2005-06-15 20:10:47
Now, with the header-link part, not sure if it is XHTML compliant, when there is a camelcase word in a header, the validator complaints about it.
There should be a link under the <h3> but, i would have to check about ATOM feeds, currently working on that.

I have deleted the obsolete variable and modified the "maxitems" comment.

And, it IS based on the old RSS action, I just adapted it with Magpie RSS' codes.

Thanks for commenting anyway !
Comment by SamuelDr
2005-06-15 21:40:24
Now, I have put the FEED URL, but I don't know if I made it appear as it should.
It works with my atom and my RSS feed.
Please tell (and edit) the link as it should appear, but as I said, I'm sure that a link in a h3 is bad, just verify it.

_____
When there is a cache version of a feed that can't be joined (server down), it reads the cache version, today, one of my feed has gone offline.
Comment by 162.71.232.253
2005-08-05 23:16:41
Need to add proxy support, so intranets behind an HTTP proxy can download the feeds. I'm working on figuring that out now.
Comment by IanHayhurst
2005-12-02 13:15:30
Ohh yes thanks it works a treat (had to apply the patch lines manualy as Magpierss download was 0.72 and the proxy patch is against 0.71
Valid XHTML :: Valid CSS: :: Powered by WikkaWiki