]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/RssParser.php
updated RssParser for XmlParser quirks (store parser object params in globals)
[SourceForge/phpwiki.git] / lib / RssParser.php
1 <?php // -*-php-*-
2 rcs_id('$Id: RssParser.php,v 1.8 2004-06-08 21:03:20 rurban Exp $');
3 /**
4  * Simple RSSParser Class
5  * Based on Duncan Gough RSSParser class
6  * Copyleft Arnaud Fontaine
7  * Licence : GPL
8  * See lib/plugin/RssFeed.php and lib/XmlParser.php
9  */
10
11 /*
12  This file is part of PhpWiki.
13
14  PhpWiki is free software; you can redistribute it and/or modify
15  it under the terms of the GNU General Public License as published by
16  the Free Software Foundation; either version 2 of the License, or
17  (at your option) any later version.
18
19  PhpWiki is distributed in the hope that it will be useful,
20  but WITHOUT ANY WARRANTY; without even the implied warranty of
21  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22  GNU General Public License for more details.
23
24  You should have received a copy of the GNU General Public License
25  along with PhpWiki; if not, write to the Free Software
26  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
27  */
28
29 /**
30  * 2004-04-09 16:30:50 rurban: 
31  *   added fsockopen allow_url_fopen = Off workaround
32  * 2004-04-12 20:04:12 rurban: 
33  *   fixes for IMAGE element (sf.net)
34  */
35
36 require_once('lib/XmlParser.php');
37
38 class RSSParser 
39 extends XmlParser {
40
41     var $title = "";
42     var $link  = "";
43     var $description = "";
44     var $inside_item = false;
45     var $item  = array();
46     var $items;
47     var $channel;
48     var $divers = "";
49     var $date = "";
50
51     function tag_open($parser, $name, $attrs=''){
52         global $current_tag;
53
54         $current_tag = $name;
55         if ($name == "ITEM")
56             $this->inside_item = true;
57         elseif ($name == "IMAGE")
58             $this->inside_item = true;
59     }
60
61     function tag_close($parser, $tagName, $attrs=''){
62         global $current_tag;
63
64         if ($tagName == "ITEM") {
65             if (empty($this->items)) {
66                 $this->items = array(); 
67                 $GLOBALS['rss_parser_items'] =& $this->items;
68             }
69             $this->items[] = array("title"       => $this->item['TITLE'],
70                                    "description" => $this->item['DESCRIPTION'],
71                                    "link"        => $this->item['LINK']);
72             $this->item['TITLE']       = "";
73             $this->item['DESCRIPTION'] = "";
74             $this->item['LINK']        = "";
75             $this->inside_item = false;
76         } elseif ($tagName == "IMAGE") {
77             $this->item['TITLE']       = "";
78             $this->item['DESCRIPTION'] = "";
79             $this->item['LINK']        = "";
80             $this->inside_item = false;
81         } elseif ($tagName == "CHANNEL") {
82             $this->channel = array("title" => $this->title,
83                                    "description" => $this->description,
84                                    "link" => $this->link,
85                                    "date" => $this->date,
86                                    "divers" => $this->divers);
87             $GLOBALS['rss_parser_channel'] =& $this->channel;
88             $this->title       = "";
89             $this->description = "";
90             $this->link        = "";
91             $this->divers      = "";
92             $this->date        = "";
93         }
94     }
95
96     function cdata($parser, $data){
97         global $current_tag;
98
99         if ($this->inside_item) {
100             if (empty($this->item[$current_tag]))
101                 $this->item[$current_tag] = '';
102             if ($current_tag == 'LINK') {
103                 if (trim($data))
104                     $this->item[$current_tag] = trim($data);
105             } else {
106                 $this->item[$current_tag] .= trim($data);
107             }
108         } else {
109             switch ($current_tag) {
110             case "TITLE":
111                 if (trim($data))
112                     $this->title .= " " . trim($data);
113                 break;
114             case "DESCRIPTION":
115                 if (trim($data))
116                     $this->description .= trim($data);
117                 break;
118             case "LINK":
119                 if (trim($data))
120                     $this->link = trim($data);
121                 break;
122             case "DC:DATE":
123                 if (trim($data))
124                     $this->date .= " " . trim($data);
125             default:
126                 if (trim($data))
127                     $this->divers .= " " . $current_tag."/".$data;
128                 break;
129             }
130         }
131     } // characterData
132     
133     function parse($content, $is_final = true) {
134         xml_parse($this->_parser, $content, $is_final) or 
135             trigger_error(sprintf("XML error: %s at line %d", 
136                                   xml_error_string(xml_get_error_code($this->_parser)), 
137                                   xml_get_current_line_number($this->_parser)),
138                           E_USER_WARNING);
139         //OO workaround: parser object looses its params. we have to store them in globals
140         if (empty($this->items)) {
141             $this->items = $GLOBALS['rss_parser_items'];
142             $this->channel = $GLOBALS['rss_parser_channel'];
143         }
144         unset($GLOBALS['rss_parser_items']);
145         unset($GLOBALS['rss_parser_channel']);
146     }
147 }
148
149 // $Log: not supported by cvs2svn $
150 // Revision 1.7  2004/05/24 17:31:31  rurban
151 // new XmlParser and HtmlParser, RssParser based on that.
152 //
153 // Revision 1.6  2004/05/18 16:18:36  rurban
154 // AutoSplit at subpage seperators
155 // RssFeed stability fix for empty feeds or broken connections
156 //
157 // Revision 1.5  2004/04/26 20:44:34  rurban
158 // locking table specific for better databases
159 //
160 // Revision 1.4  2004/04/18 01:11:51  rurban
161 // more numeric pagename fixes.
162 // fixed action=upload with merge conflict warnings.
163 // charset changed from constant to global (dynamic utf-8 switching)
164 //
165
166 // For emacs users
167 // Local Variables:
168 // mode: php
169 // tab-width: 8
170 // c-basic-offset: 4
171 // c-hanging-comment-ender-p: nil
172 // indent-tabs-mode: nil
173 // End:
174 ?>