Recent Topics

Final version of magpieRSS hack

Started by on Sep 04, 2004 – Contents updated: Sep 04, 2004

Sep 04, 2004 02:01    

You can check out http://forums.b2evolution.net/viewtopic.php?t=2257 to use zfeeder or you can use magpieRSS to put a little aggregator on your sidebar. I had avoided sharing the code here because of it's length and because it was easier to keep "the version" constant while I tinkered. I'm done tinkering and I can't promise that my web will last forever, so here you go! Step 1 is to get magpieRSS http://magpierss.sourceforge.net/ and unzip then upload it to your skins/skinname folder. Step 2 is to get _magpierss.php from right here and upload it to your skins/skinname folder. Step 3 is to get a little bit of code from right here and add it to your _main.php file.

Save this as _magpierss.php in your skins/skinname folder:

<?php 
/**
 * This is the template that displays XML based feeds on your sidebar
 *
 * This is rev2 - with StayPuft Improvements!
 * http://forums.b2evolution.net/viewtopic.php?t=2258
 *
 * This file is not meant to be called directly.
 * It is meant to be called by an include in the _main.php template.
 *
 * This requires magpieRSS which is not part of the b2evolution distribution
 * magpieRSS is released under the GPL License and the author holds copyright
 * This application makes absolutely no modifications to the magpieRSS files!
 * Download it, unzip it, and upload the files to your /skins/skinname folder
 *
 * This file is made possible by my complete lack of a life and any responsibilities
 * normally associated with a life.  This file utilizes code copyrighted by Kellan
 * and has no value outside of code copyrighted by Francois, and still I get to say
 * @copyright (c) 2004 by Ed Bennett - {@link http://wonderwinds.com}
 * Released under the GNU GPL - {@link http://www.gnu.org/copyleft/gpl.html}
 * Feel free to tweak/hack/fix/improve or otherwise modify this file as you see fit.
 *
 * magpieRSS - {@link http://magpierss.sourceforge.net/}
 * Released under the GNU General Public License.
 * @copyright(c) 2002 kellan@protest.net. All rights reserved.
 * Please read the disclaimer at the top of the /extlib/Snoopy.class.inc file.
 *
 * b2evolution - {@link http://b2evolution.net/}
 * Released under GNU GPL License - {@link http://b2evolution.net/about/license.html}
 * @copyright (c)2003-2004 by Francois PLANQUE - {@link http://fplanque.net/}
 *
 * @package evoskins
 * @subpackage custom
 */

if( !defined('DB_USER') ) die( 'Please, do not access this page directly.' );

# This section allows you to control some features with true/false and decorate your display
# set this to false if you don't want to show the name of the site your feed comes from
# if this is false then 'header_start' and 'header_end' and 'header_linked' don't get used
$show_site_name = true;
# this is for the name of the site you are getting your feed from
if(!isset($header_start)) $header_start = '<h4>';
if(!isset($header_end)) $header_end = '</h4>';
# set this to false if you don't want the site's title linked to it's url
$header_linked = true;

# this is for the start and end of each block of items from each feed
if(!isset($block_start)) $block_start = '<ul>';
if(!isset($block_end)) $block_end = '</ul>';

# this is for the start and end of each individual item in each feed
if(!isset($item_start)) $item_start = '<li>';
if(!isset($item_end)) $item_end = '</li>';

# this is for the start and end of each item's title
if(!isset($item_title_start)) $item_title_start = '<strong>';
if(!isset($item_title_end)) $item_title_end = '</strong>';
# set this to false if you don't want the each item linked to it's url
$item_title_linked = true;

# set this to false if you don't want to show the author's name for each item
# if this is false then 'author_start' and 'author_end' don't get used
$show_author_name = true;
# this is for before and after the author's name for each item
# if there is no author information then these do not get used
if(!isset($author_start)) $author_start = ' (By: ';
if(!isset($author_end)) $author_end = ')<br />';
# for atom feeds, link to the author's website if they have one?
# note that for rss2 feeds email ids are not linked or displayed.
$link_atom_author = true;

# should really long items be cut short?
$cut_item_desc = true;
# how short should they be cut down to?
$cut_item_desc_to = 374;
# set this to false if you don't want a link to the end of a shortened item
$link_truncated_item = true;

