Subversion Repositories Applications.papyrus

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
341 jpm 1
<?php
2
// vim: set expandtab tabstop=4 shiftwidth=4 fdm=marker:
3
// +----------------------------------------------------------------------+
4
// | PHP Version 4                                                        |
5
// +----------------------------------------------------------------------+
6
// | Copyright (c) 1997-2003 The PHP Group                                |
7
// +----------------------------------------------------------------------+
8
// | This source file is subject to version 2.02 of the PHP license,      |
9
// | that is bundled with this package in the file LICENSE, and is        |
10
// | available at through the world-wide-web at                           |
11
// | http://www.php.net/license/2_02.txt.                                 |
12
// | If you did not receive a copy of the PHP license and are unable to   |
13
// | obtain it through the world-wide-web, please send a note to          |
14
// | license@php.net so we can mail you a copy immediately.               |
15
// +----------------------------------------------------------------------+
16
// | Authors: Martin Jansen <mj@php.net>                                  |
17
// |                                                                      |
18
// +----------------------------------------------------------------------+
19
//
20
// $Id: RSS.php,v 1.1 2005-04-18 16:13:31 jpm Exp $
21
//
22
 
23
require_once 'XML/Parser.php';
24
 
25
/**
26
* RSS parser class.
27
*
28
* This class is a parser for Resource Description Framework (RDF) Site
29
* Summary (RSS) documents. For more information on RSS see the
30
* website of the RSS working group (http://www.purl.org/rss/).
31
*
32
* @author Martin Jansen <mj@php.net>
33
* @version $Revision: 1.1 $
34
* @access  public
35
*/
36
class XML_RSS extends XML_Parser
37
{
38
    // {{{ properties
39
 
40
    /**
41
     * @var string
42
     */
43
    var $insideTag = '';
44
 
45
    /**
46
     * @var string
47
     */
48
    var $activeTag = '';
49
 
50
    /**
51
     * @var array
52
     */
53
    var $channel = array();
54
 
55
    /**
56
     * @var array
57
     */
58
    var $items = array();
59
 
60
    /**
61
     * @var array
62
     */
63
    var $item = array();
64
 
65
    /**
66
     * @var array
67
     */
68
    var $image = array();
69
 
70
    /**
71
     * @var array
72
     */
73
    var $textinput = array();
74
 
75
    /**
76
     * @var array
77
     */
78
    var $textinputs = array();
79
 
80
    /**
81
     * @var array
82
     */
83
    var $parentTags = array('CHANNEL', 'ITEM', 'IMAGE', 'TEXTINPUT');
84
 
85
    /**
86
     * @var array
87
     */
88
    var $channelTags = array('TITLE', 'LINK', 'DESCRIPTION', 'IMAGE',
89
                              'ITEMS', 'TEXTINPUT');
90
 
91
    /**
92
     * @var array
93
     */
94
    var $itemTags = array('TITLE', 'LINK', 'DESCRIPTION', 'PUBDATE');
95
 
96
    /**
97
     * @var array
98
     */
99
    var $imageTags = array('TITLE', 'URL', 'LINK');
100
 
101
    var $textinputTags = array('TITLE', 'DESCRIPTION', 'NAME', 'LINK');
102
 
103
    /**
104
     * List of allowed module tags
105
     *
106
     * Currently Dublin Core Metadata and the blogChannel RSS module
107
     * are supported.
108
     *
109
     * @var array
110
     */
111
    var $moduleTags = array('DC:TITLE', 'DC:CREATOR', 'DC:SUBJECT', 'DC:DESCRIPTION',
112
                            'DC:PUBLISHER', 'DC:CONTRIBUTOR', 'DC:DATE', 'DC:TYPE',
113
                            'DC:FORMAT', 'DC:IDENTIFIER', 'DC:SOURCE', 'DC:LANGUAGE',
114
                            'DC:RELATION', 'DC:COVERAGE', 'DC:RIGHTS',
115
                            'BLOGCHANNEL:BLOGROLL', 'BLOGCHANNEL:MYSUBSCRIPTIONS',
116
                            'BLOGCHANNEL:MYSUBSCRIPTIONS', 'BLOGCHANNEL:CHANGES');
117
 
118
    // }}}
119
    // {{{ Constructor
120
 
121
    /**
122
     * Constructor
123
     *
124
     * @access public
125
     * @param mixed File pointer or name of the RDF file.
126
     * @return void
127
     */
128
    function XML_RSS($handle = '')
129
    {
130
        $this->XML_Parser();
131
 
132
        if (@is_resource($handle)) {
133
            $this->setInput($handle);
134
        } elseif ($handle != '') {
135
            $this->setInputFile($handle);
136
        } else {
137
            $this->raiseError('No filename passed.');
138
        }
139
    }
140
 
141
    // }}}
142
    // {{{ startHandler()
143
 
144
    /**
145
     * Start element handler for XML parser
146
     *
147
     * @access private
148
     * @param  object XML parser object
149
     * @param  string XML element
150
     * @param  array  Attributes of XML tag
151
     * @return void
152
     */
153
    function startHandler($parser, $element, $attribs)
154
    {
155
        switch ($element) {
156
            case 'CHANNEL':
157
            case 'ITEM':
158
            case 'IMAGE':
159
            case 'TEXTINPUT':
160
                $this->insideTag = $element;
161
                break;
162
 
163
            default:
164
                $this->activeTag = $element;
165
        }
166
    }
167
 
168
    // }}}
169
    // {{{ endHandler()
170
 
171
    /**
172
     * End element handler for XML parser
173
     *
174
     * If the end of <item>, <channel>, <image> or <textinput>
175
     * is reached, this function updates the structure array
176
     * $this->struct[] and adds the field "type" to this array,
177
     * that defines the type of the current field.
178
     *
179
     * @access private
180
     * @param  object XML parser object
181
     * @param  string
182
     * @return void
183
     */
184
    function endHandler($parser, $element)
185
    {
186
        if ($element == $this->insideTag) {
187
            $this->insideTag = '';
188
            $this->struct[] = array_merge(array('type' => strtolower($element)),
189
                                          $this->last);
190
        }
191
 
192
        if ($element == 'ITEM') {
193
            $this->items[] = $this->item;
194
            $this->item = '';
195
        }
196
 
197
        if ($element == 'IMAGE') {
198
            $this->images[] = $this->image;
199
            $this->image = '';
200
        }
201
 
202
        if ($element == 'TEXTINPUT') {
203
            $this->textinputs = $this->textinput;
204
            $this->textinput = '';
205
        }
206
 
207
        $this->activeTag = '';
208
    }
209
 
210
    // }}}
211
    // {{{ cdataHandler()
212
 
213
    /**
214
     * Handler for character data
215
     *
216
     * @access private
217
     * @param  object XML parser object
218
     * @param  string CDATA
219
     * @return void
220
     */
221
    function cdataHandler($parser, $cdata)
222
    {
223
        if (in_array($this->insideTag, $this->parentTags)) {
224
            $tagName = strtolower($this->insideTag);
225
            $var = $this->{$tagName . 'Tags'};
226
 
227
            if (in_array($this->activeTag, $var) ||
228
                in_array($this->activeTag, $this->moduleTags)) {
229
                $this->_add($tagName, strtolower($this->activeTag),
230
                            $cdata);
231
            }
232
 
233
        }
234
    }
235
 
236
    // }}}
237
    // {{{ defaultHandler()
238
 
239
    /**
240
     * Default handler for XML parser
241
     *
242
     * @access private
243
     * @param  object XML parser object
244
     * @param  string CDATA
245
     * @return void
246
     */
247
    function defaultHandler($parser, $cdata)
248
    {
249
        return;
250
    }
251
 
252
    // }}}
253
    // {{{ _add()
254
 
255
    /**
256
     * Add element to internal result sets
257
     *
258
     * @access private
259
     * @param  string Name of the result set
260
     * @param  string Fieldname
261
     * @param  string Value
262
     * @return void
263
     * @see    cdataHandler
264
     */
265
    function _add($type, $field, $value)
266
    {
267
        if (empty($this->{$type}) || empty($this->{$type}[$field])) {
268
            $this->{$type}[$field] = $value;
269
        } else {
270
            $this->{$type}[$field] .= $value;
271
        }
272
 
273
        $this->last = $this->{$type};
274
    }
275
 
276
    // }}}
277
    // {{{ getStructure()
278
 
279
    /**
280
     * Get complete structure of RSS file
281
     *
282
     * @access public
283
     * @return array
284
     */
285
    function getStructure()
286
    {
287
        return (array)$this->struct;
288
    }
289
 
290
    // }}}
291
    // {{{ getchannelInfo()
292
 
293
    /**
294
     * Get general information about current channel
295
     *
296
     * This function returns an array containing the information
297
     * that has been extracted from the <channel>-tag while parsing
298
     * the RSS file.
299
     *
300
     * @access public
301
     * @return array
302
     */
303
    function getChannelInfo()
304
    {
305
        return (array)$this->channel;
306
    }
307
 
308
    // }}}
309
    // {{{ getItems()
310
 
311
    /**
312
     * Get items from RSS file
313
     *
314
     * This function returns an array containing the set of items
315
     * that are provided by the RSS file.
316
     *
317
     * @access public
318
     * @return array
319
     */
320
    function getItems()
321
    {
322
        return (array)$this->items;
323
    }
324
 
325
    // }}}
326
    // {{{ getImages()
327
 
328
    /**
329
     * Get images from RSS file
330
     *
331
     * This function returns an array containing the set of images
332
     * that are provided by the RSS file.
333
     *
334
     * @access public
335
     * @return array
336
     */
337
    function getImages()
338
    {
339
        return (array)$this->images;
340
    }
341
 
342
    // }}}
343
    // {{{ getTextinputs()
344
 
345
    /**
346
     * Get text input fields from RSS file
347
     *
348
     * @access public
349
     * @return array
350
     */
351
    function getTextinputs()
352
    {
353
        return (array)$this->textinputs;
354
    }
355
 
356
    // }}}
357
 
358
}
359
?>