Page 1 of 1

Writing a plugin - it works, but a second use doesn't

Posted: Fri May 28, 2004 2:55 pm
by Auz
I've written (well, hacked someone else's RSS reader) a serendipity plugin that displays an RSS feed in the panels. It works, but if I include a second instance of it, then it breaks the plugin panel and front page...

The front page just displays the right-hand panel and, where the plugin would appear. the error "no element found".

The plugin admin page stops at the point where the links to the plugin config would appear:

Image

It's installed on a fairly standard setup - the only difference is I had to comment out the php settings in the .htaccess due to the way my host sets things up.

Any suggestions as to where I'm going wrong?

Code for the plugin is as follows.

Code: Select all

<?php # $Id: $

/* RSS reader plugin by Auz (www.auzsoft.net). Reads RSS. Based on rss-reader by Kailash Nadh */

class serendipity_plugin_rssreader extends serendipity_plugin {
    function introspect(&$propbag)
    {
        $propbag->add('name',          'RSS Reader Plugin');
        $propbag->add('description',   'Reads RSS Feed');
        $propbag->add('configuration', array(
										'title',
                                        'url',
                                        'items',
										'chop'
                                       )
        );
    }

    function introspect_config_item($name, &$propbag)
    {
        switch($name) {
			case 'title':
                $propbag->add('type',        'string');
                $propbag->add('name',        'Feed Title');
                $propbag->add('description', 'Title of the RSS feed (leave blank to use feed\'s)');
                break;

			case 'url':
                $propbag->add('type',        'string');
                $propbag->add('name',        'URL');
                $propbag->add('description', 'URL of the RSS feed');
                break;

			case 'chop':
                $propbag->add('type',        'string');
                $propbag->add('name',        'Truncate');
                $propbag->add('description', 'Max length of headlines');
                break;

            case 'items':
				$select["1"] = "One";
				$select["2"] = "Two";
				$select["3"] = "Three";
				$select["4"] = "Four";
				$select["5"] = "Five";
				$select["10"] = "Ten";
                $propbag->add('type',        'select');
                $propbag->add('name',        'Display');
                $propbag->add('description', 'No. of items to display');
				$propbag->add('select_values', $select);
                break;
           
            default:
                return false;
        }
        return true;
    }

    function generate_content(&$title, $output = true)
    {
        $title = $this->get_config('title');
		$url   = $this->get_config('url');
		$disp  = $this->get_config('items');
		$chop  = $this->get_config('chop');
        //echo "Viewing $disp items from $url";
		$data = @fread( fopen("$url","r"), 10000 );
		$myar = $this->getXmlData($data);
		
		if (!$title) {
			$title = $myar["TITLE"][0];
		}

		echo "<div>";
		for ( $i=1; $i<=$disp; $i++ ) {
			$head = $myar["TITLE"][$i];
			$full = $head;
			$link = $myar["LINK"][$i];
			if ( $chop ) {
				if ( strlen($head) > $chop ) {
					$head = rtrim(substr( $head, 0, $chop-3 )) . "...";
				}
			}
			echo "<a href=\"$link\" target=\"_blank\" title=\"$full\">$head</a><br>\n";
		}
		echo "</div>";
		//echo "<!--\n ".print_r($myar,TRUE)." \n-->";
    }

	/*##############################################

	RSS parser written for bMachine feed.
	This script can be used to read ANY kind of RSS feeds from anywhere!

	*--------------------------------------------*
	Written by Kailash Nadh,
	http://bnsoft.net , kailash@bnsoft.net

	Author of bMachine, http://boastology.com

	I wrote this script on the 1st day I studied
	XML by running through the documentation at www.php.net ;)
	*--------------------------------------------*

	WARNING!: This script is Heavily commented! :)

	This is how the script works.
	> Reads the XML file
	> Parses it into an array using xml_parse_into_struct()
	> Converts that array into a more sensible, easily usable array
	[ See the structure.txt file to get an idea of the array structure ]
	> Finally, Display the data in anyway you want!

	This script demonstrates the use of simple logic
	to do Complex XML/RSS parsing functions
	This script can be easily developed into a powerful application.

	##############################################*/

	//#####################################################################

	// This is the function. It returns the array of the parsed XML data

	function getXmlData($xml_doc) {

		$n=0; 		 // Counter used for arraying the XML data
		$ar=array(); // The main array for storing parsed xml using xml_parse_into_struct()

		// Parse the XML document
		$parser = xml_parser_create();
		xml_parse_into_struct($parser,$xml_doc,$vals,$index) or die(xml_error_string(xml_get_error_code($parser)));
		xml_parser_free($parser);

		$ttags=array(); // Temporary arry for storing tag names

		// The main part. This is MY CREATION
		// and this piece of code makes this script simple :)
		// This is the "MAGIC LOOP" !! :)

		for($n=0;$n<=count($vals)-1;$n++) {
			if(trim($vals[$n][value])) {
				$ar[$vals[$n][tag]][count($ar[$vals[$n][tag]])]=$vals[$n][value];
				$ttags[$vals[$n][tag]]=$vals[$n][tag];
			}
		}

		// Array for storing all the tag names
		// This array will hold all the Tag names found in the XML document
		// eg: ("TITLE","LINK","AUTHOR","DOMAIN")..
		// Use this if you need it.

		$tags=array();

		// Extract and save the tag names to the array
		foreach($ttags as $tagi) { array_push($tags,$tagi); }

		return $ar;
	}
}

?>

Re: Writing a plugin - it works, but a second use doesn't

Posted: Fri May 28, 2004 3:07 pm
by garvinhicking
Hi Auz!
Auz wrote:The front page just displays the right-hand panel and, where the plugin would appear. the error "no element found".
Hm. Is that output available somewhere? s9y has no output string 'no element found' anywhere, so I guess it could come out of your plugin function or from PHP?

You may want to look at latest CVS version, a plugin 'serendipity_plugin_remoterss' does basically the same like you're proposing?

Regards,
Garvin.

Re: Writing a plugin - it works, but a second use doesn't

Posted: Fri May 28, 2004 3:31 pm
by Auz
garvinhicking wrote:Hi Auz!

Hm. Is that output available somewhere? s9y has no output string 'no element found' anywhere, so I guess it could come out of your plugin function or from PHP?
I add the plugin a second time... you can see the broken page at http://auzsoft.totalbiscuit.com/serendipity/
garvinhicking wrote:You may want to look at latest CVS version, a plugin 'serendipity_plugin_remoterss' does basically the same like you're proposing?
But then I'd have to pick something else to do to learn writing a plugin :)

Re: Writing a plugin - it works, but a second use doesn't

Posted: Fri May 28, 2004 4:17 pm
by garvinhicking
Auz wrote:But then I'd have to pick something else to do to learn writing a plugin :)
True. :-)

So, the first thing I'd suggest you to do is debug your plugin.

Comment out all parts of the plugin - contents of the generate_content() function first. And then always go and reload your browser and see if something has changed.

This way you can get to the core of the problem and see which line of your plugin creates the error. It definitely is a problem of your plugin's code, so you'll have to debug that - I guess the XML-parsing function can't find an element or something like that, so that's where I'd go search for first.

Re: Writing a plugin - it works, but a second use doesn't

Posted: Fri May 28, 2004 4:31 pm
by Auz
garvinhicking wrote:So, the first thing I'd suggest you to do is debug your plugin.

Comment out all parts of the plugin - contents of the generate_content() function first. And then always go and reload your browser and see if something has changed.
Alternatively, realise you forgot to cope with an empty url to parse... :oops: