feed_writer_library.php
1 <?php
2
/**
3  * Feed Writer Class
4  */
5
class Feed_Writer_Library {
6     
/**
7      * The feed version - rss20, rss10, rss092, atom
8      * 
9      * @var string 
10      */
11     
private $_format 'rss20';
12     
13     
/**
14      * The items in feed 'body'
15      *
16      * @var string 
17      */
18     
private $_items '';
19     
20     
/**
21      * The complete feed content ('head' + 'body')
22      *
23      * @var string 
24      */
25     
private $_feed '';
26     
27     
/**
28      * Constructor
29      */    
30     
public function __construct(array $config NULL) { 
31         if (
count($config) > 0) {
32             
$this->_format $config['format'];
33         }
34     }
35
36     
/**
37      * Build complete feed string from feed_data
38      *
39      * @param    array('body' => array(), 'head' => array())
40      * @return    the complete feed string
41      */
42     
public function write_feed(array $feed_data NULL) {
43         
// Build feed body
44         
foreach ($feed_data['body'] as $value) {
45             
$this->_add_item($value);
46         }
47         
// Build feed head then put head and body together
48         
$this->_assemble_feed($feed_data['head']) ;
49         
50         return 
$this->_feed;
51     }
52     
53     
/**
54      * Strip '&', '<', and '>' into XML entities
55      *
56      * @param    string
57      * @return    string
58      */
59     
private function _strip($s) {
60         
$s str_replace('&''&amp;'$s);
61         
$s str_replace('<''&lt;'$s);
62         
$s str_replace('>''&gt;'$s);
63         if (
$s == '') {
64             
$s ' ';
65         }
66         return 
$s;
67     }
68     
69     
/**
70      * Add each feed item or entry to feed 'body'
71      *
72      * @param    array('id', 'title', 'link', 'abstract', 'author', 'detail')
73      * @return    void
74      */    
75     
private function _add_item($a) {
76         switch (
$this->_format) {
77             case 
'rss092':
78                 
$item "\t" '<item>' "\n";
79                 
$item .= "\t\t" '<title>' $this->_strip($a['title']) . '</title>                ' "\n";
80                 
$item .= "\t\t" '<description><![CDATA[' $a['abstract'] . ']]></description>' "\n";
81                 
$item .= "\t\t" '<link>' $this->_strip($a['link']) . '</link>' "\n";
82                 
$item .= "\t" '</item>' "\n";
83                 break;
84             
85             case 
'rss10':
86                 
$item "\t" '<item rdf:about="' $this->_strip($a['link']) . '">' "\n";
87                 
$item .= "\t\t" '<title>' $this->_strip($a['title']) . '</title>' "\n";
88                 
$item .= "\t\t" '<dc:title>' $this->_strip($a['title']) . '</dc:title>' "\n";
89                 
$item .= "\t\t" '<description><![CDATA[' $a['abstract'] . ']]></description>' "\n";
90                 
$item .= "\t\t" '<link>' $this->_strip($a['link']) . '</link>' "\n";
91                 
$item .= "\t\t" '<dc:date>' substr($a['date'], 04) . '-' substr($a['date'], 42) . '-' substr($a['date'], 62) . ' ' substr($a['date'], 82) . ':' substr($a['date'], 102) . ':' substr($a['date'], 124) . '</dc:date>' "\n";
92                 
$item .= "\t\t" '<dc:creator>' $this->_strip($a['author']) . '</dc:creator>' "\n";
93                 
$item .= "\t" '</item>' "\n";
94                 break;
95             
96             case 
'rss20':
97                 
$item "\t" '<item>' "\n";
98                 
$item .= "\t\t" '<title>' $this->_strip($a['title']) . '</title>' "\n";
99                 
$item .= "\t\t" '<link>' $this->_strip($a['link']) . '</link>' "\n";
100                 
$item .= "\t\t" '<pubDate>' date('D, d M Y H:i:s'$a['date']) . ' GMT</pubDate>' "\n";
101                 
$item .= "\t\t" '<description><![CDATA[' $a['abstract'] . ']]></description>' "\n";
102                 
$item .= "\t\t" '<author>' $this->_strip($a['author']) . '</author>' "\n";
103                 
$item .= "\t" '</item>' "\n";
104                 break;
105             
106             case 
'atom':
107                 
$item "\t" '<entry>' "\n";
108                 
$item .= "\t\t" '<id>' $this->_strip($a['id']) . '</id>' "\n";
109                 
$item .= "\t\t" '<title>' $this->_strip($a['title']) . '</title>' "\n";
110                 
$item .= "\t\t" '<link>' $this->_strip($a['link']) . '</link>' "\n";
111                 
$item .= "\t\t" '<updated>' substr($a['date'], 04) . '-' substr($a['date'], 42) . '-' substr($a['date'], 62) . 'T' substr($a['date'], 82) . ':' substr($a['date'], 102) . ':' substr($a['date'], 124) . '</updated>' "\n";
112                 
$item .= "\t\t" '<summary><![CDATA[' $a['abstract'] . ']]></summary>' "\n";
113                 
$item .= "\t\t" '<author>' "\n";
114                 
$item .= "\t\t\t" '<name>' $this->_strip($a['author']) . '</name>' "\n";
115                 
$item .= "\t\t" '</author>' "\n";
116                 
$item .= "\t\t" '<content><![CDATA[' $a['detail'] . ']]></content>' "\n";
117                 
$item .= "\t" '</entry>' "\n";
118                 break;
119         }
120         
$this->_items .= $item;
121     }
122     
123     
/**
124      * Combine feed 'head' with feed 'body'
125      *
126      * @param    array('title', 'description', 'link', 'date')
127      * @return    void
128      */
129     
private function _assemble_feed($fa) {
130         
$this->_feed '<?xml version="1.0" encoding="utf-8"?>' "\n"
131         
// $fa = func_get_args();
132         
switch ($this->_format) {
133             case 
'rss092':
134                 
$this->_feed .= '<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns="http://my.netscape.com/rdf/simple/0.9/">' "\n";
135                 
$this->_feed .= '<channel>' "\n";
136                 
$this->_feed .= "\t" '<title>' $this->_strip($fa['title']) . '</title>' "\n";
137                 
$this->_feed .= "\t" '<description><![CDATA[' $fa['description'] . ']]></description>' "\n";
138                 
$this->_feed .= "\t" '<link>' $this->_strip($fa['link']) . '</link>' "\n";
139                 
$this->_feed .= '</channel>' "\n";
140                 
$this->_feed .= $this->_items;
141                 
$this->_feed .= '</rdf:RDF>';
142                 break;
143             
144             case 
'rss10':
145                 
$this->_feed .= '<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns="http://purl.org/rss/1.0/" xmlns:dc="http://purl.org/dc/elements/1.1/">' "\n";
146                 
$this->_feed .= '<channel>' "\n";
147                 
$this->_feed .= "\t" '<title>' $this->_strip($fa['title']) . '</title>' "\n";
148                 
$this->_feed .= "\t" '<description><![CDATA[' $fa['description'] . ']]></description>' "\n";
149                 
$this->_feed .= "\t" '<link>' $this->_strip($fa['link']) . '</link>' "\n";
150                 
$this->_feed .= $this->_items;
151                 
$this->_feed .= '</channel>' "\n";
152                 
$this->_feed .= '</rdf:RDF>' "\n";
153                 break;
154             
// http://cyber.law.harvard.edu/rss/rss.html#hrelementsOfLtitemgt
155             
case 'rss20':
156                 
$this->_feed .= '<rss version="2.0">' "\n";
157                 
$this->_feed .= '<channel>' "\n";
158                 
$this->_feed .= "\t" '<title>' $this->_strip($fa['title']) . '</title>' "\n";
159                 
$this->_feed .= "\t" '<description><![CDATA[' $fa['description'] . ']]></description>' "\n";
160                 
$this->_feed .= "\t" '<link>' $this->_strip($fa['link']) . '</link>' "\n";
161                 
$this->_feed .= "\t" '<lastBuildDate>' date('D, d M Y H:i:s'$fa['date']) . ' GMT</lastBuildDate>' "\n";
162                 
$this->_feed .= $this->_items;
163                 
$this->_feed .= '</channel>' "\n";
164                 
$this->_feed .= '</rss>' "\n";
165                 break;
166             
167             
// http://www.atomenabled.org/developers/syndication/
168             
case 'atom'
169                 
$this->_feed .= '<feed version="0.3" xmlns="http://purl.org/atom/ns#" xmlns:dc="http://purl.org/dc/elements/1.1/" xml:lang="zh-cn">' "\n";
170                 
$this->_feed .= "\t" '<id>http://example.com/</id>' "\n";
171                 
$this->_feed .= "\t" '<title>' $this->_strip($fa['title']) . '</title>' "\n";
172                 
$this->_feed .= "\t" '<link>' $this->_strip($fa['link']) . '</link>' "\n";
173                 
$this->_feed .= "\t" '<updated>' substr($fa['date'], 04) . '-' substr($fa['date'], 42) . '-' substr($fa['date'], 62) . 'T' substr($fa['date'], 82) . ':' substr($fa['date'], 102) . ':' substr($fa['date'], 124) . '</updated>' "\n";
174                 
$this->_feed .= $this->_items;
175                 
$this->_feed .= '</feed>' "\n";
176                 break;
177         }
178     }
179
180  
181
/* End of file: ./system/libraries/feed_writer/feed_writer_library.php */

Page URI: http://www.infopotato.com/index.php/code/library/feed_writer/