# this is how many items to show if it is not set in _main.php
if(!isset($show_me)) $show_me = 15;
# set this to false if you do not want to fix invalid tags in your feeds
# this will not fix ALL bad stuff in your feeds but it catches some common problems
$fix_bad_tags = true;
$remove_tags = true; // StayPuft improvement

// BEYOND HERE BE DRAGONS!

if ( $rss ) 
	{
	if ( $show_site_name ) 
		{ // do this if the site name gets displayed
		$site_title = $rss->channel['title'];
		$site_url = $rss->channel['link'];
		$site_desc = $rss->channel['description'];
		if ( isset($rss->channel['tagline']) )
			{ // get the atom tagline as the description
			$site_desc = $rss->channel['tagline'];
			}
		$header_string = "";
		$header_string .= $header_start;
		if ( $header_linked ) 
			{ // do this if the site name gets linked
			$header_string .= "<a href=\"".$site_url."\" title=\"".$site_desc."\">".$site_title."</a>";
			}
		else 
			{ // do this if the site name is not linked
			$header_string .= $site_title;
			}
		$header_string .= $header_end."\n";
		echo $header_string;
		}
	echo $block_start."\n";
	$ii = 1;
	foreach ($rss->items as $item ) 
		{ // now start cycling through items
		if ( $ii <= $show_me ) 
			{ // do it again if we have not reached the limit
			$item_title = $item[title];
			$item_url = $item[link];
			$item_desc = $item[description];
			if ( isset($item[atom_content]) )
				{ // get the atom content as the description
				$item_desc = $item[atom_content];
				}
			if ( $show_author_name ) 
				{ // do this if the authors name gets displayed
				if ( isset($item[author]) ) 
					{ // is it rss2?
					$item_auth = $item[author];
					$have_auth = true;
					// remove the author's email id if present
					$has_email = strpos($item_auth, '@');
					if ( $has_email !== false ) 
						{ // we found an email id so remove it and starve a spam-bot
						$item_auth = substr($item_auth, 0, strrpos($item_auth, ' '));
						}
					}
				if ( isset($item[dc][creator]) ) 
					{ // is it rdf?
					$item_auth = $item[dc][creator];
					$have_auth = true;
					}
				if ( isset($item[author_name]) ) 
					{ // is it atom?
					$item_auth = $item[author_name];
					$have_auth = true;
					// link to the author's website if they have one
					if ( isset($item[author_url]) && $link_atom_author )
						{ // website present so build a good link
						$item_auth_url = $item[author_url];
						if( !preg_match('#://#', $item_auth_url) )
							{ // add the http part if it's not there
							$item_auth_url = 'http://'.$item_auth_url;
							}
						$item_auth = "<a href=\"$item_auth_url\" title=\"".$item_auth."'s website: $item_auth_url\">".$item_auth."</a>";
						}
					}
				}
			if ( $item_title == '' ) 
				{ // if the item is untitled then make one up
				$item_title = substr(strip_tags($item_desc), 0, 20).'...';
				}
			if ( $item_url == '' ) 
				{ // if the item has no link then use the site link
				$item_url = $rss->channel['link'];
				}
			if ( $fix_bad_tags )
				{ // clean up some bad xhtml that might be in the feed - keep your page valid!
				$item_desc = preg_replace("(<br>)", "<br />", $item_desc);
				$item_desc = preg_replace("(.gif>)", ".gif\" alt=\"image\" />", $item_desc);
				$item_desc = preg_replace("(.jpg>)", ".jpg\" alt=\"image\" />", $item_desc);
				$item_desc = preg_replace("(.png>)", ".png\" alt=\"image\" />", $item_desc);
				$item_desc = preg_replace("(src=htt)", "src=\"htt", $item_desc);
				}
				// START TAG Removal added by StayPuft
				if ( $remove_tags )  // added by StayPuft
					{ // remove HTML tags
					$item_desc = strip_tags($item_desc);
					}
				// END TAG Removal added by StayPuft
				if ( $cut_item_desc )
					{ // do this if long descriptions are to be cut down
					if ( $remove_tags )
						{ // tags have already been removed
						$strip_item_desc = $item_desc;
						} else { // remove HTML tags
						$strip_item_desc = strip_tags($item_desc);
						}
					$strip_item_desc_len = strlen($strip_item_desc);
					if ( $strip_item_desc_len >= $cut_item_desc_to )
						{
						if ( $link_truncated_item )
							{ // do this if cut down items get linked to the article
							$continued_text = "<a href=\"".$item_url."\" title=\"read more!\"><em> (read more)</em></a>";
							} else { // do this if truncated items do not get linked to the article
							$continued_text = "<em> (read more)</em>";
							}
						$item_desc = substr($strip_item_desc, 0, $cut_item_desc_to).$continued_text;
						} else {
						$item_desc = $strip_item_desc;
						}
					}
				$item_title_string = "";
				$item_title_string .= $item_start.$item_title_start;
				if ( $item_title_linked ) 
				{ // do this if the item title gets linked
				$item_title_string .= "<a href=\"".$item_url."\" title=\"link to this item\">".$item_title."</a>";
				} else { // do this if the item title does not get linked
				$item_title_string .= $item_title;
				}
			$item_title_string .= $item_title_end."\n";
			echo $item_title_string;
			if ( ( $show_author_name ) && ( $have_auth ) ) 
				{ // do this if we want to show the author and have one to show
				echo $author_start.$item_auth.$author_end;
				}
			echo $item_desc.$item_end;
			}
		$ii++;
		}
	echo $block_end."\n";
	}
