beginnings of NAA data import
[disclosr.git] / lib / FeedWriter
1 <?php
2
3 /*
4 * Copyright (C) 2008 Anis uddin Ahmad <anisniit@gmail.com>
5 * Copyright (C) 2010-2012 Michael Bemmerl <mail@mx-server.de>
6 *
7 * This file is part of the "Universal Feed Writer" project.
8 *
9 * This program is free software: you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation, either version 3 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21 */
22
23 /**
24 * Universal Feed Writer
25 *
26 * FeedItem class - Used as feed element in FeedWriter class
27 *
28 * @package UniversalFeedWriter
29 * @author Anis uddin Ahmad <anisniit@gmail.com>
30 * @link http://www.ajaxray.com/projects/rss
31 */
32 class FeedItem
33 {
34 private $elements = array(); //Collection of feed elements
35 private $version;
36
37 /**
38 * Constructor
39 *
40 * @param contant (RSS1/RSS2/ATOM) RSS2 is default.
41 */
42 function __construct($version = RSS2)
43 {
44 $this->version = $version;
45 }
46
47 /**
48 * Add an element to elements array
49 *
50 * @access public
51 * @param string The tag name of an element
52 * @param string The content of tag
53 * @param array Attributes(if any) in 'attrName' => 'attrValue' format
54 * @param boolean Specifies, if an already existing element is overwritten.
55 * @return void
56 */
57 public function addElement($elementName, $content, $attributes = null, $overwrite = FALSE)
58 {
59 // return if element already exists & if overwriting is disabled.
60 if (isset($this->elements[$elementName]) && !$overwrite)
61 return;
62
63 $this->elements[$elementName]['name'] = $elementName;
64 $this->elements[$elementName]['content'] = $content;
65 $this->elements[$elementName]['attributes'] = $attributes;
66 }
67
68 /**
69 * Set multiple feed elements from an array.
70 * Elements which have attributes cannot be added by this method
71 *
72 * @access public
73 * @param array array of elements in 'tagName' => 'tagContent' format.
74 * @return void
75 */
76 public function addElementArray($elementArray)
77 {
78 if (!is_array($elementArray))
79 return;
80
81 foreach ($elementArray as $elementName => $content)
82 {
83 $this->addElement($elementName, $content);
84 }
85 }
86
87 /**
88 * Return the collection of elements in this feed item
89 *
90 * @access public
91 * @return array
92 */
93 public function getElements()
94 {
95 return $this->elements;
96 }
97
98 /**
99 * Return the type of this feed item
100 *
101 * @access public
102 * @return string The feed type, as defined in FeedWriter.php
103 */
104 public function getVersion()
105 {
106 return $this->version;
107 }
108
109 // Wrapper functions ------------------------------------------------------
110
111 /**
112 * Set the 'dscription' element of feed item
113 *
114 * @access public
115 * @param string The content of 'description' or 'summary' element
116 * @return void
117 */
118 public function setDescription($description)
119 {
120 $tag = ($this->version == ATOM) ? 'summary' : 'description';
121 $this->addElement($tag, $description);
122 }
123
124 /**
125 * @desc Set the 'title' element of feed item
126 * @access public
127 * @param string The content of 'title' element
128 * @return void
129 */
130 public function setTitle($title)
131 {
132 $this->addElement('title', $title);
133 }
134
135 /**
136 * Set the 'date' element of feed item
137 *
138 * @access public
139 * @param string The content of 'date' element
140 * @return void
141 */
142 public function setDate($date)
143 {
144 if(!is_numeric($date))
145 {
146 if ($date instanceof DateTime)
147 {
148 if (version_compare(PHP_VERSION, '5.3.0', '>='))
149 $date = $date->getTimestamp();
150 else
151 $date = strtotime($date->format('r'));
152 }
153 else
154 $date = strtotime($date);
155 }
156
157 if($this->version == ATOM)
158 {
159 $tag = 'updated';
160 $value = date(DATE_ATOM, $date);
161 }
162 elseif($this->version == RSS2)
163 {
164 $tag = 'pubDate';
165 $value = date(DATE_RSS, $date);
166 }
167 else
168 {
169 $tag = 'dc:date';
170 $value = date("Y-m-d", $date);
171 }
172
173 $this->addElement($tag, $value);
174 }
175
176 /**
177 * Set the 'link' element of feed item
178 *
179 * @access public
180 * @param string The content of 'link' element
181 * @return void
182 */
183 public function setLink($link)
184 {
185 if($this->version == RSS2 || $this->version == RSS1)
186 {
187 $this->addElement('link', $link);
188 }
189 else
190 {
191 $this->addElement('link','',array('href'=>$link));
192 $this->addElement('id', FeedWriter::uuid($link,'urn:uuid:'));
193 }
194 }
195
196 /**
197 * Set the 'encloser' element of feed item
198 * For RSS 2.0 only
199 *
200 * @access public
201 * @param string The url attribute of encloser tag
202 * @param string The length attribute of encloser tag
203 * @param string The type attribute of encloser tag
204 * @return void
205 */
206 public function setEncloser($url, $length, $type)
207 {
208 if ($this->version != RSS2)
209 return;
210
211 $attributes = array('url'=>$url, 'length'=>$length, 'type'=>$type);
212 $this->addElement('enclosure','',$attributes);
213 }
214
215 /**
216 * Set the 'author' element of feed item
217 * For ATOM only
218 *
219 * @access public
220 * @param string The author of this item
221 * @return void
222 */
223 public function setAuthor($author)
224 {
225 if ($this->version != ATOM)
226 return;
227
228 $this->addElement('author', '<name>' . $author . '</name>');
229 }
230
231 /**
232 * Set the unique identifier of the feed item
233 *
234 * @access public
235 * @param string The unique identifier of this item
236 * @return void
237 */
238 public function setId($id)
239 {
240 if ($this->version == RSS2)
241