File: RssParser.php

package info (click to toggle)
phpwiki 1.3.14-3
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 15,716 kB
  • ctags: 23,548
  • sloc: php: 88,295; sql: 1,476; sh: 1,378; perl: 765; makefile: 602; awk: 28
file content (216 lines) | stat: -rwxr-xr-x 7,766 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
<?php // -*-php-*-
rcs_id('$Id: RssParser.php,v 1.11 2005/04/10 10:24:58 rurban Exp $');
/**
 * Simple RSSParser Class
 * Based on Duncan Gough RSSParser class
 * Copyleft Arnaud Fontaine
 * Licence : GPL
 * See lib/plugin/RssFeed.php and lib/XmlParser.php
 *
 * The myth of RSS compatibility:
 *   http://diveintomark.org/archives/2004/02/04/incompatible-rss
 */

/*
 This file is part of PhpWiki.

 PhpWiki is free software; you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation; either version 2 of the License, or
 (at your option) any later version.

 PhpWiki is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with PhpWiki; if not, write to the Free Software
 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

/**
 * 2004-04-09 16:30:50 rurban: 
 *   added fsockopen allow_url_fopen = Off workaround
 * 2004-04-12 20:04:12 rurban: 
 *   fixes for IMAGE element (sf.net)
 * 2005-04-10 11:17:35 rurban
 *   certain RSS dont contain <item> tags to describe the list of <items>
 *     http://ws.audioscrobbler.com/rdf/ for example
 */

require_once('lib/XmlParser.php');

class RSSParser 
extends XmlParser {

    var $title = "";
    var $link  = "";
    var $description = "";
    var $inside_item = false;
    var $list_items = false;
    var $item  = array();
    var $items;
    var $channel;
    var $divers = "";
    var $date = "";

    function tag_open($parser, $name, $attrs=''){
        global $current_tag, $current_attrs;

        $current_tag = $name;
        $current_attrs = $attrs;
        if ($name == "ITEM")
            $this->inside_item = true;
        elseif ($name == "ITEMS")
            $this->list_items = true;
        elseif ($name == "IMAGE")
            $this->inside_item = true;
    }

    function tag_close($parser, $tagName, $attrs=''){
        global $current_tag;

        if ($tagName == "ITEM") {
            if (empty($this->items)) {
                $this->items = array();	
                $GLOBALS['rss_parser_items'] =& $this->items;
            } elseif (!empty($this->items[0]['link']) and $this->items[0]['title'] == '') {
            	// override the initial <items> list with detailed <item>'s
                $this->items = array();
                $GLOBALS['rss_parser_items'] =& $this->items;
            }
            $this->items[] = array("title"       => $this->item['TITLE'],
                                   "description" => @$this->item['DESCRIPTION'],
                                   "link"        => $this->item['LINK']);
            $this->item = array("TITLE"       => "",
                                "DESCRIPTION" => "",
                                "LINK"        => "");
            $this->inside_item = false;
        } elseif ($tagName == "IMAGE") {
            $this->item = array("TITLE"       => "",
                                "DESCRIPTION" => "",
                                "LINK"        => "");
            $this->inside_item = false;
        } elseif ($tagName == "CHANNEL") {
            $this->channel = array("title" => $this->title,
                                   "description" => $this->description,
                                   "link" => $this->link,
                                   "date" => $this->date,
                                   "divers" => $this->divers);
            $GLOBALS['rss_parser_channel'] =& $this->channel;
            $this->title       = "";
            $this->description = "";
            $this->link        = "";
            $this->divers      = "";
            $this->date        = "";
        } elseif ($tagName == "ITEMS") {
            $GLOBALS['rss_parser_items'] =& $this->items;
            $this->item = array("TITLE"       => "",
                                "DESCRIPTION" => "",
                                "LINK"        => "");
            $this->list_items = false;
        }
    }

    function cdata($parser, $data){
        global $current_tag, $current_attrs;

        if ($this->inside_item) {
            if (empty($this->item[$current_tag]))
                $this->item[$current_tag] = '';
            if ($current_tag == 'LINK') {
            	if (trim($data))
            	    $this->item[$current_tag] = trim($data);
            } else {
                $this->item[$current_tag] .= trim($data);
            }
        } elseif ($this->list_items) {
            if ($current_tag == 'RDF:LI') {
            	// FIXME: avoid duplicates. cdata called back 4x per RDF:LI
            	if ($this->items[count($this->items)-1]['link'] != @$current_attrs['RDF:RESOURCE'])
                    $this->items[] = array('link' => @$current_attrs['RDF:RESOURCE'],
                                           'title' => '');
            }
        } else {
            switch ($current_tag) {
            case "TITLE":
                if (trim($data))
                    $this->title .= " " . trim($data);
                break;
            case "DESCRIPTION":
                if (trim($data))
                    $this->description .= trim($data);
                break;
            case "LINK":
                if (trim($data))
                    $this->link = trim($data);
                break;
            case "DC:DATE":
                if (trim($data))
                    $this->date .= " " . trim($data);
            default:
                if (trim($data))
                    $this->divers .= " " . $current_tag."/".$data;
                break;
            }
        }
    }
    
    function parse($content, $is_final = true) {
        xml_parse($this->_parser, $content, $is_final) or 
            trigger_error(sprintf("XML error: %s at line %d", 
                                  xml_error_string(xml_get_error_code($this->_parser)), 
                                  xml_get_current_line_number($this->_parser)),
                          E_USER_WARNING);
        //OO workaround: parser object looses its params. we have to store them in globals
        if ($is_final) {
    	    if (empty($this->items)) {
                $this->items   = @$GLOBALS['rss_parser_items'];
                $this->channel = @$GLOBALS['rss_parser_channel'];
    	    }
    	    unset($GLOBALS['rss_parser_items']);
    	    unset($GLOBALS['rss_parser_channel']);
        }
    }
}

// $Log: RssParser.php,v $
// Revision 1.11  2005/04/10 10:24:58  rurban
// fix for RSS feeds without detailled <item> tags:
//   just list the <items> urls then (Bug #1180027)
//
// Revision 1.10  2005/01/22 11:45:09  rurban
// docs
//
// Revision 1.9  2004/06/08 21:12:02  rurban
// is_final fix for incremental parsing
//
// Revision 1.8  2004/06/08 21:03:20  rurban
// updated RssParser for XmlParser quirks (store parser object params in globals)
//
// Revision 1.7  2004/05/24 17:31:31  rurban
// new XmlParser and HtmlParser, RssParser based on that.
//
// Revision 1.6  2004/05/18 16:18:36  rurban
// AutoSplit at subpage seperators
// RssFeed stability fix for empty feeds or broken connections
//
// Revision 1.5  2004/04/26 20:44:34  rurban
// locking table specific for better databases
//
// Revision 1.4  2004/04/18 01:11:51  rurban
// more numeric pagename fixes.
// fixed action=upload with merge conflict warnings.
// charset changed from constant to global (dynamic utf-8 switching)
//

// For emacs users
// Local Variables:
// mode: php
// tab-width: 8
// c-basic-offset: 4
// c-hanging-comment-ender-p: nil
// indent-tabs-mode: nil
// End:
?>