else {
	echo "<p>Houston we have a problem</p>";
	}
?>

Add this little bit to your _main.php file:

<div class="bSideItem">
	<?php 
	require_once( dirname(__FILE__).'/rss_fetch.inc' );
	?>
	<h3>Read the Feeds</h3>
	<?php 
	$show_me = 3;
	$url = "http://b2evolution.net/xmlsrv/rss2.php?blog=2";
	$rss = fetch_rss($url);
	require( dirname(__FILE__).'/_magpierss.php' );
	?>
	<a href="http://magpierss.sourceforge.net/" title="MagpieRSS provides an XML-based (expat) RSS parser in PHP"><img src="http://magpierss.sf.net/black_grey_magpie_news.gif" alt="MagpieRSS provides an XML-based (expat) RSS parser in PHP" width="80" height="15" border="0" /></a>
	</div>


The four-line-bit beginning with $show_me is where you add your feeds. How many, what feed, fetch it, then the required file to display it. You can set $show_me in _magpierss.php and leave it out of here if you're okay with the same number of items for each feed. In my real-world application I snagged the magpie gif and put it on my own server so I would not have to see the alt text when sourceforge fails to deliver.

Cool. Have fun.

Sep 06, 2004 06:25

EdB wrote:

{ // do this if truncated items do not get linked to the article
	$continued_text = "<em> (read more)</em>";
	}

Okay well that's a bit embarrasing! I had a validation killing error in _magpierss.php - the big one. Way down towards the bottom I left a "close link" tag after the /em for the non-linked 'read more' text. (like :oops: dude). Anyway I edited the post above AND copied the correct version here for those into making things right. Validation is funny. Most of the time is doesn't matter if you get it wrong and it hardly ever matters if you get it right. Even so it's nice to think that some aspect of my world has been deemed "valid". I once had a CAT scan done and I saved the report because it said I had a normally shaped brain. I was proud of that report. :roll:

Hey I got an idea that's a teeny tiny step towards what Isaac (and others) have looked for: a way to re-feed a feed. Rather than do the cron job thing and always automagically post whatever came through your aggregator I'm thinking of something like this: You visit your site and see something cool on your feed reader. You open the link in a new window or a new tab and can't wait to blog about it on your blog. You come back to your site and click a "ReBlog This" link and - abracadabra - the snippet is fed into the back office as the beginning of a titled and linked post. You add your two bits... er... um... profound wisdom and post it on your blog. Good? Stupid? Get a Life? (Wow now it's time to play with Zfeeder!)

Sep 21, 2004 11:25

Hey EdB

Awsome plugin. Thanks so much for it. Really is quite slick.
Done any more??

Cheers gacjezv

Sep 21, 2004 14:08

