Re: [PATCH] allow XML_Parser & XML_RSS to support string inputs
| From: | Alan Knowles | Date: | Wed, 30 Jul 2003 01:10:06 +0000 |
| Subject: | Re: [PATCH] allow XML_Parser & XML_RSS to support string inputs | ||
| References: | 1 | Groups: | php.pear.dev |
| Request: | Send a blank email to pear-dev+get-18916@lists.php.net to get a copy of this message | ||
Marshall,
I cant see anything wrong with this - It's probably best to post it as a bug on bugs.php.net - that way it's traceable and wont get forgot.
Regards
Alan
[Note: this is my first time submitting a patch (and making a diff), so please let me know if I did anything wrong] Problem: I am caching RSS feeds in a database. There currently is no way to use XML_RSS on these feeds without having to write them to a temporary file. Fix: Changes are needed to both XML_Parser and XML_RSS. XML_Parser: I didn't take anything out of setInput(). If a resource is passed to setInput(), it works exactly as it does currently. If a non-resource (i.e. string) is passed, it checks to see if it has a scheme (http://, ftp://, etc) and if so, uses the existing setInputFile() to create the resource. If it isn't an absolute URL, it checks to see if the file exists locally. If so, setInputFile() opens it. Finally, if it's a string, it just passes the value through. The changes to parse() are also very minor. Instead of having parseString() executed each time a block of the file is read, the whole file is read into $data first. If $this->fp is a string, it is assigned straight to $data. Either way, $data ends up as the XML for the feed, which is then processed all at once. XML_RSS: Because setInput() checks whether the input is a resource, XML_RSS() doesn't need to do it anymore, so that code is removed. -- Marshall Roch ------------------------------------------------------------------------ --- orig/Parser.php 2003-07-26 12:37:00.000000000 -0400 +++ new/Parser.php 2003-07-27 19:08:00.000000000 -0400 @@ -213,8 +213,21 @@-- Can you help out? Need Consulting Services or Know of a Job? http://www.akbkhome.com$this->fp = $fp; return true; } + // see if it's an absolute URL (has a scheme at the beginning) + elseif (eregi('://', substr($fp, 0, 10))) { + return $this->setInputFile($fp); + } + // see if it's a local file + elseif (file_exists($fp)) { + return $this->setInputFile($fp); + } + // it must be a string + else { + $this->fp = $fp; + return true; + } - return $this->raiseError("not a file resource"); + return $this->raiseError("not able to find feed"); } // }}}@@ -230,21 +243,27 @@*/ function parse() { - if (!is_resource($this->fp)) { - return $this->raiseError("no input"); - } - while ($data = fread($this->fp, 2048)) {-- $err = $this->parseString($data, feof($this->fp)); - if (PEAR::isError($err)) { - fclose($this->fp); - return $err; + // if $this->fp was fopened previously + if (is_resource($this->fp)) { + // write entire file to $data + while (!feof($this->fp)) { + $data .= fread($this->fp, 2048); }-+ fclose($this->fp); + // otherwise, $this->fp must be a string + } else { + $data = $this->fp; } - fclose($this->fp); + // This allows outside scripts to grab downloaded + // feed data and cache it + $this->data = $data;++ $err = $this->parseString($data, true); + if (PEAR::isError($err)) { + return $err; + } return true; }------------------------------------------------------------------------ --- orig/RSS.php 2003-07-26 12:45:00.000000000 -0400 +++ new/RSS.php 2003-07-27 19:20:00.000000000 -0400 @@ -129,11 +129,9 @@{ $this->XML_Parser(); - if (@is_resource($handle)) { - $this->setInput($handle); - } elseif ($handle != '') { - $this->setInputFile($handle); - } else { + $this->setInput($handle);++ if ($handle == '') { $this->raiseError('No filename passed.'); } }