[Phpslash-commit] CVS: phpslash-ft/class Block_render_rss.class,1.1,1.2
Brought to you by:
joestewart,
nhruby
|
From: Joe S. <joe...@us...> - 2002-01-23 17:26:41
|
Update of /cvsroot/phpslash/phpslash-ft/class
In directory usw-pr-cvs1:/tmp/cvs-serv1307/phpslash-ft/class
Modified Files:
Block_render_rss.class
Log Message:
Patch [ #506128 ] Rewrite of RSS parser to use templates
Index: Block_render_rss.class
===================================================================
RCS file: /cvsroot/phpslash/phpslash-ft/class/Block_render_rss.class,v
retrieving revision 1.1
retrieving revision 1.2
diff -C2 -d -r1.1 -r1.2
*** Block_render_rss.class 2001/05/15 22:36:15 1.1
--- Block_render_rss.class 2002/01/23 17:26:37 1.2
***************
*** 1,221 ****
! <?php
!
! /* Block_render_rss.class -> Methods for parsing RSS 0.91 data */
! /* Id:$ */
!
! class rdfparser {
! var $xml;
! var $current;
! var $count;
! var $initem = false;
! var $error_str = ""; /* if you got an error reading a file
! this will store an error */
!
! var $item_element;
! var $title_element;
! var $link_element;
!
! var $titlelist;
! var $linklist;
!
! /***************************************
! ** Constructor function. Creates the xml
! ** parser.
! ***************************************/
! function rdfparser() {
! // standard rdf setup
! $this->item_element = "item";
! $this->title_element = "title";
! $this->link_element = "link";
!
! $this->count = 0;
!
! $this->titlelist = array();
! $this->linklist = array();
!
! }
!
! /***************************************
! ** Accessor function for $data
! ***************************************/
! function get_data() {
! debug("rdf::get_data", $this_data);
! return $this->data;
! }
!
! /***************************************
! ** Start element function.
! ***************************************/
! function start_element($xml, $element, $attributes) {
!
! if (!strcasecmp($element, $this->item_element)) {
! $this->initem = true;
! } elseif (!strcasecmp($element, $this->title_element)) {
! $this->current="title";
! } elseif (!strcasecmp($element, $this->link_element)) {
! $this->current="link";
! }
! }
!
! /***************************************
! ** End element function.
! ***************************************/
! function end_element($xml, $element) {
!
! if (!strcasecmp($element, $this->item_element)) {
! $this->count++;
! $this->initem = false;
! } elseif (!strcasecmp($element, $this->title_element)) {
! $this->current="";
! } elseif (!strcasecmp($element, $this->link_element)) {
! $this->current="";
! }
! }
!
! function character_data($xml, $data) {
! if ($this->initem) {
! if ($this->current == "title") {
! $this->titlelist[$this->count] .= $data;
! }
! elseif ($this->current == "link") {
! $this->linklist[$this->count] .= $data;
! }
! }
! }
!
! function get_titles() {
! return $this->titlelist;
! }
!
! function get_links() {
! return $this->linklist;
! }
!
! function parse_setup($story, $title, $url) {
! debug("rdfparser::parse_setup", "init and object reset");
!
! $this->item_element = $story;
! $this->title_element = $title;
! $this->link_element = $url;
!
! $this->xml = xml_parser_create();
! xml_set_object($this->xml, &$this);
!
! $this->error_str = "";
! $this->titlelist = array();
! $this->linklist = array();
! $this->count = 0;
! $initem = false;
! }
!
! function parse_file($url) {
! debug("rdfparser::parse_file", "Now parsing $url");
!
! xml_parser_set_option($this->xml, XML_OPTION_CASE_FOLDING, true);
! xml_set_element_handler($this->xml, "start_element", "end_element");
! xml_set_character_data_handler($this->xml, "character_data");
!
! $fpread = fopen($url, "r");
! if ( !($fpread) ) {
! // error in opening url
! $this->error_str = "rdfparser::parse_file could not open $url";
! debug("rdfparser::parse_file", $this->error_str);
!
! // Where does $err* come from?
! debug("rdfparser::parse_file", "$errstr $errno");
!
! } else {
! while (($this->error_str == "") && ($data = fread($fpread, 4096))) {
! if (!xml_parse($this->xml, $data, feof($fpread))) {
! // error
! $this->error_str = xml_error_string(xml_get_error_code($this->xml)) .
! " at line " . xml_get_current_line_number($this->xml);
! debug("rdfparser::parse_file", $this->error_str);
! }
! }
! fclose($fpread);
! if ($this->error_str == "")
! debug("rdfparser::parse_file", "Data parsed successfully!");
! }
! }
! }
!
! class Block_render_rss {
!
! var $type;
! var $output;
! var $rdf;
!
! /* constructor */
! function Block_render_rss() {
!
! $this->type = "rss"; /* set the 'type' */
! $this->output = ""; /* clear the output */
! $this->rdf = new rdfparser;
! }
!
! /* returns the TYPE of this class */
! function getType() {
! return $this->type;
! }
!
! function parse($block_info) {
! debug("Block_render_rss::parse", "Starting RDF Block parsing");
!
! $this->output = "";
!
! $link_prefix = " <font size=\"-1\">";
! $link_postfix = "</font><br>\n";
! $max_items = 10;
! $target = "_top";
!
! $item_element = "item";
! $title_element = "title";
! $link_element = "link";
!
! // End of customizations
! if (is_array($block_info[block_options])) {
! if (is_string($block_info["block_options"]["item_element"]))
! $item_element = $block_info["block_options"]["item_element"];
! if (is_string($block_info["block_options"]["title_element"]))
! $title_element = $block_info["block_options"]["title_element"];
! if (is_string($block_info["block_options"]["link_element"]))
! $link_element = $block_info["block_options"]["link_element"];
! if (is_string($block_info["block_options"]["max_items"]))
! $max_items = $block_info["block_options"]["max_items"];
! if (is_string($block_info["block_options"]["link_prefix"]))
! $link_prefix = $block_info["block_options"]["link_prefix"];
! if (is_string($block_info["block_options"]["link_postfix"]))
! $link_postfix = $block_info["block_options"]["link_postfix"];
!
! }
! $items = 0;
!
! debug("Block_render_rss::parse", "setting parser with $item_element, $title_element, $link_element");
! $this->rdf->parse_setup($item_element, $title_element, $link_element);
!
! $this->rdf->parse_file($block_info['source_url']);
!
! if ($this->rdf->error_str == "") {
! debug("Block_render_rss::parse", "xml has no errors, making block");
!
! $titles = $this->rdf->get_titles();
! $links = $this->rdf->get_links();
!
! while ($items < $max_items && $items < $this->rdf->count) {
! $this->output .= "$link_prefix<A HREF=\"" . trim($links[$items]) . "\" TARGET=\"$target\">" . trim($titles[$items]) . "</A>$link_postfix";
! ++$items;
! }
! } else {
! debug("Block_render_rss::parse", "Error: $rdf->error_str");
! $this->output = $this->rdf->error_str;
! }
! }
!
!
! function getFinal() {
! return $this->output;
! }
!
! }
! ?>
--- 1,326 ----
! <?php
!
! /* Block_render_rss.class -> Methods for parsing RSS 0.91 data */
! /* CHANGES: 4Jan02 - RSS title stored */
! /* $Id$ */
! /*
! * VARAIBLES USED
! * target = [_blank],_self etc
! * max = 10
! * tpl = alternate template ( no .tpl extension) [rssblock.tpl]
! *
! * NOTES
! * - image width and height are not used since most sites seem not to give this info anyway
! * - rdfparser ignores max_items - it pulls everything it can from the RDF file
! */
! class rdfparser {
!
! // Settings
! var $max_items;
! var $tags; // tags to process (all others are ignored)
!
! // Output information
! var $channel;
! var $image;
! var $items;
!
! var $error_str = ""; /* if you got an error reading a file this will store an error */
!
! // Private
! var $xml;
! var $node;
! var $current;
! var $count;
!
! /***************************************
! ** Constructor function. See parse_setup
! ***************************************/
! function rdfparser() {
! }
!
! /***************************************
! ** Start element function.
! ***************************************/
! function start_element($xml, $element, $attributes) {
!
! $this->current = $element;
!
! switch ($element) {
! case $this->tags["item"]:
! case $this->tags["image"]:
! case $this->tags["channel"]:
! $this->node = $element; // Should be a stack really, but nesting is only one deep
! // debug("rdf:start element+node","$element+".$this->node);
! break;
!
! default:
! }
!
! }
!
! /***************************************
! ** End element function.
! ***************************************/
! function end_element($xml, $element) {
! $this->current = "";
!
! switch ($element) {
! case $this->tags["item"]:
! $this->node = "";
! $this->count++;
! break;
! case $this->tags["channel"]:
! case $this->tags["image"]:
! $this->node = "";
! break;
! }
! }
!
! function character_data($xml, $data) {
!
! switch ($this->node) {
!
! case $this->tags["item"]:
! switch ($this->current) {
! case $this->tags["title"]:
! $this->items[$this->count]["title"] .= $data;
! break;
! case $this->tags["link"]:
! $this->items[$this->count]["link"] .= $data;
! break;
! case $this->tags["description"]:
! $this->items[$this->count]["description"] .= $data;
! break;
! default:
! }
! break;
!
! case $this->tags["image"]:
! switch ($this->current) {
! case $this->tags["title"]:
! $this->image["title"] .= $data;
! break;
! case $this->tags["link"]:
! $this->image["link"] .= $data;
! break;
! case $this->tags["url"]:
! $this->image["url"] .= $data;
! break;
! default:
! }
! break;
!
! case $this->tags["channel"]:
! switch ($this->current) {
! case $this->tags["title"]:
! $this->channel["title"] .= $data;
! break;
! case $this->tags["link"]:
! $this->channel["link"] .= $data;
! break;
! case $this->tags["description"]:
! $this->channel["description"] .= $data;
! break;
! default:
! }
! break;
!
! default:
! }
! }
!
! function parse_setup($tags="",$max_items="") {
!
! if (is_array($tags)) {
! $this->tags = $tags;
! } else {
! // standard rdf setup - must be upper case (assuming case-folding is on which is PHP's default)
! $this->tags = array("channel" => "CHANNEL",
! "image" => "IMAGE",
! "item" => "ITEM",
! "title" => "TITLE",
! "link" => "LINK",
! "url" => "URL",
! "description" => "DESCRIPTION" );
! }
!
! // Internal initialisation
! $this->count = 0;
! $this->node = ""; // can be "" (ie channel), "item" or "image"
! $this->current = array(); // element stack
!
! $this->error_str = "";
!
! $this->channel = array("title"=>"", "link"=>"", "description" => "");
! $this->image = array("title"=>"", "link"=>"");
! $this->items = array();
!
! $this->xml = xml_parser_create();
! xml_set_object($this->xml, &$this);
! }
!
! function parse_file($url) {
! // debug("rdfparser::parse_file", "Now parsing $url");
!
! xml_parser_set_option($this->xml, XML_OPTION_CASE_FOLDING, true);
! xml_set_element_handler($this->xml, "start_element", "end_element");
! xml_set_character_data_handler($this->xml, "character_data");
!
! $fpread = fopen($url, "r");
! if ( !($fpread) ) {
! // error in opening url
! $this->error_str = "rdfparser::parse_file could not open $url";
! // debug("rdfparser::parse_file", $this->error_str);
!
! // Where does $err* come from?
! // debug("rdfparser::parse_file", "$errstr $errno");
! } else {
! while (($this->error_str == "") && ($data = fread($fpread, 4096))) {
! if (!xml_parse($this->xml, $data, feof($fpread))) {
! // error
! $this->error_str = xml_error_string(xml_get_error_code($this->xml)) . " at line " . xml_get_current_line_number($this->xml);
! debug("rdfparser::parse_file", $this->error_str);
! }
! }
! fclose($fpread);
! if ($this->error_str == "") {
! // debug("rdfparser::parse_file", "Data parsed successfully!");
! }
! }
! }
! }
!
! class Block_render_rss {
! var $type;
! var $output;
! var $rdf;
!
! /* constructor */
! function Block_render_rss() {
! global $_PSL;
!
! $this->type = "rss"; /* set the 'type' */
! $this->output = ""; /* clear the output */
! $this->rdf = new rdfparser;
! $this->psl = $_PSL;
! }
!
! /* returns the TYPE of this class */
! function getType() {
! return $this->type;
! }
!
! function parse($block_info) {
! global $_PSL;
! // debug("Block_render_rss::parse", "Starting RDF Block parsing");
!
! $default_tpl = "rssblock.tpl";
!
! $target = "_self";
! $max_items = 10;
! $tpl = $default_tpl;
!
! if (is_array($block_info[block_options])) {
! if (is_string($block_info["block_options"]["max_items"])) {
! $max_items = $block_info["block_options"]["max_items"];
! }
! if (is_string($block_info["block_options"]["target"])) {
! $target = $block_info["block_options"]["target"];
! }
! if (is_string($block_info["block_options"]["tpl"])) {
! if (@file_exists($this->psl[templatedir] . "/" . basename($block_info[block_options][tpl]) . ".tpl") ) {
! // should be secure as path directives are discarded and extension added
! $tpl = basename($block_info["block_options"]["tpl"]) . ".tpl";
! }
! }
! }
!
! // debug("Block_render_rss::parse", "setting parser with $item_element, $title_element, $link_element");
! $this->rdf->parse_setup(); //$item_element, $title_element, $link_element);
! $this->rdf->parse_file($block_info['source_url']);
!
! if (empty($this->rdf->error_str)) {
! // debug("Block_render_rss::parse", "xml has no errors, making block");
! // debug("rss::parse::channel",$this->rdf->channel);
! // debug("rss::parse::image",$this->rdf->image);
! // debug("rss::parse::items", $this->rdf->items);
!
! $template = new Template($this->psl["templatedir"]);
! $template->set_file ("block", $tpl);
!
! $template->set_block ("block", "channel_title","title_block");
! $template->set_block ("block", "channel_image","image_block");
! $template->set_block ("block", "channel_description","channeldescription_block");
! $template->set_block ("block", "each_query", "query_block");
! $template->set_block ("block", "each_description", "description_block");
!
! // <title> and <image> tags seem to hold identical description and link info in most sites
! // ... so use an image if possible, else show title
! if (empty($this->rdf->image["url"])) {
! if ( !empty( $this->rdf->channel["title"]) ) {
! $template->set_var("CHANNEL_TITLE", $this->rdf->channel["title"]);
! $template->set_var("CHANNEL_LINK", empty($this->rdf->channel["link"])?"#":$this->rdf->channel["link"]);
! $template->parse("title_block","channel_title");
! }
! } else {
! $template->set_var(array(
! "IMAGE_URL" => $this->rdf->image["url"],
! "IMAGE_TITLE" => $this->rdf->image["title"],
! "IMAGE_LINK" => $this->rdf->image["link"],
! ));
! $template->parse("image_block","channel_image");
! }
!
! if ( !empty( $this->rdf->channel["description"]) ) {
! $template->set_var("CHANNEL_DESCRIPTION", $this->rdf->channel["description"]);
! $template->parse("channeldescription_block","channel_description",true);
! }
!
! $numitems = min($max_items, $this->rdf->count);
! // debug("rss::numitems",$numitems);
! for ($items = 0; $items < $numitems; $items++ ) {
! $template->set_var(array(
! "TITLE" => $this->rdf->items[$items]["title"],
! "LINK" => $this->rdf->items[$items]["link"]
! ));
! $template->parse ("query_block", "each_query", true);
!
! if (!empty($this->rdf->items[$items]["description"] )) {
! $template->set_var("DESCRIPTION",$this->rdf->items[$items]["description"] );
! $template->parse ("query_block", "each_description", true);
! }
! }
!
! $template->set_var(array(
! "TARGET" => $target,
! "ROOTDIR" => $this->psl["rooturl"],
! "IMAGEDIR" => $this->psl["imageurl"]
! ));
! $this->output = $template->parse(OUT,"block");
! $this->title = $this->rdf->channel["title"];
! // debug("rss::parse::title", $this->title);
!
! return true;
!
! } else {
!
! debug("Block_render_rss::parse", "Error: $this->rdf->error_str");
! $this->output = $this->rdf->error_str;
!
! return false;
! }
!
! }
!
! function getFinal() {
! return $this->output;
! }
!
! function getTitle() {
! // $this->title = "RSS Channel";
! // debug("rss::parse::getTitle", $this->title);
! return $this->title;
! }
! }
!
! ?>
|