Nope. I tried to move some of the labor involved in the rss hack off to tables and the back office, but I kept stumbling over the simple parts so I gave up. Then I thought about the reblog idea but couldn't make heads or tails of actually getting it done. I also tried getting the bugs out of "recent posts" and "recent comments" but they're both way smarter than me so I gave up on that too. So I decided I like skins! I took a bunch of the golden oldies (and a handful of recent ones) and made them be CSS/XHTML valid, work well in 5 different browsers at 1024 or 800 wide resolution, and made sure they are cool with NinetyTen. Including 3 new ones of my own I've got 24 skins just about ready for prime time.

Sep 22, 2004 01:57

Hey EdB, I ran this last night and found that a b2e blog I agregated updated immediately, whereas a Blogger (atom I expect) blog didnt update upon a page refresh....?

The code I used in the _main.php on my site is:

<div class="bSideItem">
   <?php
   require_once( dirname(__FILE__).'/rss_fetch.inc' );
   ?>
   <h3>Read the Feeds   <a href="http://magpierss.sourceforge.net/" title="MagpieRSS provides an XML-based (expat) RSS parser in PHP"><img src="http://gacjezv.com/b2blogs/skins/clean-rss/img/black_grey_magpie_news.gif" alt="MagpieRSS provides an XML-based (expat) RSS parser in PHP" width="80" height="15" border="0" /></a></h3>

	 <?php
   $show_me = 2;
   $url = "http://www.gacjezv.com/b2blogs/xmlsrv/rss2.php?blog=10";
   $rss = fetch_rss($url);
   require( dirname(__FILE__).'/_magpierss.php' );
   ?>
   <?php
   $show_me = 3;
   $url = "http://redsultana.com/cellobella/index.rss";
   $rss = fetch_rss($url);
   require( dirname(__FILE__).'/_magpierss.php' );
   ?>
	  
	 </div>

Do you have any thoughts on this? i.e. is there a refresh issue with Atom feeds? Is the order important?

I will play around and post what I discover.

Regards
gacjezv

Sep 22, 2004 02:57

You might consider looking to see if magpieRSS made a cache folder or not, but if one blog updates and another doesn't it seems odd to blame a cache. When reading through all of magpieRSS's text files you learn that it tries to make a cache folder if it can, then cache what it reads for an hour. When I was working on this I went to my web host to see if magpieRSS made up a cache folder and saw that it did not. At the time I thought "I should add one and see about easily incorporating it into the hack instructions" but never got a round tuit for that piece of the action. I should for the sake of server loading, but I haven't noticed any perceptible uptake in bandwidth consumption at the only site I use this feature on. I'm only looking at one feed and only posting one read so maybe that's part of why I don't see a bandwidth hit?

I suspect it has something to do with how Blogger feeds the world. It would sort of make sense for them to limit traffic by not updating feeds every time a post is updated - maybe just telling all the aggregators in the world "go away" instead of letting them all in to have a peek at content every second of the day. Keep in mind I'm no expert on this or any other topic! Have you noticed any frequency with which the Blogger blog gets updated? If for example you get your new content hourly no matter when the post was posted during the hour that would offer circumstantial evidence of Blogger locking the gates.

Mar 22, 2005 02:09

I am a user of this great plugin/hack... but having seen the wonderful tool http://www.reblog.org/ reblog, I was wondering if the idea that EdB came up with earlier in this post was a potential??

i.e. use reblog to get a list of feeds and the content, then have a reblog "option" to post selected feed elements to the B2E back office for posting?

there is some WP and MT plugins developed and available in the released version. I wonder if any one is interested in pursuing a mod'd plugin for B2E?

Hope some one can? I cant I am a dunce PHP programmer....

Cheers gacjezv

Jun 08, 2005 02:51

this is a great plugin/hack - thank you SO much to edB and joe. :D

just wanted to let you know how much i appreciate it!

Jun 12, 2005 20:18

gacjezv,

I agree, it would be a wonderful plugin/mod.
I need this very badly. I am an programmer, but was trying to fix it for example with the use of tags. But it is much harder then it seems to be.

Everytime i thought i got it became a total mess.
I am still trying, but maybe somebody can give me some help how to use the tags to make sort of link tag wich you can use to add your rss-feed etc.

Regards,
Raptor

Sep 07, 2005 14:30

Great plugin, Ed!

