4 * Simple RSSParser Class
5 * Based on Duncan Gough RSSParser class
6 * Copyleft Arnaud Fontaine
8 * See lib/plugin/RssFeed.php and lib/XmlParser.php
10 * The myth of RSS compatibility:
11 * http://diveintomark.org/archives/2004/02/04/incompatible-rss
15 This file is part of PhpWiki.
17 PhpWiki is free software; you can redistribute it and/or modify
18 it under the terms of the GNU General Public License as published by
19 the Free Software Foundation; either version 2 of the License, or
20 (at your option) any later version.
22 PhpWiki is distributed in the hope that it will be useful,
23 but WITHOUT ANY WARRANTY; without even the implied warranty of
24 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 GNU General Public License for more details.
27 You should have received a copy of the GNU General Public License
28 along with PhpWiki; if not, write to the Free Software
29 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
33 * 2004-04-09 16:30:50 rurban:
34 * added fsockopen allow_url_fopen = Off workaround
35 * 2004-04-12 20:04:12 rurban:
36 * fixes for IMAGE element (sf.net)
37 * 2005-04-10 11:17:35 rurban
38 * certain RSS dont contain <item> tags to describe the list of <items>
39 * http://ws.audioscrobbler.com/rdf/ for example
42 require_once('lib/XmlParser.php');
49 var $description = "";
50 var $inside_item = false;
51 var $list_items = false;
58 function tag_open($parser, $name, $attrs=''){
59 global $current_tag, $current_attrs;
62 $current_attrs = $attrs;
64 $this->inside_item = true;
65 elseif ($name == "ITEMS")
66 $this->list_items = true;
67 elseif ($name == "IMAGE")
68 $this->inside_item = true;
71 function tag_close($parser, $tagName, $attrs=''){
74 if ($tagName == "ITEM") {
75 if (empty($this->items)) {
76 $this->items = array();
77 $GLOBALS['rss_parser_items'] =& $this->items;
78 } elseif (!empty($this->items[0]['link']) and $this->items[0]['title'] == '') {
79 // override the initial <items> list with detailed <item>'s
80 $this->items = array();
81 $GLOBALS['rss_parser_items'] =& $this->items;
83 $this->items[] = array("title" => $this->item['TITLE'],
84 "description" => @$this->item['DESCRIPTION'],
85 "link" => $this->item['LINK']);
86 $this->item = array("TITLE" => "",
89 $this->inside_item = false;
90 } elseif ($tagName == "IMAGE") {
91 $this->item = array("TITLE" => "",
94 $this->inside_item = false;
95 } elseif ($tagName == "CHANNEL") {
96 $this->channel = array("title" => $this->title,
97 "description" => $this->description,
98 "link" => $this->link,
99 "date" => $this->date,
100 "divers" => $this->divers);
101 $GLOBALS['rss_parser_channel'] =& $this->channel;
103 $this->description = "";
107 } elseif ($tagName == "ITEMS") {
108 $GLOBALS['rss_parser_items'] =& $this->items;
109 $this->item = array("TITLE" => "",
112 $this->list_items = false;
116 function cdata($parser, $data){
117 global $current_tag, $current_attrs;
119 if ($this->inside_item) {
120 if (empty($this->item[$current_tag]))
121 $this->item[$current_tag] = '';
122 if ($current_tag == 'LINK') {
124 $this->item[$current_tag] = trim($data);
126 $this->item[$current_tag] .= trim($data);
128 } elseif ($this->list_items) {
129 if ($current_tag == 'RDF:LI') {
130 // FIXME: avoid duplicates. cdata called back 4x per RDF:LI
131 if ($this->items[count($this->items)-1]['link'] != @$current_attrs['RDF:RESOURCE'])
132 $this->items[] = array('link' => @$current_attrs['RDF:RESOURCE'],
136 switch ($current_tag) {
139 $this->title .= " " . trim($data);
143 $this->description .= trim($data);
147 $this->link = trim($data);
151 $this->date .= " " . trim($data);
154 $this->divers .= " " . $current_tag."/".$data;
160 function parse($content, $is_final = true) {
161 xml_parse($this->_parser, $content, $is_final) or
162 trigger_error(sprintf("XML error: %s at line %d",
163 xml_error_string(xml_get_error_code($this->_parser)),
164 xml_get_current_line_number($this->_parser)),
166 //OO workaround: parser object looses its params. we have to store them in globals
168 if (empty($this->items)) {
169 $this->items = @$GLOBALS['rss_parser_items'];
170 $this->channel = @$GLOBALS['rss_parser_channel'];
172 unset($GLOBALS['rss_parser_items']);
173 unset($GLOBALS['rss_parser_channel']);
183 // c-hanging-comment-ender-p: nil
184 // indent-tabs-mode: nil