A little bug though... I learned the hard way that if an RSS Feed is super-short, but it contains a gigantic image, the <img> tag is NOT removed and so the huge image posted on someone elses blog overshadows my blog. After a bit of trial and error, I also discovered that links and other tags are not stripped out either of short posts that are supposed to be truncated.

I've found the cause of this anomaly around Line 174 in _magpierss.php

         if ( $cut_item_desc )
            { // do this if long descriptions are to be cut down
            $strip_item_desc = strip_tags($item_desc);
            $strip_item_desc_len = strlen($strip_item_desc);
            if ( $strip_item_desc_len >= $cut_item_desc_to ) {
               if ( $link_truncated_item )
                  { // do this if cut down items get linked to the article
                  $continued_text = "<a href=\"".$item_url."\" title=\"read more!\"><em> (read more)</em></a>";
                  }
               else
                  { // do this if truncated items do not get linked to the article
                  $continued_text = "<em> (read more)</em>";
                  }
               $item_desc = substr($strip_item_desc, 0, $cut_item_desc_to).$continued_text;
               }
            }

Basically, HTML Tags are only removed by the declaration:
$strip_item_desc = strip_tags($item_desc);
but this only happens if:
$cut_item_desc is set to "true"

And then the stripped string replacement only occurs by the declaration: $item_desc = substr($strip_item_desc, 0, $cut_item_desc_to).$continued_text;
if the post is longer than value set ($strip_item_desc_len >= $cut_item_desc_to)

This means that if "$cut_item_desc = false;" or if a post is shorter than the value of $cut_item_desc_to, then HTML Tags are NOT stripped from the RSS Feed.

[u]Long story short, here's my fix:[/u]

FIND (in LINE 81):

$fix_bad_tags = true;

ADD after:

$remove_tags = true;

then FIND (around Line 172):

         if ( $cut_item_desc )
            { // do this if long descriptions are to be cut down
            $strip_item_desc = strip_tags($item_desc);
            $strip_item_desc_len = strlen($strip_item_desc);
            if ( $strip_item_desc_len >= $cut_item_desc_to ) {
               if ( $link_truncated_item )
                  { // do this if cut down items get linked to the article
                  $continued_text = "<a href=\"".$item_url."\" title=\"read more!\"><em> (read more)</em></a>";
                  }
               else
                  { // do this if truncated items do not get linked to the article
                  $continued_text = "<em> (read more)</em>";
                  }
               $item_desc = substr($strip_item_desc, 0, $cut_item_desc_to).$continued_text;
               }
            }

REPLACE with:

        // START TAG Removal added by StayPuft
         if ( $remove_tags )  // added by StayPuft
            { // remove HTML tags
            $item_desc = strip_tags($item_desc);
           }
        // END TAG Removal added by StayPuft
         if ( $cut_item_desc )
            { // do this if long descriptions are to be cut down
              if ( $remove_tags )
                { // tags have already been removed
                $strip_item_desc = $item_desc;
                }
              else
                { // remove HTML tags 
                $strip_item_desc = strip_tags($item_desc);
                }
              $strip_item_desc_len = strlen($strip_item_desc);
            if ( $strip_item_desc_len >= $cut_item_desc_to )
               {
               if ( $link_truncated_item )
                  { // do this if cut down items get linked to the article
                  $continued_text = "<a href=\"".$item_url."\" title=\"read more!\"><em> (read more)</em></a>";
                  }
               else
                  { // do this if truncated items do not get linked to the article
                  $continued_text = "<em> (read more)</em>";
                  }
               $item_desc = substr($strip_item_desc, 0, $cut_item_desc_to).$continued_text;
               }
            else 
               {
               $item_desc = $strip_item_desc;
               }
            }

My edit fixes it so HTML tags are stripped out of the short posts (as Ed had originally intended) but it also strips out the tags from the long posts if so desired based on the added variable $remove_tags.

If you set $remove_tags to "true", I'd recommend setting $fix_bad_tags to "false" since there's no need to clean the tags if you're straight-up removing them.

Sep 07, 2005 15:57

Thanks! I think I have this hack on my blog so I'll go back and add your improvements to it (with credits of course). I'll also make your changes part of the initial post here.


Form is loading...

Photo gallery software – This forum is powered by b2evolution CMS, a complete engine for your website.